Send Email when IP Address Changes

I have a ubuntu linux box in my office that I occasionally access from home. But unfortunately its IP address is constantly changing due to the dynamic IP allocation policy. Here’s how I set up a “daemon” that will send myself an email upon the change of the machine’s IP address.

1. Setup Gmail and sSMTP

This time we are using sSMTP because it’s easy, and Gmail because it’s free.

I would prefer to create a new Gmail account specific only for this purpose. Use your creativity to create long and obfuscated password, e.g. with mkpasswd -m sha-512 yOuRp4ssW0rD you can get a long string which is should be very nice for password.

Next, install sSMTP and its mail “client”:

sudo apt-get install ssmtp mailutils

Then edit the config file at /etc/ssmtp/ssmtp.conf:

root=youremail@gmail.com

mailhub=smtp.gmail.com:587
AuthUser=youremail@gmail.com
AuthPass=yOuRfUnKyP4ssWorD
UseTLS=YES
UseSTARTTLS=YES
AuthMethod=LOGIN

sSMTP is not a daemon, so don’t worry about starting the service or such.

Next, test your setup:

echo "test message" | mail -s "testing ssmtp" yourothermail@gmail.com

2. The Script

This time we use Bash, because you might not realize that you’re already fluent with Bash.

#!/bin/bash
# check and send ip address to email

MYIP=`ifconfig eth0 | grep 'inet addr'| awk '{print $2}' | cut -d ':' -f 2`;
TIME=`date`;

LASTIPFILE='/home/yourUserName/.last_ip_addr';
LASTIP=`cat ${LASTIPFILE}`;

if [[ ${MYIP} != ${LASTIP} ]]
then
        echo "New IP = ${MYIP}"
        echo "sending email.."
        echo -e "Hello\n\nTimestamp = ${TIME}\nIP = ${MYIP}\n\nBye" | \
                /usr/bin/mail -s "[INFO] New IP" yourothermail@gmail.com;
        echo ${MYIP} > ${LASTIPFILE};
else
        echo "no IP change!"
fi

Save this anywhere in your home folder. I personally have my own /home/username/bin/ to keep my small scripts and tools, and I have it in the system path ($PATH).

This script will save your current eth0‘s IP address to the hidden file .last_ip_addr. The next time this script is called, it will check the last IP address, if it’s different then it will notify to the given email address.

3. Cron
To make this run periodically, add the script as a cron job. More detail on cron you can STFG (Search The Fine Google).

crontab -e

Then add this to run the script every 30 minutes

*/30 * * * * /home/username/bin/emailipaddr.sh

Sources:

Calculate Age Automatically in LaTEX Document

Source

I use this to minimize the effort in updating my CV.

 

\documentclass{article}

...

\usepackage{datenumber, fp}
\newcounter{birthday}
\newcounter{today}
\setmydatenumber{birthday}{1988}{02}{29}
\setmydatenumber{today}{\the\year}{\the\month}{\the\day}
\FPsub\result{\thetoday}{\thebirthday}
\FPdiv\myage{\result}{365.2425}
\FPround\myage{\myage}{0}

...

\begin{document}

Hello, I am \myage years old.

\end{document}

How To Change/Override Linux Password from other Linux PC

Or if you have linux running on an SD card (e.g. Raspberry Pi) and you want to change the password from another your Linux PC, here’s how to do it.

1. Mount the target system on your linux PC. If it’s a Raspberry Pi SD card, simply plug it in. Now let’s say the mount point is /media/stick, and inside that folder you will find system folder such as /etc, /dev, /proc, and others.

2. Information of user and passwords in most linux systems are kept in a text file, consecutively /etc/passwd and /etc/shadow. What we need to change is the latter. Open the shadow file (use sudo cat /etc/shadow), and see the entry for the user whom you want to change the password. The entry contains 9 fields, separated by colon (“:”). More detail on this type man shadow on terminal. Our interest lies on the second field, which is the encrypted password field. This field has format of following:

$id$salt$hashedpassword

Now look into the format of your shadow file, if the id is 6, most likely SHA-512 encryption is used. The other possibilities are MD5 (id 1) and SHA-256 (id 5).

