Review: Using the Raspberry Pi for a Home Automation project

Saturday, 23 March 2013

For some years, I have been thinking of computerising the heating system in my house. I am unsatisfied by the both the (high) expense and the (poor) feature set provided by commercial heating timers.

I wanted a dual-channel timer, capable of controlling hot water and radiators independently. I wanted it to accommodate any number of periods of operation within each day. I wanted it to take thermostat inputs into account when operating the heating system - unless overridden by the program or a user. And I wanted to be able to reprogram it via WiFi.

A central heating system similar to the one in my house (from here). When the system is on, hot water from the boiler is pumped through the hot cylinder to warm the water reaching the taps. Optionally, an electrically-controlled valve allows the hot water to flow through the radiators too.

I think the only low-cost way to achieve all these requirements is to build a home-made system based on a small computer. I have not found any commercial solution that ticks all the boxes.

It's not too hard to embed a computer as a control system; indeed a device known as a microcontroller is ideal for the purpose. The trouble is that microcontrollers are not usually capable of connecting to a WiFi network. The network requirement eliminates all sorts of low-cost solution, and stalled the project for several years.

The Raspberry Pi has been hyped very strongly. It's a small, cheap computer that is capable of connecting to WiFi if you add a USB WiFi dongle.

Typical example of press coverage of Raspberry Pi, in this case from BBC News.

I try to be very skeptical of anything that has been so strongly hyped, and in the case of the Pi, I am particularly skeptical of its value in education. It is said that access to this computer could inspire a new generation of programmers. But the new generation of programmers already have computers, and are already programming them. You might as well hand out free footballs in the hope of increasing the population of professional football players, oblivious to the facts that (a) footballs are ubiquitous already, and (b) some people are just not interested in football.

Computer Science certainly can be taught in a school, in much the same way that schools can teach Latin and Further Maths. But there are other priorities. At a time when plenty of pupils are leaving school at 16 with poor literacy, there are clearly more pressing problems, which should be handled properly before introducing advanced topics and new technologies.

Pi before Pi - in this picture from 2002, I have just completed my university's "Microcomputer Project" (MCP) course. The course did not (and does not) use Raspberry Pis, but it has always used similar devices with similar capabilities. The device on the desk is a Z180-based single-board computer (SBC). The course now uses an ARM system on chip.

However, this does not mean the Pi is not useful. For instance, it certainly is useful for teaching at university level. At my university, a course based on similar small computers has been running for decades, using hardware developed in-house by our hardware team. I gather that the Pi has inspired other universities to introduce or update similar courses. The Pi is also useful to hobbyists, like myself.

