Monday, July 14, 2014

The engine in an nut shell: the main logic

The main logic of the engine has already been implemented and tested. What is now missing is the management of a soil moisture sensor and the management of the weather forecast.
Implementing the first feature is not so hard, once we have the value of the humidity of the soil we just need to add an IF statement to enable/disable the irrigation valves.
Implementing the second feature (weather forecast) is a bit more tricky: we need to collect the values of pressure and humidity and use them to understand if it's going to rain.

The code is ready to manage both the features in a specific function named 'evaluateTurningOnOutput' that now always returns TRUE. So when we will develop those functionalities, we will not change the engine code a part from this function.

Now let's start with the code explanation of that is a bit more complex than the sketch, so we will not follow the script line by line.

The engine: an overview of the architecture

We have seen how the sketch works: it is just a layer to interact with the input/output element of the project, retrieving sensors values and activating/deactivating outputs. The logic of the project is demanded to a python script running on the Linux side of the Arudino Yun due to two main reasons:

  1. the logic is preatty complex and cannot be loaded into the small memory of the ATmega32u4;
  2. we need to interact with a logging system and a database, and it's much more easy to do that using already developed python libraries than reinventing the wheel in C.
Also if we develop the main logic on the Linux side we can easily take advantage of the OS itself: rotate the log files, schedule activities with crontab and so on.

The engine is based on three .PY files: the main file defining the logic and two simple library defining the bridge and the database objects.
  • => the python script running the logic of the engine;
  • => the python class (WnWBridge) used to access the mailbox and share data and request with the lower layer (the sketch running on the ATmega 32u4 side);
  • => the python class (WnWDatabaseConnection) managing the connection to the database and wrapping the communication with it.
I created a shell script to automatically run the engine as soon as the system (Linux) completes the startup procedure. In order to be sure that the engine is always up and running, I added a line to the crontab calling the same script every minute. The script check if the engine is working and re-start it only in case it's not running.

The sketch in a nut shell

The developing of the sketch has been finished few days ago and can now be considered pretty much complete, so I guess it's time for a review.

The sketch.ino file is an easy 200 lines C++ source code that is supposed to work as a slave. Main purposes are:
  • read the current values of the sensors and provide them to the upper layer;
  • read the current values of the outputs and provide them to the upper layer;
  • get, from the upper layer, the expected status of the outputs and operate to address the request.

Tuesday, July 1, 2014


A more detailed HLD of the architecture:

The engine ( is written in Python, but the bridge library to connect to the sketch ad been re-written. This process is the only one able to communicate with the ATmega32U4 processor that means it's teh only process that will interact with the sensors and output using the dedicated mailbox technology. Assuming that, I write the bridge library (  in order to open a socket to connect to the mailbox and maintain it open untill the process is killed instead of opening and closing a socket every time you want to read or write a value into the mailbox.
The database access is centralized into a specific library ( in order to make easy any database changes we will do in the future (including the database itself).
The web interface must be mantained easy and quik so I choose jQuery Mobile and PHP to interact with the DB that is now the only point of contact between out system and the outside world.