{"id":1766,"date":"2020-12-11T14:55:44","date_gmt":"2020-12-11T20:55:44","guid":{"rendered":"https:\/\/www.dalemorin.com\/?p=1766"},"modified":"2020-12-12T06:42:32","modified_gmt":"2020-12-12T12:42:32","slug":"raspberry-pi-project-integration","status":"publish","type":"post","link":"https:\/\/www.dalemorin.com\/?p=1766","title":{"rendered":"Raspberry Pi project &#8211; integration"},"content":{"rendered":"<p>Ideally, the logging \/ data storage script would run automatically, as would the graph image generation script.\u00a0 There are multiple ways to make that happen, and some of them will vary depending on your operating system.<\/p>\n<p>The simplest way is to just run the scripts a a scheduled process using cron.\u00a0 If you do this, you should remove the endless loop and sleep commands from the scripts.\u00a0 Since cron will execute the scripts on the schedule you choose, there is no need for the script to sleep and then wake up and run through a loop again.<\/p>\n<p>If you choose to run them through cron, you&#8217;ll need to run the logging \/ data storage script every 5 minutes, so the minute parameter in your cron file will be &#8220;*\/5&#8221;, meaning every 5 minutes.\u00a0 The other timing parameters will be simply &#8220;*&#8221;.<\/p>\n<p>For the graph image generation script, you would probably run it once each hour.\u00a0 To run it at the top of the hour, set the minute value to &#8220;0&#8221;, and the other parameters to &#8220;*&#8221;.  That means cron will run the script every time the minute is 0 (top of the hour).<\/p>\n<p>Since I like the scripts to run as system services, my scripts have the loop and sleep commands in them.\u00a0 On a linux operating system using systemd, you&#8217;ll create a system service for running the scripts.  To do this you need root access &#8211; either switch to the root user or use sudo.\u00a0 Go to the \/etc\/systemd\/system directory, and create a file name temperature_logger.service.\u00a0 Put the following code in it, adjusting the path to the file as required for your environment.<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nAfter=network.service\r\n\r\n&#x5B;Service]\r\nExecStart=\/&lt;path-to-your-script&gt;\/temperature_logger.py\r\n\r\n&#x5B;Install]\r\nWantedBy=default.target\r\n<\/pre>\n<p>This file should be owned by root, with 644 permissions.<\/p>\n<p>To activate this as a system service, type &#8220;systemctl enable temperature_logger.service&#8221;, followed by &#8220;systemctl start temperature_logger.service&#8221;.  This will now start automatically when the computer is booted.  To stop it from running, type &#8220;systemctl stop temperature_logger.service&#8221;.  Note that it will restart if the computer is rebooted.  If you don&#8217;t want it to start unless you start it, type &#8220;systemctl disable temperature_logger.service&#8221;.<\/p>\n<p>Setting up the graph image generations as a system service is basically identical, except that the service file will have a different name, and the file name on the ExecStart line will be different.<\/p>\n<p>It is your preference as to how you want to execute these scripts.  Other OSes will offer different methods that give the same result.<\/p>\n<p>To rotate the log file, we need to add a file into the \/etc\/logrotate.d\/ directory.  The name isn&#8217;t critical; I chose &#8220;mylogs&#8221; to differentiate it from the files added when other packages are installed.  Presuming that you left the name of the log file as it was in the example code, here is what you&#8217;ll need to add.<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n\/var\/log\/temp_humidity.log {\r\n    su root adm\r\n    daily\r\n    missingok\r\n    rotate 7\r\n    delaycompress\r\n    compress\r\n    notifempty\r\n    create 644 root adm\r\n    sharedscripts\r\n    postrotate\r\n    endscript\r\n    maxage 7\r\n}\r\n<\/pre>\n<p>This will keep 7 days worth of log files.  The current log file and yesterday&#8217;s file are not compressed, the other log files are compressed to save space.<\/p>\n<p>I hope you&#8217;re found this little project documentation useful.  You could extend it by adding another sensor, perhaps locating this one outdoors.  The documentation says that the sensor can be connected to the Raspberry Pi by wires as long as 50 yards, so you have some flexibility as to where you place it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ideally, the logging \/ data storage script would run automatically, as would the graph image generation script.\u00a0 There are multiple ways to make that happen, and some of them will vary depending on your operating system. The simplest way is to just run the scripts a a scheduled process using cron.\u00a0 If you do this, &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.dalemorin.com\/?p=1766\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Raspberry Pi project &#8211; integration&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[55],"tags":[],"class_list":["post-1766","post","type-post","status-publish","format-standard","hentry","category-raspberry-pi"],"_links":{"self":[{"href":"https:\/\/www.dalemorin.com\/index.php?rest_route=\/wp\/v2\/posts\/1766","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dalemorin.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dalemorin.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dalemorin.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dalemorin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1766"}],"version-history":[{"count":5,"href":"https:\/\/www.dalemorin.com\/index.php?rest_route=\/wp\/v2\/posts\/1766\/revisions"}],"predecessor-version":[{"id":1781,"href":"https:\/\/www.dalemorin.com\/index.php?rest_route=\/wp\/v2\/posts\/1766\/revisions\/1781"}],"wp:attachment":[{"href":"https:\/\/www.dalemorin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1766"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dalemorin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1766"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dalemorin.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1766"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}