Modified from the source at https://learn.adafruit.com/dht-humidity-sensing-on-raspberry-pi-with-gdocs-logging/python-setup
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