I always have a ‘want list’ of projects I’d like to do. This one had been on that list for longer than any other. In fact, I’ve been wanting to make my heating ‘remote controlled’ since I was at high school. It’s a huge relief to have finally got round to doing it!
I live in Scotland, it’s freezing, and unless I were to come home at the same time every day (which I don’t), the standard timer on my boiler is useless.
What finally convinced me to build this system was a quote by Scottish Gas for their system. £300. I decided I could probably do it for £30, and I was right.
I wasn’t wanting anything particularly fancy. My aim was to be able to log into a webpage with my phone and, from there, hit a button on the screen to turn the heating on and off. While I was there, I also wanted to be able to control other things around the house, and get a temperature readout.
For now, we’ll just focus on controlling the heating. If I can do that, the other things are trivial.
This is quite a fun post because it involved creating a web server and web applications. This is completely opposite to the sort of stuff I normally do. Luckily it wasn’t too difficult.
Basically I needed to achieve the following:
- Create a web server to host a webpage.
- Make sure I can always access that page from an external network.
- Write some script to display buttons on that webpage and detect when they had been pressed.
- Write some more script to turn the boiler on/off when a button is pressed.
- Find a way to switch the boiler on and off physically using the above.
My source material
Unsurprisingly, this kind of thing has been done before many times. I roughly followed the guide written by TheFreeElectron Over at Instructables . While my end result isn’t a direct copy, it is certainly very similar to his setup so there is no way I’m not crediting him here. I strongly advise anyone attempting this reads through his guide. It is excellent.
Part 1 – Create a Web Server
A web server is basically just a computer that handles requests. In one sentence, when you visit a website, you send a request to the relevant server to fetch the webpage you want and it sends the information back to your computer.
So I needed a computer capable of storing my ‘home control’ webpage and the associated scripts. When I accessed this computer from elsewhere on the internet, it would fetch the webpage, and then run the necessary code when a command was sent to turn the heating on or off.
Clearly, these requirements are pretty minimal. So, only a pretty ‘minimal’ computer was needed. No prizes for guessing I used a Raspberry Pi for this.
In fact, regular readers will note that I have a cluster of 4 Raspberry Pis on my home network. I use them for running most of my programs that need a long runtime. However, much of the time, they are sitting idle. One of them is continuously running a neural network , but the other three basically do nothing when I haven’t sent them a program specifically. So, I decided to make Pi number 3 my web server.
Apache Web Server
Apache is the most commonly used software for building servers. I went ahead and installed it on the Pi. I won’t go into full details but it’s pretty simple to get started with. Apache has a ‘documentroot’ folder, for the Raspberry Pi, the documentroot folder is at /var/www/html. An html file in this folder will be displayed when a visitor accesses the Pis IP address.
For most websites, there would be many files in different folders in the documentroot folder. These would contain the files for the different pages of the website being hosted by the server. In this case, I only needed one page (for now) which is the ‘heating control’ page. So, I wrote a page in html to be displayed when I logged into the site. Nothing complicated, just a black background and some test text. The buttons would be be added later.
It’s worth noting that I very rarely use HTML. It’s really easy though – anyone can pick up the basics in a few hours.
I password protected my newly created webpage. Instructions for this are provided in the links. The need for this is obvious – I don’t want the whole world to be able to control my heating for me.
Part 2 – Making sure I can always access the webpage
Let’s quickly discuss how web pages are accessed.
The internet is built on the TCP/IP protocol. I’ll try and summarise it in 3 sentences because I’m not an expert on it, and a full description would take more than an entire post:
- Every device on the internet has an IP address which is just a number. When you type in a URL, This is associated with a specific IP address. In fact, if you know the IP address of the server, you can just type that in your address bar and it will display the site as normal.
- So, by visiting a URL (or IP) address of a server, you create a connection with the server. The server and your computer will continue to communicate as long as this connection is open.
- When you visit a server, you send an HTTP request for content and it will be fetched and sent along this connection.
So, if I enter the IP address of my Raspberry Pi, will it open up the webpage I just wrote?
Not quite. There are actually different kinds of IP address:
- My home router has a Public IP address. This is the one which the rest of the internet can ‘see’.
- Each device on my network has a Private IP address. We played around with those when I build the cluster.
So, if I type my public IP address into a browser from an external network, how does it know which device to send the request to? There are several devices on my network:
When the router receives an HTTP request (ie someone enters the routers public IP address into their browser), it needs to know what to do with it. The router by default blocks any attempts to access the private network from outside. This is why no one can access your computer from outside your home network.
However, the router can also be configured to forward traffic through any of a number of ‘ports’. There are several thousand ports available on a typical home router. By default, a device running a web server listens for requests on port 80. So, we need to configure the router to forward HTTP requests to port 80. That way, the server will receive the request and respond by returning the requested information.
This is probably easier explained with a diagram, let’s zoom into the Pi cluster from the image above:
OK, so now if I put my public IP address into a web browser, will I get my page?
Yes, and no…
There are two issues remaining:
- I don’t want to be typing an IP address into my browser every time I want to access the webpage. I want a proper URL.
- Far more importantly, most home networks have a dynamic IP address. In other words, it changes periodically.
A free account at no-ip.com solves both of these problems. I was able to give a hostname to my IP address. In other words, it lets me create a URL for the webpage. This is the form of myhostname.ddns.net
More importantly, I downloaded the free software which runs DDNS on the Pi. DDNS stands for Dynamic Domain Name Service. In short, this is a program which constantly checks to see whether my IP address has changed. If it has, it updates the new IP address to be associated with the hostname I chose earlier.
The DDNS software runs automatically and continuously as long as the Pi is turned on. For those interested, I just added it to the init.d directory accordingly to achieve this.
In other words, it ensures the URL above will always direct to my Raspberry Pi on my home network.
So, entering my new URL into a web browser anywhere in the world will bring up my webpage.
If you understand all the above, you can now host your own webpage, and understand how it actually works!
Part 3 – Create ‘pressable’ buttons
This is the part I was a bit unfamiliar with as it involved writing a web application. Nothing too complicated but not something I normally do.
I followed TheFreeElectron’s template and wrote this in PHP. In retrospect I should have just used Python – I have never used PHP in my life. My PHP script is therefore just a modification of his and I can’t really take any credit for it.
Let’s go through everything one step at a time:
- The HTML script used for the homepage displays four images, each signifying whether a button is pressed on or off.
- A PHP script is included as an extension within the HTML code. This contains an array of four 1s and 0s. A 1 results in the corresponding image being the ‘button on’ image, a 0 means the corresponding image is the ‘button off’ image.
This took me a while to figure out but it makes sense when looking through the code.
So now we have a webpage with four buttons which change from ‘off’ to ‘on’ (and vice versa) when pressed. This is all great but they don’t actually do anything. We now need to make the Pi somehow switch the boiler on or off when the correct button is pressed.
Step 4 – Switching the GPIO pins
Anyone familiar with a Raspberry Pi knows what these are:
They are GPIO (General Purpose Input and Output) pins. Executing an appropriate command on the Pi will switch the respective pin(s) on (apply 3.3V) or off (0V). If we can switch these pins on and off remotely, we can basically do anything.
Normally, a Bash script is used to switch the pins on or off. However, a library is available, called ‘wiringPi’. This still runs in Bash but simplifies the commands for switching the pins.
The relevant pin switching commands were included in the PHP script. Now, when a button is pressed, the image in not only changed, but the state of the respective GPIO pin is switched as well. That’s all there is to it!
To check everything was working, I hooked up and LED to the pins defined in the script. After a little bug-fixing, a press of the respective button switched the LED on for one second and then off again. The on-screen button state switched and remained on ‘on’ or ‘off’. The reason for only temporary switching on the LED will become apparent in a second.
Step 5 – Using the GPIO pins to switch the boiler
Here is the control panel on my boiler:
In an ideal world, I’d unscrew the panel and solder the wires from the GPIO pins across the on/of switch. The boiler would see a briefly applied voltage as a press on the button and switch the heating on or off.
Unfortunately, the damn boiler is virtually tamper proof. I normally have no problem taking things to pieces but I couldn’t find a way in without breaking something. I really didn’t want to risk wrecking my boiler so I had to settle for a less ‘professional’ solution.
I wired up a solenoid like this:
Now, a voltage applied to the GPIO pin will cause the solenoid to extend its thrust pin for a second. The voltage is then cut and the pin retracts again. The assembly is then mounted to the boiler so it is able to press the button:
That’s a basic system in place. It is far from perfect, so I’ll be making the following improvements in time:
Active on/off confirmation
The main issue with this system is that it cannot detect if the button was actually pressed. Eg if the solenoid battery runs out, the webpage will indicate the heating has been switched off even though it remains on. If I could access the back of the LCD, I could find the connection which is energised to display the ‘on’ symbol on the screen. Reading this connection would give a confirmation. However this is not an option, so I will address this problem by:
- Installing a light sensor to detect the flame in the boiler.
- Installing a thermometer to give the temperature in the house (this is really simple to do).
Solenoid is battery powered
As alluded above, once the battery runs out, the solenoid will not switch the heating. This will be solved by replacing the batteries with a rechargeable Li-ion battery connected to a small solar panel. I have a charging circuit in my spares drawer so this should be simple enough.
Only one GPIO pin is used
I have four buttons on the webpage but am only using one. I plan to use the rest for other purposes. I’d like to make a small plant-watering system and maybe control some of the house lights too.
For now, I’m happy with my budget system though. It works absolutely fine and it’s a great feeling to finally tick it off my list.
References and Further Reading
My system was largely based on this. I strongly recommend reading this guide if attempting this yourself.
Password protecting your webpage. Very important for applications like this.
Apache web server homepage. Loads of material here to get you started hosting your own webpage.
Here is a website about hosting on a Raspberry Pi…which itself is hosted on a Raspberry Pi! The linked page discusses how to automatically execute scripts at startup (required for the DDNS script).
Note that this site isn’t very fast. There is a reason Amazon and Facebook aren’t hosted on Raspberry Pis!