From faecbb793919c195f3fcf3d5e47a63d41da39a49 Mon Sep 17 00:00:00 2001 From: jerick Date: Mon, 28 Nov 2022 11:18:19 -0500 Subject: [PATCH] Temp sensor functionality, changed README --- mqtt/tempSensor.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 mqtt/tempSensor.py diff --git a/mqtt/tempSensor.py b/mqtt/tempSensor.py new file mode 100644 index 0000000..0e7e30f --- /dev/null +++ b/mqtt/tempSensor.py @@ -0,0 +1,24 @@ +import time +import board +import adafruit_dht +import psutil +# We first check if a libgpiod process is running. If yes, we kill it! +for proc in psutil.process_iter(): + if proc.name() == 'libgpiod_pulsein' or proc.name() == 'libgpiod_pulsei': + proc.kill() +#D20 is the pin number, DHT11 is the sensor type +sensor = adafruit_dht.DHT11(board.D20) +while True: + try: + temp = sensor.temperature + humidity = sensor.humidity + print("Temperature: {}*C Humidity: {}% ".format(temp, humidity)) + except RuntimeError as error: + print(error.args[0]) + time.sleep(2.0) + continue + except Exception as error: + sensor.exit() + raise error + #time between temp and humidity checks + time.sleep(2.0) \ No newline at end of file