Controller – Sensor Software

Fortunately, there is a fantastic library available from Adafruit which interfaces with our temperature sensor. Use the following command to install the necessary pre-requisites…

sudo apt-get install git python-dev python-openssl

and then fetch and install the library.

cd ~
git clone
cd Adafruit_Python_DHT
sudo python install

Next we need the sensor helper module for this sensor type, from the project code repository:

wget -P /usr/local/bin/code/controller/sensor_helpers

and then we need to make the controller aware of the type by uncommenting the following line in /usr/local/bin/code/controller/sensor_helpers/

from sensor_helpers.am2302 import am2302

Stop the current controller thread, and start a new one…

sudo killall python
sudo su - -c /etc/rc.local

and then we are tantalisingly close to taking a measurement. But we haven’t configured the hub to tell it about our sensor, so this is a quick hack to prove our sensor is working. In the next post we will enhance our website with the forms needed to manage all this stuff, like adding sensors, setting up zones and measurands, etc. but I can’t wait that long, so lets use pgAdmin to edit the database and change the existing sensor in the Sensor table.

The inaugural sensor S50 was a virtual sensor. It had an expression in its SensorFunction column rather than details of a real sensor. Save this expression somewhere if you want to restore after the test, and replace it with the following:


This is shorthand for:

call the am2302 function with the parameters bcm_pin, channel, sample_count

You will need to replace nn with the bcm pin number that your sensor is connected to. The channel specifies the measurand: 0 for temperature or 1 for relative humidity. The sample_count sets the number of readings which will be simultaneously compared for error detection. Different sensor types will support different parameters as we will see later.

In my development system the sensor function is:


While editing the sensor, change the MeasurandID from 1 Angle to 2 Temperature. Otherwise the reading will exceed the limits set for the measurand, and an error will occur.

Once you have updated your sensor function and measurand in the database you should be able to visit the Statistics website page and see the temperature reading.

Enough of hacking, let’s sort out the website facilities for editing sensors.

Controller – Current Values

The core function of the controller is to read current values from the sensors, and this is done in the current_values module. In order to make the design extensible, the sensor types are handled by sensor helper modules dropped into the sensor_helpers directory. The __init.py__ file just has to be updated to include the reference to the new sensor type.

The skeleton controller includes one sensor type – evaluate, which enables us to read the virtual sensor described in a previous post. To run the controller, without rebooting the pi, use the following command:

sudo su - -c /etc/rc.local

and inspect the logfile for any errors:

cat /var/log/hub/hub.log

and view the statistics on the website:


You should now see the sensor value changing every minute. It should be tracing out a sine wave, but we will need the graph features in later modules to prove this!  What we should do now is connect up a real sensor.

Controller – Main Loop

The main controller program is It consists of:

  • a continuous loop which performs periodic routine activities e.g. reading sensors
  • a thread that controls time-sensitive functions e.g. updating timers
  • a thread that does high-priority work e.g. responding to user instruction
  • logging initialisation

This program needs to run when the pi is started, so it is placed in the rc.local script:

sudo nano /etc/rc.local

Add the following between the section that prints the IP Address, and the exit statement:

# Run the Hub Program in a seperate process (&)
cd /usr/local/bin/code/controller
sudo python --log=INFO --slave=false >> hub.log &

Save the file.

The parameters set the logging level and the slave status. This will be our master node, so slave is false.

To assist with the staged implementation of the controller code, as per the project plan, I have commented out a number of lines in the main scheduler with the double hash # #. This enables the controller to run, and future modules to be added later, with their required dependencies.

The following command will fetch the files for this first stage…

wget -nH -x --cut-dirs=3 -P /usr/local/bin/code/controller/ -i /usr/local/bin/code/controller/manifest1.txt


The next post will cover reading values from sensors.