PiThermostat
Posted on Sun 07 April 2019 in Home Automation,
I recently wrote a post about Raspberry Pi temperature and humidity sensors and alluded to the fact I use scripts to turn appliances on and off dependent on temperature.
Be kinda rude not to share…
I have a cron that runs every 3 minutes that probes a BME280 sensor for data and against a threshold switches a plug on or off via a 433mhz transmitter.
*/3 * * * * sudo python /home/pi/Adafruit_Python_BME280/HTC.py
If you don’t have a working 433mhz setup yet, check out this post :433Mhz
The below script is pretty easy to follow, it imports and sets up the Adafruit BME280 sensor, probes it then sends a code using 433Utils to a plug socket, switching it on or off. I'm also using the same script on another Pi, using SSH (badly) to trigger the same codes remotely. (Only one Pi has a 433mhz transmitter).
You can see from the below I've set the humidity threshold to 40%, if it drops below, it will send a 433 code to a plug socket attached to a humidifier. 3 minutes later it will check the reading again, if it's over the threshold, it will send the off command. Likewise for the temperature however it's plug socket has a large fan attached.
from Adafruit_BME280 import *
import os
sensor = BME280(t_mode=BME280_OSAMPLE_8, p_mode=BME280_OSAMPLE_8, h_mode=BME280_OSAMPLE_8)
temperature = sensor.read_temperature()
pascals = sensor.read_pressure()
hectopascals = pascals / 100
humidity = sensor.read_humidity()
if humidity is None or temperature is None:
print('Temp={0:0.1f}*C Humidity={1:0.1f}%'.format(temperature, humidity))
else:
humthresh = 40.0
if humidity < humthresh:
#ON
os.system ("/433Utils/RPi_utils/codesend 4543573")
else:
#OFF
os.system ("/433Utils/RPi_utils/codesend 4543572")
tempthresh = 25.0
if temperature > tempthresh:
#ON
os.system ("/433Utils/RPi_utils/codesend 4539733")
else:
#OFF
os.system ("/433Utils/RPi_utils/codesend 4539732")
This is the remote SSH version, you (I) really should be using key files.
humthresh = 65.0
if humidity < humthresh:
#ON
os.system ("sshpass -p PASSWORD ssh -o StrictHostKeyChecking=no pi@IPADDRESS 'cd /var/www/public_html && python iii.2.on.py' ")
else:
#OFF
os.system ("sshpass -p PASSWORD ssh -o StrictHostKeyChecking=no pi@IPADDRESS 'cd /var/www/public_html && python iii.2.off.py' ")
This is example used to power a vivarium heat mat.
![PiThermostat] (images/vivpi.png "PiThermostat")
It consists of:
• Raspberry Pi A
• Wifi USB
• 433 Tx / Rx Pair
• DHT22 sensor
• 1x 433Mhz plug (and remote for programming)
• 1x Vivarium heat mat
• Jumper Cables
• Power
Temperature drops, heat mat on, heat ok, heat mat off. Simple. Use my 433 guide to get one working. 433Mhz