Realtime Streaming from Raspberry Pi Camera to Android Phone

Ever wanted to build a low energy battery camera that you can place somewhere and be able to view the video on your phone? Does it need to be nearly realtime? Well.. then you’re right here.

Background Story

A couple of month ago I was working on a friends farm and we were driving around with the tractor bringing in the grass from the field. The problem was that when the trailer was full, we couldn’t see the screw conveyors at the back of the trailer anymore. As the controls didn’t work reliable anymore we had to get off the tractor, run all the way to the back of the trailer, check if they were still running and go back inside. Then I had the idea.. why not build a small camera that runs of a battery and streams the video to the Android Phone which is in the Tractor? The we had a few other use-cases were we wanted to use the camera so we had to build it very robust since it was supposed for outdoor use under pretty rough conditions.

Our requirements for the Camera

  • IP66 Protected
  • Battery based
  • Can also run off power plug (for long running installations)
  • Nearly realtime
  • Wireless (no cables)

That list looks quite difficult to implement, eh? Well.. Raspberry Pi Zero W was able to help us out.
Here’s a list of all the hardware we needed to build the camera.

Requirements / Hardware

To mention is, that you have to be careful about choosing the right power-bank here. Most power-banks don’t like to be charged and discharged at the same time. So basically you’re looking for a small “USV” for your raspberry pi. The power-bank I’ve linked above is capable of being charged while the raspberry pi is draining battery on the other usb port.

Stacking the hardware together

First off: I didn’t take pictures during the assembly process. That being said, two wholes need to be made into the case so that the usb connector and power-button will fit into into. After that, the battery pack needs to be opened. The manual power switch needs to be removed and instead the external power button needs to be soldered on. The short usb cable will be plugged into the power-bank while the other end needs to be cut off in order to solder on the external IP rated micro-usb adapter. The raspberry-pi will be connected to the power bank using the second usb cable. After that, put in some filling material so that the raspberry pi and power bank won’t move in the case.

Software

The camera is supposed to communicate directly with the Android Phone if necessary. When you’re out on the field, you don’t necessarily have a wifi router around that you can connect both devices to. So therefore we used the phone to host a WiFi Access-Point (How to setup Hotspot on Android Phone.
After that’s done, you’ll have to prepare your Raspberry Pi.

Install & Configure Raspberry PI

First you’ll need an operating system. Since you might not need a Graphical User-Interface you can download the Raspbian Stretch Lite Image from here. It’s quite small and super fast.
In order to flash Raspbian onto your raspberry pi SD-Card, I recommend using Etcher, it’s easy to use, works perfectly and looks great!
Just download the image, put the sd-card into your computer, start etcher and follow the instructions given on the screen. After you’re done flashing, you’ll have to change some files on the sd-card in order to enable ssh access.

  1. Open SD-Card on Computer (Open Editor)
  2. Add dtoverlay=dwc2 at the bottom of the existing file config.txt on the sdcard (followed by an empty line)
  3. Add modules-load=dwc2,g_ether to file cmdline.txt (after the rootwait parameter)
  4. create an empty file called ssh (no file extension!)

After that, you can eject the sdcard from your computer and put it in your raspberry pi. Then you can connect it to your computer (via USB) and
boot it up.. Then wait a little (1-2 min). After that you should be able to connect to your raspberry pi via ssh pi@raspberrypi.local (username is “pi” and password is “raspberry”)

When you’re logged in you’ll have to configure your Raspberry Pi to connect to your WiFi Hotspot. In order to do that, you’ll need to modify the wpa_supplicant.conf file (Link). Type in nano /etc/wpa_supplicant/wpa_supplicant.conf and copy the following content in there (adjust accordingly to your WiFi Information)

network={
    ssid="SmartTractor"
    psk="12345678"
    key_mgmt=WPA-PSK
    id_str="ap"
}

After that, save, exit and reboot it using the command sudo reboot. After the restart you should be able to see it connect to your WiFi by checking the ip-address (ifconfig).

Enable Camera on Raspberry Pi

By default the raspberry pi camera is disabled. To enable your camera, run the following command: sudo raspi-config. In the menu you’ll find an option were you can enable the camera. After that, you need to restart your raspberry pi.

Auto-Reconnect WiFi on Raspberry Pi

By default your Raspberry will connect to your WiFi only once. So if your WiFi goes down for a minute or your raspberry pi loses it’s connection due to being to far away from your hotspot for a minute, it won’t reconnect automatically. Therefore we need to make a few tweaks to your raspberry configuration to make it reconnect to your WiFi automatically.

Type in nano /etc/network/interfaces and replace the existing wpa-conf line with the wpa-roam line. Your config should look like the following:

allow-hotplug wlan0
iface wlan0 inet manual
    wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf

After that you’ll need to reboot in order to make your changes work.

Raspberry PI camera server

As for the software I’m using the great raspivid library as well as plain TCP Sockets to send the Video-Stream. The output of the raspivid command is piped into a netcat tcp server that the client connects to. Additionally a WebSocket Server is started to enable some communication between the Android Phone and the Raspberry Pi. The raspberry pi regularly reports it’s temperature, signal strength, package loss and network speed to the Android Client App. Therefore you’ll always know if you’re raspberry pi is getting a little hot. In order to save some energy and to be able to change the resolution from the Android Client at runtime, the Camera is only running when the Android Client is actually connected. After a connection loss, the camera will be able to reconnect to the client.

Android Client

The Android Client is capable of reading a H264 Video Stream from a TCP Socket. For connection and device monitoring it also uses a WebSocket to receive additional debug information from the Raspberry Pi. Things like vflip/hflip can be done within the app as well. Note that this will be done on Android itself and not on the camera. This makes it way faster to change camera settings without the need to restart the camera. It is also possible to choose between 4 different resolutions, depending on your WiFi connection strength and your bandwidth available. The Android App will automatically detect the Camera when it’s connected to the Android Hotspot we created before.

Setup Software (Raspberry PI)

In order to get this project working on your raspberry pi, you’ll have to get the python code here: github.com/David-Development/raspberrypi-camera-server

Follow the instructions given on the GitHub Page to install the software.

After that, you’ll have to create a cron-job entry to make the camera server start when you boot your raspberry pi as you might not want to manually ssh into it and start it manually.

Therefore login in to your raspberry pi via ssh and execute the following command: crontab -e. Paste the following content (make sure to adjust the path according to your setup!)

@reboot nice -n -20 python3 /home/pi/raspberrypi-camera-server/socket_server.py >> /home/pi/raspberrypi-camera-server/cron.log 2>&1

Setup Software (Android)

The source code as well as the instructions on how to setup the Android project can be found here: github.com/David-Development/raspberrypi-camera-android-client/

In the following video you can see how great and smooth the camera quality is when streaming the raspberry pi camera video feed to Android.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.