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.

Saturday, June 21, 2014

It is live

Connected the sensors to the YUN and receiving the first feedback from the board:

You're connected
Initializing I2C...
Communicating with RTC...
Set system date according to the onboard RTC (201105212352.06)
Communicating with DHT...
Communicating with BMP...
Timestamp from the RTC module: 05/21/2011 23:52:06
Data from DHT sensor: 
 -> Humidity: 59.70%
 -> Temperature: 21.30*C
Data from BMP180 module: 
 -> Pressure: 979.94Pa
 -> Temperature: 21.00*C

Thursday, June 19, 2014

HLD changes

Here a High Level Design of the SW, more detailed than the last one:

Wednesday, June 18, 2014

PHP and SQLite3 installation

At the end I changed my mind and I'll go with SQLite3 instead of MySQL, copying what they do in Officine Arduino.

I have found this useful tutorial to install everything on the Yun, perfect for me.

HLD of the used technologies

What follow is just a very high level design of the technologies we are going to use on Arduino's side:

In a nutshell, I'm going to use the 32U4 to access our hardware (sensors, RTC and relays) providing a set of simple read and write API to a Python script running on the AR9331 processor. It stores/retrieves info in/from a MySQL (or sqlite) database. A web server is responsible to communicate with the client and provide/change info stored into the DB via PhP scripts.

This way:
  1. the sketch will be very simple and 'light' and we shouldn't have any problem in uploading it into the small space provided by Yun;
  2. all the logic to manage the sensors and actuators will be implemented with python scripts;
  3. we will use the flexibility of a DB to store configurations, logs, sensors/actuators status and any other info we'd like to share with the rest fo the world;
  4. a light web server will make easier the design and development (and test) of the interface we will provide to the final user.
Also there is another important reason why I'm choosing a web server based on PHP/MySQL technology: it's a common technology used by a lot of platform providing web hosting for free. When (in the future) I will create a website to manage my Arduino, I will just need to move my PHP scripts and MySQL structures/data to the new host. Maybe it will not be so easy, but I assume I can reuse 90% of the code.

PHP and its configuration

I still can't see the bonjour service so I cannot upload sketch via Wifi from my Mac, but I keep configuring the Yun.

I've just added PHP5 following this original forum topic from Arduino Forum or this answer from Stackoverflow, it's the same:
- installing php5 and php5-cgi packages
- configure php.ini (doc_root)
- configure and restart uhttpd

Not sure why but this morning (after the Yun has been up and running all the night long) I needed to manually reset it (YUN RST button) because it wasn't able to access internet to download php5 package. The strange think was that the SSH connection with my MacBook was running and I was able to access the device through my browser (, but the device itself was not able to ping the router ( and consequently not able to ping/access internet websites.

Tuesday, June 17, 2014

USB 3.0 issue

I wasted a lot of time understanding Arduino Yun has a lot of problem when connected to USB 3.0 port. This morning I connected the Yun to my Windows 7 laptop (a HP EliteBook 8460p) and the problems came...

I wasn't able to maintain a stable association between the device and the driver and I wasn't able to upload any sketch to the Yun using the IDE. Changed the port (USB 2.0) and now it seems the uploading process works and the driver is correctly associated with the device.

Unfortunatley I tried a lot of different solutions before understanding it was the USB and now I have to upgrade OpenWrt to the latest version and format the SD card again.

Monday, June 16, 2014

Testing the board

Arduino Yun is just arrived.

I changed the wireless setting to make it able to access my network (following this guide) and I connected the board to the USB port of my MacBook Pro. Tested the connection uploading the 'Blink' example provided with the IDE (File > Examples > 01 Basics > Blink).

The board seems to work fine even if I cannot upload sketch using the wireless connection: I don't see any IP address when I open Tools > Port... tomorrow I'll try to understand why.

Then I inserted a 2GB SD card I got and followed the suggested procedure to expand the Yún disk space, obtaining more than 1GB of free space for the GNU/Linux operating system (OpenWrt) and about 700MB of space to share data with my computer, if needed.

Git project

During the weekend, I created the Git repository to store the project under GPLv3 license:
Watch'n'Water project on GitHub

The electronic stuff is going to arrive so I've also prepared a sketch to test the Arduino Yun, sensors and RTC communication. But first of all we have to test the Yun alone using the standard boring blinking led.

My first impression is that we have not enough space on 32U4 side (32K) to store a complex sketch. In my first (not optimized) .INO file, I just added BMP180, DHT22 and RTC libraries and other two libraries  (Wire and Bridge) needed to communicate with I2C modules and AR9331. I used more than 65% of the space and I'm only retrieving temperature, pressure, humidity and timestamp...
My idea is now to use the sketch just to retrieve the basic info and activate relays as needed, porting all the logic at AR9331 level, where we don't have space problem (I'm going to use a 2 GB SD).

This way I'll use python to implement both the logic of the watering system and the communication with external actors. Why python? Just beacuse Python 2.7 is included with OpenWrt-Yun.

Friday, June 13, 2014

First shopping list

The first order has been done, here the list of what I bought from a local store via Internet (tax included):

Arduino YUN € 61.61
DS1307 Breakout Board (Real Time Clock Module) € 11.20
DHT22 (Humidity and Temperature Sensor) € 10.68
BMP180 Breakout Board (Barometric Pressure Sensor) € 8.71
Jumper Wires - Connected 6" (M/F, 20 pack) € 1.56
Strip Male 2,54 - 40 pin € 0.51
Shipping € 0.00
Total € 103.44