Skip to content

Instantly share code, notes, and snippets.

@CamDavidsonPilon

CamDavidsonPilon/DHT22.py

Last active Jun 30, 2020
Embed
What would you like to do?
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
You can’t perform that action at this time.