Skip to content

Instantly share code, notes, and snippets.

@mikegreen
Created July 6, 2020 18:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikegreen/6eac8903f372a02f2ab7a52a8b32de76 to your computer and use it in GitHub Desktop.
Save mikegreen/6eac8903f372a02f2ab7a52a8b32de76 to your computer and use it in GitHub Desktop.
Send dht22 temp and humidity to Stathat
import time
import adafruit_dht
import board
from datetime import datetime
from stathat import StatHat
stathat = StatHat()
stathatKey = "foo@foo.com"
# 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