Controlling the Thermostat, and Better Reliability
16 FEB 2017
•
7 mins read
This past week I spent some time improving the reliability of the thermostat, and also working on a way to set the temperature
without having to SSH into the Pi.
An Interface for the Thermostat
I decided to embed a small Flask application inside the thermo package in order to create a simple web-based UI. This would
allow me to control the thermostat from a screen attached to the Pi itself, and also through any other device on my local network.
I could theoretically port-forward the application so that I could access it from outside my local network, but I will need to
add some security before doing something like that.
The first step was to sketch out a plan for the UI. I came up with this simple interface after a few iterations. This is just the ‘main menu,’ I haven’t
designed the other pages yet.
My Flask application is laid out so that it generally calls a function from an API file. The functions in the API file reach out to other
modules within thermo or go straight to the database to insert or update a record.
Here is an example of a function that sets a constant temperature target for the thermostat by inserting a row in the message table.
An Aside on Reliability
@duplicate_locally is a new decorator in thermo.common.models that duplicates the call to a local SQLite database. This is part of my push to improve
reliability. I also created a decorator for read operations. The distinction is that I always write to the local database, but only read from the local database
If thermo cannot reach the normal server. Additionally, I now specify a ‘fallback’ sensor in the local_settings.py file. This is a sensor
that is connected directly to this Raspberry Pi, and can be used to run the thermostat if it can’t connect to the database.
Back to the Interface
Once I had a handful of API functions to call, I was able to create a simple Flask app to get me started:
This app allows me to see the current temperature and target, and also allows me to override the default schedule for a specified number of hours!