import machine # Importing machine to access the GP (General Purpose) pins
import time # Importing time to access the sleep function to add delay (waiting) time
import dht # Importing DHT to access and control the DHT22 sensors
sensor = dht.DHT22(machine.Pin(28)) # Define the pin connected to the DHT22 data line
while True:
# Try-Except block
try:
sensor.measure() # Measure and store the sensors' reading
print(f"Temperature: {sensor.temperature()}°C") # Print the temperature
print(f"Humidity: {sensor.humidity()}%") # Print the humidity
print("---------------------")
except OSError as e: # if the Try block fails
print("[ERROR]Failed to read sensor.", e) # Print the error
time.sleep(1) # delay between readings (1 Second)