Backing up Raspberry Pi SD Cards on Mac OS X, Linux & Windows

If you want to back up the Raspberry Pi SD card, you cannot simply plug the SD Card into your Mac or PC and copy all the files to your hard drive. Instead, you need to create what it is called disk image. This is because,  when you create a disk image of your SD card, you will preserve not only all the files but also the entire filesystem structure.
Therefore, once you decide to flash a new SD card, you just have to plug it in and it will work.

Mac

Firstly, insert the SD card into a USB card reader of  your Mac and open the Terminal and type the following command in order to list all the disks attached to your Mac:

diskutil list

From the list, identify which /dev/disk corresponds to the SD card.

Now just use the dd command to backup of your SD card:

dd if=/dev/rdiskx of=/path/to/image.img bs=1m

where /dev/rdiskx is your SD card and /path/to/image is the path where you want to save the backup. The backup image will be a file .img.

However, it is also possible to create a compressed SD card image (.gz) as follows:

sudo dd if=/dev/rdisk1 bs=1m | gzip > /path/to/backup.gz

To restore an SD card from a backup image, use the following command:

sudo dd if=/path/to/image.img of=/dev/rdisk1 bs=1m

Linux

On Linux, similar to Mac, you can use the standard dd tool:

dd if=/dev/sdx of=/path/to/image.img bs=1M

Where /dev/sdx is your SD card.

To create a compressed SD card image use the following command:

sudo dd if=/dev/rdisk1 bs=1M | gzip > /path/to/backup.gz

To restore an SD card from a backup image (.img):

sudo dd if=/path/to/image.img of=/dev/rdisk1 bs=1M

Windows

On Windows, you can use Win32 Disk Imager. The following screen-shoot is the main window.

Firstly you need to select which drive is your SD card. In this case my SD card is F:\.

Then, click on the blue icon just next to the F:\ to open a file-explorer window. Here you have to select where you want to save the image file and the type of image (.img, .bin, etc..).

Win32 Disk Imager

You have to confirm the folder and filename are correct. When ready just click the Read button. When completed it will show Done!

To restore the SD card from a backup image, just follow the same process but this time click the Write button.

How to set up Wi-Fi USB dongle on Raspberry Pi using the terminal + Android Tethering

It is relatively simple and cheap to add Wi-Fi connectivity to your Raspberry Pi. You only need to buy a Wi-Fi USB dongle and make sure that is compatible with GNU/Linux. On http://elinux.org/RPi_USB_Wi-Fi_Adapters you can find a list of compatible Wi-Fi adapters.

In this tutorial, I am using Edimax EW-7811UN available for less than 10 £ on Amazon.

Edimax EW-7811UN 150Mbps Wireless Nano USB Adapter

Before to start with the actual setup, you need to take note of the following details of your Wi-Fi connection:

  • Network SSID
  • Wireless password
  • Security (e.g.: WPA2-PSK)

In order to make this project a little more interesting, I am going to use my Nexus 4 as a Wi-Fi Hotspot. Therefore, I am going to share my device’s 3G data connection as a portable Wi-Fi hotspot.

(If you are not using an Android device as Hotspot just skip the following section)

On your Android device (This steps apply to Nexus 4, Galaxy Nexus, Nexus 7 (2013), and Google Play edition devices):

  1. Go to Settings > Under the section Wireless & networks > More > Tethering & portable hotspot.
  2. Go to Set up Wi-Fi hotspot and type your Network SSID, Security (e.g. I selected WPA2-PSK), Password > Save
  3. Tick Portable Wi-Fi hotspot

After a moment, the device starts broadcasting its Wi-Fi network name (SSID). Once you want to stop sharing your data connection, just untick Portable Wi-Fi hotspot.

Now, it’s time to go back to our Raspberry setup.

It is always good practice to update the system to the latest available software. Open your Terminal and type:

sudo apt-get update
sudo apt-get install

