{"id":2304,"date":"2018-04-01T12:53:40","date_gmt":"2018-04-01T12:53:40","guid":{"rendered":"http:\/\/www.daveakerman.com\/?p=2304"},"modified":"2018-04-01T14:44:43","modified_gmt":"2018-04-01T14:44:43","slug":"pi-zero-dashcam","status":"publish","type":"post","link":"http:\/\/www.daveakerman.com\/?p=2304","title":{"rendered":"Pi Zero W Streaming Dashcam"},"content":{"rendered":"<p>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&#8217;s a lot better for that application.\u00a0\u00a0One of the monitors is a flip-up model, and I wondered how much space there was inside.\u00a0\u00a0I use the Pi Zero a lot for balloon trackers, as it&#8217;s small and light compared to other Pi models, but perhaps one could fit one inside the base to make a smart dashcam &#8211; one that can stream my balloon chases to Youtube as well as record to SD.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full wp-image-2308\" src=\"http:\/\/www.daveakerman.com\/wp-content\/uploads\/2018\/03\/5909b3b7d80321.jpg\" alt=\"\" width=\"700\" height=\"531\" srcset=\"http:\/\/www.daveakerman.com\/wp-content\/uploads\/2018\/03\/5909b3b7d80321.jpg 700w, http:\/\/www.daveakerman.com\/wp-content\/uploads\/2018\/03\/5909b3b7d80321-300x228.jpg 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/p>\n<p>About the same time, Michael Horne (author of the excellent <a href=\"http:\/\/www.recantha.co.uk\/blog\/\">Pi Pod blog<\/a>), posted a picture of a similar-looking model on Twitter, asking how to power it from 5V.\u00a0 That&#8217;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.\u00a0 As it turned out, Michael&#8217;s unit had a very different PCB to mine, but the seed was sewn so I decided to start building my dashcam.<\/p>\n<h2>Pi Zero to Monitor<\/h2>\n<p>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.\u00a0 Once I&#8217;d used the correct video pin this worked without issue!<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-large wp-image-2310\" src=\"http:\/\/www.daveakerman.com\/wp-content\/uploads\/2018\/03\/tft-1024x554.jpg\" alt=\"\" width=\"640\" height=\"346\" srcset=\"http:\/\/www.daveakerman.com\/wp-content\/uploads\/2018\/03\/tft-1024x554.jpg 1024w, http:\/\/www.daveakerman.com\/wp-content\/uploads\/2018\/03\/tft-300x162.jpg 300w, http:\/\/www.daveakerman.com\/wp-content\/uploads\/2018\/03\/tft-768x416.jpg 768w, http:\/\/www.daveakerman.com\/wp-content\/uploads\/2018\/03\/tft.jpg 2046w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/p>\n<p>I don&#8217;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.\u00a0 The Pi Zero booted and ran continuously overnight with no issues and with nothing on the display PCB getting hot.<\/p>\n<p>I connected the Pi Zero to my LAN via a USB LAN adapter, ssh&#8217;d to it, then set the video aspect ratio to match the monitor (16:9).<\/p>\n<pre>sdtv_aspect=3<\/pre>\n<p>The full set of options is:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full wp-image-2311\" src=\"http:\/\/www.daveakerman.com\/wp-content\/uploads\/2018\/03\/aspect.png\" alt=\"\" width=\"171\" height=\"130\" \/><\/p>\n<p>I also updated\/upgraded Raspbian, and set up the WiFi.<\/p>\n<h2>ffmpeg<\/h2>\n<p>Next steps were to enable and connect the camera, and to install ffmpeg, which is what I use to stream to YouTube.\u00a0 I used <a href=\"http:\/\/engineer2you.blogspot.co.uk\/2016\/10\/rasbperry-pi-ffmpeg-install-and-stream.html\">these instructions<\/a>\u00a0to install the library:<\/p>\n<pre>cd \/usr\/src\r\ngit clone git:\/\/git.videolan.org\/x264\r\ncd x264\r\n.\/configure --host=arm-unknown-linux-gnueabi --enable-static --disable-opencl\r\nmake\r\nsudo make install<\/pre>\n<p>and then ffmpeg.\u00a0 This takes several hours to build, so it&#8217;s a good time to find something else to do!<\/p>\n<pre>cd \/usr\/src\r\ngit clone git:\/\/source.ffmpeg.org\/ffmpeg.git\r\ncd ffmpeg\/\r\nsudo .\/configure --arch=armel --target-os=linux --enable-gpl --enable-libx264 --enable-nonfree\r\nmake<\/pre>\n<h2>Aspect Ratio<\/h2>\n<p>I then tested the video streaming to YouTube with a command like this:<\/p>\n<pre>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\u00a0rtmp:\/\/a.rtmp.youtube.com\/live2\/&lt;stream ID&gt;<\/pre>\n<p>which worked to Youtube, however the preview displayed on the monitor was distorted.\u00a0 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.\u00a0 Eventually I concluded that the issue was within raspivid, and then I soon found a <a href=\"https:\/\/www.raspberrypi.org\/forums\/viewtopic.php?t=64635\">very relevant forum post<\/a>, which explained how to modify raspivid:<\/p>\n<pre>git clone https:\/\/github.com\/raspberrypi\/userland.git\r\ncd userland\r\n.\/buildme<\/pre>\n<p>Then add these lines near line 116 of RaspiPreview.c:<\/p>\n<pre>param.set |= MMAL_DISPLAY_SET_NOASPECT;\r\nparam.noaspect= MMAL_TRUE;<\/pre>\n<p>then rebuild.\u00a0 After following these steps, the problem was gone!<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-large wp-image-2317\" src=\"http:\/\/www.daveakerman.com\/wp-content\/uploads\/2018\/04\/P1130414-768x1024.jpg\" alt=\"\" width=\"640\" height=\"853\" srcset=\"http:\/\/www.daveakerman.com\/wp-content\/uploads\/2018\/04\/P1130414-768x1024.jpg 768w, http:\/\/www.daveakerman.com\/wp-content\/uploads\/2018\/04\/P1130414-225x300.jpg 225w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/p>\n<h2>Assembly<\/h2>\n<p>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,\u00a0connected the camera and fixed it in position with Sugru:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-large wp-image-2313\" src=\"http:\/\/www.daveakerman.com\/wp-content\/uploads\/2018\/03\/sugru-1024x751.jpg\" alt=\"\" width=\"640\" height=\"469\" srcset=\"http:\/\/www.daveakerman.com\/wp-content\/uploads\/2018\/03\/sugru-1024x751.jpg 1024w, http:\/\/www.daveakerman.com\/wp-content\/uploads\/2018\/03\/sugru-300x220.jpg 300w, http:\/\/www.daveakerman.com\/wp-content\/uploads\/2018\/03\/sugru-768x563.jpg 768w, http:\/\/www.daveakerman.com\/wp-content\/uploads\/2018\/03\/sugru.jpg 1416w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/p>\n<p>I then added a piece of black duct tape covered the cable to stop it snagging and make it look tidy.<\/p>\n<h2>Wiring<\/h2>\n<p>The Pi Zero has 3 wires connected &#8211; 5V, 0V and video, which on my monitor go to a convenient electrolytic capacitor and the back of a PCB socket.\u00a0 Finally, a switch is mounted near the front of the monitor&#8217;s base, and connected to a GPIO pin and GND:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-large wp-image-2316\" src=\"http:\/\/www.daveakerman.com\/wp-content\/uploads\/2018\/04\/P1130413-1024x768.jpg\" alt=\"\" width=\"640\" height=\"480\" srcset=\"http:\/\/www.daveakerman.com\/wp-content\/uploads\/2018\/04\/P1130413-1024x768.jpg 1024w, http:\/\/www.daveakerman.com\/wp-content\/uploads\/2018\/04\/P1130413-300x225.jpg 300w, http:\/\/www.daveakerman.com\/wp-content\/uploads\/2018\/04\/P1130413-768x576.jpg 768w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/p>\n<p>Everything was then insulated with duct tape before screwing on the metal base.<\/p>\n<h2>Script<\/h2>\n<p>I wanted the dashcam to work in one of 2 modes &#8211; 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).\u00a0 So I wrote a small script that switches modes according to the position of a switch connected to a GPIO pin.\u00a0 On startup, or when the switch position changes, the script runs the appropriate shell command for that mode.\u00a0 For regular dashcam recording, that&#8217;s a simple raspivid command; for streaming it pipes the raspivid output through ffmpeg (see command above).\u00a0 At present I&#8217;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.<\/p>\n<h2>Results<\/h2>\n<p>&nbsp;<\/p>\n<p><iframe loading=\"lazy\" width=\"640\" height=\"360\" src=\"https:\/\/www.youtube.com\/embed\/gvdW3EeAm_U?feature=oembed\" frameborder=\"0\" allow=\"autoplay; encrypted-media\" allowfullscreen><\/iframe><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;s a lot [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[],"_links":{"self":[{"href":"http:\/\/www.daveakerman.com\/index.php?rest_route=\/wp\/v2\/posts\/2304"}],"collection":[{"href":"http:\/\/www.daveakerman.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.daveakerman.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.daveakerman.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.daveakerman.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2304"}],"version-history":[{"count":12,"href":"http:\/\/www.daveakerman.com\/index.php?rest_route=\/wp\/v2\/posts\/2304\/revisions"}],"predecessor-version":[{"id":2322,"href":"http:\/\/www.daveakerman.com\/index.php?rest_route=\/wp\/v2\/posts\/2304\/revisions\/2322"}],"wp:attachment":[{"href":"http:\/\/www.daveakerman.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2304"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.daveakerman.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2304"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.daveakerman.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2304"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}