Web Controlled Central Heating

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!

 

My Motivation

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.

 

Overview

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.

 

cluster

Since my last post, I’ve packaged up the Pi cluster. It sits in my utility room, connected to my network via a wireless ethernet bridge

 

 

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:

  1. 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.
  2. 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.
  3. 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:

  1. My home router has a Public IP address. This is the one which the rest of the internet can ‘see’.
  2. 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:

 

network

How the devices on my home network connect to the rest of the internet through the router. Where does the router send an HTTP request to?

 

Port Forwarding

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:

bramble

How the cluster communicates with the router. Any HTTP requests received by the router will be forwarded onto Pi3, which is listening on port 80. Port 22 handles SSH requests when open, but that is out of the scope of this post.

 

OK, so now if I put my public IP address into a web browser, will I get my page?

Yes, and no…

DDNS

There are two issues remaining:

  1. 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.
  2. 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:

  1. The HTML  script used for the homepage displays four images, each signifying whether a button is pressed on or off.
  2. 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.
  3. The PHP extension also runs a Javascript file. This updates the corresponding element of the array and changes the respective image from ‘off’ to ‘on’ if a button is pressed on screen. This changes the required image without having to reload the entire page.

This took me a while to figure out but it makes sense when looking through the code.

IMG_0893

My home control webpage. The on/off statements for each button change as each button is pressed.

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:

labelled gpio cropped

Someone is bound to notice this is a Pi mark 2 and not a mark 3 as used in my cluster…

 

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.

pi led

The cluster (removed from its case) connected to an LED. The LED is connected across a GPIO pin on Pi3 and the ground pin. This allows it to be switched. Ignore the other components on the board – they are from another project.

 

Step 5 – Using the GPIO pins to switch the boiler

Here is the control panel on my boiler:

panel png

It feels boring posting images of dull, outdated technology on this site…

 

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:

circuit (2)

Switching on the GPIO pin will allow the battery voltage to run through the transistor and therefore activate the solenoid. The diode is a ‘flyback diode’, used to protect the solenoid from voltage spikes when the voltage is switched off.

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:

solenoid

When the GPIO pin is energised, the thrust pin extends and presses the on/off button. It is retracted after 1 second. Simple.

 

 

cover off

With the cover off. It looks particularly untidy in this photo, but the board with the circuitry clips on the cover so it isn’t quite so bad with the cover in place.

 

 

Conclusion

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:

  1. Installing a light sensor to detect the flame in the boiler.
  2. 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

 

http://www.instructables.com/id/Simple-and-intuitive-web-interface-for-your-Raspbe/

My system was largely based on this. I strongly recommend reading this guide if attempting this yourself.

 

https://www.digitalocean.com/community/tutorials/how-to-set-up-password-authentication-with-apache-on-ubuntu-14-04

Password protecting your webpage. Very important for applications like this.

 

https://httpd.apache.org/

Apache web server homepage. Loads of material here to get you started hosting your own webpage.

 

http://raspberrywebserver.com/serveradmin/run-a-script-on-start-up.html

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!

 

My code

The three scripts can be found at the links here. The ‘index’ file contains the html for the webpage itself. The ‘gpio’ file is a PHP script to deal with button presses and pass the instructions to the GPIO pins. The ‘script’ file is the Javascript file which changes the appearance of the buttons as they are pressed.

https://www.dropbox.com/s/ryztjbigvbj3rde/gpio.php?dl=0

https://www.dropbox.com/s/6n5u3z6hgk3ebol/index.php?dl=0

https://www.dropbox.com/s/0x53aez6o1pd714/script.js?dl=0

 

 

Advertisements

2 thoughts on “Web Controlled Central Heating

  1. Hi! Love your blog, I found it a few months ago and have truly enjoyed reading it and catching up. Right up my alley! (I study physics and computer science at the University of Toronto and work on electronics in my spare time).
    I was wondering why you plan to use a rechargable battery with the solenoid. It’s almost too fancy to provide a rechargable long-term current delivery device when, at least for the purposes of what you’ve described here, all you need is a capacitor to deliver charge in the short term. Some benefits:

    -No complex charge circuit required, simply calculate the size resistor you need for the charge time you want (given the size of the capacitor you choose)
    -Solenoids produce magnetic flux from current, and capacitors are exceedingly good peak current providers
    -Cheap and easy, reduces complexity of your circuit, provides power for the second that you need it to push the button and recovers quickly afterward
    -Can be charged directly from the +5V pin of the RPi

    This becomes moot if you wish to provide power to more things that may actually require more power over a longer period of time, but then that begs the question… why use a battery at all when you could just skip a step and get power straight from a supply?

    Like

    • Hi Matt,

      Thanks for the kind words, nice to know you’re a fan of the blog!

      Yeah, this post was definitely one of my ‘programming first, electronics second’ ones. The actual physical setup was more of a prototype really. I actually had a solar panel/lithium battery set lying around that I was planning on using to power this and a few other bits and pieces.

      Since I never actually hooked anything up to that supply, you are absolutely correct, a capacitor is by far a better solution. In fact, I’m going to use one from now on (charging from the +5v pin as you suggest).

      As you’ve probably guessed, my electronics knowledge is pretty lacking so comments like this are really helpful to me.

      Thanks,
      Alex

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s