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

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

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 BTicino interface L4686. 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:

sudo apt-get install cron

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

crontab -e

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

crontab -l

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:

30 7  * 6-9 *     /usr/local/bin/scs -r1 -d1 -cdown
  • 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.

sunwait sun up +01:30:00 41.8N 12.4E
  • 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:

2 1 * 6-9 * /usr/local/bin/sunwait sun up +01:30:00 41.8N 12.4E ; /usr/local/bin/scs -r1 -d1 -cdown

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.

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

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:

Below a couple of references for sunwait:

Andrea Montefusco
Currently employed as network architect, always Internet working man, real C/C++ programmer in the past, network and Unix system engineer as needed, HAM Radio enthusiast (former IW0RDI, now IW0HDV), aeromodeller (a person who builds and flies model airplanes) since 1976 (ex FAI10655).
http://www.montefusco.com - https://github.com/amontefusco - https://github.com/IW0HDV - andrew@montefusco.com