from machine import Pin
from time import sleep_ms
import dht
d = dht.DHT11(Pin(22))
led1 = Pin(2, Pin.OUT)
led2 = Pin(4, Pin.OUT)
button = Pin(23, Pin.IN)
while True:
try:
d.measure()
temp = d.temperature() #( ํC)
humid = d.humidity() #(%RH)
print('Temp:',temp, 'Humid:',humid)
sleep_ms(1000)
if temp > 20 and humid > 40:
led2.on()
print('Normal.....')
else:
led2.off()
print('Unnormal.....')
except OSError as e:
print('Failed to read sensor!')
sw = button.value()
if sw == 1:
led1.on()
print('LED1 ON')
sleep_ms(500)
else:
led1.off()
print('LED1 OFF')
sleep_ms(500)