From e3f90596276890aff21611e98ffa66eda1c176e2 Mon Sep 17 00:00:00 2001 From: Kshitij Date: Mon, 7 Oct 2024 00:39:24 +0530 Subject: [PATCH] Added unchanged temp-calc.py --- temp-calc.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 temp-calc.py diff --git a/temp-calc.py b/temp-calc.py new file mode 100644 index 0000000..6843426 --- /dev/null +++ b/temp-calc.py @@ -0,0 +1,38 @@ +# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries +# SPDX-License-Identifier: MIT + +import time +import board +import adafruit_dht + +# Initial the dht device, with data pin connected to: +dhtDevice = adafruit_dht.DHT22(board.D18) + +# you can pass DHT22 use_pulseio=False if you wouldn't like to use pulseio. +# This may be necessary on a Linux single board computer like the Raspberry Pi, +# but it will not work in CircuitPython. +# dhtDevice = adafruit_dht.DHT22(board.D18, use_pulseio=False) + +while True: + try: + # Print the values to the serial port + temperature_c = dhtDevice.temperature + temperature_f = temperature_c * (9 / 5) + 32 + humidity = dhtDevice.humidity + print( + "Temp: {:.1f} F / {:.1f} C Humidity: {}% ".format( + temperature_f, temperature_c, humidity + ) + ) + + except RuntimeError as error: + # Errors happen fairly often, DHT's are hard to read, just keep going + print(error.args[0]) + time.sleep(2.0) + continue + except Exception as error: + dhtDevice.exit() + raise error + + time.sleep(2.0) +