Skip to content

Instantly share code, notes, and snippets.

@CamDavidsonPilon
Last active June 30, 2020 17:47
Show Gist options
  • Save CamDavidsonPilon/42eed294317614b64426d65bab4ca519 to your computer and use it in GitHub Desktop.
Save CamDavidsonPilon/42eed294317614b64426d65bab4ca519 to your computer and use it in GitHub Desktop.
import time
import board
import adafruit_dht
from paho.mqtt import publish
# Initial the dht device, with data pin connected to:
dhtDevice = adafruit_dht.DHT22(board.D21)
while True:
try:
# Print the values to the serial port
temperature_c = dhtDevice.temperature
humidity = dhtDevice.humidity
print(
"Temp:{:.1f} C Humidity: {}% ".format(
temperature_c, humidity
)
)
publish.single("dht22/humidity", humidity)
publish.single("dht22/temperature", temperature_c)
except (RuntimeError, OverflowError) as error:
# Errors happen fairly often, DHT's are hard to read, just keep going
print(error.args[0])
time.sleep(2.0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment