from machine import Pin, SoftI2C, ADC
import dht
import time
from lcd_api import LcdApi
from i2c_lcd import I2cLcd
led1 = Pin(5, Pin.OUT)
sensor = dht.DHT22(Pin(14))
led2 = Pin(15, Pin.OUT)
lrd= ADC(Pin(33))
I2C_ADDR = 0x27
totalRows = 2
totalColumns = 16
i2c = SoftI2C(scl=Pin(22), sda=Pin(21), freq=10000) #initializing the I2C method for ESP32
lcd = I2cLcd(i2c, I2C_ADDR, totalRows, totalColumns)
lcd.putstr("Hello")
h = -1
while True:
sensor.measure() # read the parameters from the sensor
t = sensor.temperature()
if h != sensor.humidity():# if the humidity is changed the LCD will display the value
h = sensor.humidity()
lcd.clear()
lcd.putstr("The Humidity: ")
lcd.putstr(str(h))
'''
nếu nhiệt độ > 30 thì led 1 bật và ngược lại
'''
if t > 30:
led1.on()
else:
led1.off()
time.sleep(1)
lcd.clear()
# đk ánh sáng
while True:
y = lrd.read()
if y< 2600 :
print("Đèn đang tắt")
led2.value(0)
else:
print("Đèn đang bật")
led2.value(1)
time.sleep(0.3)