List

Live streaming is getting very popular. Use cases are mostly for surveillance, parental control, touristic, advertising, sharing events and self-branding.

You can use YouTube, Facebook, Ustream, LiveStream, Periscope, SnapChat, and other applications for broadcasting a live stream. But, it has to be public if you don’t care and the privacy is not the problem.

If you need a private system there are other options which can cost more than $100 one-time payment for hardware and minimum $20/month payment for the server. If you want to learn these options you can read my blog post “How to stream live video?“.

Now, we know why it is important to building a server independent, portable, live streaming camera because doing so we don’t have to pay the monthly service fee and we don’t have to invest money on licenses and expensive hardware. Our criteria are

  • Low cost,
  • Standalone,
  • Portable,
  • Flexible,
  • Secure,
  • Easy to access,
  • Easy to build,
  • Durable and robust system.

What do we need?

  1. Raspberry Pi Motherboard.
    I used Raspberry Pi 3 Model B Motherboard which was the latest version for the project. The price was $37.
  2. Power Supply.
    I bought CanaKit 5V 2.5A Raspberry Pi 3 Power Adapter for $10. You must be careful while selecting a power supply. The power supply must be generating at least 2.5 amperages because we will be connecting a camera and may be more than one via USB interface which will be draining power from the Raspberry.
  3. Enclosure for the Raspberry Pi.
    I selected a clear enclosure which is compatible with Raspberry Pi Camera v1 and v2. I paid $6 for it. You can select any enclosure because we don’t have to use a Raspberry Camera, actually in this project we will be using a webcam instead of it.
  4. Web Camera.
    There are numerous of the webcam as you know. I used a Logitech C270 which can provide 720p (1280×720) resolution video. The price was $21. You can use any webcam. If you already have, you don’t have to buy a new one.
  5. Micro SD Card.
    You need at least an 8 GB capacity because of the operating system which needs 4GB. Bigger is better sure, but you must check the compatibility of the SD card. There is a list of compatible storages on the following link http://elinux.org/RPi_SD_cards. If you don’t have a spare micro SD card, you can buy a new one, and they are very cheap. A Samsung 32GB 80MB/s EVO is for only $10. There is plenty enough space for further applications especially for recording videos on local.
  6. Power Bank.
    It is not mandatory if you don’t need it to be working without a power cable attached. You can use any power bank which generates 5V/2.1A. Having more mAh capacity makes it work longer. I used an iMuto 5000mah 2USB 2.1A Portable Charger. There was a sale, and I paid $15 for two of them. You can use any reliable power bank. Do not search a special one for the Raspberry Pi. If the voltage and amperage are right, it is the right product.

In total, we need $99 investment which is under $100 bucks.

Other tools and devices you need to use Raspberry Pi (these items are just for preparing the system).

  1. Monitor: If you have an LCD monitor you can use it as a display for the Raspberry Pi. You can use a TV as a display too. All you need is an HDMI interface because it is the only way to connect. You can use adapters like VGA to HDMI. If you don’t have a display, you have to use remote access to control your Raspberry Pi. You can learn how to connect remotely in here. I won’t be talking about it. There is another way if you can pay more you can buy an LCD screen for your Raspberry Pi but it is not needed for this project.
  2. Keyboard and Mouse: You can use USB keyboard and mouse to control the Raspberry Pi. I’m using a wireless keyboard and mouse of Logitech which can use the same receiver. There is only 4 USB interface on the Raspberry Pi, and we must use them wisely.

Let’s start to make it!

  1. Preparing micro SD card: First, we must choose an operating system for the Raspberry Pi. Raspbian is the official operating system based on Linux Debian released by Raspberry Pi. The latest version of the Raspbian was Jessie (2016-09-23 release).  There are other third parties operating systems too. NOOBS is the installer package for Raspbian. I downloaded NOOBS offline and network install package from raspberrypi.org to my computer. It has 1.1 GB file size. Extract the compressed file after download.  Before moving files to micro SD card, it has to be formatted correctly. Otherwise, Raspberry Pi can’t recognize and use the SD card. There is an application, SD Formatter 4.0 for preparing your storage. But basically, it is formatting the medium in FAT32 file system because Raspberry can only work FAT file system (16 and 32). You can find more information about formatting in here.2016-11-10_124844
    2016-11-10_124859Once you formatted, you can copy the files inside the NOOB folder to your micro SD card. Do not copy the entire folder, just copy the files inside the extracted main folder. You need a micro SD card adapter to use your micro SD card with your PC. If your PC doesn’t have a card reader, you can use USB adapters.

    20161110_1224092016-11-10_124751After copying files, you can remove the SD card from your PC and put it back to Raspberry Pi. Please be careful not removing and inserting while the Raspbian is working. I’m using a micro USB extension cable cord with a button to switch power for Raspberry Pi which I have paid $5. I didn’t put this cable on the material list because it is not mandatory but it is useful for not damaging Raspberry Pi Motherboard and easily switching power.
  2. Putting parts together: We have everything ready; it is time to put them together.
    1. Put the Raspberry Pi Mother Board inside the enclosure/case and close it. Be gentle and patience, you don’t want to harm it.

      20161110_12032620161110_121129
    2. Put micro SD card. Be sure the right side of the SD card inserted properly. Do not push strongly. There is no locking mechanism, therefore, don’t expect a click sound to understand it is located.20161110_121219
    3. Connect the webcam to any USB port.20161110_121330
    4. Connect the power cord. Do not plug the adapter into outlet yet.
      20161110_121430
    5. Connect keyboard and the mouse. I’m connecting only the receiver, but you can connect cables to USB ports.
      20161110_121541
      20161110_121936
    6. Connect the display.
      20161110_121756
      20161110_121850
    7. Plug the adapter into the outlet. The red LED on the side will star to blinking.
      20161110_121436
    8. Raspberry Pi will boot up.
      20161115_150540
      20161115_150434
    9. Connect your Raspberry Pi to the internet over Wi-Fi following these instructions.
    10. Open Chromium Web Browser and navigate to this website and allow the browser to access web camera if it asks.
    11. If you can watch the camera stream on the browser, it is perfect.

