{"id":1585,"date":"2016-02-04T15:30:11","date_gmt":"2016-02-04T13:30:11","guid":{"rendered":"https:\/\/blog.zitnik.si\/?p=1585"},"modified":"2016-02-04T15:30:11","modified_gmt":"2016-02-04T13:30:11","slug":"part-23-openhab-rpi-leds-camera","status":"publish","type":"post","link":"https:\/\/blog.zitnik.si\/?p=1585","title":{"rendered":"Part 2\/3: OpenHAB + RPi + LEDs + Camera"},"content":{"rendered":"<p>We are continuing with the example from the first part of the tutorial: <a href=\"https:\/\/blog.zitnik.si\/2016\/02\/04\/part-13-controlling-leds-using-raspberry-pi\/\" target=\"_blank\">https:\/\/blog.zitnik.si\/2016\/02\/04\/part-13-controlling-leds-using-raspberry-pi\/<\/a>.<\/p>\n<p>In this part we are going to install OpenHAB platform on an RPi and remotely control LEDs.<\/p>\n<p>OpenHAB (<a href=\"http:\/\/www.openhab.org\/\" target=\"_blank\">http:\/\/www.openhab.org\/<\/a>) \u00a0is a software that is intended to connect to many sensors or devices for home automation. It works as a smart devices gateway and includes tools to design a custom GUI to control these connected smart devices. A GUI can be accessed using a web browser or from a nice mobile application (iOS, Android).<\/p>\n<p><strong>OpenHab Installation<\/strong><\/p>\n<p>We first installed OpenHAB core and then Demo from the official site: <a href=\"http:\/\/www.openhab.org\/getting-started\/downloads.html\" target=\"_blank\">http:\/\/www.openhab.org\/getting-started\/downloads.html<\/a>. These are compressed packages which I unzip into \/opt\/openhab folder.\u00a0Within the new directory, there\u2019ll be a folder called configurations. Within this folder create a copy of the default configuration file openhab_default.cfg and name it openhab.cfg. This is where you\u2019ll store all your settings for openHAB and any bindings you need.<\/p>\n<p>To start the OpenHAB server, run \/opt\/openhab\/start.sh.<\/p>\n<p><em>OpenHAB configuration<\/em><\/p>\n<p>To control our LEDs we will need to create three files.<\/p>\n<p>Create a file configurations\/items\/default.items. This will be an inventory for definition of our sensors. The file should look like:<\/p>\n<p>[codesyntax lang=&#8221;text&#8221;]<\/p>\n<pre>Switch RedLight\t\t\t\t\"Rdeca luc\"\r\nSwitch GreenLight\t\t\t\"Zelena luc\"\r\nSwitch YellowLight\t\t\t\"Rumena luc\"<\/pre>\n<p>[\/codesyntax]<\/p>\n<p>The next file should define the rules and actions for these sensors. So, create a file named configurations\/rules\/default.rules to look like:<\/p>\n<p>[codesyntax lang=&#8221;text&#8221;]<\/p>\n<pre>import org.openhab.core.library.types.*\r\nimport org.openhab.core.persistence.*\r\nimport org.openhab.model.script.actions.*\r\nimport org.openhab.action.pushover.*\r\nimport org.openhab.action.pushover.internal.*\r\n\r\nrule \"RedLight changed\"\r\nwhen\r\n Item RedLight received command ON\r\nthen\r\n logInfo(\"RedLight\", \"Received red light command to turn on.\")\r\n executeCommandLine(\"python \/home\/pi\/red_on.py\")\r\n pushover(\"Rdeca luc je bila prizgana.\")\r\nend\r\n\r\nrule \"RedLight changed\"\r\nwhen\r\n Item RedLight received command OFF\r\nthen\r\n logInfo(\"RedLight\", \"Received red light command to turn on.\")\r\n executeCommandLine(\"python \/home\/pi\/red_off.py\")\r\nend<\/pre>\n<p>[\/codesyntax]<\/p>\n<p>As you can see I also configured the pushover service so that I am notified about the change via a push notification. It can also be observed that on some command received, a log message is created and a command line script is executed. These scripts look the same as in the first part of the tutorial, where we introduced how to turn LED on or off.<\/p>\n<p>In the last file, named configuration\/sitemaps\/default.sitemap we will define the GUI for our application. Our file looks like this:<\/p>\n<p>[codesyntax lang=&#8221;text&#8221;]<\/p>\n<pre>sitemap demo label=\"Raspberry demo\"\r\n{\r\n Frame label=\"Luci\" {\r\n   Switch item=GreenLight label=\"Zelena luc\"\r\n   Switch item=YellowLight label=\"Rumena luc\"\r\n   Switch item=RedLight label=\"Rdeca luc\"\r\n }\r\n Frame label=\"Videokamere\" {\r\n   Text label=\"Raspberry kamera\" icon=\"video\" {\r\n     Frame label=\"Raspberry kamera\" {\r\n       Video url=\"http:\/\/localhost:8081?action=stream\" encoding=\"mjpeg\"\r\n     }\r\n   }\r\n   Text label=\"Raspberry kamera\" icon=\"garden\" {\r\n     Frame label=\"Raspberry kamera (snapshot)\" {\r\n       Image url=\"http:\/\/localhost:8081?action=snapshot\" refresh=5000\r\n     }\r\n   }\r\n }\r\n}<\/pre>\n<p>[\/codesyntax]<\/p>\n<p>For now you can skip the &#8220;Videokamere&#8221; part as it defines video stream and snapshot images of a camera that we will attach to the raspberry (below).<\/p>\n<p>As you save the file and run the server, the GUI can be accessed via a web browser using URL http:\/\/RPi_IP:8080:<\/p>\n<p><a href=\"https:\/\/blog.zitnik.si\/wp-content\/uploads\/2016\/02\/Screen-Shot-2016-02-04-at-14.01.19.png\" rel=\"attachment wp-att-1589\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-1589\" src=\"https:\/\/blog.zitnik.si\/wp-content\/uploads\/2016\/02\/Screen-Shot-2016-02-04-at-14.01.19-300x179.png\" alt=\"Screen Shot 2016-02-04 at 14.01.19\" width=\"300\" height=\"179\" srcset=\"https:\/\/blog.zitnik.si\/wp-content\/uploads\/2016\/02\/Screen-Shot-2016-02-04-at-14.01.19-300x179.png 300w, https:\/\/blog.zitnik.si\/wp-content\/uploads\/2016\/02\/Screen-Shot-2016-02-04-at-14.01.19-768x457.png 768w, https:\/\/blog.zitnik.si\/wp-content\/uploads\/2016\/02\/Screen-Shot-2016-02-04-at-14.01.19-1024x610.png 1024w, https:\/\/blog.zitnik.si\/wp-content\/uploads\/2016\/02\/Screen-Shot-2016-02-04-at-14.01.19.png 1200w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>You can also use this URL in a mobile application and have a native interface &#8211; e.g. on your iPhone, which looks like this (for our sitemap definition):<\/p>\n<p><a href=\"https:\/\/blog.zitnik.si\/wp-content\/uploads\/2016\/02\/IMG_0162.png\" rel=\"attachment wp-att-1588\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-1588 size-medium\" src=\"https:\/\/blog.zitnik.si\/wp-content\/uploads\/2016\/02\/IMG_0162-169x300.png\" alt=\"IMG_0162\" width=\"169\" height=\"300\" srcset=\"https:\/\/blog.zitnik.si\/wp-content\/uploads\/2016\/02\/IMG_0162-169x300.png 169w, https:\/\/blog.zitnik.si\/wp-content\/uploads\/2016\/02\/IMG_0162-576x1024.png 576w, https:\/\/blog.zitnik.si\/wp-content\/uploads\/2016\/02\/IMG_0162.png 750w\" sizes=\"auto, (max-width: 169px) 100vw, 169px\" \/><\/a><\/p>\n<p>You should now be able to turn on or off your LEDs using OpenHAB platform. Try it out!<\/p>\n<p><strong>OpenHAB and Raspberry Pi Camera Module<\/strong><\/p>\n<p>Now we would also like to check our lights remotely via a camera. We will use mjpeg stream for video streaming.<\/p>\n<p>We have an RPi Camera Module, which we attached to the RPi. After that we run &#8220;sudo raspi-config&#8221; command and enable the camera module.<\/p>\n<p>First we need to install some dependencies:<\/p>\n<p>[codesyntax lang=&#8221;bash&#8221;]<\/p>\n<pre>sudo apt-get install libjpeg8-dev\r\nsudo apt-get install imagemagick<code class=\"language-bash\" data-lang=\"bash\">\r\nsudo apt-get install libjpeg62-dev\r\nsudo apt-get install cmake<\/code><\/pre>\n<pre><code class=\"language-bash\" data-lang=\"bash\">sudo apt-get install libv4l-dev\r\n#create a symbolic link\r\nsudo ln -s \/usr\/include\/libv4l1-videodev.h \/usr\/include\/linux\/videodev.h<\/code><\/pre>\n<p>[\/codesyntax]<\/p>\n<p>Then we download mjpeg repository and build the library:<\/p>\n<p>[codesyntax lang=&#8221;bash&#8221;]<\/p>\n<pre><\/pre>\n<pre><code class=\"language-bash\" data-lang=\"bash\"><span class=\"c\"># You can also choose to download some stable version instead of current master<\/span>\r\ngit clone https:\/\/github.com\/jacksonliam\/mjpg-streamer.git .\r\nsudo make clean all\r\n<\/code><\/pre>\n<p>[\/codesyntax]<\/p>\n<p>Now copy the directory of build files to \/opt\/mjpg-streamer and\u00a0run mjpeg streamer using the following command (I am using parameter <em>vf<\/em> to vertically flip the image):<\/p>\n<p>[codesyntax lang=&#8221;bash&#8221;]<\/p>\n<pre>LD_LIBRARY_PATH=\/opt\/mjpg-streamer \/opt\/mjpg-streamer\/mjpg_streamer -i \"input_raspicam.so -fps 5 -q 50 -x 640 -y 480 -vf\" -o \"output_http.so -w \/opt\/mjpg-streamer\/www -p 8081\" &amp;<\/pre>\n<p>[\/codesyntax]<\/p>\n<p>If everything is okay, a video stream should be accessible at http:\/\/RPi_IP:8081:<\/p>\n<p><a href=\"https:\/\/blog.zitnik.si\/wp-content\/uploads\/2016\/02\/Screen-Shot-2016-02-04-at-14.26.03.png\" rel=\"attachment wp-att-1590\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-1590\" src=\"https:\/\/blog.zitnik.si\/wp-content\/uploads\/2016\/02\/Screen-Shot-2016-02-04-at-14.26.03-261x300.png\" alt=\"Screen Shot 2016-02-04 at 14.26.03\" width=\"261\" height=\"300\" srcset=\"https:\/\/blog.zitnik.si\/wp-content\/uploads\/2016\/02\/Screen-Shot-2016-02-04-at-14.26.03-261x300.png 261w, https:\/\/blog.zitnik.si\/wp-content\/uploads\/2016\/02\/Screen-Shot-2016-02-04-at-14.26.03-768x883.png 768w, https:\/\/blog.zitnik.si\/wp-content\/uploads\/2016\/02\/Screen-Shot-2016-02-04-at-14.26.03-891x1024.png 891w, https:\/\/blog.zitnik.si\/wp-content\/uploads\/2016\/02\/Screen-Shot-2016-02-04-at-14.26.03.png 989w\" sizes=\"auto, (max-width: 261px) 100vw, 261px\" \/><\/a><\/p>\n<p>Also if OpenHAB is configured as above, videostream should be accesible over its GUI:<\/p>\n<p><a href=\"https:\/\/blog.zitnik.si\/wp-content\/uploads\/2016\/02\/Screen-Shot-2016-02-04-at-14.28.36.png\" rel=\"attachment wp-att-1591\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-1591\" src=\"https:\/\/blog.zitnik.si\/wp-content\/uploads\/2016\/02\/Screen-Shot-2016-02-04-at-14.28.36-300x178.png\" alt=\"Screen Shot 2016-02-04 at 14.28.36\" width=\"300\" height=\"178\" srcset=\"https:\/\/blog.zitnik.si\/wp-content\/uploads\/2016\/02\/Screen-Shot-2016-02-04-at-14.28.36-300x178.png 300w, https:\/\/blog.zitnik.si\/wp-content\/uploads\/2016\/02\/Screen-Shot-2016-02-04-at-14.28.36-768x455.png 768w, https:\/\/blog.zitnik.si\/wp-content\/uploads\/2016\/02\/Screen-Shot-2016-02-04-at-14.28.36-1024x607.png 1024w, https:\/\/blog.zitnik.si\/wp-content\/uploads\/2016\/02\/Screen-Shot-2016-02-04-at-14.28.36.png 1200w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<div style=\"margin-top: 0px; margin-bottom: 0px;\" class=\"sharethis-inline-share-buttons\" ><\/div>","protected":false},"excerpt":{"rendered":"<p>We are continuing with the example from the first part of the tutorial: https:\/\/blog.zitnik.si\/2016\/02\/04\/part-13-controlling-leds-using-raspberry-pi\/. In this part we are going to install OpenHAB platform on an RPi and remotely control LEDs. OpenHAB (http:\/\/www.openhab.org\/) \u00a0is a software that is intended to connect to many sensors or devices for home automation. It&#8230;<\/p>\n<div class=\"more-link-wrapper\"><a class=\"more-link\" href=\"https:\/\/blog.zitnik.si\/?p=1585\">Continue reading<span class=\"screen-reader-text\">Part 2\/3: OpenHAB + RPi + LEDs + Camera<\/span><\/a><\/div>\n","protected":false},"author":1,"featured_media":1592,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13,30],"tags":[],"class_list":["post-1585","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-computer-engineering","category-free-time","entry"],"_links":{"self":[{"href":"https:\/\/blog.zitnik.si\/index.php?rest_route=\/wp\/v2\/posts\/1585","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.zitnik.si\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.zitnik.si\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.zitnik.si\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.zitnik.si\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1585"}],"version-history":[{"count":3,"href":"https:\/\/blog.zitnik.si\/index.php?rest_route=\/wp\/v2\/posts\/1585\/revisions"}],"predecessor-version":[{"id":1593,"href":"https:\/\/blog.zitnik.si\/index.php?rest_route=\/wp\/v2\/posts\/1585\/revisions\/1593"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.zitnik.si\/index.php?rest_route=\/wp\/v2\/media\/1592"}],"wp:attachment":[{"href":"https:\/\/blog.zitnik.si\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1585"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.zitnik.si\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1585"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.zitnik.si\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1585"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}