{"id":85,"date":"2017-11-21T22:26:24","date_gmt":"2017-11-21T22:26:24","guid":{"rendered":"http:\/\/peder.ddns.net\/aursand\/?p=85"},"modified":"2021-11-09T17:03:42","modified_gmt":"2021-11-09T17:03:42","slug":"raspberry-pi-camera-stream-with-control-dashboard","status":"publish","type":"post","link":"https:\/\/drgaff.net\/?p=85","title":{"rendered":"Raspberry Pi camera stream with control dashboard"},"content":{"rendered":"<p>I made my Raspberry Pi into a controllable security camera so I can stalk my cat from work. Here is how I did it.<\/p>\n<h2>Pre-requisites<\/h2>\n<ul>\n<li><a href=\"https:\/\/www.raspberrypi.org\/products\/raspberry-pi-3-model-b\/\" target=\"_blank\" rel=\"noopener noreferrer\">Raspberry Pi 3 B<\/a><\/li>\n<li>Micro SD card (8GB or larger) with <a href=\"https:\/\/www.raspberrypi.org\/downloads\/raspbian\/\" target=\"_blank\" rel=\"noopener noreferrer\">raspbian<\/a> installed<\/li>\n<li><a href=\"https:\/\/www.raspberrypi.org\/products\/camera-module-v2\/\" target=\"_blank\" rel=\"noopener noreferrer\">Raspberry Pi camera module<\/a><\/li>\n<\/ul>\n<h2>Hooking up the camera module and enabling it<\/h2>\n<p>The camera is connected to the Raspberry by inserting the Flex cable into the slot situated between the HDMI port and the Ethernet port.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-99\" src=\"http:\/\/drgaff.net\/wp-content\/uploads\/1475993783654807.jpg\" alt=\"\" width=\"700\" height=\"525\"><\/p>\n<p>Once the camera in inserted it must be enabled. Type<\/p>\n<blockquote>\n<pre>sudo raspi-config<\/pre>\n<\/blockquote>\n<p>and go to&nbsp;<strong>Interfacing Options&nbsp;<\/strong>and enable the camera module.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter  wp-image-97\" src=\"https:\/\/drgaff.net\/wp-content\/uploads\/rasp-config.png\" alt=\"\" width=\"479\" height=\"288\"><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-96\" src=\"https:\/\/drgaff.net\/wp-content\/uploads\/raspi_camera_menu_large.jpg\" alt=\"\" width=\"479\" height=\"257\"><\/p>\n<p>Once the camera has been plugged in and enabled you can test it by taking a still picture<\/p>\n<blockquote>\n<pre> raspistill -o image.jpg<\/pre>\n<\/blockquote>\n<h2>Video stream using mjpg-streamer<\/h2>\n<p>I used <a href=\"https:\/\/github.com\/jacksonliam\/mjpg-streamer\" target=\"_blank\" rel=\"noopener noreferrer\">mjpeg-streamer<\/a> to run a lightweight webcam stream hosted on a website. It works well because it has explicit support for the Raspberry Pi camera module. First, install the dependencies:<\/p>\n<blockquote>\n<pre>sudo apt-get update\nsudo apt-get install git cmake libjpeg8-dev<\/pre>\n<\/blockquote>\n<p>Then, clone the repository<\/p>\n<blockquote>\n<pre>git clone https:\/\/github.com\/jacksonliam\/mjpg-streamer.git<\/pre>\n<\/blockquote>\n<p>and build the code<\/p>\n<blockquote>\n<pre>cd mjpg-streamer-experimental\nmkdir _build\ncd _build\ncmake ..\nmake\nsudo make install<\/pre>\n<\/blockquote>\n<p>Move the compiled .so-files to an appropriate system directory:<\/p>\n<blockquote>\n<pre>cd ..\nsudo cp output_http.so \/usr\/local\/lib\/.\nsudo cp input_raspicam.so \/usr\/local\/lib\/.<\/pre>\n<\/blockquote>\n<p>Make a folder for the website and move the sample live stream html file there :<\/p>\n<blockquote>\n<pre>sudo mkdir \/var\/www\nsudo cp .\/www\/stream_simple.html \/var\/www\/index.html<\/pre>\n<\/blockquote>\n<p>We are now ready to run the live stream. The following command will stream the input from the Raspberry Pi camera to a web server listening to the port 8080:<\/p>\n<blockquote>\n<pre>mjpg_streamer -o \"\/usr\/local\/lib\/output_http.so -w \/var\/www\" -i \"\/usr\/local\/lib\/input_raspicam.so\"<\/pre>\n<\/blockquote>\n<p>The red light on the camera module should now light up. On the same network as the Raspberry, the stream can now be found by opening a browser and going to <strong>http:\/\/&lt;raspberry ip&gt;:8080<\/strong><\/p>\n<h2>Making it into a service<\/h2>\n<p>By making the live stream into a system service, we can make it turn on automatically on reboot (or after power outage) and more easily control turning it on and off. Create a new file called <strong>\/etc\/systemd\/system\/livestream.service<\/strong>&nbsp;with the following content:<\/p>\n<blockquote>\n<pre>[Unit]\nDescription=Webcam service\nAfter=multi-user.target network.target network-online.target\n\n[Service]\nType=idle\nExecStart=\/usr\/local\/bin\/mjpg_streamer -o \"\/usr\/local\/lib\/output_http.so -w \/var\/www -c <span style=\"color: #ff0000;\">&lt;username&gt;<\/span>:<span style=\"color: #ff0000;\">&lt;password&gt;<\/span>\" -i \"\/usr\/local\/lib\/input_raspicam.so\"\n\n[Install]\nWantedBy=multi-user.target\n<\/pre>\n<\/blockquote>\n<p>Remember to replace <strong>&lt;username&gt;<\/strong> and <strong>&lt;password&gt;<\/strong>&nbsp;in the line starting with ExecStart above. This will be the credentials used to access the livestream.<\/p>\n<p>To run the live stream automatically on boot, we enable the service:<\/p>\n<blockquote>\n<pre>sudo chmod 664 \/etc\/systemd\/system\/livestream.service\nsudo systemctl enable livestream.service<\/pre>\n<\/blockquote>\n<h2>Accessing the stream from outside the local network<\/h2>\n<p>To be able to view the live stream from outside your local network, go into your router&#8217;s Port Forwarding settings and forward any outgoing requests for port 8080 to the internal port 8080 on the Raspberry Pi&#8217;s local IP address.<\/p>\n<h2>Turning the camera on at specific times<\/h2>\n<p>The set up so far will make the camera stream stay on for as long as the Raspberry Pi is on. For extra security and privacy, it might be useful to only run the camera stream when you are at work or out of the house. One simple way to make this happen is to make a&nbsp;<strong>cron<\/strong> job. Edit the system crontab of the Raspberry:<\/p>\n<blockquote>\n<pre>sudo crontab -e<\/pre>\n<\/blockquote>\n<p>Then, write the following:<\/p>\n<blockquote>\n<pre>30 9 * * 1-5 systemctl start livestream\n30 16 * * 1-5 systemctl stop livestream<\/pre>\n<\/blockquote>\n<p>This will make the livestream turn itself on every weekday (1-5) at 9:30 am and turn itself off every weekday at 4:30 pm. Change the example above as appropriate.<\/p>\n<h2>Control dashboard<\/h2>\n<p>In addition to the steps above I also set up a dashboard so I can more easily control the camera from any computer or phone connected to my local network at home. This was a little bit more involved and might be the topic of a future post.<\/p>\n<figure id=\"attachment_89\" aria-describedby=\"caption-attachment-89\" style=\"width: 550px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-89\" src=\"https:\/\/drgaff.net\/wp-content\/uploads\/dashboard.png\" alt=\"\" width=\"550\" height=\"721\"><figcaption id=\"caption-attachment-89\" class=\"wp-caption-text\">Dashboard for controlling camera hosted on the local network<\/figcaption><\/figure>\n<h2><\/h2>\n","protected":false},"excerpt":{"rendered":"<p>I made my Raspberry Pi into a controllable security camera so I can stalk my cat from work. Here is how I did it. Pre-requisites Raspberry Pi 3 B Micro SD card (8GB or larger) with raspbian installed Raspberry Pi camera module Hooking up the camera module and enabling it The camera is connected to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-85","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/drgaff.net\/index.php?rest_route=\/wp\/v2\/posts\/85","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/drgaff.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/drgaff.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/drgaff.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/drgaff.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=85"}],"version-history":[{"count":31,"href":"https:\/\/drgaff.net\/index.php?rest_route=\/wp\/v2\/posts\/85\/revisions"}],"predecessor-version":[{"id":194,"href":"https:\/\/drgaff.net\/index.php?rest_route=\/wp\/v2\/posts\/85\/revisions\/194"}],"wp:attachment":[{"href":"https:\/\/drgaff.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=85"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/drgaff.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=85"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/drgaff.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=85"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}