Auto loading chromium browser and navigating to a specific web page when Raspberry Pi starts.

I tried different ways of auto loading a web page none of them worked except the method below.

Open the Terminal and be sure that you are actively under the /home/pi directory. You can check it with “pwd” command. 

mkdir ~/.config/autostart 
nano ~/.config/autostart/autoChromium.desktop

Paste the code below to the created file

[Desktop Entry]

Type=Application

Exec=/usr/bin/chromium-browser --noerrdialogs --disable-session-crashed-bubble --disable-infobars --kiosk https://www.bunver.com/projects/raspcam/player.html?id=put_your_id_here

Hidden=false

X-GNOME-Autostart-enabled=true

Name[en_US]=AutoChromium

Name=AutoChromium

Comment=Start Chromium when GNOME starts

You must edit the web page address https://www.bunver.com/projects/raspcam/camera.html?id=put_here_your_id by replacing the “put_here_your_id” parameter with your own. Do not use spaces; you can use underscores letters and numbers only. For example; “raspberrypicam_123xyz”. The edited address is https://www.bunver.com/projects/raspcam/camera.html?id= raspberrypicam_123xyz in this condition. Please use long and complex ids. This id is your secret key as a combination of username and password. Anyone can watch the video who knows this key and web address.

Save file and exit (Ctrl-X, Yes)

And reboot the Raspberry Pi with the reboot command. You can see that Raspberry Pi auto loads the web page in full screen. This mode is called Kiosk, for existing kiosk mode use Ctrl+Escape. It brings the menu, and you can access the tools again. We are using the kiosk mode because it is checking that the browser is running properly in silent mode which does not populate errors and other alerts.

On Chromium browser settings, “On start-up option” –  “Open the New Tab page” must be selected. Otherwise it opens another tab in each reboot, and after a while, Chrome browser can crash because of using too much memory. And we only need one active tab which connects to our web page and streams the camera feed.

Accessing Raspberry Pi remotely and watching the video.

You can use both desktop and mobile versions of Google Chrome, Firefox, and Internet Explorer to watch video from the camera. Just enter the link below in address or search bar.
https://www.bunver.com/projects/raspcam/player.html?id=put_your_id_here

Source Code

The source code of the WebRTC implementation is on GitHub.
https://github.com/brsunv/RaspberryPiStreamingCamera
There are some known bugs:

  • Video resolution is changing unexpectedly.
  • WebRTC connection cannot establish again if internet disconnects while streaming. You can restart the Raspberry Pi.

Next Steps

  1. Recording live stream on Raspberry Pi can be useful if you need to access later. It is possible; WebRTC is supporting this feature.
  2. For now, the system can work up to only 4 hours with 5000 mAh battery according to my trials. You can use a better power bank for more hours.
  3. You can use more than one camera simultaneously if you want.

Alternative Use-Cases

  1. WebRTC is providing two-way peer-to-peer audio, video, and data channel. You can use these channels for any purpose. In our implementation, I showed using of one-way video channel. It is possible to use two-way video and audio channel with some alterations in the code. You can create a video conferencing system.

Troubleshooting

  1. If you are having difficulties with connecting Raspberry Pi to secure networks like WPA2 Enterprise which is used by most of the institutions like universities and big companies, you can read my new blog post “Connecting Raspberry Pi to WPA2 Enterprise Wireless Network“.

Please don’t hesitate to contact me if you have any questions…

Video Demo

3 Responses to “Building an affordable live streaming camera using a Raspberry Pi”

  1. ketan ravat

    hello sir i followed all steps but at remote access it just appear fully black screen
    at pi side video appear but at remote side don’t
    can u please suggest what i do now…….

    • Baris Unver

      Hi Ketan, sorry I’m busy nowadays and will be replying your question soon. Thanks, Baris.

    • Baris Unver

      Basically, you need a signaling server for initiating WebRTC connection. If you can give more detail, I can help better. Thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *