====== "Streaming Motion Pictures” : Project 1 ======
=====Pi Army Group=====
**Felipe Moya Coto B24609 \\
Luis Diego Soto Ugalde B26613 \\
Dannier Castro León B21594**
===== Introduction =====
The name of this proyect is //**“Streaming Motion Pictures”**//, we'll call it SMPI for short. A Raspberry Pi we'll be used to stream videos through the Internet in real time. This process is known as streaming. SMPI will be developed on its entirety using open platforms.
We will use **Raspbian**, which is an OS designed specifically for the Raspberry Pi that's based on Linux's Debian Wheezy distribution. A web server will be used to stream the video. We will use motion to stream the video. Openvpn shall be used to create a Virtual Private Network. Some basic knowledge of HTML will be used to create a aesthetically appealing web page where the video will be displayed. Finally, some knowledge in computer networks will be used.
SMPI has the potential of being applied in a diversity of situations. Options such as streaming conferences or video surveillance will be considered.
To see the initial work proposal, enter following link: [[https://docs.google.com/file/d/0B50L99OoaOobTXBVZE55Y2REU2M/edit?usp=sharing | SMPI Proposal (Spanish)]]
===== Objectives =====
==== General Objective ====
* Configure a Raspberry Pi as a portable video streamer that is able to display live video form a web cam.
==== Specific Objectives ====
* Install Raspbian on the Raspberry Pi.
* Install the appropriate packages to stream video from the Raspberry Pi.
* Configure a VPN server and connect the Raspberry Pi to it.
* Mount a web server on the Raspberry pi to stream video captured from a web cam that can be accessed for the Internet.
[[teaching:ie0117:proyectos:2012:i:linux_2013:smpi|Top]] - [[ie0117_proyectos:linux_2013:|Projects]]
=====Implementation=====
Once you've got your Raspberry Pi, a SD card, a proper power source and Internet connection from a wired network, you should follow the instructions in this [[http://www.raspberrypi.org/wp-content/uploads/2012/12/quick-start-guide-v1.1.pdf | PDF Document]] to download and setup Raspbian (The most reliable Pi OS). During the initial configuration process you can change the password for the “pi” user, among other options.
After Raspbian is installed, begins to install the following packages using the command **sudo apt-get install**, and follow the instructions in each:
==== MOTION ====
Before to install Motion, you should verify that your webcam is compatible with Raspberry Pi. To do it, you can enter this page [[http://elinux.org/RPi_VerifiedPeripherals#USB_Webcams | Verified Peripherals : USB Webcams]]. To just check if your webcam works, you can use packages like [[http://packages.debian.org/sid/camorama | Camorama]].
[[http://packages.debian.org/stable/video/motion | Motion]] is a program that monitors the video signal from one or more cameras and detects if a significant part of the picture has changed. In this part of the tutorial we'll get **Motion** working and we'll activate the //motion daemon// so that motion starts every time the Pi is turned on.
* First, we need to update and upgrade :
$ sudo apt-get update
$ sudo apt-get upgrade
* Now, we'll get motion:
$ sudo apt-get motion
* We shall edit motion's configuration file:
$ sudo nano /etc/motion/motion.conf
* There are four very important configuration options that we should change.
First ,we need to set motion's daemon to on.
daemon on
* Second, we need to change the option //“webcam_localhost”// from on to off. Failing to do this will result in the video stream being accessible only from the Pi itself.
webcam_localhost off
* The third option is to pick your stream's resolution. It is very important that the dimensions that you choose are multiples of 16. Also some cameras may not work with the Pi at certain resolutions, a list of cameras with their respective working resolutions on the Pi can be found here: [[http://elinux.org/RPi_VerifiedPeripherals#USB_Webcams | Verified Peripherals : USB Webcams]]
width 352
height 288
* The fourth and final option is to select the port that motion will use to stream the video. For this project, port 8081 was chosen. Also select port 8080 as a control port
webcam_port 8081
control_port 8080
Don't forget to save the changes you've made to motion's configuration file.
* Now, we need to activate motion's daemon:
$ cd /etc/default
$ sudo nano motion
* Once **nano** has opened, all we need to do is switch motion's daemon from off to on:
start_motion_daemon=yes
* Save and exit nano. Now we'll start motion's service:
$ sudo service motion start
If you want to check everything is working fine, get the Pi's IP with command **//sudo ifconfig//**, then open Iceweasel and go to Pi's IP: 8081 and you should get your video stream.
==== APACHE ====
Now we will create a web page to display our video stream using this package called [[http://www.apache.org/ | Apache]].
*First, we'll need to get **Apache**
$ sudo apt-get apache2
* Now we will start the apache server
$ sudo /etc/init.d/apache2 start
* Now we'll switch to the directory where our web page is contained
$ cd /var/www
* Now we'll edit the index.html file. Please do this **as root**:
$ sudo nano index.html
* Once nano opens up you can edit your webpage. To display the video stream on the webpage be sure to insert the following line in the index.html file:
* Now open up //Iceweasel// and go to the Pi's IP address, you should see the video stream displayed on your new web page.
==== WI-FI ====
In this part of the tutorial we'll connect the Pi to a wireless network. We will focus on setting up our adapter and editing some configuration files. For our project's presentation we used a wired network due to the instability of the EIE's network.
We used a TP-Link WN821N Wireless Stick. We need to get it's driver:
$ wget 'http://wireless.kernel.org/en/users/Drivers/carl9170/fw1.9.6?action=AttachFile&do=get&target=carl9170-1.fw' -O carl9170-1.fw
Now we will create a directory to move the driver to. Please do this as root:
$ sudo mkdir /usr/local/lib/firmware
$ sudo mv carl9170-1.fw /usr/local/lib/firmware
Run sudo ifconfig you should see a new interface “wlan”. To scan wireless networks:
$ sudo iwlist scan
To configure **other Wireless Stick** to connect it with wireless networks automatically, follow the next steps:
* Edit the archive /etc/network/interfaces, it should be:
auto lo
iface lo inet loopback
allow-hotplug eth0
iface eth0 inet dhcp
allow-hotplug wlan0
iface wlan0 inet manual
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
iface home inet dhcp
iface work inet dhcp
iface default inet dhcp
* Then, edit the network's configuration in the directory /etc/wpa_supplicant/wpa_supplicant.conf, and write in:
network={
ssid="Your network's ssid "
scan_ssid=1
psk="a passkey"
id_str="An id for your network"
priority=5
network={
ssid="Another network's ssid"
scan_ssid=1
psk="a passkey"
id_str="another id"
priority=5
network={
key_mgmt=NONE
}
* This will work for a network that's not secured with WPA,WPA2,etc. If you want to connect to a network that's secured, you'll have to modify the file properly.
==== OpenVPN ====
Now we will configure a VPN using [[http://wiki.debian.org/OpenVPN | OpenVPN]]. This tutorial intends to explain the **OpenVPN**'s main features in a brief way, so people who are not used to work with this kind of software will understand its way of functioning. It also includes an installation tutorial which will guide the user through the basic steps in order to run an **OpenVPN** server and client on Debian. In this case, the **OpenVPN** server will be installed on a PC, while the **OpenVPN** client will be installed on the Raspberry Pi.
* What is [[http://wiki.debian.org/OpenVPN | OpenVPN]]?
As its name states, **OpenVPN** is a source open application which creates a virtual private network, which is the extension of a private network over the Internet. It could be considered as a WAN (wide area network) link between the clients and the network that is private. How does OpenVPN manages to keep this connection private? It does it via OpenSSL, which provides a secure connection using certificates and authentication protocols, which allows OpenSSL to build and exchange keys for both server and client. It also uses the Diffie-Hellman algorithm for data encryption as well as the SSL tunnels for greater security. This means that if a remote computer (i.e an employee who wishes to establish a connection to his company network) needs to connect to a private network (which has an OpenVPN server), it will only need to have an OpenVPN client installed. All of these features make OpenVPN an ideal software for private streaming services and more.
**Installation guide for OpenVPN**
We'll split this guide into two parts, one for the OpenVPN server installation and the other for the OpenVPN client installation on the Raspberry Pi.
- **Setting up the OpenVPN server**
* Since we are doing this project in Debian, we'll be using the Advanced Packaging Tool to install OpenVPN and its dependencies. Everything we'll be doing will be in the root directory, so we will need permission in order to move, create and copy files, so make sure you login as root before you begin with this installation process. Do the following:
$ apt-get install openvpn libopensc2
* This will create an empty folder named openvpn in your ///etc// directory, which makes sense since the network configuration is kept in this directory. However, this will also create a folder containing the examples files in the ///usr// directory, which will be used for building the keys for client and server, so we will need to copy these files into the openvpn folder in the /etc directory:
$ mkdir /etc/openvpn/easy-rsa
$ cp -ai /usr/share/doc/openvpn/examples/easy-rsa/2.0/ /etc/openvpn/easy-rsa
$ cd /etc/openvpn/easy-rsa/2.0
$ nano vars
* This vars file contains all the environment variables that will be used for building the keys and certificates when the proper commands are executed. First, edit the KEY_* entries, such as KEY_COUNTRY, KEY_PROVINCE, KEY_CITY, KEY_ORG, etc. Next, source the vars file to load the environment variables and clean the directory, in case there are any certificates and keys in the //$EASY_RSA/keys// directory ($EASY_RSA is the environment variable pointing to easy-rsa tree):
$ source ./vars
$ ./clean-all
* If the last two commands were not executed successfully, check again that you are logged in as root or check that the environment variables in the vars file are set accordingly to your directory hierarchy in the /etc directory. If not, continue with the following lines:
$ ./build-ca
$ ./build-key-server
$ ./build-key
* When building the key server and client, you'll be asked to enter your KEY_COUNTRY and all that stuff all over again, to simplify this, just write whatever is in the parenthesis next to the KEY_* variables. Next, generate the Diffie-Hellman parameters for your server:
$ ./build-dh
* This will create the certificates and keys for both server and client. You'll need to copy the keys and certificates related to the server to your //**/etc/openvpn**// directory, as follows:
$ cd /etc/openvpn
$ cp easy-rsa/2.0/keys/ca.crt .
$ cp easy-rsa/2.0/keys/server.key .
$ cp easy-rsa/2.0/keys/server.crt .
$ cp easy-rsa/2.0/keys/dh1024.pem .
* Once this has been done, you'll need to copy the example server configuration named server.conf in the /usr directory to the **Openvpn** folder in ///etc// (you may have to gunzip this config):
$ cp /usr/share/doc/openvpn/examples/sample-config-files/server.conf .
* Next, let's edit this configuration accordingly:
port 1194
proto udp
dev tun
ca /etc/openvpn/ca.crt
cert /etc/openvpn/server.crt
key /etc/openvpn/server.key
dh /etc/openvpn/dh1024.pem
server 10.8.0.0 255.255.255.0
ifconfig-pool-persist ipp.txt
push "redirect-gateway def1 bypass-dhcp"
push "dhcp-option DNS 202.107.105.13"
push "dhcp-option DNS 202.108.107.21"
keepalive 10 120
comp-lzo
user nobody
group nogroup
persist-key
persist-tun
status openvpn-status.log
verb 3
* Now, we'll start the **OpenVPN** server:
$ /etc/init.d/openvpn start
* Next, we'll need to enable IP forwarding so the server will be able to route the client properly, so do as follows:
$ cd /etc
$ nano sysctl.conf
* Once you are in the sysctl.conf file, you can enable IP forwarding by uncommenting the line that says:
$ net.ipv4.ip_forward = 1
This should be enough to make your OpenVPN server run.
- **Setting up the OpenVPN client on the Raspberry Pi**
* To setup an OpenVPN client on the Raspberry Pi, we do pretty much the same as we did before when we began to download OpenVPN (as root):
$ apt-get install openvpn
* We'll be needing the ca.crt, and the key clients and certificates generated in the server installation, so we copy them into the /etc/openvpn folder on the Raspberry Pi, via usb or scp. Next, we copy the client.conf example into the /etc/openvpn folder and edit it as follows:
$ cd /etc/openvpn
$ cp /usr/share/doc/openvpn/examples/sample-config-files/client.conf .
$ nano client.conf
client
dev tun
proto udp
remote 1194
resolv-retry infinite
nobind
user nobody
group nogroup
persist-key
persist-tun
mute-replay-warnings
ca /etc/openvpn/ca.crt
cert /etc/openvpn/client_kevin.crt
key /etc/openvpn/client_kevin.key
ns-cert-type server
comp-lzo
verb 3
up /etc/openvpn/update-resolv-conf
down /etc/openvpn/update-resolv-conf
* Once you've done this, you are ready to run your OpenVPN client. You can start it with the following line:
$ openvpn --script-security 2 --config /etc/openvpn/client.conf &
* If you want to be sure that you are running your OpenVPN client properly, run this command in the Raspberry PI:
$ ping 10.8.0.6
The output of this shows if the raspberry pi is pinging the server.
[[teaching:ie0117:proyectos:2012:i:linux_2013:smpi|Top]] - [[ie0117_proyectos:linux_2013:|Projects]]
===== Results =====
{{:ie0117_proyectos:linux_2013:dsc_0079.jpg?nolink&200 |}} \\ \\ This picture shows the correctly assembly of all devices: Rasberry Pi, Microsoft Web Cam USB, proper power source of 5.0V == 850mA Output.
\\
Among problems that can occur, is a bad configuration of Motion, and it caused that the Streamer didn't work. We had this problem, the web cam resolution was incorrect, and found the solution in this [[http://www.youtube.com/watch?v=ctYWA5ttd3o | YouTube Video]] and set the stream's resolution to 352x288.
\\ \\ To improve the design of Streamer Web Page you can insert a code in the **Apache**'s archive ///var/www/index.html//, this code allows you to change and incorporate grafical characters in the Web Page. In this project we use the following code and we had the next result:
{{:ie0117_proyectos:linux_2013:931338_10151596455914165_390011455_n.jpg?direct&300 |}} \\ \\
SMPI-IE0117
Video-Streamer

\\ \\
Finally, the OpenVPN works correctly in a home network, but has problems of certificates in the EIE's internal network. We could not solve it. Then here are the pictures that show the proper functioning of the Video Streamer as client through a server:
* This image shows when the **Screenshot** initializes the client in the Raspberry:
{{ :ie0117_proyectos:linux_2013:openvpn_pi.png?direct |}}
\\
* OpenVPN created a new network interface called TUN, and you can see it in two ways:
- //$ sudo ifconfig// from Raspberry Pi (client)
- //$ sudo ifconfig// from Server
{{ :ie0117_proyectos:linux_2013:ifconfig_pi.png?direct | }}
\\
{{ :ie0117_proyectos:linux_2013:ifconfig_pc.png?direct |}}
\\
* The proof that the connection exists between client and server, we get with **Ping**, it sends data and check for response.
{{ :ie0117_proyectos:linux_2013:ping_pc.png?direct |}}
\\
The next video shows the final result, the project totally complete, a Video Streamer works perfectly:
* **SMPI Streaming Motion Pictures Project** \\
[[http://www.youtube.com/watch?v=YhnRwCsXKzg&feature=youtu.be|{{:ie0117_proyectos:linux_2013:youtube.png?300}}]]
[[teaching:ie0117:proyectos:2012:i:linux_2013:smpi|Top]] - [[ie0117_proyectos:linux_2013:|Projects]]
===== Conclusions =====
* We installed Raspbian on the Raspberry Pi using the instructions provided by the Raspberry Pi foundation.
* We were able to set up the Raspberry Pi as a portable video streamer. We configured it to connect to wireless connections automatically using a WI-FI USB Stick.
* We were able to setup a VPN using a PC as the server and the Raspberry Pi as a client. Unfortunately the VPN server didn't work with the laboratory's network, nevertheless, we were able to set it up at home.
* A web page page was created to display the video stream in a more appealing manner.
[[teaching:ie0117:proyectos:2012:i:linux_2013:smpi|Top]] - [[ie0117_proyectos:linux_2013:|Projects]]
----
http://www.palebluedot.nl/jml/computer-stuff/3-linux/33-webcam-streaming-with-raspberry-pi.html \\
http://wolfpaulus.com/jounal/embedded/raspberrypi_webcam \\
http://www.raspberrypi.org/phpBB3/viewtopic.php?t=9465&p=111445 \\
http://ramalave.blogspot.com/2012/11/instalar-tp-link-wn821n-en-debian-wheezy.html \\
http://www.raspberrypi.org/phpBB3/viewtopic.php?t=11517 \\
http://scottlinux.com/2010/06/29/stream-webcam-with-vlc/ \\
http://docs.openvpn.net/how-to-tutorialsguides/virtual-platforms/install-openvpn-access-server-on-linux-debian-6/ \\
http://sanjosetech.blogspot.com/2013/03/web-cam-streaming-from-raspberry-pi-to.html \\