print("\n\tProgram: Temp and humidity sensor")
print("\tCreated by: Lyomel")
print("\tDate: 14/11/2024")
print("\t=================================")
#Import libraries
from machine import Pin
from time import sleep
import dht
# 3pin declaration
sensor= dht.DHT22(Pin(27))
LED = Pin(4,Pin.OUT)
# Parameter declaration
#Main Program
while True:
try:
sleep(2) # Wait 2 seconds
sensor.measure() # Take measurements
temp = sensor.temperature() # Read temperature
hum = sensor.humidity() # Read humidity
print('Temperature: %3.1f C' % temp)
print('Humidity: %3.1f %%' % hum)
# Check temperature and humidity thresholds
if temp >= 30 or hum >= 55: # Use `or` for logical OR
LED.on() # Turn the LED on
else:
LED.off() # Turn the LED off
except OSError as e:
print('Failed to read sensor.')
Loading
esp32-devkit-v1
esp32-devkit-v1