This project is a thermostat control system that allows you to monitor and control temperature settings through a web interface or native app. It consists of two main components: a Flask-based web application for control and data display and a Raspberry Pi-based temperature sensor system for data collection. You can find more information on the app here.
- A VM or physical machine running some sort of SQL database, python, and optionally a web server of some sort.
- A Raspberry Pi with a DHT22 temperature and humidity sensor, a 30V 4 channel relay, and mine has a 7 inch touch screen. (you can do without)
- Basic knowledge of your specific HVAC system and the willingness to modify this code to work with anything other than a 6 wire system that has an orange wire for the energized reversal valve for COOLING. (NOTE) You can probably just wire up the pins that will respond as you would need them to for your specific system. EG: With a 6 wire system, the same pin for heat may be used to energize the heat for a 4 wire furnace while ignoring the heat strips pinout.
On your VM/Physical machine you will want to install apache/nginx, mysql, and python as well as prepare the database and the wsgi mod which allows your python and apache to work in harmony.
sudo apt update
sudo apt install apache2 ibapache2-mod-wsgi python3 libexpat1 python3-pip
sudo a2enconf mod-wsgi | sudo a2enmod cgi
to allow the wsgi mod to run in apachesudo systemctl restart apache2
to restart apache and complete that side of the configuration.- You will copy the contents of the Thermostat Server and modify the config.json to match your environment. Then either run the server.py or add it as a service by running these commands. In lieu of the legacy HTML included in this, you can use the build of the web app that is included in the Thermostat-App here. This will unify the app and web app making it one smooth experience.
sudo nano /etc/systemd/system/thermostatserver.service
| adding this into the service file.
[Unit]
Description=Thermostat Service
After=network.target
[Service]
ExecStart=/usr/bin/python3 "/LOCATION/OF/YOUR/.PY/HERE
WorkingDirectory=/YOUR/WORKING/DIRECTORY/HERE
Restart=always
User=YOURUSERHERE
[Install]
WantedBy=multi-user.target
- You will want to pip install these packages |
pip install Flask, render_template, request, jsonify, mysql.connector, pytz, datetime, flask_cors, json
- Add the index.html file in the root of the folder to your /var/www/html directory. This is the UI.
- Setup your SQL and get it ready to prepare a database.
- Run
CREATE DATABASE thermostat_data; | USE thermostat_data;
-- Table to store temperature and humidity data
CREATE TABLE sensor_data (
id INT AUTO_INCREMENT PRIMARY KEY,
device_id VARCHAR(255),
temperature DECIMAL(5, 2),
humidity DECIMAL(5, 2),
timestamp TIMESTAMP,
ip_address VARCHAR(15)
);
-- Table to store user-set temperature values
CREATE TABLE user_settings (
id INT AUTO_INCREMENT PRIMARY KEY,
device_id VARCHAR(255),
target_temperature DECIMAL(5, 2),
timestamp TIMESTAMP
);
-- Table to store outside temperature
CREATE TABLE outside_temperature (
id INT AUTO_INCREMENT PRIMARY KEY,
temperature DECIMAL(5, 2),
timestamp TIMESTAMP
);
sudo systemctl daemon-reload | sudo systemctl start thermostatserver.service
This should conclude the setup for the server side of this. This will allow you to store the temp/humididy/deviceid as well as the user settings when the user selects a temp to change the thermostat to. This will log the IP address the request came from as well as time stamps all around for every interaction.
Assuming the same setup as I have with a Raspberry Pi 4 running raspbian, with a DHT22, and a 30V 4 channel relay.
sudo apt update
sudo apt install python3 libexpat1 python3-pip
pip install Flask, render_template, request, jsonify, mysql.connector, pytz, datetime, flask_cors, json
- Copy the contents of Thermostat Pi into a working directory you wanna run it from and modify the config.json to match your environment.
- You will copy the contents of the Thermostat Pi and then either run the thermostat.py or add it as a service by running these commands.
sudo nano /etc/systemd/system/thermostat.service
| adding this into the service file.
[Unit]
Description=Thermostat Service
After=network.target
[Service]
ExecStart=/usr/bin/python3 "/LOCATION/OF/YOUR/.PY/HERE
WorkingDirectory=/YOUR/WORKING/DIRECTORY/HERE
Restart=always
User=YOURUSERHERE
[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload | sudo systemctl start thermostat.service | sudo systemctl start thermostat.service
This should start the thermostat which will read the dht22 and report info to the webUI and the database on timers set within the python.
Here are some of the screenshots I have from the app showing various states and settings.
This is one of the thermostat in the between_states mode. After having run a previous mode. Polling to see if it needs another state change.
This is the Previous States screen. This shows the previous 20 states.
And here is the Historical Temperatures. This will show the previous 200 reported temperatures.