Now, type the following command to open the interfaces configuration file:

sudo nano /etc/network/interfaces

You should see the following lines:

auto loiface lo inet loopback
iface eth0 inet dhcp

This is the default setup of your Ethernet connection (eth0). We need to add the following lines for the new Wireless connection that we want to set up (wlan0):

allow-hotplug wlan0
iface wlan0 inet dhcp
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
iface default inet dhcp

Press Ctrl-x to save and exit.

In the terminal enter the following command:

sudo nano /etc/wpa_supplicant/wpa_supplicant.conf

Replace all the content of the file with the following:

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={

ssid=”YOUR_NETWORK_SSID”

# scan_ssid type can be: value of 1 means broadcast and value of 2 means hidden
scan_ssid=1

psk=”YOUR_WIRELESS_PASSWORD”

# Protocol type can be: RSN (for WP2) and WPA (for WPA1)
proto=RSN

# Key management type can be: WPA-PSK or WPA-EAP (Pre-Shared or Enterprise)
key_mgmt=WPA-PSK

# Pairwise can be CCMP or TKIP (for WPA2 or WPA1)
pairwise=CCMP

# Authorization option should be OPEN for both WPA1/WPA2 (in less commonly used are SHARED and LEAP)
auth_alg=OPEN

}

The lines with # are only comments that should guide you. When you’re done with the editing, press CTRL+x to save and exit.

Now you only need to reboot in order to load the new network set up.

Raspberry will connect automatically during the boot to the Wi-Fi network.

How to: Setup a bluetooth connection between Arduino and a PC/Mac

