Created
July 6, 2020 18:27
-
-
Save mikegreen/6eac8903f372a02f2ab7a52a8b32de76 to your computer and use it in GitHub Desktop.
Send dht22 temp and humidity to Stathat
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import adafruit_dht | |
import board | |
from datetime import datetime | |
from stathat import StatHat | |
stathat = StatHat() | |
stathatKey = "[email protected]" | |
# Initial the dht device, with data pin connected to: | |
dhtDevice = adafruit_dht.DHT22(board.D17) | |
runTimes = 1 | |
while runTimes > 0: | |
try: | |
# Print the values to the serial port | |
temperature_c = dhtDevice.temperature | |
temperature_f = temperature_c * (9 / 5) + 32 | |
humidity = dhtDevice.humidity | |
print(str(datetime.now()) + " Temp: {:.1f} F / {:.1f} C Humidity: {}% " | |
.format(temperature_f, temperature_c, humidity)) | |
# print("Temp: {:.1f} F / {:.1f} C Humidity: {}% " | |
# .format(temperature_f, temperature_c, humidity)) | |
print("Sending temp to stathat: " + str(temperature_f)) | |
stathat.ez_post_value(stathatKey, 'rv.temp', temperature_f) | |
print("Sending humidity to stathat: " + str(humidity)) | |
stathat.ez_post_value(stathatKey, 'rv.humidity', humidity) | |
runTimes = (runTimes - 1) | |
except RuntimeError as error: | |
# Errors happen fairly often, DHT's are hard to read, just keep going | |
print(error.args[0]) | |
print("Error") | |
time.sleep(3.0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment