import machine
import time
from machine import Pin, I2C
import dht
from esp8266_i2c_lcd import I2cLcd
i2c = I2C(scl=Pin(22), sda=Pin(21), freq=100000)
lcd = I2cLcd(i2c, 0x27, 2, 16)
dht_sensor = dht.DHT22(Pin(4))
led_north = Pin(14, Pin.OUT)
led_south = Pin(12, Pin.OUT)
led_east = Pin(27, Pin.OUT)
led_west = Pin(26, Pin.OUT)
def update_lcd(temp, hum):
lcd.clear()
lcd.putstr("Temp: {:.1f}C\n".format(temp))
lcd.putstr("Hum: {:.1f}%".format(hum))
def set_led_direction(temp):
# Turn off all LEDs initially
led_north.off()
led_south.off()
led_east.off()
led_west.off()
if temp < 20:
led_north.on()
elif 20 <= temp < 25:
led_east.on()
elif 25 <= temp < 30:
led_west.on()
else:
led_south.on()
while True:
try:
dht_sensor.measure()
temp = dht_sensor.temperature()
hum = dht_sensor.humidity()
update_lcd(temp, hum)
set_led_direction(temp)
except Exception as e:
lcd.clear()
lcd.putstr("Error:\n{}".format(e))
time.sleep(2)