Control heaters automatically using Raspberry Pi

17.12.2015

This app is built to run on Raspberry Pi, combined with some off-the-shelf remote control sockets, it makes controlling AC appliances easy. By adding temperature sensors and some scripting we can demonstrate how to control your room heating according to smart load schedule.

Here’s a breakdown of the Project:

  • The Raspberry Pi will act as the brains and the gateway of operations – it will run a Node app, interface with temperature sensor and 433 MHz transceiver
  • By switching the socket on or off we will control how much heating power is radiated to the room

To do this, you will need:

  • Raspberry Pi
  • Some remote controlled sockets (we used these)
  • DS18B20 temperature sensor
  • Weather Underground account for outside temperature

Setting up hardware

Eventually we would suggest a hardwired relay, but messing around with a household mains electrical supply is not something to be undertaken lightly. So, for now we’ll be using the safer option of radio-controlled switches.

We’ve used some reasonably cheap £20 sets from Energenie4u.co.uk which come with 2 sockets. They have come up with these great Pi-mote control boards that make installation easy as possible. The instructions is found in here.

In addition Energenie have also Two way Pi-mote with remote control sockets that can do both, switching and monitor power consumption. This would would make the power consumption analysis more accurate. Unfortunately there’s no EU version available yet.

Setting up the DS18B20 temperature sensor is also a no-brainier. In addition to the sensor itself, you would need 4.7 kΩ resistor, some jumper wires and a soldering iron, because the Pi-mote control board covers all the necessary pins. The wiring diagram for GPIO pins and a good set of instructions can be found here.

Raspberry Pi fitted with Pi-mote 433 MHz transceiver board

Now it is time to set up the software.

Start afresh

We’re going to use Raspian for this project, and I’d strongly suggest starting from a fresh install if you’ve previously performed other hacks and such (follow the installation guide here). Download the latest Raspian image and copy it to your SD card, plug in the Ethernet cable and connect Raspberry to your home internet router.

Working remotely

The easiest way to work with Raspberry Pi is over the SSH connection from your computer. In order to to that you have to find out what IP address was assigned to your Raspberry PI (e.g go to 192.168.1.254) by your router. In order to connect over SSH, download a free telnet client.

Type the IP address of your Pi to Host name with connection type SSH and click Open. Putty security alert – this will be displayed once whenever there is a new connection, press yes.

Login using your credentials: default user name – pi; password – raspberry

Finish the configuration by running sudo raspi-config

  • Expand file-system
  • Go to Internationalization Options
    • Change TimeZone – choose your TimeZone

If you need to have your Raspberry connected to your home over WiFi, there is a very useful guide over here.

These first few commands will update the system and install prerequisites:

sudo apt-get update
sudo apt-get upgrade

Install Node.js by following this guide

Download latest Node.js build for Raspberry Pi:

wget https://nodejs.org/download/release/v0.10.0/node-v0.10.0-linux-arm-pi.tar.gz 

The actual url to get the files for node will change as the version changes, you can always see the list of files available for download here: http://nodejs.org/download/

Unpack files in local directory:

cd /usr/local sudo tar xzvf ~/node-v0.10.0-linux-arm-pi.tar.gz --strip=1 

That’s it. You can confirm it’s there by checking the node version with:

node -v npm -v 

Then we need to Install node-cron

npm install cron 

Finally, it is time to download the various scripts  from Github that can read the inside and outside temperature, control the sockets and send data to SLS cloud and request a working schedule to work with

cd ~pi git clone https://github.com/slsapplications/heater.git 

Before starting the script there are few things that you need to configure.

Set up 1-wire support

Start by adding the following line to /boot/config.txt. You can edit that file with nano by running sudo nano /boot/config.txt and then scrolling to the bottom and typing it there

dtoverlay=w1-gpio 

Then reboot with sudo reboot. When the Pi is back up and you’re logged in again, type the commands you see below into a terminal window. When you are in the ‘devices’ directory, the directory starting ’28-‘ may have a different name, so cd to the name of whatever directory is there.

sudo modprobe w1-gpio sudo modprobe w1-therm cd /sys/bus/w1/devices ls cd 28-xxxx (change this to match what serial number pops up) cat w1_slave 

The interface is a little unreliable, but fortunately it tells us if there is a valid temperature to read. It’s
like a file, so all we have to do is read. This command is used in file tempIn.py in your home folder.

cd ~pi sudo nano tempIn.py 

change from the line 6 – tfile = open(/sys/bus/w1/devices/00-00000xxxxxxx/w1_slave) to match the folder to whatever your directory name is.

Controlling the sockets

When using the Energenie remote controlled sockets, the sockets must be programmed to learn a control code from the transmitter. Launch the following program and go as instructed:

sudo python ENER002.py 

After this you can test if the sockets work as intended by typing in

sudo switchOn.py sudo switchOff.py 

Get the outside temperature

In order to to find the most optimal schedule the value of outside temperature has to be sent every time together with the inside temperature. This value could be obtained from the remote wireless sensors like these or form an open weather service like Weather Underground. Register an account and look for assigned API Key ID. Enter this Key into the file

sudo nano tempOut.py 

Find this line:

f= rllib2.urlopen('http://api.wunderground.com/api/enter your KEY here/geolookup/conditions/q/EE/Tallinn.json')

Reporting of outside temperature will be omitted in the future versions by SLS service as this can be obtained depending from your location.

Configure heaterConfig.js

Final thing to do is to configure the heaterConfig.js file. You need to make an account in our developers site http://slsui.azurewebsites.net. It is reccommended to read the heating model API documentation by selecting  Documentation -> Heating model.

Then edit the heaterConfig.js file:

sudo nano heaterConfig.js 

Edit the following lines:

  • config.deviceID = paste here the device ID that is obtained by creating a new device in developers site.
  • config.dataAuthorizationHeader = paste here the Device Access Signature that is obtained by creating a new device in developers site.
  • config.k = insert the total power of your heaters in MW (e.g 2x 2.5kW heaters are total 0.005 MW)
  • config.minTemp = minimum temperature allowed in the room;
  • config.maxTemp = maximum temperature allowed in the room;

That’s it, all done! You can start running the app by typing

sudo node heater.js 

Now all you can do is configure the Raspberry Pi to automatically start the script on boot.

Sulle meeldis see artikkel?