import machine
from machine import I2C, Pin
import time
import dht
from i2c_lcd1602 import I2cLcd
RED_PIN = 14
GREEN_PIN = 27
BLUE_PIN = 26
HUMIDITY_LED_PIN = 17
DHT_PIN = 12
SDA_PIN = 21
SCL_PIN = 22
red_led = machine.Pin(RED_PIN, machine.Pin.OUT)
green_led = machine.Pin(GREEN_PIN, machine.Pin.OUT)
blue_led = machine.Pin(BLUE_PIN, machine.Pin.OUT)
humidity_led = machine.Pin(HUMIDITY_LED_PIN, machine.Pin.OUT)
dht_sensor = dht.DHT22(machine.Pin(DHT_PIN))
AddressOfLcd = 0x27
i2c = I2C(scl=Pin(SCL_PIN), sda=Pin(SDA_PIN), freq=400000)
# connect scl to GPIO 22, sda to GPIO 21
lcd = I2cLcd(i2c, AddressOfLcd, 2, 16)
while True:
try:
dht_sensor.measure()
temperature = dht_sensor.temperature()
humidity = dht_sensor.humidity()
print("Nhiệt độ:", temperature)
print("Độ ẩm:", humidity)
red_led.value(0)
green_led.value(0)
blue_led.value(0)
humidity_led.value(0)
if temperature > 40:
red_led.value(1)
if humidity > 50:
humidity_led.value(1)
# Hiển thị trên LCD
lcd.clear()
lcd.move_to(0, 0)
lcd.putstr("Temp: {} C".format(temperature))
lcd.move_to(0, 1)
lcd.putstr("Humidity: {}%".format(humidity))
except OSError as e:
print("Không thể đọc cảm biến:", e)
time.sleep(2)