The following tutorial is based on the one available at the following link (http://playground.arduino.cc/Learning/Tutorial01). 

The cheapest way to connect your Arduino to your PC wirelessly is using Bluetooth. The drawbacks of this solution are the limited range, about 10 meters, and the low bandwidth speed. If your project requires either higher range or more bandwidth you should opt for a more expensive WiFi module.

This tutorial will show you how to setup a Bluetooth Module for use as a serial data connection. I am using this particular module but should be similar with others.

Screen Shot 2013-09-21 at 10.17.31

Before to start, you need to have either a built-in Bluetooth module or a Bluetooth dongle installed in your PC/Mac. In addition you also need to have installed on your system both Arduino Software and Processing.

Firstly you need to set up the bluetooth module whitin your Mac or PC.

Under Mac

Open System Preferences. Under the section Internet & Wireless click on the Bluetooth icon.

Now click on Set Up New Device. The Bluetooth Module will appear as linvor.  Select it and press Continue. You need a specific passcode to pair the Bluetooth module and your Mac. Therefore, click on Passcode Option and enter 1234 (other bluetooth modules may have different codes). Then just wait until the pairing is completed. No other configuration steps are required.

Under Windows 7

Right click the Bluetooth Icon in the System Tray and select Bluetooth Devices. Click on the Add a Device. The search should bring up a bluetooth device called linvor. Select linvor and click Next. When asked select the option Enter the device’s pairing code. Enter the code 1234 (other bluetooth modules may have different codes).

The device should now be successfully added to the computer.

Click Close to return to the Bluetooth Devices list. Right Click on the linvor device and select Properties. Click on the Services Tab and you should see the Serial Port service along with a port number. In my case is COM4. Take note of the port number because you will need it later.

Code

Connect your Arduino to your Computer using an USB cable as usual.

Open your Arduino Software, create a new sketch and copy the following code:


char val; // variable to receive data from the serial port
int ledpin = 13; // Arduino LED pin 13 (on-board LED)
void setup() {
   pinMode(ledpin, OUTPUT); // pin 13 (on-board LED) as OUTPUT
   Serial.begin(9600); // start serial communication at 9600bps
}

void loop() {
   if( Serial.available() ) // if data is available to read
   {
      val = Serial.read(); // read it and store it in 'val'
   }
   if( val == 'H' ) // if 'H' was received
   {
      digitalWrite(ledpin, HIGH); // turn ON the LED
   } else {
      digitalWrite(ledpin, LOW); // otherwise turn it OFF
   }
   delay(100); // wait 100ms for next reading
}

Once you have upload the code on your Arduino, disconnect the Arduino USB cable from your computer (this step is important). If you leave your Arduino connected to the computer with the USB cable the Bluetooth connection will not work! You can use an external battery or any other external source to power your Arduino.

Feel free to close the Arduino software. You will not need it anymore.

The following step is to connect the Bluetooth module to the Arduino as follows:

  • Connect the +5V bluetooth module Pin to the 5V  Arduino Pin
  • Connect the 0V bluetooth module Pin to the GND  Arduino Pin
  • Connect the TX bluetooth module Pin to the RX  Arduino Pin
  • Connect the RX bluetooth module Pin to the TX  Arduino Pin

Connect Arduino to a power source and the module Bluetooth led should start flashing.

Open Processing and copy the following code:

//import class to set up serial connection with wiring board
import processing.serial.*;
Serial port;
//button setup
color currentcolor;
RectButton rect1, rect2;
boolean locked = false;

void setup() {
   //set up window
   size(200, 200);
   color baseColor = color(102, 102, 102);
   currentcolor = baseColor;
   // List all the available serial ports in the output pane.
   // You will need to choose the port that the Wiring board is
   // connected to from this list. The first port in the list is
   // port #0 and the third port in the list is port #2.
   println(Serial.list());
   // Open the port that the Wiring board is connected to (in this case 1
   // which is the second open port in the array)
   // Make sure to open the port at the same speed Wiring is using (9600bps)
   port = new Serial(this, Serial.list()[2], 9600);
   // Define and create rectangle button #1
   int x = 30;
   int y = 100;
   int size = 50;
   color buttoncolor = color(153, 102, 102);
   color highlight = color(102, 51, 51);
   rect1 = new RectButton(x, y, size, buttoncolor, highlight);
   // Define and create rectangle button #2
   x = 90;
   y = 100;
   size = 50;
   buttoncolor = color(153, 153, 153);
   highlight = color(102, 102, 102);
   rect2 = new RectButton(x, y, size, buttoncolor, highlight);
}

void draw() {
   background(currentcolor);
   stroke(255);
   update(mouseX, mouseY);
   rect1.display();
   rect2.display();
}

void update(int x, int y) {
   if(locked == false) {
      rect1.update();
      rect2.update();
   } else {
      locked = false;
   }
   //Turn LED on and off if buttons pressed where
   //H = on (high) and L = off (low)
   if(mousePressed) {
      if(rect1.pressed()) { //ON button
         currentcolor = rect1.basecolor;
         port.write('H');
      } else if(rect2.pressed()) { //OFF button
         currentcolor = rect2.basecolor;
         port.write('L');
      }
   }
}

class Button {
   int x, y;
   int size;
   color basecolor, highlightcolor;
   color currentcolor;
   boolean over = false;
   boolean pressed = false;
   void update() {
      if(over()) {
         currentcolor = highlightcolor;
      } else {
         currentcolor = basecolor;
      }
   }
   boolean pressed() {
      if(over) {
          locked = true;
          return true;
      } else {
          locked = false;
          return false;
      }
   }
   boolean over() {
      return true;
   }
   void display() {
   }
}

class RectButton extends Button {
   RectButton(int ix, int iy, int isize, color icolor, color ihighlight) {
      x = ix;
      y = iy;
      size = isize;
      basecolor = icolor;
      highlightcolor = ihighlight;
      currentcolor = basecolor;
   }
   boolean over() {
      if( overRect(x, y, size, size) ) {
         over = true;
         return true;
       } else {
         over = false;
         return false;
       }
    }
   void display() {
      stroke(255);
      fill(currentcolor);
      rect(x, y, size, size);
   }
}

boolean overRect(int x, int y, int width, int height) {
   if (mouseX >= x && mouseX <= x+width && mouseY >= y && mouseY <= y+height) {
      return true;
   } else {
      return false;
   }
}

The only line you have to modify is the following:

port = new Serial(this, Serial.list()[2], 9600);

where [2] is the number of the serial port of your Bluetooth device (ex: [3] is COM4 in my case). If you run the Processing code you should see in the logs a list of all available ports. Make sure you select the right one for you.

Once the Bluetooth module is connected to your Computer the LED on the module will stop flashing and be on permanently.

This code creates two buttons. When the left button is clicked it will send an H signal to the board and turn the LED on. Or if the right button is clicked it will send an L signal to Wiring board to turn the LED off.

How To: Rasperry PI Camera Module web streaming and motion detector

In this post I am going to describe how to setup a web streaming server and motion detector using a Raspberry PI with the PI Camera Module.

Before to start, I am assuming that you have already your Raspberry PI Camera Module setup and working.

Step 1: Download the software

The software that we are going to install is called Motion; this is both a motion detector and streaming software. You could potentially install Motion using the apt-get command from the terminal. However, if you want to use Motion with the Raspberry PI Camera Module you need a special version of Motion (called Motion – MMAL Camera) – see this post for more info and credits.

Therefore, what you are going to do is the following:

  1. Install motion using apt-get command so it will install all the required dependencies;
  2. Remove Motion that we have just installed leaving all the dependencies
  3. Download Motion – MMAL Camera

Ok then, let’s start..

sudo apt-get install motion

Press Y when it is requested to install all the required dependencies.  Now, we can remove Motion because, as I wrote above, we are going to use Motion – MMAL Camera.

sudo apt-get remove motion

At this point, we can download the pre-compiled version of Motion – MMAL Camera:

wget https://www.dropbox.com/s/xdfcxm5hu71s97d/motion-mmal.tar.gz

Unzip:

tar zxvf motion-mmal.tar.gz

Step 2: Config the software

As you can see, the archive contains two files: the executable and a config file.  Before running the software you need to make some changes to the config file. Make sure you are in the same directory where the config file is and open it:

sudo nano motion-mmalcam.conf

The config contains a lot of settings that you are free to modify as you like. However the most important ones are the following:

  • Image width and height: I suggest to set 640×480;
  • Framerate should be between 2 and 5 in order to not overload the Raspberry PI;
  • output_pictures: set to on if you want to generate a picture when a motion is detected;
  • ffmpeg_output_movies: set to on if you want to generate a movie when a motion is detected;
  • target_dir: set the folder where do you want to save the videos or the pictures. Example: /home/pi;
  • Under the section Live Stream Server set stream_port 8081 if you want to enable the web streaming video; moreover, set stream_localhost to off if you want to be able to access the streaming from any host other than the localhost. If you do not want the web streaming set stream_port to 0;
  •  Under the section HTTP Based Control set webcontrol_port 8082 if you want to access the Motion config from the web browsers. This is a very useful capability that allows you to change all the setting of Motion directly from the browsers. Set stream_localhost to off if you want to access the configs  from any host;

As you can see, Motion offers a lot of setting that you can play with…

Step 3: Run the software

Once you are happy with your config you can run motion using the following command:

sudo ./motion -n -c motion-mmalcam.conf

Now, if you want see the streaming open a browser (use Firefox or Opera but do not use Chrome because it does not work for me) and go to the following address:

http://IP_ADDRESS_OF_YOUR_RASPBERRYPI:8081

and you should be able to see the streaming! If you want to see the config tool go to:

http://IP_ADDRESS_OF_YOUR_RASPBERRYPI:8082

You can access to the web streaming from different devices such as iPhone, Android, etc..

That’s it. When a motion is detected Motion will generate pictures and/or videos according to what you chose in the config file.

How to control Arduino with your voice

Adding speech recognition capability to your Arduino project is easier than you can imagine. I am currently working on an 4WD Arduino robot project. My robot can be controlled using only my voice. So far, I have implemented the following commands: go ahead, go back, turn left/right and stop.

If you want to add voice recognition to your project, you need to bear in mind that Arduino is not powerful enough to run a speech recognition code. But this is not a big issue. There are two alternative ways to do that:

  1. Using a Speech Recognition shield (Ex. https://www.sparkfun.com/products/10963).
  2. Connecting a PC to your Arduino. The PC computes the voice recognition algorithm and sends the command to the Arduino.

The Speech Recognition shield is a quite expensive solution. Moreover, I am already using a Motor Shield with my Arduino so I cannot use another shield.

That’s why I decided to go for the second option. My Arduino robot is connected to my PC with a Bluetooth connection, but you can use a USB cable or a wireless connection as well.

Before to start, you will need the following:

  • A PC with Microsoft Windows Vista, 7 or 8.
  • A microphone connected either to your PC or Arduino board.
  • Arduino has to be connected to your PC with one of the following communication interfaces:  USB (used as a virtual COM port), Bluetooth Adapter or Wireless Adapter.
  • The speech recognition software BitVoicer installed in your window PC. You will need to buy this software, but do not worry because it is actually very cheap and it works very well. BitVoicer requires an Internet connection for activation and installation of additional languages.

Once you are ready to start, you can download the BitVoicer manual from their website. Here it is the link.

The manual contains all the information you will need. It also provides two examples.

In the first example, the audio is captured by the computer’s microphone. A LED is connected to the digital pin 4 of the Arduino will turn on and off in response to a voice command captured by the computer’s microphone. In the second one, the microphone is directly connected to the Arduino board.

Once you understand how to implement these two basic examples, you will be ready to work on more advanced speech recognition project with your Arduino.

How to run a command automatically at system boot in Raspberry Pi (Linux)

In the previous post I explained step by step how to set up a VPN connection in Raspberry Pi. When you want to connect to the VPN you have to run the following command:

sudo openvpn config-filename-goes-here.ovpn

where config-filename-goes-here.ovpn  is the VPN configuration file. The only problem is that every time you want to connect to the VPN you have to manually execute this command. It would be much more convient to run it automatically during the system startup. So let’s do it..

Firstly, you need to create a script in /etc/init.d directory:

sudo nano /etc/init.d/VPNConnection

where VPNConnection is the name of the script (you are free to choose any name that you prefer).

Once the nano editor is open, just copy and paste the following content:

#! /bin/sh
# /etc/init.d/VPNConnection

### BEGIN INIT INFO

# Short-Description: Simple script to start a program at boot
# Description:       A simple script from http://www.stuffaboutcode.comwhich will start / stop a program a boot / shutdown.
### END INIT INFO

# If you want a command to always run, put it here

# Carry out specific functions when asked to by the system
case “$1” in
start)
echo “Starting VPN Connection”
# Connect to the VPN
cd /etc/openvpn
sudo openvpn config-filename-goes-here.ovpn
;;
stop)
echo “Stopping VPN Connection”
# Disconnect
killall openvpn
;;
*)
echo “Usage: /etc/init.d/VPNConnection {start|stop}”
exit 1
;;
esac

Now you can save and close the editor. The next step is to make the script executable:

sudo chmod 755 /etc/init.d/VPNConnection

Before to proceed any further test if the script works as expected. Try to connect to the VPN using the following command:

sudo /etc/init.d/VPNConnection start

and then test the stop command (disconnect from the VPN):

sudo /etc/init.d/VPNConnection stop

The final step is to register the script to be run at boot and shutdown:

sudo update-rc.d VPNConnection defaults

Next time you will boot the system you will be automatically connect to the VPN.

If you want to remove the script from the start-up:

sudo update-rc.d -f  VPNConnection remove

Let me know if you have any comments or suggestion!