Skip to content

Instantly share code, notes, and snippets.

Last active Jun 30, 2020
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:
# Print the values to the serial port
temperature_c = dhtDevice.temperature
humidity = dhtDevice.humidity
"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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment