Tuesday, June 23, 2015

Connect camera to Raspberry Pi and stream video

In my previous blog posts I have described how to configure Raspberry Pi for first time and how to prepare SD card with raspbian image.

Work with Raspberry without Key board, Mouse or Monitor
Write raspbian disk image to SD and Backup your SD Card on Linux

Connecting Raspberry Pi camera module with a Raspberry Pi is pretty simple. All you need is connect camera ribbon cable to CSI (Camera Serial Interface) port as follows. Please note that you have to aware about the correct side of the ribbon cable.

The flex cable inserts into the connector situated between the Ethernet and HDMI ports, with the silver connectors facing the HDMI port. The flex cable connector should be opened by pulling the tabs on the top of the connector upwards then towards the Ethernet port. The flex cable should be inserted firmly into the connector, with care taken not to bend the flex at too acute an angle. The top part of the connector should then be pushed towards the HDMI connector and down, while the flex cable is held in place.

Here is small video guide created by Raspberry Pi team about, how to connecting the camera correctly.


After you connecting the camera correctly, it is needed to enable kernel modules and drivers for camera functionality. So use "sudo raspi-config" to start configurations.


Then Enable camera and restart your Raspberry Pi. Once device is booted use following command to start the camera. If you have a display connected with raspberry Pi, it will display the video feed from camera. Also RED LED in the camera module will turn on by indicating camera activity.

raspivid -t 0

Then press CTRL+C to terminate the camera feed.

Now we need an internet connection to install the streaming server. I preferred vlc for video streaming since it supports h264 encoding and RTSP protocol. To install vlc, you need to update your software repositories in Raspberry Pi. Use this command to update software repositories.

sudo apt-get update

Then install vlc;

sudo apt-get install vlc

Now you are ready to test video streaming. So in first you need to run command with following format. It start raspivid and pipe output to console. Then cvlc tool can use to encode and stream video feed.

raspivid -o - -t 0 -w <width> -h <height> -fps <frame rate> -b <bit rate> |cvlc -v stream:///dev/stdin --sout '#rtp{sdp=rtsp://:<port>/}' :demux=h264

You need to replace <width> with frame width and <height> with frame height which is compatible with 4:3 or 16:9 accept ratios. Replace <frame_rate> with the value that you need to grab frames per second. Higher frame rates contains smooth video but need higher bit rate and high speed network connection. Video bit rate should be assigned as bit per second value. Higher bit rates deliver much clear video and deliver higher video quality. But it also required high speed stable network connection. Finally <port> is used to write stream data to network and you will be able to access video stream via the specified port of your network interface.

Finally you should have to find IP address of your Raspberry Pi device. If you don't know your IP address using ifconfig command.

This is a sample command for stream video with 640x480@15fps 378kbps. This stream can be access through the port 8554.

raspivid -o - -t 0 -w 640 -h 480 -fps 15 -b 393216 |cvlc -v stream:///dev/stdin --sout '#rtp{sdp=rtsp://:8554/}' :demux=h264

So if you got IP address like 192.168.43.245, then you can access your video stream using your vlc media player in computer or with your mobile phone which are connected with the same network which your Raspberry connected.

Lets assume IP of your Raspberry Pi is 192.168.43.245, you can access video stream via rtsp://192.168.43.245:8554/

If you have MX Player in your android device you can access video stream via Network stream.



This is how I run it at my office. You can see my mobile is also showing the video feed.





Wednesday, June 10, 2015

Write raspbian disk image to SD and Backup your SD Card on Linux

In Linux, we have lot more cool stuffs to create raspberry SD card and backup your SD card in to PC or Laptop. You might need root permission to execute commands with dd tool.

Identify the SD card
  1. Remove if you have any SD card inserted and then run df -h to see what devices are currently mounted.

  2. Insert the SD card and run df -h again. The device that wasn't there last time is your SD card. The left column gives the device name of your SD card. It will be listed as something like /dev/mmcblk0p1 or /dev/sdd1.

    The last part ("p1" or "1" respectively) is the partition number, but you want to write to the whole SD card, not just one partition, so you need to remove that part from the name (getting for example /dev/mmcblk0 or /dev/sdd) as the device for the whole SD card. Note that as in screenshot the SD card can show up more than once in the output of df: in fact it will if you have previously written a Raspberry Pi image to this SD card, because the Raspberry Pi SD images have more than one partition.
  3. Now that you've noted what the device name is, you need to unmount it so that files can't be read or written to the SD card while you are copying over the SD image. So run the command below, replacing mmcblk0p5 with whatever your SD card's device name is (including the partition number) If your SD card shows up more than once in the output of df due to having multiple partitions on the SD card, you should unmount all of these partitions.
    umount /dev/mmcblk0p5
Write raspbian disk image to SD
    WARNING: 
    You might need some knowledge about Linux terminal commands and if something went wrong, you might even lost your data.
    Please note that the use of the dd tool can overwrite any partition of your machine. If you specify the wrong device in the instructions below you could delete your primary Linux partition. Please be careful.

    In the terminal write the image to the card with dd command, making sure you replace the input file if= argument with the path to your .img file, and the /dev/mmcblk0 in the output file of= argument with the right device name (this is very important: you will lose all data on the hard drive on your computer if you get the wrong device name). Make sure the device name is the name of the whole SD card as described above, not just a partition of it (for example, sdd, not sdds1 or sddp1, or mmcblk0 not mmcblk0p1)

    dd bs=1M if=~/Documents/2015-05-05-raspbian-wheezy.img of=/dev/mmcblk0


    Note that if you are not logged in as root you will need to prefix this with sudo. The dd command does not give any information of its progress and so may appear to have frozen. It could take more than five minutes to finish writing to the card. To see the progress of the copy operation you can run pkill -USR1 -n -x dd in another terminal (prefixed with sudo if you are not logged in as root). The progress will be displayed (perhaps not immediately, due to buffering) in the original window, not the window with the pkill command. Another option to monitor progress is to use Pipe Viewer (pv). Pipe dd input part through pv to the output part of dd:

    dd bs=4M if=2014-09-09-wheezy-raspbian.img | pv | dd of=/dev/mmcblk0

    Remove SD card from card reader, insert it in the Raspberry Pi, and have fun.


    Backup your SD Card

    You can simply backup your SD card to PC or Laptop using dd tool. Just only have to swap if and of in the image writing command:

    dd bs=1M if=/dev/mmcblk0 of=~/Documents/raspi-backup.img

    But if the SD card is with higher capacity and plenty of free space; (example of 32GB with only 4GB is used) the image file is a waste of space since it took entire SD card size from your hard drive (32GB image file will save in your hard drive according to the example).

    To avoid hard disk space wastage, you can use gzip to compress your SD card image while you creating it.

    dd if=/dev/mmcblk0 | gzip > ~/Documents/raspi-backup.img.gz

    Then your image will compressed it self and you can extract it later if required. How ever if you need to write your compressed image directly without extracting it from compressed archive, you can do it with following command.

    cat backup.img.gz | gunzip | dd of=/dev/mmcblk0