The Pi is cheap but well made. I bought a "model A" - the name scheme is surely a nod to a certain lower-cost variant of the BBC Microcomputer. This version is missing a couple of components (which I don't need) and therefore costs less. And at £20 on Ebay (including PP) I thought it a bargain.

Raspberry Pi model A.

The Pi also provides a very nice environment for writing embedded applications. You can install a Linux distribution and get basically the same feature set that you would get on a PC: so you can write embedded software in high-level languages, and you can make use of all of the usual Linux features, like networking and support for a huge number of USB devices. Not to mention a graphical user interface, if you want it, and web browsers, if you want them.

The CPU is not very powerful, and the memory is limited, and an SD card is used in place of a hard disk. Everything runs quite slowly in comparison to a PC. But this is expected. The software is still very usable by attaching a keyboard, mouse and monitor. I installed the "Raspbian" Linux distribution onto a 2Gb SD card and on first boot, it asked me some questions about installation. On the second boot, I was in the GUI, and was able to use the provided WiFi setup utility to connect to the Internet. I don't think any improvement is needed here.

Raspbian Configuration Tool (runs automatically on first boot).

I did have a few problems. Many of these were related to (electrical) power. If you buy a Pi, you really have to buy a powered USB hub as well, because the Pi's power supply is only sufficient to run extremely economical USB components. Keyboards and mice are probably ok, though not always. WiFi dongles are definitely not, because they use too much power. Unfortunately the usual symptom of a power glitch is a spontaneous reboot or crash, which is no fun at all.

Further problems were caused by my initial choice of WiFi dongle. It seems that any dongle with a "Ralink" chipset will not work perfectly. Things will work at first, but certain network events seem to upset the WiFi dongle, requiring a reboot. Other Pi users generally blame the manufacturer for failing to support Linux properly and not fixing buggy firmware. I worked around the problem by finding a dongle with a "Realtek" chipset, which is better supported, and if you are looking for your own Pi, I recommend you do the same.

An annoying feature of the Pi is a lack of an on-board real-time clock (RTC). When the Pi boots, it does not know the date and time, and of course the time is quite important for heating control, because heat is required at certain times of day. The time can be obtained via an Internet connection, but nobody wants a heating system that won't work without an Internet connection! I solved the problem by buying a RTC module on Ebay for about £5. The RTC module connects to the Pi via I2C, and very little work is required to connect it to Linux.

Real-time clock (RTC) module which can be connected to a
Raspberry Pi after removing two pull-up resistors.


A nice feature of the Pi is the 26-pin header for external connections. Most of the pins are usable as GPIOs - i.e. programmable pins for communicating with external hardware - though some can also be used for various secondary functions, e.g. a serial port, or the aforementioned I2C. I used some of these to capture data from the heating system (e.g. thermostats) and control the heating system hardware (valve, boiler and pump). PCs now lack anything even remotely similar to GPIO pins.

One annoyance of the 26-pin header is that the logic level is 3.3V and the pins are unprotected against overvoltage, short circuit and so on. This made me paranoid about using them at all, knowing that it would be easy to damage the Pi's hardware by connecting them wrongly (e.g. output to output). I decided to connect the pins via opto-isolators which provide a degree of protection. One side of each opto-isolator is connected to the Pi and the other is connected to some other hardware, like a thermostat or the drive transistor for a relay. The opto-isolators provide no protection against mistakes on the Pi side, but mistakes on the other side will (at worst) wreck the opto-isolator, which is a very cheap component. Additionally the opto-isolators can be used to level-shift to higher voltages (e.g. 5V, 12V). My design used a 12V supply to drive the relays.

Given the Pi's target audience, I am surprised that the device does not include a greater degree of protection from miswiring. But there is a cost argument against including that protection, and the buffering can be provided by an addon board, which would presumably be permanently connected to the Pi if used in a student lab environment.

The ease of programming the Pi meant that I could write the heating control software in Twisted Python and have it act as a web server. The control buttons and information on the web page are dynamically generated and updated in (almost) real-time using the JQuery Javascript library. These allow the normal timed operation of the device to be overridden temporarily with a single mouse click.

The heating system's web interface.

It is a nice experience to have so much flexibility and freedom while writing embedded software. Usually, embedded software runs in an extremely constrained environment, but in this case there is far more computing power than necessary. I can write software that is very inefficient, provided it is correct! My Python program uses, on average, about 10% of the CPU of the device: this includes serving web pages, polling the inputs, and switching the outputs as necessary.

The main result has been a warmer house, because I have better control of the heating system. Overall, given that I could reuse parts from earlier projects, the total cost has been about £40 - the major costs being the Pi (£20), a 5V/12V PSU (£7), a Realtek WiFi dongle (£5), a RTC module (£5) and various other electronic parts such as relays, LEDs and opto-isolators (~£5).

Home-made Pi-based heating system, installed in my airing cupboard. The device on the front of the box is a powered USB hub (Asda, £3). The black box is a 5V/12V external hard disk PSU. The LEDs show the state of the thermostats and relays.


Without the Pi, the system cannot be built at such a low cost. The board is ideal for a hobbyist and I am already thinking of other home projects that it might be used for.

I also think that the new system brings a more efficient house, because better control means that the heating is not used unnecessarily. I hope that I will make that £40 back in energy savings someday.