3. We will use mkpasswd, a front-end for crypt, the function for linux password encryption. To create a new password with SHA-512 encryption, do this:

mkpasswd -m sha-512 <password> <salt>

You can pick anything for the password, but for salt you need to give 8-16 random sequence of characters. If salt is empty, mkpasswd will generate a random salt, hence everytime you call mkpasswd it will give different result. For shadow, make sure you give in your salt. For example:

ariandy@linux:~$ mkpasswd -m sha-512 mypassword OhIuVG9LLE79.5XV
$6$OhIuVG9LLE79.5XV$GCO6Oyd2.IeONgjXWZDXzZbU8QFDUYD2mrrhQQeRzsceGmek4SRGMqXiJbod5UQ1WivVD1GPt4k5sPo6.PVs//

Now put this output in the second field of /etc/shadow file. Note that hashed password contains no colon, because colon is the delimiter between fields in shadow.

Source

ROS + OpenNI2 + NiTE2

After weeks banging my head with OpenNI version 1.5.4 that comes with my ROS fuerte installation, I finally come to the conclusion that OpenNI 1.5.4 is highly frustrating, difficult to use, and has very low code readability. Or maybe it is just me.

It is time to migrate to OpenNI2, the latest version of the library that has been completely hauled with new architecture, with (much) better code readability. And another good thing about this release is that this will not mess the other version of OpenNI, so we can still work with both version in the same time.

The installation process is pretty straight forward. We can get the installation files after registering on OpenNI website. Then just simply run the install script from each folder.

Then, to use OpenNI2 and NiTE2 with ROS, we need to add some parameters to the CMakeLists.txt of our ROS project/package to link them with the libraries. Here’s mine:

cmake_minimum_required(VERSION 2.4.6)
include($ENV{ROS_ROOT}/core/rosbuild/rosbuild.cmake)

# Change two lines below according to your installation
#
set(OPENNI2_DIR /home/ariandy/src/OpenNI-Linux-x64-2.2/)
set(NITE2_DIR /home/ariandy/src/NiTE-Linux-x64-2.2/)
rosbuild_init()

#set the default path for built executables to the "bin" directory
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
#set the default path for built libraries to the "lib" directory
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)

link_directories(${OPENNI2_DIR}/Redist)
include_directories(${OPENNI2_DIR}/Include)

link_directories(${NITE2_DIR}/Redist)
include_directories(${NITE2_DIR}/Include)

rosbuild_add_executable(testing src/main.cpp)
target_link_libraries(testing OpenNI2 NiTE2)

Now grab any sample program codes from the OpenNI2 or NiTE2 and put it inside our ROS package for testing. It should compile just fine.

