Arduino HC-SR04 Ultrasonic sensor


The HC-SR04 Ultrasonic Sensor is a very cheap and neat sensor tha gives eyes to your Arduino. This one comes in a Robot kit from Deal Extreme. It works by sending and receiving (bouncing) ultrasound pulses, thus giving you the distance to the nearest object.

In this sketch we’re going to do, we’ll send a pulse to trigger the detection and then listen for the echo using the pulse() function. The duration of this second pulse is equal to the time taken by the ultrasound to travel to the object and back to the sensor. Using the speed of sound, this time can be converted to distance.

Raspberry PI Bluetooth Wireless Speaker

Recently I’ve acquired a Conceptronic 2-Way Audio Wireless Speakerphone (Bluetooth)  and I wanted my PI to connect to the speakers for a small project and after an afternoon struggling with it, here’s how I’ve managed to connect them using only alsa and command line tools.  No GUI tools are used.

The bluetooth module I’ve used is an Edimax EB-DGC2 ! It’s obsolete, but it was the only one that worked with the bluetooth audio. I was first testing with a Conceptronic CBT2NANO (v2.0), but it didn’t work and I’ve switched to the EDIMAX


Gentoo vmware-player and systemd

Gentoo has moved into systemd for those who are using Gnome3 – systemd is mandatory – But not all services from sysVinit or openrc have the new .service files needed for systemd. Vmware player, a closed source virtualization solution still doesn’t has the files, being necessary to create one.

After digging a while, I found the solution.

The old vmware-player sysVinit file just started the network – we’re going to replicate this with a vmware.service file systemd style

As root, go to /usr/lib/systemd/system (if you’re in x86_64 system, lib will be a symlink to lib64) and create the .service file

Put the following lines in it

Save the file.

Enable the service

Check the status:

That’s it ! Now you have vmware starting with systemd


Arch Linux forums for the .service file

Solving thumbnails problems in Gnome

Sometimes, Gnome doesn’t show the thumbnails, whether for videos or pictures.  It can be by the lack of a thumbnailer, but almost because the location of the thumbnails folder has changed !

Now, the thumbnails are stored under the .cache folder.

To solve the problem, just follow these steps:

  1. Go to your cache folder : cd .cache
  2. Create a symbolic  link to the folder (.)thumbnails in your home folder:  ln -s ../.thumbnails thumbnails
  3. You now get a link in .cache to thumbnails:thumbnails -> ../.thumbnails/

Open Nautilus and see the previews appear !


Firefox new tab page as home page

Firefox is an amazing browser, and it’s getting better and better.

What i love most is the “new tab” page, where it lays down the nine most visited pages.


The problem is, Firefox startup options doesn’t has any  to put the same page as our home page.


But there is a small trick we can use. Opening a new tab, the page we want appears. We can use the right mouse button and click it.  Choose “view page info”.

A new window appears with some details about the page.  You can see the address – about:newtab –

Now, returning to the Firefox Startup options, choose “Show my home page” and in the URL box bellow, just type : about:newtab

Now, every time you press the home button or open firefox, the tabs page will be your home page.

Install and configure moodle in CentOS 6.3 with video streaming and recording

Moodle is a E-learning platform free as in free beer. Moodle is a CMS (Course Management System) and also known as a LMS (Learning Management System) or a VLE (Virtual Learning Environment).

I was in need to install a configure a system like this and I’ve chosen Moodle not also because is one of the best out there, but because is also free.

One of the key features that i was also needing was a video streaming and recording facility.  Moodle has it all (through plugins).

What I’m going to describe here is an installation of Moodle from the very beginning:

Using gnome nautilus as ssh and smb gui

Everyday i use ssh in the command line to administrative tasks and the need to copy files is big. I’m comfortable using the command line and at any given time, i have 4 and 5 consoles open but sometimes it’s nice to have a GUI for some tasks (when the character set of one system is not the same and all the accentuated characters do not display correctly and scp has an hard time dealing with it).

I wounder if Linux had such a tool like WinSCP. I sometimes (only when absolutely needed and work related) use windows and WinSCP comes handy.

If you use Gnome, you don’t need a GUI, because Nautilus has support for several protocols. I use Samba when i need to connect to Windows computers, and Nautilus is my tool.

