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.

Advertisements

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!

How to set up a VPN (Private Internet Access) in Raspberry Pi

In this ./note I am going to show you how set up the VPN connection provided by Private Internet Access in Raspberry PI (but the same steps are valid for any other Linux distribution).

I found these instruction in the Private Internet Access forum (https://www.privateinternetaccess.com/forum/).

1. If you don’t have done this already, you need to install OpenVPN.

sudo apt-get install openvpn

2. Move to the OpenVPN directory in /ect:

cd /etc/openvpn

3. Download from the Private Internet Access website the zip folder. This folder contains all the config files for OpenVPN.

wget https://www.privateinternetaccess.com/openvpn/openvpn.zip

4. Unzip the folder using unzip. If you don’t have unzip already installed in your system run the following command:

sudo apt-get install unzip

and then unzip:

sudo unzip openvpn.zip

5. In order to see the list of servers to which you can connect run:

ls -l

6. As you can see each server config file has .ovpn extension. Now you can run the final command to connect to the server:

sudo openvpn config-sever-filename.ovpn

Once the previous command is executed you are required to insert your username and password of your Private Internet Access account. If you want to avoid to insert username and password every  time you want to connect to the VPN you can follow these steps:

1. Create a .txt file in the /etc/openvpn folder (same folder of the .ovpn file) . You can name this file ‘pass.txt
2. In the first two lines of the file put your username and password, like:

username

password

3. Save and close the file.
4. Open up your .ovpn and add the following line at the bottom:

auth-user-pass pass.txt

5. Save and try to connect again. You should not be required to insert username and password this time.

That’s it.

In a next post I will show how to connect automatically to the VPN at boot.