Thursday, November 5, 2015

IoHeating Part 1

Recently I had a few days off from work and decided to solve one of the problems that had been bothering me throughout winter. Namely a cheap heater which had a temperature dial that was:
  1. Completely unlabeled
  2. Driven by a large loud low accuracy thermostat.
Instead of solving these problems by getting a new heater or off the shelf controller I decided to make things a little more interesting. That meant it was parts shopping time. 

If you are switching mains power you have two options relays and triacs. Triacs are a sextuple layer silicon switch similar to the better known transistor but compatible with alternating current. They are great because you can switch them many millions of time with no wear and because they are completely silent in operation. Relays are simply electronically operated physical switches. A triac would clearly be optimal here. A quick Google later and it turns out that the majority of parts for the hobbyist market are based around relays. I wanted to utilize one of these readily available parts because:
  1. When I have time off work I'm trying to be as lazy as possible
  2. Relays and triacs often need a drive transistor which I would have to solder on veroboard (eww)
  3. Isolation.
In New Zealand we use 240V AC in every country the AC transmission voltage is dangerous, here especially so. Isolating the low voltage electronics especially the controller (more on that soon) is important for safety and longevity. Fortunately electrocuting kids is very bad press so most of the hobbyist relay boards include a drive transistor AND an optoisolator. If you haven't come across these before I'm about to make your day.

Optoisolators are essentially a transistor where the gate is not electrically connected but optically connected. So on on side of the package you have an LED and on the other a transistor with a gate that is excited by photons not those dirty old electrons. This whole thing is packaged up in a nice black box that puts the input side far enough away from the output side that everything is electrically isolated and safe enough to lick (do not lick). We are actually using the same power supply for driving the relay and the controller so this doesn't actually make us all that much safer but it is still good to isolate our delicate IO lines from nasty physical things like relay coils.

Something like this.
So we have a switch that we can control. Now we need to know when to turn the heater on and off. The clunky old thermostat relied on a bimetallic strip to sense the temperature of the room. This sensor if you want to call it that is large and has an inherent hysteresis. We can do better. How precise does our temperature need to be half a degree you say? Perfect we'll use the DS18B20. This is one of the first digital parts I ever utilized. They are a robust, decently accurate, and voltage compensated sensor. Even better you only need a single IO line to talk to them due to the exceedingly well named "1-wire protocol". 

Finally time for the controller! Particle (previously known as Spark) are a group who have developed a seriously amazing little bit of kit. The Photon dev board. To boil it down. ARM microcontroller, WIFI 802.11n, Cloud API and development platform, ludicrously simple code and integrations, $20. Go and buy one right now.

So with our shopping list complete let's build our thing. Just so you know a couple of additional components will probably come in handy, those are.
  1. A heater with some space in it
  2. A power supply for your controller
  3. A high temperature insulation. Don't use heat shrink this is a heater. Instead use Sugru

With that the beautiful build is revealed! From top to bottom we have. A USB power supply from a hapless phone and behind it the Photon board. Below that we have the original heater switches and the thermostat. At the bottom you'll see the relay board and the actual heating elements. The relay board has been tapped into the AC power such that it can break the circuit to the heater whilst leaving the Photon board powered (clever huh). It is also wired in such a way that all the heater's original safeties are in place. The original thermostat (turned up to full), a thermal fuse (in between two of the elements you might spot it) and a thermal cut off at the other end of the element which isn't pictured. Also not seen the the DS18B20 as that is carefully located outside the case as to make it less susceptible to heat soak from the heater chassis. There are more pictures of the build here.

The genius of the Photon dev board is just how easy it is to step integrations between real world items like our heater and the world of data. For this project I used the development platform and whipped up a simple firmware that knows how to turn the heater on and off, how to measure the temperature from our temperature sensor.

There were a couple of small complications discovered whilst writing the code.
  1. I didn't read the datasheet for the DS18B20 temperature sensor quite well enough you need to use a pull up resistor to get the 1-wire communication working with the Photon. No issue that five minutes with a soldering iron can't fix.
  2. Our relay board likes to turn on but doesn't like to turn off. To deal with this one we can get away with setting the IO line to INPUT_PULLDOWN this soaks up any stray electrical potential and makes sure the relay optoisolator and thus the relay returns to the off state.
Want to have a look at how simple this was to implement or even spare yourself that effort and copy it for your project? Go right ahead and read this gist of heater code. Just remember that you'll have to use the library include system in Particle Build to add the libraries listed at the top of the source.

At this point we have a heater that can turn on and off at the whim of anything that can use a REST protocol. While using a web browser to turn a heater on and off is pretty cool that isn't going to cut the mustard in this connected world. What we need is apps! lots of apps. Well maybe just one. Next post I will tell you about the application below.