{"id":8286,"date":"2013-02-06T08:11:08","date_gmt":"2013-02-06T13:11:08","guid":{"rendered":"http:\/\/scruss.com\/blog\/?p=8286"},"modified":"2019-11-11T00:03:21","modified_gmt":"2019-11-11T05:03:21","slug":"hey-its-the-sun-heyu-and-sunwait-and-cron-on-the-raspberry-pi","status":"publish","type":"post","link":"https:\/\/scruss.com\/blog\/2013\/02\/06\/hey-its-the-sun-heyu-and-sunwait-and-cron-on-the-raspberry-pi\/","title":{"rendered":"hey, it&#8217;s the sun &#8230; heyu and sunwait and cron on the Raspberry Pi"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"the polyphonic spree - it&#039;s the sun\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/P9gk1PGZxaE?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<p>Yep, springtime&#8217;s coming, and today&#8217;s the first day I know it, despite the -5.8\u00c2\u00b0C outside. I know spring is coming because my sunrise-adjusted lights came on before my alarm today. I&#8217;m controlling them with a Raspberry Pi, cron, and X10.<\/p>\n<p>I&#8217;d described <a href=\"http:\/\/scruss.com\/blog\/2012\/12\/08\/x10-home-automation-with-raspberry-pi-heyu\/\">how to build and use heyu<\/a> previously, so I won&#8217;t go into it further. I use <a href=\"https:\/\/web.archive.org\/web\/20130202223741\/http:\/\/www.risacher.org\/sunwait\/\">sunwait<\/a> to control the timing relative to local sunrise and sunset. Sunwait is a simple C program which builds quickly, and you can put the executable somewhere in your path.<\/p>\n<p>(<strong>NB<\/strong>: newer versions of sunwait use a completely incompatible command line format. Everything here refers to the 2004 version I linked to above, which does exactly what I need in the way it&#8217;s described here.)<\/p>\n<p>You need to know your latitude and longitude to use sunwait. To check its setting for the day, you can call it with the <code>-p<\/code> option:<\/p>\n<pre>$ sunwait -p 43.729N 79.292W\nUsing location:\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 43.729000N, 79.292000W\nDate:\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 6 Feb 2013 \nLocal time:\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 7:44 \nDay length:\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 10:13 hours\nWith civil twilight\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 11:10 hours\nWith nautical twilight\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 12:18 hours\nWith astronomical twilight\u00c2\u00a0 13:25 hours\nLength of twilight:\u00c2\u00a0 civil\u00c2\u00a0\u00c2\u00a0 0:28 hours\n\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 nautical\u00c2\u00a0\u00c2\u00a0 1:02 hours\n\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 astronomical\u00c2\u00a0\u00c2\u00a0 1:35 hours\nCurrent specified time zone: EST (-5 from UTC) \nSun transits meridian 1231 EST\n\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 Sun rises 0726 EST, sets 1736 EST\n\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 Civil twilight starts 0656 EST, ends 1806 EST\n\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 Nautical twilight starts 0622 EST, ends 1840 EST\nAstronomical twilight starts 0548 EST, ends 1913 EST<\/pre>\n<p>So for me, today&#8217;s sunrise is at 0726, and sunset is at 1736. All sunwait does is wait until a specific solar time is reached, and then exit. Whatever command you call <em>after<\/em> sunwait, therefore, is what gets run at the right time. So if I wanted X10 device H1 to come on an hour before sunrise, I&#8217;d run:<\/p>\n<pre>sunwait sun up -1:00:00 43.729N 79.292W; heyu on h1<\/pre>\n<p>Remembering to run this every day before sunrise would be a pain, so this is where cron helps. cron uses a slightly odd config file that is edited using the <code>crontab -e<\/code> command. Here&#8217;s the relevant bit of my crontab, showing the light control times:<\/p>\n<pre># m h\u00c2\u00a0 dom mon dow\u00c2\u00a0\u00c2\u00a0 command\n\u00c2\u00a001 00\u00c2\u00a0\u00c2\u00a0 *\u00c2\u00a0\u00c2\u00a0 *\u00c2\u00a0\u00c2\u00a0 *\u00c2\u00a0\u00c2\u00a0 \/usr\/local\/bin\/sunwait sun up -1:00:00 43.729N 79.292W; \/usr\/local\/bin\/heyu on h1\n\u00c2\u00a002 00\u00c2\u00a0\u00c2\u00a0 *\u00c2\u00a0\u00c2\u00a0 *\u00c2\u00a0\u00c2\u00a0 *\u00c2\u00a0\u00c2\u00a0 \/usr\/local\/bin\/sunwait sun up +1:00:00 43.729N 79.292W; \/usr\/local\/bin\/heyu off h1\n\u00c2\u00a003 00\u00c2\u00a0\u00c2\u00a0 *\u00c2\u00a0\u00c2\u00a0 *\u00c2\u00a0\u00c2\u00a0 *\u00c2\u00a0\u00c2\u00a0 \/usr\/local\/bin\/sunwait sun down -1:00:00 43.729N 79.292W; \/usr\/local\/bin\/heyu on h1\n\u00c2\u00a045 22\u00c2\u00a0\u00c2\u00a0 *\u00c2\u00a0\u00c2\u00a0 *\u00c2\u00a0\u00c2\u00a0 *\u00c2\u00a0\u00c2\u00a0 \/usr\/local\/bin\/heyu off h1<\/pre>\n<p>(you can view your crontab with <code>crontab -l<\/code>)<\/p>\n<p>The columns in crontab are:<\/p>\n<ul>\n<li>minute<\/li>\n<li>hour<\/li>\n<li>day of month<\/li>\n<li>month<\/li>\n<li>day of week<\/li>\n<li>command<\/li>\n<\/ul>\n<p>So the four crontab lines mean:<\/p>\n<ol>\n<li>Every day at 00:01, wait until an hour <em>before<\/em> <strong>sunrise<\/strong> and turn light H1 <em>on<\/em><\/li>\n<li>Every day at 00:02, wait until an hour <em>after<\/em> <strong>sunrise<\/strong> and turn light H1 <em>off<\/em><\/li>\n<li>Every day at 00:03, wait until an hour <em>before<\/em> <strong>sunset<\/strong> and turn light H1 <em>on<\/em><\/li>\n<li>At 22:45, turn light H1 <em>off<\/em>.<\/li>\n<\/ol>\n<p>So for quite a bit of the day, there are a couple of sunwait tasks just quietly waiting until sunrise or sunset to do their thing. cron, incidentally, is picky about executable paths; that&#8217;s why I specified full paths to both sunwait and heyu.<\/p>\n<p>What I&#8217;d really like to do is have time on this machine update without a network connection, because it&#8217;s running from a particularly messy router set up in a spare bedroom. I should investigate a real-time clock, with GPS time updates from an <a href=\"http:\/\/www.aliexpress.com\/item\/Free-shipping-Flight-control-Ublox-NEO-6M-0001-model-aircraft-GPS-module-IIC-out\/651762086.html\">I\u00c2\u00b2C GPS<\/a>, talking through a <a href=\"http:\/\/scruss.com\/blog\/2012\/12\/18\/adding-a-bluetooth-serial-terminal-to-raspberry-pi\/\">bluetooth console<\/a>. In my copious free time, of course.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Yep, springtime&#8217;s coming, and today&#8217;s the first day I know it, despite the -5.8\u00c2\u00b0C outside. I know spring is coming because my sunrise-adjusted lights came on before my alarm today. I&#8217;m controlling them with a Raspberry Pi, cron, and X10. I&#8217;d described how to build and use heyu previously, so I won&#8217;t go into it [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[2],"tags":[1466,2600,2510,244,2618,2601],"class_list":["post-8286","post","type-post","status-publish","format-standard","hentry","category-goatee-stroking-musing-or-something","tag-cron","tag-heyu","tag-raspberrypi","tag-spring","tag-sunwait","tag-x10"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pQNZZ-29E","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts\/8286","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/comments?post=8286"}],"version-history":[{"count":4,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts\/8286\/revisions"}],"predecessor-version":[{"id":15798,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts\/8286\/revisions\/15798"}],"wp:attachment":[{"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/media?parent=8286"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/categories?post=8286"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/tags?post=8286"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}