Pi Zero W Streaming Dashcam

Tidying my office a few days ago, I came across some car reversing monitors that I used to use as cheap Pi displays for use in the chase car, to show the distance and direction to the payload; these days I use the official Pi touchscreen as it’s a lot better for that application.  One of the monitors is a flip-up model, and I wondered how much space there was inside.  I use the Pi Zero a lot for balloon trackers, as it’s small and light compared to other Pi models, but perhaps one could fit one inside the base to make a smart dashcam – one that can stream my balloon chases to Youtube as well as record to SD.

About the same time, Michael Horne (author of the excellent Pi Pod blog), posted a picture of a similar-looking model on Twitter, asking how to power it from 5V.  That’s the opposite of what I wanted to do (power the Pi Zero from the 5V rail inside the monitor) but I felt I might be able to help so I opened up my unit to find where the 5V could be tapped.  As it turned out, Michael’s unit had a very different PCB to mine, but the seed was sewn so I decided to start building my dashcam.

Pi Zero to Monitor

First job was to connect the the display to a Pi Zero W (W because I want to be able to stream the camera video). This requires the 5V and GND lines on the GPIO pins, plus the composite video output pin, to be wired to their counterparts in the monitor.  Once I’d used the correct video pin this worked without issue!

I don’t know how much spare current capacity the display has on the 5V rail, but it dropped slightly from 5V to 4.9V which is OK.  The Pi Zero booted and ran continuously overnight with no issues and with nothing on the display PCB getting hot.

I connected the Pi Zero to my LAN via a USB LAN adapter, ssh’d to it, then set the video aspect ratio to match the monitor (16:9).

sdtv_aspect=3

The full set of options is:

I also updated/upgraded Raspbian, and set up the WiFi.

ffmpeg

Next steps were to enable and connect the camera, and to install ffmpeg, which is what I use to stream to YouTube.  I used these instructions to install the library:

cd /usr/src
git clone git://git.videolan.org/x264
cd x264
./configure --host=arm-unknown-linux-gnueabi --enable-static --disable-opencl
make
sudo make install

and then ffmpeg.  This takes several hours to build, so it’s a good time to find something else to do!

cd /usr/src
git clone git://source.ffmpeg.org/ffmpeg.git
cd ffmpeg/
sudo ./configure --arch=armel --target-os=linux --enable-gpl --enable-libx264 --enable-nonfree
make

Aspect Ratio

I then tested the video streaming to YouTube with a command like this:

raspivid -o - -t 0 -w 640 -h 360 -fps 25 -b 500000 -g 50 | ffmpeg -re -ar 44100 -ac 2 -acodec pcm_s16le -f s16le -ac 2 -i /dev/zero -f h264 -i - -vcodec copy -acodec aac -ab 128k -g 50 -strict experimental -f flv rtmp://a.rtmp.youtube.com/live2/<stream ID>

which worked to Youtube, however the preview displayed on the monitor was distorted.  I thought that this was to do with Raspbian not correctly applying the screen resolution, but no matter what I did to fix that (specifying the resolution explicitly in config.txt, or specifying the preview window dimensions) fixed it.  Eventually I concluded that the issue was within raspivid, and then I soon found a very relevant forum post, which explained how to modify raspivid:

git clone https://github.com/raspberrypi/userland.git
cd userland
./buildme

Then add these lines near line 116 of RaspiPreview.c:

param.set |= MMAL_DISPLAY_SET_NOASPECT;
param.noaspect= MMAL_TRUE;

then rebuild.  After following these steps, the problem was gone!

Assembly

With the base plate removed, I threaded the flat camera cable up through the back of the base, behind the support that goes up to the top of the display, connected the camera and fixed it in position with Sugru:

I then added a piece of black duct tape covered the cable to stop it snagging and make it look tidy.

Wiring

The Pi Zero has 3 wires connected – 5V, 0V and video, which on my monitor go to a convenient electrolytic capacitor and the back of a PCB socket.  Finally, a switch is mounted near the front of the monitor’s base, and connected to a GPIO pin and GND:

Everything was then insulated with duct tape before screwing on the metal base.

Script

I wanted the dashcam to work in one of 2 modes – to record as a dashcam normally would, and to also live stream to YouTube (via the WiFi connection to a phone or MyFi device, for example).  So I wrote a small script that switches modes according to the position of a switch connected to a GPIO pin.  On startup, or when the switch position changes, the script runs the appropriate shell command for that mode.  For regular dashcam recording, that’s a simple raspivid command; for streaming it pipes the raspivid output through ffmpeg (see command above).  At present I’m not recording the video, so I need to do that using rotating file names, over-writing old files before the SD card fills up, and recording at a high resolution but streaming at a lower resolution.

Results

 

This entry was posted in Weather Balloon. Bookmark the permalink.

2 Responses to Pi Zero W Streaming Dashcam

  1. Pingback: Streaming Dashcam with Pi Zero W #piday #raspberrypi @Raspberry_Pi « Adafruit Industries – Makers, hackers, artists, designers and engineers!

  2. Pingback: Streaming Dashcam with Pi Zero W #piday #raspberrypi @Raspberry_Pi – Area 42

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.