import machine
from machine import Pin
import dht
import time
# Define the pin where the DHT22 is connected
dht_pin = machine.Pin(21)
dht_sensor = dht.DHT22(dht_pin)
temperature = 0
humitidity = 0
#Setup LED
led = Pin(5, mode = Pin.OUT)
fanRelay = Pin(16, mode = Pin.OUT )
print("Go ESP32")
while True:
try:
dht_sensor.measure()
temperature = dht_sensor.temperature()
humidity = dht_sensor.humidity()
print("Temperature: {}°C Humidity: {}%".format(temperature, humidity))
if(temperature > 30):
led.on()
fanRelay.on()
time.sleep(0.5)
else:
led.off()
fanRelay.off()
time.sleep(0.5)
except OSError as e:
print("Failed to read from DHT sensor!")