Building an affordable live streaming camera using a Raspberry Pi

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?

Raspberry Pi Motherboard.
I used Raspberry Pi 3 Model B Motherboard which was the latest version for the project. The price was $37.

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.

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.

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.

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.

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).

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.

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!

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.Once 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.After 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.

Putting parts together: We have everything ready; it is time to put them together.

Put the Raspberry Pi Mother Board inside the enclosure/case and close it. Be gentle and patience, you don’t want to harm it.

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.

Connect the webcam to any USB port.

Connect the power cord. Do not plug the adapter into outlet yet.

Connect keyboard and the mouse. I’m connecting only the receiver, but you can connect cables to USB ports.

Connect the display.

Plug the adapter into the outlet. The red LED on the side will star to blinking.

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.

Source Code

WebRTC connection cannot establish again if internet disconnects while streaming. You can restart the Raspberry Pi.

Next Steps

Recording live stream on Raspberry Pi can be useful if you need to access later. It is possible; WebRTC is supporting this feature.

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.

You can use more than one camera simultaneously if you want.

Alternative Use-Cases

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.