There’s still one issue though. NiTE2 uses machine learning method for the human recognition and also skeleton fitting, which relies heavily on training data. It keeps the training data on NiTE2 folder inside NiTE-Linux-*/Samples/Bin folder. And somehow, when NiTE2 initializes it will look for the training data relative to the path (e.g. your executable is at /home/user, then it will look for /home/user/NiTE2/*). That is a bummer, since we can run ROS executable (node) regardless of the path and this NiTE2 thing defeats the purpose. Workaround is by navigating first to NiTE-Linux-*/Samples/Bin/ or NiTE-Linux-*/Redist/ then do rosrun your_package your_node, otherwise it won’t find the training data.

Running roscore and Launching ROS nodes as Background Process

I access my ROS running robot wirelessly using secure shell (SSH). To run my program, I have to first run all the startup scripts such as roscore and my robot’s driver. It’s kind of tedious to open a lot of shells just to keep this services on. And I can’t simply run this on background by using & operator because sometimes I need to check the node’s output.

So to keep it simple and neat, I made simple script to run roscore and robot driver on background, but still be able to check the outputs of this two (e.g. error messages).

First thing first, I like to have my own bin folder, to keep my own scripts.

cd ~
mkdir bin
echo "export PATH=$PATH:/home/ariandy/bin" >> ~/.bashrc
source ~/.bashrc

Then all this scripts will be kept inside this folder. If you need to run roscore and some nodes as a startup script, it will be better if you keep this scripts in /usr/local/bin/ folder.

#!/bin/bash
# file: youbot-roscore.sh

source /opt/ros/fuerte/setup.bash
export ROS_PACKAGE_PATH=$ROS_PACKAGE_PATH:/home/ariandy/youbot_driver:/home/ariandy/applications:/home/ariandy/ros_stacks

roscore

This is for my node:

#!/bin/bash
# file: youbot-oodl.sh

source /opt/ros/fuerte/setup.bash
export ROS_PACKAGE_PATH=$ROS_PACKAGE_PATH:/home/ariandy/youbot_driver:/home/ariandy/applications:/home/ariandy/ros_stacks

# this is my driver
roslaunch youbot_oodl youbot_oodl_driver.launch
# or your own node/launch script
#roslaunch {your_package} {your_thing}

The main tool for our topic is linux’ “screen“. Install it first if you don’t have it.

#!/bin/bash
# file: run-youbot-ros-startup.sh

echo "running roscore daemon.."
screen -dmS roscore youbot-roscore.sh
sleep 2
echo "running youbot oodl daemon"
screen -dmS oodl youbot-oodl.sh
echo "done. view with screen -ls"

The format is screen -dmS [any_name] [your_script]. The -dm argument will tell screen to run this session as a daemon. Then we use -S argument to give the session a name, so it will be easier for later. Please note that if your_script is not located on system path folder (e.g. /usr/bin/, /usr/local/bin/, etc.), you have to give the full path for the screen argument.

roscore needs some time (1-2 seconds) to reach its full operational state. That is why I give 2 seconds delay (with sleep 2) before running my node.

With this done, then we can run the things on the background:

ariandy@youbot$ run-youbot-ros-startup.sh 
running roscore daemon..
running youbot oodl daemon
done. view with screen -ls

To view what are the running sessions, call screen -ls

There are screens on:
	9285.oodl	(05/08/2013 05:01:50 PM)	(Detached)
	9224.roscore	(05/08/2013 05:01:49 PM)	(Detached)
2 Sockets in /var/run/screen/S-ariandy.

The format is [pid].[name]. Use this name if you want to go to each session: screen -r name.

When we’re inside the screen session, Ctrl-C will send a signal to the program we are running inside screen, makes it quitting and then the screen session closes. We don’t want this. What we want is to go back to our original shell session, and put the screen session back as background process. We do that by using key combination Ctrl-A-D, that is while holding Ctrl, press A, then press D.

Forking Own Repo on Github

In some cases I need to develop a new project which is based my other project. Some people suggest to do branching but that was not exactly what I needed, because I need to have a new name.

So, after reading several sources (this and this), this is how I fork my own repo (please note that I did this under linux):

git clone https://github.com/your_user_name/your_old_repo your_new_repo
cd your_new_repo/

Now edit the Git configuration file (your_new_repo/.git/config) using your favorite editor, and find this lines:

[remote "origin"]
        fetch = +refs/heads/*:refs/remotes/origin/*
        url = https://github.com/your_user_name/your_old_repo.git

Then update the url by replacing “your_old_repo” with “your_new_repo“, so it will look like this:

url = https://github.com/your_user_name/your_new_repo.git

After that:

git push origin master

WiFi Access Point with TP-Link TL-WN722n on Ubuntu 12.04

My system’s configuration:
– Ubuntu 12.04 Precise Pangolin
– TP-Link TL-WN722n USB WiFi Dongle

TO DO:
– Slow network performance, getting only 3-10Mbit/s
– With N-Mode activated, connection drops after a while

Sources:
http://forum.doozan.com/read.php?2,6300

Steps:

1. Network configuration

First edit /etc/network/interfaces, remove anything related to wlan0 (or whatever your device’s name) and add this lines to set the IP address of the wlan device as static:

auto wlan0
iface wlan0 inet static
    address 10.0.0.1
    network 10.0.0.0
    netmask 255.255.255.0
    broadcast 10.0.0.255

Although the configuration is already set, but in my system somehow it fails to set the IP address. So to make sure the setting will work, add ifup wlan0 inside /etc/rc.local file just before the exit 0:

#!/bin/sh -e
#
# rc.local

...

ifup wlan0

exit 0

2. hostapd installation

hostapd is the daemon who is responsible for broadcasting the access point and managing the connections. Rather than using hostapd package provided by Ubuntu repository, I would prefer to get the latest revision of hostapd from the developer’s git, then compile it manually. I do this because I want to enable the WiFi N-mode support.

But, installation from source will only give hostapd binaries, while we need several config files to run hostapd automatically as a service. I am no linux/Debian expert so I will just install hostapd from the repository, just to get the service configuration at /etc/default/, /etc/init.d/, and other places, then uninstall or remove the hostapd binaries.

git clone git://w1.fi/srv/git/hostap.git
cd hostap/hostapd

Then inside this folder, copy the defconfig file into a new file named .config, then open it using your favorite editor. For my setup I only need to activate (by uncommenting) the nl80211 driver and the N-Mode:

...
CONFIG_DRIVER_NL80211=y
...
CONFIG_IEEE80211N=y

Before installing hostapd, we need to install some dependencies:

sudo apt-get install libnl1 libnl1-dev

Then after that, compile and install hostapd:

make
sudo make install

The resulting binaries are hostapd and hostapd_cli located by default at /usr/local/bin/.

Now we can do a test run. Edit the config file /etc/hostapd/hostapd.conf:

interface=wlan0
ssid=MyAccessPoint
hw_mode=g
channel=6
auth_algs=1

# to enable N-Mode
# UPDATE: N-Mode is still problematic
#ieee80211n=1
#wmm_enabled=1

# config for WPA security
macaddr_acl=0
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=MyPassphrase123
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP

Make sure you don’t have trailing whitespaces, because hostapd is very sensitive. Now run hostapd:

sudo hostapd -dd /etc/hostapd/hostapd.conf

If nl80211 driver fails to start, quite possibly because NetworkManager is still managing the wireless network. Turn off the wlan management from NetworkManager by doing this on terminal (ref.):

sudo nmcli nm wifi off
sudo rfkill unblock wlan

If no problem exist, the output would be somehow like this:

random: Trying to read entropy from /dev/random
Configuration file: /etc/hostapd/hostapd.conf
nl80211: interface wlan0 in phy phy0
rfkill: initial event: idx=0 type=1 op=0 soft=0 hard=0
nl80211: Using driver-based off-channel TX
nl80211: Add own interface ifindex 4
nl80211: Set mode ifindex 4 iftype 3 (AP)
nl80211: Setup AP - device_ap_sme=0 use_monitor=1
nl80211: Create interface iftype 6 (MONITOR)
nl80211: New interface mon.wlan0 created: ifindex=7
nl80211: Add own interface ifindex 7
BSS count 1, BSSID mask 00:00:00:00:00:00 (0 bits)
nl80211: Regulatory information - country=CN
nl80211: 2402-2482 @ 40 MHz
nl80211: 5735-5835 @ 40 MHz
nl80211: Added 802.11b mode based on 802.11g information
Allowed channel: mode=1 chan=1 freq=2412 MHz max_tx_power=20 dBm
Allowed channel: mode=1 chan=2 freq=2417 MHz max_tx_power=20 dBm
Allowed channel: mode=1 chan=3 freq=2422 MHz max_tx_power=20 dBm
Allowed channel: mode=1 chan=4 freq=2427 MHz max_tx_power=20 dBm
Allowed channel: mode=1 chan=5 freq=2432 MHz max_tx_power=20 dBm
Allowed channel: mode=1 chan=6 freq=2437 MHz max_tx_power=20 dBm
Allowed channel: mode=1 chan=7 freq=2442 MHz max_tx_power=20 dBm
Allowed channel: mode=1 chan=8 freq=2447 MHz max_tx_power=20 dBm


....

nl80211: ifindex=4
nl80211: beacon_int=100
nl80211: dtim_period=2
nl80211: ssid - hexdump_ascii(len=8):
     59 6f 75 42 6f 74 41 50                           MyAccessPoint        
nl80211: hidden SSID not in use
nl80211: privacy=1
nl80211: auth_algs=0x1
nl80211: wpa_version=0x2
nl80211: key_mgmt_suites=0x2
nl80211: pairwise_ciphers=0x10
nl80211: group_cipher=0x10
wlan0: Event RX_MGMT (20) received
wlan0: Event TX_STATUS (18) received
wlan0: Event RX_MGMT (20) received
wlan0: Event RX_MGMT (20) received
wlan0: Event TX_STATUS (18) received
wlan0: Event RX_MGMT (20) received
wlan0: Event RX_MGMT (20) received
wlan0: Event TX_STATUS (18) received
wlan0: Event TX_STATUS (18) received
wlan0: Event RX_MGMT (20) received
wlan0: Event RX_MGMT (20) received
wlan0: Event RX_MGMT (20) received
wlan0: Event RX_MGMT (20) received
wlan0: Event RX_MGMT (20) received
wlan0: Event RX_MGMT (20) received

This lines

wlan0: Event TX_STATUS (18) received
wlan0: Event TX_STATUS (18) received
wlan0: Event RX_MGMT (20) received
wlan0: Event RX_MGMT (20) received

Will show up when another PC/client is probing or scanning our Access Point. This is a good sign. Now quit the test run by pressing Ctrl-C

The next step to configure the Ubuntu services so hostapd could start automatically upon booting. First, edit /etc/init.d/hostapd file, and make sure the variables are correct:

...
DAEMON_SBIN=/usr/local/bin/hostapd
DAEMON_CONF=/etc/hostapd/hostapd.conf
...

Second, edit /etc/default/hostapd

...
RUN_DAEMON="yes"
DAEMON_CONF="/etc/hostapd/hostapd.conf";
DAEMON_OPTS="-dd -t";
...

And now we can manage the hostapd through Ubuntu’s service, and it will run automatically upon booting.

sudo service hostapd restart

For more information about managing the service, this could be a good reading.

3. DHCP and DNS

For DHCP purpose we are going to use dnsmasq because of the simplicity of configuration. Please note that dnsmasq might be not suitable for big network.

But one thing worth noting is that Ubuntu 12.04 (Network-Manager) is using dnsmasq as DNS resolver. We have to disable it so we can use dnsmasq as our DHCP and “DNS” server. To do this, edit /etc/NetworkManager/NetworkManager.conf file and put a # (comment sign) in front of dns=dnsmasq so it will look like this:

...
#dns=dnsmasq
...

For more information about this, check this out.

Now we can configure dnsmasq to fit our need

sudo apt-get -y install dnsmasq

It will create default complete config file /etc/dnsmasq.conf, which is very useful for reference. For the sake of readability let’s just rename that file and make a new empty config file:

cd /etc/
sudo mv dnsmasq.conf dnsmasq.conf.orig
sudo touch dnsmasq.conf

Then edit the newly created file, and fill that with this basic configuration:

interface=wlan0
expand-hosts
domain=local
dhcp-range=10.0.0.10,10.0.0.20,24h
dhcp-option=3,10.0.0.1

Now we’re done. Restart the server/PC and make sure everything works. We can check the log at /var/log/syslog anytime to see our DHCP server in action.

Sometimes I got problem that dnsmasq is not running automatically upon booting, this could be because the runlevel of dnsmasq is called before the networking devices are up. The easiest hack is to put the service restart command in the /etc/rc.local:

#!/bin/sh -e
#
# rc.local

...

ifup wlan0

sleep 5
service dnsmasq restart

exit 0

Raspberry Pi Backup and Restore from Linux

0. Preparation

Before backing up the SD Card of Raspberry Pi, do clean up the system first to remove unneeded files such as package source codes and apt-get caches. You might be surprized by how much apt-get is keeping cache of downloaded packages. It was nearly 600MB for me. Cleaning this up would give you smaller backup image.

df -h
sudo apt-get clean
df -h

See how much space you free’d after cleaning apt-get’s cache.

I like to keep a piece of information, that tells me that the system is in the state of just being restored. I do that by either editing the MOTD (Message of The Day) or making an empty file on my home folder “THIS_IS_JUST_RESTORED”.

To back up the SD Card, first plug the SD Card into the SD Card reader of your laptop/PC that runs Linux. I use Ubuntu 12.04 Precise Pangolin. Then get any files that you need to copy, after that unmount the SD Card. Default Raspbian installation has 2 partitions made on the SD Card, so do not forget to unmount both. To check which folder is mounted to which device, use df -h and see the lines that start with /dev/mmcblk0p1 and /dev/mmcblk0p2. Then unmount the corresponding folders:

sudo umount /media/first_sd_partition
sudo umount /media/second_sd_partition

1. Backup

To back up the SD card, what we are about to do is making an image of the device, in this case both of the SD card partitions. For Linux, we have dd. I personally use dcfldd, an enhanced version of dd. It gives progress percentage, and also multiple on-the-fly hash calculation. To install dcfldd, in ubuntu simply:

sudo apt-get install dcfldd

If you want the backed up image to be as small as possible, you have to compress the image of the SD card. In Linux it is possible to compress an image which is currently being backed up (on-the-fly compression). With this you can save your hard disk space, because if you’re making an image without on-the-fly compression, you will require at least the same size as the SD card. If your have 8GB SD card, although only used partially, the resulting image from dd or dcfldd will be around 8GB in size.

To backup with on-the-fly compression:

sudo dcfldd bs=4M if=/dev/mmcblk0 hash=sha1 hashlog=./image.img.sha1sum | gzip > image.img.gz
sudo sync

And we’re done.

Everytime after you run dd or dcfldd, do not forget to call sudo sync to flush system buffers. I forgot to do this once and my system goes all crazy and weird.

If you want to do it the long way, you can do backup without compression:

sudo dcfldd bs=4M if=/dev/mmcblk0 of=./image.img hash=sha1 hashlog=./image.img.sha1sum
sudo sync

Then if you realize that it takes too much space, you can compress it anytime with gzip:

gzip image.img

Please note that it will take a while, since we’re dealing with gigabytes of data.

I also like to watch the image file grow in real time, so I do this:

watch ls -lh image.img*

2. Restore

If you have your image file already compressed, you can either uncompress and then restore, or uncompress while restore.

I find uncompressing and restoring in the same time (on-the-fly) is the most efficient way:

gunzip -c image.img.gz | sudo dcfldd bs=4M of=/dev/mmcblk0 hash=sha1 hashlog=./hash.log
sudo sync

Again, do not forget to flush the system buffer by calling sudo sync.

This hash thing, is for making sure that the backed up and restored image are consistent, not broken in any way which might happen during transfer/copying or upload. After the process finished, compare both of the hashes (image.img.sha1sum and hash.log) and make sure they are identical. You don’t need to read both files manually, just tell linux to do it:

diff -sq image.img.sha1sum hash.log

This will tell you either both files are identical or not.

If you are bored and have a lot of free space in your hard disk, you can do the restoring process in the long way by first uncompressing the image, then write it to the SD card:

gunzip image.img.gz
sudo dcfldd bs=4M if=./image.img of=/dev/mmcblk0 hash=sha1 hashlog=./hash.log
sudo sync

Setting Up WiFi Access Point with Edimax EW-7811UN on Raspberry Pi

I am running an image processing algorithm on my Raspberry Pi and I need to access it wirelessly (wired connection is sooo ’90s :p ). So my requirements are following:
– WiFi Access Point for remote access
– no need internet connection
– runs on Raspbian Wheezy

Steps:
1. Configuring network
Edit /etc/network/interfaces, remove anything related to wlan0 then add this lines:

iface wlan0 inet static
address 10.0.0.1
network 10.0.0.0
netmask 255.255.255.0
broadcast 10.0.0.255

Although the configuration is already defined, sometimes it won’t be applied by the system upon booting. To make sure, add ifup wlan0 inside /etc/rc.local file before exit 0 so it will look like this:

#!/bin/sh -e
#
# rc.local

...
# add this
ifup wlan0

exit 0

2. Installing DHCP Server and “DNS” Configuration
DHCP is the one that is responsible to give out IP addresses to clients who are connected to the Access Point. Meanwhile, DNS or Name Service is working as some kind of dictionary, that translates numbers of IP address into human-readable names that are easy to remember and to type.

This time I will use dnsmasq for easy and fast DHCP+DNS deployment.

sudo apt-get -y install dnsmasq

After installation it will run the daemon automatically. Stop it and then we work on the configuration:

sudo service dnsmasq stop
sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.orig
sudo touch /etc/dnsmasq.conf

Now edit the newly created dnsmasq.conf config file. For minimum setup, this should be the content of the config file:

interface=wlan0
expand-hosts
domain=local
dhcp-range=10.0.0.10,10.0.0.20,24h
dhcp-option=3,10.0.0.1

For full configuration options, you can refer to the original config file (dnsmasq.conf.orig).

Although 10.0.0.1 is easy enough to remember, isn’t it easier if we use “raspi” as address? So then you can simply type ssh raspi or http://raspi/. To make this happen, edit /etc/hosts file, append with this:

10.0.0.1        raspi raspberrypi raspi.local raspberrypi.local

3. Installing hostapd
This WiFi USB Dongle from Edimax uses Realtek chipset, it is rtl8192 to be precise. Kindly enough, Realtek has provided hostapd implementation which is specific for this chipset.

For installation instructions, I suggest you to follow Jens Segers’ blog post.

After finishing installation, edit the hostapd configuration file to fit your needs.

Streaming OpenCV Output Through HTTP/Network with MJPEG

If you want to stream your OpenCV output image to another device which connected to the same network, one way is to write the OpenCV output as MJPEG file, then stream this file through HTTP.

Please note that:
– This might be not the best way (correct me if I’m wrong)
– OpenCV 2.3 cv::VideoWriter has memory leak bug

Steps:
0. OpenCV Stuff
I assume you already have your OpenCV libraries installed, because several of OpenCV’s dependencies are also MJPEG-Streamer’s, such as libv4l-dev and libjpeg-dev. And make sure your OpenCV application works (it writes the mjpeg file)

1. Install MJPEG-Streamer
Install the dependencies first

sudo apt-get install imagemagick

Then get the latest MJPEG-Streamer from the repository:

git clone https://github.com/codewithpassion/mjpg-streamer.git
cd mjpg-streamer/mjpg-streamer
make USE_LIBV4L2=true clean all
sudo make DESTDIR=/usr/local install

Then set the environment variable LD_LIBRARY_PATH if you haven’t, either in /etc/bash.bashrc or your own ~/.bashrc, add this line:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib

2. Into The Main Show
First fire up your OpenCV application, this is my application, simple background subtraction and writes the output into mjpg file.

git clone https://github.com/ariandyy/bgsubtract.git
cd bgsubtract/
make bgsubtract2

Prepare the folder to put the mjpg file into, for example in my home folder:

mkdir /home/ariandy/mjpg

Then run the application:

./bgsubtract2 /home/ariandy/mjpg/out.mjpg -bgs

Leave it running in one terminal, and open another terminal to test the MJPEG-Streamer:

mjpeg_streamer -i "input_file.so -f /home/ariandy/mjpg" -o "output_http.so -w /usr/local/www"
 

The command above is the most basic setting to get the streaming to work. By default, the web server is listening to port 8080. If you want to use port 80, you have to run the command as root (or with sudo).

The default www root of MJPEG-Streamer is located at /usr/local/www. There you can find the demo pages, from which you can get the idea of several ways to display the stream: either direct stream (supported by most Grade-A Desktop Browsers (Sorry, IE!)), using Java MJPEG viewer (cambozola), javascript, etc.

3. Viewing The Stream
On the client, simply point the browser to the server’s IP address with corresponding port (8080). e.g. server’s IP address is 192.168.0.1, then to access it from the client, point to http://192.168.0.1:8080. If you want to omit the :8080 part, then you have to run the server on port 80 (HTTP), as following:

sudo mjpeg_streamer -i "input_file.so -f /home/ariandy/mjpg" -o "output_http.so -w /usr/local/www -p 80"
 

I tried this with Google Chrome, Firefox, Android Browser, and Firefox for Android.

Sources:
http://www.raspberrypi.org/phpBB3/viewtopic.php?t=8659
http://sourceforge.net/projects/mjpg-streamer