Thursday 3 April 2014

Controlling the boiler from the internet

With the Pi and Arduino all connected up and with the Arduino already knowing how to turn the boiler on and off, it didn't take long to get the Node server able to listen for boiler updates and signal changes of its own.



As the scheduler isn't (anywhere near) in yet, all we have for now is a greyed out flame icon next to the temperature read out. Clicking that flame icon will toggle the boiler to the alternate state:


All other connected clients will see the flame icon light up on their dashboards at the same time and the boiler fires up.

Just the scheduler to go and we've got the basics of a thermostat!

Wednesday 2 April 2014

Tidying and Linking

A lot of housekeeping today.


  • A separate test bed Arduino for the temperature sensor has been integrated with the boiler controller sketch. It polls the sensor every 10 seconds and keeps the average of the last 10 as the 'current' temp.
  • Calibration of the temperature sensor has been added. Simple as adjusting the reported value by a set amount which is stored by the Pi and (eventually) the user can calibrate the sensor from the web interface.
  • If the boiler is turned off by the system, there's a 5 minute 'cooldown' before a new 'on' signal can be triggered to prevent excessive cycling of the boiler. The boiler has this protection built in anyway but if controlled by the Arduino it's something that can be tracked and fed back to the user in case they're wondering why the boiler hasn't actually fired up.
  • Lots of code tidying and organisation.
  • NodeJS backend now handles any data sent from the Arduino and handles the various 'packets' accordingly. Temperature readings and boiler state transmissions so far (along with any error codes).
  • Same backend has rudimentary authentication for visiting users. Once authenticated, connected users receive updates from the Arduino as the Pi gets them allowing for the current temperature to be updated on the interface every 10 seconds in real time.
Well... its a start...

Tuesday 1 April 2014

Controlling the Arduino from a Pi

The Raspberry Pi is a great little device, just like the Arduino. Whilst the Arduino is a little input/output star the Pi is a little Linux box capable of doing anything (albeit a little slower ;)) than its bigger cousins.

The Pi is therefore a little more suited to hosting a standard web stack, you can run Apache/PHP/MySQL with no trouble at all. I'm taking the NodeJS route however as its nicely asynchronous so we can easily read messages from the Arduino whilst thinking about other things.

With NodeJS/NPM already installed, getting what we need on the Pi to communicate with the Arduino is as simple as...

 npm install serialport  

When plugged into the Pi via its USB socket, the Arduino serial connect you're used to connecting to on your computer is the same one that the Pi can read/write to. Make your Arduino sketch listen for incoming commands on the serial port and provide some feedback via print/println as usual.

A quick-n-dirty test routine later...

   var serialPortModule = require("serialport");  
   var SerialPort = serialPortModule.SerialPort;  
   var serial = new SerialPort("/dev/ttyUSB0",{  
     parser: serialPortModule.parsers.readline("\n")  
   });  
   serial.on("data", function(data)  
   {  
     console.log("Received: " + data);  
   });  
   console.log('Serial Port Started');  

Note, your Arduino may be somewhere else (ie not on /dev/ttyUSB0), unplug the Arduino and ls /dev. Plug the Arduino in, and ls again. Whatever appears is your Arduino. Need to create something a little more robust for the future but for testing this'll do just fine.

We use the readline parser and not the default parser. Try with the default and you'll see why!

Putting the above Javascript into an Node (and coupled with the Arduino writing the boiler state to the serial port every 10 seconds) you'll get something like:

 Serial Port Started
 Schedule loaded
 Received: OFF
 [10 second pause]
 Received: OFF

With a smidge more code (inside a JS object) and we can write a 1 to the serial connection to trigger the Arduino to turn the boiler on, wait 5 minutes (don't really want to toggle your boiler too much!) and turn it back off. You'll need to expand all of this out with code to prevent toggling your boiler on and off too often etc but this is the basics.

   serial.open(function()  
   {  
     console.log('Serial Port Opened!');  
     self.serialConnected = true;  
     setTimeout(function() { self.turnOn(); }, 5000);  
   });

   this.turnOn = function()  
   {  
     console.log("Writing to the serial connection...");  
     serial.write("1", function()  
     {  
       serial.drain(function(err)  
       {  
         if(err)  
         {  
           console.log('Uh oh', err);  
         }  
       });  
     });  
     setTimeout(function() { self.turnOff(); }, 300000);  
   };

   this.turnOff = function()  
   {  
     serial.write("0");
   };

The boiler front panel has an LED that flashes every time it receives communication from the thermostat and the manual states this will flash every 5 minutes when the thermostat is on. The boiler shuts down automatically if it doesn't receive this periodic signal after a while. I've not timed it but it seemed to be around 10 minutes but certainly longer than the 5 minutes it waits between thermostat signals.

So at this point I've also modified the Arduino sketch to keep a record of its required boiler state and to re-transmit that every 5 minutes to mimic the behaviour of the standard thermostat. This way the Pi simply tells the Arduino 'boiler on please' and then leaves it up to the Arduino to keep it alive until told otherwise.