Introduction: Raspberry-Pi Home Heating Controller
Have you ever wanted to remotely control your home heating and hot water from anywhere, but did not want to pay over £200 for a commercial system ?
Using off-the-shelf hardware and shareware code, you can build your own DIY controller for less than £50.
** Warning this project involves working with mains voltage - please seek help if you are unsure **
All code for this project is available here : https://github.com/JeffreyPowell/pi-config
Step 1: System Design
The system consists of a controller unit ( pi-heating-hub ) which sits next to your central heating and water boiler, and one or more temperature sensors ( pi-heating-sensor-01 , 02 , ... ) which are positioned around your home.
The pi-heating-hub consists of a raspberry pi zero connected to some relays - you will need to refer to your heating system manual to see if it is possible to splice in to your central heating and/or hot water circuits. I was able you connect to both circuits, but you may only be able to connect to the central heating circuit, in which case you will only need one relay connected to the pi zero.
The pi-heating-sensor consists of a pi zero connected to some 1-wire temperature sensors. You can connect as many pi-heating-sensor as required, one for each room you want to monitor ( lounge, bedroom1, conservatory, ... ) and each pi zero can have as many 1-wire temperature sensors connected as you require ( you could have a pi zero in your lounge with three 1-wire temperature sensors measuring : air temperature, one measuring the temperature of the radiator and one hanging out of the window measuring the outside temperature ).
The pi-heating-hub and pi-heating-sensors communicate via your home wifi.
Step 2: What You Will Need
The pi-heating-hub :
- Raspberry Pi Zero + SD card
- Power supply
- USB wifi dongle + micro usb adaptor
- 1 or 2 channel relay module ( 250 Vac - 10 A )
- 10 pin GPIO connector ( 2 x 5 pin )
For each pi-heating-sensor :
- Raspberry Pi Zero + SD card
- Power supply
- USB wifi dongle + micro usb adaptor
- a 4.7kΩ resistor
- 1 or more 1-Wire Digital Thermometers ( DS18B20 )
10 pin GPIO connector ( 2 x 5 pin )
Step 3: Base Setup of Each Pi Zero
Common Hardware configuration :
First solder the GPIO connectors to the GPIO port pins 1 to 10 on all pi zeros you are going to use.
the pi-heating-hub only uses pins 1, 6, 8 and/or 10, and the pi-heating-sensor only uses pins 1, 6 and 7, the other pins are used just to add physical strength.
Common Software configuration :
We need to configure all the pi zero systems with some common settings, install some handy packages and configure static IPs and hostnames.
First install a clean copy of Raspian Jessie onto the SD card and connect power to the Pi.
( you will need a USB hub to connect a keyboard, mouse and wifi dongle to your Pi zero for the following steps ) Once booted, connect to your Wifi network and open a terminal window.
check for updates,
> sudo apt-get update
Next, from the desktop open the config app [ Pi Menu > Preferences > Raspberry Pi Configuration ]
- on the System tab, expand the filesystem, change the default pi account password, boot to cli, no auto login
- on the Interfaces tab, just enable SSH and 1-wire
- on the Performance tab, reduce GPU memory to 16 - since we will only be using the cli.
- and on the Localisation tab, set all options to the correct location.
Click Ok to save, but do not reboot yet.
Next we need to give each pi zero a static IP address and new hostname.
Check your network hub DHCP settings. I was able to reserve the following static IP addresses :
- 192.168.0.100 : pi-heating-hub ( Pi with relays, connected to water heater and/or heating pump )
- 192.168.0.101 : pi-heating-remote-01 ( Pi with 1-wire thermometers in lounge )
- 192.168.0.102 : pi-heating-remote-02 ( Pi with 1-wire thermometers in bedroom1 )
- 192.168.0.103 : pi-heating-remote-03 ( Pi with 1-wire thermometers in conservatory )
To simplify the set-up process I've written a script to do this for us, run this script on each pi zero to configure the static IPs, you should leave the gateway and DNS settings as default.
> curl "https://raw.githubusercontent.com/JeffreyPowell/pi-config/master/base-install.sh" > base-install.sh && sudo bash base-install.sh
Once this is complete you should shutdown,
> sudo shutdown -h now
and disconnect your keyboard, mouse and monitor before powering on again, and you should now be able to connect over SSH using the new static IP and new password.
> ssh firstname.lastname@example.org
Step 4: Build and Configure the Controller Hub
Connect the relay to the pi zero using the following pins :
Pin 2 - GPIO 5.0V
Pin 6 - GPIO GND
Pin 8 - GPIO 14 ( connected to hot water pump relay - if available )
Pin 10 - GPIO 15 ( connected to Heating relay )
Wire the 2 channel relay as shown in the diagram. ( if you just have a 1 channel relay you only need to wire pin 10 )
Now you'll need to refer to your heating system manual / wiring diagram.
My system currently uses a 15min / 24hr electro mechanical clock and a remote wired thermostat.
The heating is controlled by the timer and thermostat, the hot water boiler constantly on and water temperature is controlled via the boiler water thermostat.
Warning, mains voltage !
I was able to splice the heating relay into the circuit inline with the timer and thermostat.
Now when you turn your old thermostat to maximum, and set your clock to constant on, the heating relay will control your heating pump.
and spliced the hot water relay inline with the boiler water thermostat.
( as a fail back option you could set the heater relay to constant on, and use your old timer and thermostat again )
After the basic install we need to install the packages and software that will switch the relays and control your heating ( apache, php and mysql )
Connect to the pi over SSH
> ssh email@example.com
and execute this script to install everything
> curl "https://raw.githubusercontent.com/JeffreyPowell/pi-config/master/pi-heating-hub-install.sh" > pi-heating-hub-install.sh && sudo bash pi-heating-hub-install.sh
As part of the MySQL instal process it will ask for a root password, make a note of this for the next step.
Now we need to configure the database :
> curl "https://raw.githubusercontent.com/JeffreyPowell/pi-config/master/pi-heating-hub-mysql-setup.sh" > pi-heating-hub-mysql-setup.sh && sudo bash pi-heating-hub-mysql-setup.sh
once everything is installed, you can browse to :
and you should see the status page displayed. - we have not set up any sensors yet, so there will be no data.
Step 5: Build and Configure a Temperature Sensor
For each pi zero sensor you want to use, follow the following step
We only need to connect to pins 1, 6 and 7, the other pins are used just to add physical strength.
Pin 1 - GPIO 3.3V ( 1-wire power )
Pin 6 - GPIO GND ( 1-wire ground )
Pin 7 - GPIO 4 ( 1-wire data )
Connect the 1-Wire Digital Thermometer as shown in the diagram. You could connect directly to the pins, or use a small prototype board to connect several 1-Wire Digital Thermometers to the same Pi zero. ( I have two connected to my pi-heating-sensor-01 and three connected to pi-heating-sensor-02 )
Connect to each pi-heating-sensor in turn,
> ssh firstname.lastname@example.org
We can now start to install the packages and software that will read the 1-wire thermometer data and make it available the the heating controller hub.
again there is a script to do this :
> curl "https://raw.githubusercontent.com/JeffreyPowell/pi-config/master/pi-heating-remote-install.sh" > pi-heating-remote-install.sh && sudo bash pi-heating-remote-install.sh
once everything is installed, reboot :
> sudo shutdown -r now
now we need to edit the config file ~/pi-heating-remote/configs/sensors
but first we need the discover the serial numbers of the connected 1-wire thermometers
> ll /sys/bus/w1/devices/
Will show that I have two 1-wire thermometers connected, 28-0000056e625e and 28-0000056ead51.
drwxr-xr-x 2 root root 0 Jan 25 15:23 . drwxr-xr-x 4 root root 0 Jan 24 16:17 .. lrwxrwxrwx 1 root root 0 Jan 24 16:43 28-0000056e625e -> ../../../devices/w1_bus_master1/28-0000056e625e lrwxrwxrwx 1 root root 0 Jan 24 16:43 28-0000056ead51 -> ../../../devices/w1_bus_master1/28-0000056ead51 lrwxrwxrwx 1 root root 0 Jan 25 15:23 w1_bus_master1 -> ../../../devices/w1_bus_master1
Now edit the config file and insert these serial numbers.
> vi home/pi/pi-heating-remote/configs/sensors
Delete the default settings and insert the serial number and a name for each 1-wire thermometer.
remove all this :
28-000005cf873e = External 28-000005d0065c = Conservatory 28-000005d01a5a = Conservatory Rad
and add your data, e.g. :
28-0000056e625e = Lounge 28-0000056ead51 = Garden
save and close the config file.
That's it :) everything should now be installed and configured, but lets test it to make sure.
First we can run some tests via our SSH session and verify how many 1-wire thermometers are connected :
> curl localhost:8080/count.php && echo 2
then we can display the names and values of each thermometer :
> curl localhost:8080/name.php?id=1 && echo Lounge > curl localhost:8080/value.php?id=1 && echo 2.5 > curl localhost:8080/name.php?id=2 && echo Garden > curl localhost:8080/value.php?id=2 && echo 21.5
as you can see by the temperatures returned, I need to swap the physical location of the sensors !
We can also test from our desktop via a browser using the following URLs with your static IP :
http://192.168.0.101:8080/count.php http://192.168.0.101:8080/name.php?id=1 http://192.168.0.101:8080/value.php?id=1 http://192.168.0.101:8080/name.php?id=2 http://192.168.0.101:8080/value.php?id=2
Repeat the above instructions for all additional sensors, pi-heating-remote-02, pi-heating-remote-03 etc.
Step 6: Configure Input Sensors and Output Devices
Now we have the hub and sensors setup we can start to connect then.
First we will configure the input sensors. ( thermometers )
Browse to the hub status page :
Click on 'Input Sensors' button then click on the 'Scan for new sensors' button.
the app will scan your local network for any thermometer sensors you have set up.
The hub updates sensor data and switches the relays every minute.
Click 'Done' to go back to the main status page.
Next we will configure the output devices. ( relays )
Click on 'Output devices' then click on the 'Add new' button, then click 'Edit' next to the new device.
Update the name to 'Heating' and enter the pin number of the heating relay ( in our case 10 ), the relays I used are active when the GPIO pin is low, so I entered zero in the Pin Active H/L field. ( if your relays are active high, enter 1 )
Click 'Save' then 'Done', then 'Done' again to get back to the status page. ( repeat the last few steps if you have a second relay for the water boiler on pin 8 )
Now we need to create a schedule to switch the heating relay(s) on and off.
Step 7: Create a Heating Schedule
A schedule can be triggered at a specific time, a day of the week, a mode, a timer, when a device is connected to the local network or a combination of these triggers.
A mode is a simple flag that can be either on or off, e.g. 'Working from Home', 'Weekend Away' etc.
A timer is similar to a mode but once activated will automatically deactivate after a specific time, e.g. 'Heating Boost'
A connected device can be anything on the local network identified by its MAC address, 'Jeff's iPhone' etc.
- To create a Mode, from the status page, click on 'Modes' > 'Add new' > 'Edit' , rename it 'Weekend Away', click 'Save' > 'Done' and 'Done' again to take you back to the status page. The 'Weekend Away' mode button is now displayed on the right of the status page.
- To create a timer, from the status page, click 'Timers' > 'Add new' > 'Edit', rename it 'Heating boost' and change the duration to '60', click 'Save' > 'Done' and 'Done' again to take you back to the status page. The 'Heating Boost' timer button is now displayed on the right of the status page.
You can click the mode button on and off, and the timer button will count down every minute.
Ok, now we can configure some schedules.
- From the status page click 'Schedules' > 'Add new' > 'Edit', rename the schedule, set the time and day of week for the schedule to run, select which output device you want to trigger 'Heating' and/or 'Water Boiler', then select the temperature required, ie. when the lounge is below 21 degrees. All these setting need to be TRUE for the schedule to trigger, all other options are set to 'be ignored'. click 'Save' > 'Done' > 'Done'
That's it, your heating is now controlled by your raspberry pi.
Step 8: External Access From the Internet
If you want to control your heating from anywhere you will need to configure your router to forward port 8080 network traffic to the heating controller hub ( 192.168.0.100 )
and for security, add some basic password protected access.
> curl "https://raw.githubusercontent.com/JeffreyPowell/pi-config/master/pi-heating-hub-secure.sh" > pi-heating-hub-secure.sh && sudo bash pi-heating-hub-secure.sh
enter a password when prompted.
The next time you browse to the app you will need to enter username 'admin' and the password you entered.