When in Nautilus, if you press Ctrl+L (the L is not in  caps. I type it in caps just for readability) , it opens a location text bar so you can type where you want to go.

How to connect Android (ICS and JB) to Linux for file access

Since Android switch from mass storage to MTP, it’s being hard to connect an Android phone to Linux and use it to browse files. I had that problem every single day, having to resort to another choices.

I’ve tried this on Gentoo Linux and Ubuntu 12.04, but I guess it should work on any distribution.

Since using mass storage, two partitions cannot be mounted simultaneously, Android developers switch to MTP. Here’s a better explanation

Some parts of this tutorial have been borrowed from here:

Most credits should go to Bilal Akhtar (from

What I’ve did was taken a step further. Since udev can execute scripts automatically, why not use udev to manage most of the operations?

Note: unless specified, all commands are to be run as the root user (or if you use sudo, just prepend sudo to the commands)

Install packages

For this to work, we’ll rely on fuse and libmtp


apt-get install mtp-tools mtpfs


emerge -av sys-fs/fuse sys-fs/mtpfs media-libs/libmtp

Go to /media (or /mnt) and create a folder with a relevant name for the device

cd /media

mkdir AsusFT201

We’re doing this because udev only knows about the removed device when you actually remove it and that’s is not good to remove the device without first umount it. Here’re going with Bilal Akhtar solution (references in the bottom of the document) and have an alias to umount the device

Now, on the computer, run the following command:

tail -f /var/log/messages (or tail -f /var/logo/syslog on Ubuntu)

Now connect your device to the computer. In your device, make sure to select “Media device (MTP)”

The kernel messages after connecting the device:

Oct 8 16:43:27 nightraider kernel: [ 5263.446971] usb 1-3: default language 0x0409
Oct 8 16:43:27 nightraider kernel: [ 5263.447477] usb 1-3: udev 11, busnum 1, minor = 10
Oct 8 16:43:27 nightraider kernel: [ 5263.447479] usb 1-3: New USB device found, idVendor=04e8, idProduct=6860
Oct 8 16:43:27 nightraider kernel: [ 5263.447481] usb 1-3: New USB device strings: Mfr=2, Product=3, SerialNumber=4
Oct 8 16:43:27 nightraider kernel: [ 5263.447483] usb 1-3: Product: SAMSUNG_Android
Oct 8 16:43:27 nightraider kernel: [ 5263.447484] usb 1-3: Manufacturer: SAMSUNG
Oct 8 16:43:27 nightraider kernel: [ 5263.447486] usb 1-3: SerialNumber: 111X1111XY11111Z
Oct 8 16:43:27 nightraider kernel: [ 5263.447552] usb 1-3: usb_probe_device
Oct 8 16:43:27 nightraider kernel: [ 5263.447555] usb 1-3: configuration #1 chosen from 2 choices

SerialNumber is the line we’re looking for.

With this value, let’s create the udev rules.

create the following file:

(I use vi, you can use emacs or gedit. Still, you don’t know what you’re missing by not trying vi)

vi /etc/udev/rules.d/81-android.rules

Read here why the file is called 81-android.rules

and add the following lines (replace 111X1111XY11111Z with the serial with your device):

SUBSYSTEM==”usb”, ATTRS{serial}==”111X1111XY11111Z”, MODE=”0666″, GROUP=”plugdev”, SYMLINK+=”AndroidPhone”, RUN+=”/usr/bin/mtpfs -o allow_other /media/ AsusFT201″


The full path of the commands is necessary. Read the udev manual for more information

I went with serial because it is unique and I’ve seen the idDevice change in the same computer. – can someone confirm if this is possible ?

Now save and quit the editor.

Reload the udev rules:

udevadm control –reload-rules

Now, edit /etc/fuse.conf and remove the comment from the last line (just delete the ‘#’)





Add yourself to the fuse group (if not already)

To find out, type:

id (as your user)

uid=1000(username) gid=1000(username) groups=1000(username),4(adm),24(cdrom),27(sudo),29(audio),30(dip),44(video),46(plugdev),60(games),105(fuse),109(scanner),111(lpadmin),115(netdev),124(sambashare),1012(sharing),1013(bumblebee)

If you don’t have the group in your list, just run the following command:

gpasswd -a <your_username> fuse
Adding user <your_username> to group fuse

On Gentoo you need to add your user to the group disk

The command is the same, just replace fuse with disk

You need to logout and login again for it to take effect (if not in the group)

After that, just plug your device and wait a bit. I’m saying wait a bit because for me, my phone (with ICS) and my tablet (with JB) take a while to mount. Don’t know why. This happens in both distributions.


To unmount the device, just edit your .bashrc file (in your home) and add the following lines:

vi ~/.bashrc

Give it the name you want – replace android-disconnect

alias android-disconnect=”fusermount -u /media/AsusFT201″

Save and quit

Now, execute the command:

source ~/.bashrc

Now you can remove the device issuing the command android-disconnect


DHCP failover / load balancing (and synchronization) with CentOS 6

DHCP is a wonderful piece of software. It keeps our networks running smoothly. For small networks, probably 100 machines or so, one server is enough, but to larger networks, is not a bad idea to have another one, just in case the firts one fails or the load is just to much..

DHCP has some configurations for load balancing and failover, the – failover declaration – that allows us to configure it.

The Servers:

Primary IP address:

Secondary IP address:

To keep things simple, you can create a new file, and then just insert it in the global dhcpd.conf (configuration below)

Primary DHCP server

Open a new file and put the following lines in it:

vi /etc/dhcp/dhcpd.failover

# Failover specific configurations

failover peer “dhcp” {
port 647;
peer address;
peer port 647;
max-response-delay 60;
max-unacked-updates 10;
mclt 600;
split 128;
load balance max seconds 3;
Now, in the secondary DHCP server, just to the same:
Secondary Server
failover peer “dhcp” {
port 647;
peer address;
peer port 647;
max-response-delay 60;
max-unacked-updates 10;
load balance max seconds 3;
After creating the files, just add the configuration to the global config file (in both servers), somewhere before (groups | share networks | host) definitions
include “/etc/dhcp/dhcpd.failover”;
Now, to the definitions.
To take advantage of the new definitions, we need to create a pool. You can put it in any (subnet| shared-network|etc..) declaration:
network netmask {
option routers;
option subnet-mask;
option broadcast-address;
pool {
failover peer “dhcp”;
Note: If you have static declarations (i have all my clients in static declarations), to avoid warnings in the log about dynamic and static mappings, reduce the range to only one client. It’s mandatory the range declaration
IMPORTANT: Is of utmost importance to have both servers with the same date and time. If they are not, DHCP will complain and the secondary server (the whole server) will not work well… You can accomplish this with ntpdate
If you are getting this messages in syslog:
Failover CONNECT to dhcp rejected: Connection rejected, time mismatch too great.

Then the time is not the same.

If you want to know more about the configuration parameters in the failover declarations, go to the ipamworldwide web site
DHCP doesn’t have a synchronization mechanism (as far as i know – please correct me if i’m wrong), so changes you’ll make to the primary server will not reflect in the secondary server. This could be done using scripting or yourself manually copying the changed file over to the secondary server. But sometimes, in the heat of the moment, because something important happened or someone is waiting for your attention, you forget…
There’s a small program that can accomplish the synchronization without you even remember that you must copy the files…
iwatch is a small program that monitors wherever you want (files, directories) and upon changes, it can perform several actions.
A few months ago i wrote about iwatch and how’s installed and configured (portuguese), but i’ll replicate the steps here, using the DHCP files has an example.
The CentOS minimal installation doesn’t have rsync and wget installed, so we need to install those.
yum install rsync wget
Note: The steps above are only required in the primary server. The changes are made here and then replicated to the secondary server.
Install the rpmforge repositories. You can get the rpm and instructions here
Install the required perl packages
yum install perl-Event perl-Mail-Sendmail perl-XML-SimpleObject perl-XML-Simple perl-Linux-Inotify2
After installing, we can finally install iwatch.
Download it from sourceforge
After download, untar it:
tar -zxvf iwatch-0.2.2.tgz
A new directory is created
cd iwatch
In there, you’ll find a few files.
Let’s copy the files to the proper places
cp iwatch /usr/local/bin/
cp iwatch.xml /etc/
cp iwatch.dtd /etc/
A few considerations before continuing:
We want to synchronize changes in the DHCP configurations, so, we’ll monitor the /etc/dhcp directory for:
  • Creation of files
  • changes in files
  • deleting of files
  • add an exception for dhcpd.failover (those are different in the servers – depending of primary or secondary server)
Now that we know what we want, let’s proceed:
Before we edit the configuration file, so we can execute iwatch as a daemon, let’s execute it in command line and edit a file so we can see what’s happening. Open two terminals: One will be used to execute iwatch and some arguments, the other will be to edit a file.
First terminal:
Execute iwatch and see some output:
/usr/local/bin/iwatch -e modify,create,close_write -c “touch /tmp/someaction” -r -v /etc/dhcp/

Watch /etc/dhcp
Watch /etc/dhcp/Configs
Watch /etc/dhcp/dhclient.d

Second terminal
Let’s edit a file in the watched directory and see what’s happening in terminal 1. You can just open it, no changes, but save the file and watch the output in Terminal 1.
vi /etc/dhcp/dhcpd.conf
In terminal 2, you’ll see:
[14/Mar/2012 16:18:34] IN_CREATE /etc/dhcp/Configs/.dhcp.vlan.swp
[14/Mar/2012 16:18:34] * Command: touch /tmp/someaction
[14/Mar/2012 16:18:34] IN_CREATE /etc/dhcp/Configs/.dhcp.vlan.swx
[14/Mar/2012 16:18:34] * Command: touch /tmp/someaction
[14/Mar/2012 16:18:34] IN_CLOSE_WRITE /etc/dhcp/Configs/.dhcp.vlan.swx
[14/Mar/2012 16:18:34] * Command: touch /tmp/someaction

Now that we saw it working, let’s configure the daemon part.

Edit the file /etc/iwatch.xml. The file syntax is XML. Here’s an example of my configuration.
You can read more in iwatch sourceforge page.
<?xml version=”1.0″ ?>
<!DOCTYPE config SYSTEM “/etc/iwatch.dtd” ><config charset=”utf-8″>

<guard email=”” name=”IWatch”/>


<title>DHCP Sync</title>
<contactpoint email=”” name=”Administrator”/>
<path type=”recursive” syslog=”on” alert=”off” events=”create,delete,close_write” exec=”/root/scripts/syncFiles”>/etc/dhcp</path>
<path type=”regexception”>b4913b</path>
<path type=”exception”>/etc/dhcp/dhcpd.failover</path>
<path type=”exception”>/etc/dhcp/dhclient.d</path>

<path type=”regexception”>.*.swp*</path>

<path type=”regexception”>.*~</path>



Now, edit that file and make the changes you want

I’ve added a few exceptions, because there are files i don’t need to sync.

Also, vi creates a few temporary files (directory 4913 and backups with ~ | swp extensions) when you’re editing, and those don’t mind.

We are not also using modify, because if a file is closed with write, it was modified, right ?

The exec  parameter tells iwatch what to do when any of the events occurs. I have a script (syncFiles) that synchronizes with the secondary server and sends and email

# Script to synchronized dhcp changes
# This script will be called by iwatch
# 15/12/2011
echo “Syncing dhcp from primary server to secondary server” >> $log
# Using rsync so it can only copy different files – Low on bandwith/usr/bin/rsync -avz –delete -e ssh /etc/dhcp/ –exclude dhcpd.failover root@secondary:/etc/dhcp >> $log
# Restart the service with the new configurations
ssh root@secondary -C “service dhcpd restart” >> $log
service dhcpd restart >> $log
# Email
if [ -a $log ]; then
mail -s “Sync dhcp ” < $log
rm -f $log

I use rsync to perform the copy. I exclude dhcpd.failover because the files are not the same and they depend on the server (primary or secondary)

Notes: A few security issues. iwatch is executed with root privileges – it’s started by /etc/rc.local

If you do nothing, every time the script is executed, you’ll have to give the root password of the secondary server. You can prevent this (if you want) by adding the ssh key to the authorized keys and have a password-less ssh configuration between those two servers (using only keys)

Now, just put iwatch executing when the machine start:

vi /etc/rc.local
# Exec iwatch
/usr/local/bin/iwatch -d

Execute iwatch as daemon

/usr/local/bin/iwatch -d

Now you have a dhcp failover instalation and synchronization

Hope it helps anyone