Pi controlled room temp

In my den where I do all my electronics projects and model railway, it gets pretty cold so I needed a way to heat up the room and did not want electric heating due to the cost. I am very handy with plumbing so decided to plum in a radiator teeing of a rad on the opposite side of the wall. My next problem was how to control the temperature in my room and not turn on all the house radiators. In the past I have run separate cables from the boiler to the room I wanted to control and have a room stat control a zone valve. This all worked great but this time I did not want to run cables from my room to the boiler. So I turned to the Raspberry Pi and started building a Hive system where I could control the room temperature using a web page and control the boiler over a network through my router.

I already had a pi next to my boiler which was controlling my solar panel. This pi which I will call Solarpi already has a server installed which is running php and mysql and is Ethernet connected to my router. So all I really needed to do was run a Pi in my room with a server which could update the Solarpi database over the network. The Solarpi will then turn on the boiler if the data base has changed state. Easy right, well this was going to be a big learning curve to get this working properly as I am not an expert programmer.

First lets get the wiring out of the way as this is the easy part.

Hopefully some of the above diagram makes sense. As I have said the SolarPi is already connected up and I already have a relay bank of 4 connected to this Pi's GPIO pins which I am only using 1 relay for the solar panel pump. So all I had to do was run one more jumper cable from one of the GPIO pins to a relay that was not being used. The contact side of the relay had two wires to the boiler where the house thermostat would connect to . By connecting these wires together the boiler would turn on, so now if I energies the new relay the boiler would start up.

In my office I had already plumbed in a zone valve. These valves open up with 240v supply to the valve motor. When open a set microswitches make as a form of feed back to show the valve is open. I connected the pi to a relay via a GPIO pin. When heating was required the Pi would turn this relay on which will motor the zone valve. The microswitch on the zone valve was connected to GPIO GND on the pi and 5V. This was so when the valve was open I could read the microswitch and tell the SolarPi to turn on the boiler. Finally the room temperature will be read using a One Wire temperature sensor DS18B20. I have used these on my solar panel for many years and they are very accurate easy to use a very reliable.

The first part of the program is a python script this is linked below. This script first connects to the batabase on this pi and ht edatabase on the solar pi. To connect to the solar pi i had to make a seperate user name and password with external permission. Once connected the script opens the file where the 1-wire temperature device stores the temperature. It reads the temperature and assigns the value to variable roomtemp.

Teh main loop gets the heatingdemand value and theremoste value from the database which was updated from the web site. It then runs some IF functions to see if heatingDemand is equal to 1 which means heating should be on and compares the value with the thermoste and the room temperture. If the room is cold then make GPIO 18 HIGH which will turn on the relay for the zonevalve. If not then turn off. When the zonevalve has fully opened the micorswitch contacts will make and input into GPIO 14 on the pi. The Python script will read this and is HIGH indicating the valve is open the BoilerOn variable will be set to 1 and if pin LOW then boilerOn will be 0. This is then sent to the SolarPi database next to my boiler.

temperature_display.py

For the web pages I had to produce a page which would display the temperature and control for turning heating on/off and a temperature control. I chose the HTML slider for the temperature control and the check box with a CSS button slide over this. This page would have to call a php page to update the OfficePi database and will also have a separate php file to read from this same database. Finally, a security logging and password was set up so I could connect to all this from outside my house.

Below is a list of the web pages and then the code

  • Index.php (logging page)
  • logout.php (called when logout button clicked to end session)
  • session.php (used to ensure anyone connected to main page has logged in)
  • login.php (checks logging credentials with stored user details on data base to allow access)
  • profile.php (main page showing room temp and controls)
  • data.php (php file to collect data from database for profile.php to use)
  • dataUpdate.php (php file to update new settings to database sent from profile.php)
  • office.css ( css style file for buton slider)

The Index page is below with a link to the script. This is very basic page with no style at present. It includes the loging.php file which is at the top. The HTML form data for username and password is sent to the loging.php which checks the credentials and is all OK enables the $_SESSION and so this page will open profile.php

Index.php logging page

Next below is the login.php script. This checks a username and password has been filled into the text boxes on the Index page and then checks the user name and password against the data stored on the database. If all OK then start session so the Index page can redirect me to the profile page for the temperature control.

login.php file

The session script is below which ensures anyone on the profile page has been logged in and has the correct credentials to what is stored in the database

session.php file

Now for the main profile.php page. This is a form page with css to allow it to adjust to screen size. The main HTML body shows the room temperature which is acquired by the to ajax function. This first ajax function runs every 2 seconds. It collects the data from the database, room temp, thermostat setting and if heating has been selected.

The slider button id "on/off" has a css for the check box to make it a sliding switch. when clicked it will run the onOffFuction() which will toggle id variable from 1 to 0 or vise verse. This will then run myFunction which will send to dataUpdate.php the thermostat setting and the id value.

That it it for the main page all the work is done with the php scripts

profile.php file

The data.php file link below

data.php file

Finally the dataUpdate.php script

dataUpdate.php file

Below is the link to the office.css which was needed to create the button slider for turning the heating on/off

office.css file

Finally I have given a link to the solarPi python script. Most of this script is dedicated to my water solar panel and alarm system. The main part for the office heating is near the bottom. It checks the database for a heating demand and if it is 1 it makes GPIO 18 HIGH which will turn on a relay and this will turn on the boiler. If it is LOW it will turn it off. 

Solar.py file

Leave a Reply