In this article we explore the possibilities of using a Raspberry Pi for the remote control and monitoring of a home heating system using servo control, analogue to digital conversion, C programming and web development.
We decided to use a servo device to control the on/off switch of the boiler. We could have used relay switches for the same purpose, but tried to stick to the principle of making no modifications to the boiler (as it’s owned by the landlord :D). The following image shows how the servo is mounted and the various states of control.
Our mini servo has reduced power requirements, so we could power it directly from the 5V pin of the Raspberry Pi. To create the needed pulse for servo control, we utilized wiringPi library’s softPWM implementation. Small glitches exist, but the accuracy is sufficient for this kind of control. The code below uses GPIO pin 0 as a PWM signal output to control the servo. We assume that wiringPi library is already installed.
pinMode(0, OUTPUT); digitalWrite(0, LOW); softPwmCreate(0,0,200); softPwmWrite(0,control);
The “control” variable accepts integer values in the range of 180 to 194. The input to that variable will control the three positions of the switch. This will eventually be controlled from a web based user interface.
Sensors and ADC
Our servo device can now control the state of the boiler from our Raspberry Pi. However, when we remotely control the system, we cannot be sure that the servo worked as it should and that the boiler is in the state we want. For this reason we need a way to get feedback from the Raspberry Pi regarding the boiler’s state. The most straight-forward way to achieve this is to check the status of the LED indicator on the boiler. When the LED is on the boiler is turned on and when the LED is off the boiler is off. A simple LDR (Light Dependent Resistor) light sensor, in a dark box, mounted on the boiler’s two 7 segment LED displays should do the job. A LDR sensor is a variable resistor whose resistance depends on the amount of light arriving on it.
We also use two LM35 temperature sensors with analogue output; one for air temperature (mounted on the breadboard) and one for water temperature (mounted on a radiator).
You’ll need an analogue-to-digital converter (ADC) when using analogue sensors. We chose a BV4205 (a 10 channel I²C enabled IC) though others like the MCP3008 would also work. The analogue output from the three sensors gets converted to digital and is fed to the Raspberry Pi through the I²C bus. However, due to known issues with the I²C bus on the Raspberry Pi (see goo.gl/IQvN2O), a “patch” to slow down the clock of the I²C bus had to be applied before using the BV4205.
Below, we use the most basic parts of the code needed for communications over the I²C bus to read the analogue input of a sensor connected to channel 7 (use the same code for other channels). Similar code can be used for interaction with any I²C compatible device.
//Initialize I2C bus int fd; char *fileName = "/dev/i2c-1"; //Note: When using a Rev 1 Pi, use "/dev/i2c-0" int address = 0x31; unsigned char buf; fd = open(fileName, O_RDWR); ioctl(fd, I2C_SLAVE, address); //Select ADC channel 7 buf = 1; buf = 7; write(fd, buf, 2); //Start conversion buf = 2; write(fd, buf, 1); //Fetch result of conversion buf = 4; write(fd, buf, 1); read(fd, buf, 2); unsigned short packedword; packedword = (buf <<8 buf="" pre="">
User interfaceThe following schematic shows the setup and how the devices connect and interact. A simple web interface provides easy access from web enabled devices. The web interface is mainly based on jQuery, PHP and MySQL.Using PHP’s exec command, we get access to our two main executables that control the servo’s position and read sensor output. Ajax has been used extensively in order to achieve real-time monitoring of the boiler’s state and sensor readings. Furthermore, we calculate how long the system was on each day (in minutes) by analysing the light sensor’s output. The user is also presented with an SVG (d3.js library) graph of the air temperature for the last 24 hours with time resolution of one minute.By exploiting data from the sensors, we could further extend the system and make it more intelligent by fully automating on and off events based on sensor output and specific usage profiles. For more information (code, schematics, pictures) about the projectref: http://www.themagpi.com/issue/issue-11/article/home-heating-automation-with-a-raspberry-pi-and-a-smartphone/8>