This weekend I decided to take the leap and add thermostat functionality to my setup.
- Connect a Pi to furnace
- Write the software to control the furnace
Connecting to the Furnace
This was actually relatively straightforward. My thermostat is connected to the furnace via two wires. When the thermostat completes the circuit, the furnace turns on. When it breaks the circuit, the furnace turns off. There’s a little nuance here, as I imagine the details can vary from furnace to furnace. For example, my furnace has a variable blower, but I don’t have enough wires connected to my thermostat to dynamically control it, I can only turn it on and off. I’ll address this limitation in the future.
In order to break the circuit from my Pi, I connected a relay ($5.80 on Amazon) to a GPIO pin.
Then, I set up a simple test on my breadboard with an LED:
When the relay is off, the circuit is broken, and the LED is off.
Then, I tested the relay by SSHing into the Pi, and running this code interactively:
After I had it working, I had to write some code to flip the relay based on the temperature.
Expanding the Database
I decided to expand my database a bit to keep track of the various actions that my system will eventually control. Now, it looks like this:
Writing the Thermostat code
I started off fairly simple, querying the database for the temperature of my sensors, and then checking if a single sensor was above or below a certain target.
I defined a Thermostat
class to check the temperature of all the indoor sensors. This could have just been a function, but at the time I was
planning on expanding on this class.
I also defined an HVAC
class that manages the GPIO communication:
Finally, I wrote this loop to trigger the relay based on the temperature:
Then, I ran the script via python -m thermo.control.thermostat
:
Success!
I spent a good amount of time iterating on the code after this. You can see the current version on Github
Pi on the Wall!
Once I felt that the code was good enough, I put the relay on my second Pi (which I had been using to collect temperature data from my bedroom), and connected it to the furnace circuit on my wall.
As you might expect, my girlfriend isn’t too thrilled about this, so I’ll be working on a more aesthetically pleasing version soon. For now, I’ll just be putting this up to test my control algorithm.
-Dan