Markdown source

#How to dynamically bound domotics actions to sunrise and sunset using cron

<abstract>
How to dynamically bound domotics actions to sunrise and sunset using cron
</abstract>

When we come to Unix domotic system supervisor, there is a wide range of software 
able to run on Acmesystems Unix embedded platforms.
However, we even have a very simple solution, using cron as action scheduler.
The origin of the name cron is from the Greek word for time, χρόνος (chronos) (source Wikipedia).
Over Internet, there is a huge amount of tutorials, articles and documentation about cron, one of older and used daemon in Unix since ages.
Together with a command line utility able to send commands over our domotic field bus, they are a powerful and simple way 
to schedule commands on purpose.
I'll use here, as examples, my own BTicino SCS domotic system, working in my home since August 2001. 


##SCS: unofficial history

At that time the SCS system was entirely closed, the only software available from vendor being BTicino Visual SCS.
It was running on legacy operating system and, even worst, having as user interface just a GUI one.
In the end, it was just a toy, not suitable as supervisor.
So, in order to run some simple automation on a Unix embedded system, I developed a small perl utility able to send 
commands over the bus using the <a href="http://www.bticino.cl/0/pdf/manuales/T5265A.pdf">BTicino interface L4686</a>. 
The protocol was reversed enginereed by myself sniffing what the VisualSCS software was sending over the bus.
Afterward, switching to Acmesystems Linux embedded systems, I changed it to C language in order to keep it more efficient
and with less prerequisites than perl.


##Cron on FoxG20/Arietta

In the Debian distro provided by Acmesystems, there is no cron preinstalled.
So, you have to install it manually:

<pre class="prettyprint">
sudo apt-get install cron
</pre>	

cron is ba

Now I'll provide here a short description about cron (see references section): it is a scheduler that uses
as input a simple text file where each row contains one schedule item and a 
command to be run when the schedule applies.

The cron configuration file (in /etc/crontab in the past) is changed not directly, but using the command

<pre class="prettyprint">
crontab -e
</pre>
that will open an editor allowing you to do the changes; saving the file will referesh cron daemon.

In order to display the crontab schedule use

<pre class="prettyprint">
crontab -l
</pre>

All the rows starting with '#' are ignored.


##Time fixed operations

As an exampl, let show how to configure crontab in order to close a window' roll-up shutter early in the morning
in order to shade sun light:

<pre class="prettyprint">
30 7  * 6-9 *     /usr/local/bin/scs -r1 -d1 -cdown
</pre>

* 30 7 means that the command will be schedule 07:30 local time
* subsequent asterisk stands for: execute on each day of month
* 6-9 means that the schedule has to considered only between June and September
* the trailing '*' , means: do that all the weeks days (0-7) 
* finally there is the command to be executed, that is what you should
type at the command line. Please note the full path specified, as in the cron's
execution environment no assuption can be done about PATH variable value

Now, whilst the above works perfectly, it is far from being satisfactory as in the 
time elapsed between June and September the sun rises at very different time and so
7:30 time will result too early in the first and last month and too late in the hottiest months. 
One could think to create more rows, one for each month, wit a manually crafted timestamp,
but that would be quite unconvenient to maintain.


##sunwait: sunrise and sunset schedules

This old utility has been designed just to be used inside crontab.
The base concept is to run the utility with a simple setpoint (e.g. sun rise) well in advance 
before the event time: the program wioll wait untile the right time is it.

<pre class="prettyprint">
sunwait sun up +01:30:00 41.8N 12.4E
</pre>

* sun up specify for sun rise time
* 41.8N 12.4E are the geographical coordinates of domotic plant, can be determined using GPS receiver or Google Maps
and similar systems
* +01:30:00 is the time offset (HH:MM:SS), in this case as the value is positive
that means: wait one hour and half after sunrise 


So, the cron command becomes:

<pre class="prettyprint">
2 1 * 6-9 * /usr/local/bin/sunwait sun up +01:30:00 41.8N 12.4E ; /usr/local/bin/scs -r1 -d1 -cdown
</pre>

please note that with that row cron daemon will start the sunwait command well in advance of sunrise.
It will block quietly (without any cpu consumption !) till the time plus the offset has arrived.
At the point, it terminates and the next command in the row (after the semicolon) is executed:
in this case it is lowering the roll-up shade.

Last, in order to get the event right on time, the local clock of the system has to be kept well synchronized.


##How to build sunwait

Build the sunwai utility does require just the gcc C compiler.
The procedure follows.

<pre class="prettyprint">
cd /tmp
wget http://www.risacher.org/sunwait/sunwait-20041208.tar.gz
tar -xvzf sunwait-20041208.tar.gz
cd sunwait-20041208/
make  
sudo cp sunwait /usr/local/bin
</pre>

If you want to retain the sources locally, skip the first step and start the build from a suitable directory.


##References

Really, on cron, there is a quite large literature, for a good recap see, for example:

* [Cron tutorial](http://www.unixgeeks.org/security/newbie/unix/cron-1.html)

Below a couple of references for sunwait: 

* [Dan Risacher: sunwait original web page](http://www.risacher.org/sunwait/)
* [Stewart C. Russell: sunwait usages](http://scruss.com/blog/2013/02/06/hey-its-the-sun-heyu-and-sunwait-and-cron-on-the-raspberry-pi/)


@include='bio_andrea_montefusco'



Le attività del TanzoLab si svolgono ogni mercoledi sera, salvo casi speciali, dalle ore 18:30 presso i locali della Acme Systems srl e consistono in:

  • Talk monotematici a cura di professionisti in vari settori tecnologici
  • Workshop pratici su elettronica embedded, produzione e informatica
  • Progettazione e realizzazione di nuovi prodotti embedded per l'IT

Le attività vengono coordinate tramite questo sito, in cui vengono pubblicati tutti i lavori svolti o in via di sviluppo, e tramite un gruppo Telegram con cui per interagire direttamente via chat con gli altri membri.