# Import necessary libraries
from machine import Pin, SoftI2C
import dht
import time
from lcd_api import LcdApi
from i2c_lcd import I2cLcd
# Define a GPIO pin for an LED
led = Pin(5, Pin.OUT)
# Create a DHT22 sensor object
sensor = dht.DHT22(Pin(14))
# Set the I2C address, total rows, and total columns for an LCD display
I2C_ADDR = 0x27
totalRows = 2
totalColumns = 16
# Create an I2C interface
i2c = SoftI2C(scl=Pin(22), sda=Pin(21), freq=10000)
# Initialize an LCD display
lcd = I2cLcd(i2c, I2C_ADDR, totalRows, totalColumns)
# Display a "Hello" message on the LCD
lcd.putstr("Lab 5 Bai 1")
time.sleep(1)
# Initialize a variable for humidity
h = -1
# Start an infinite loop
while True:
# Measure the sensor data
sensor.measure()
t = sensor.temperature()
# Check if humidity has changed
if h != sensor.humidity():
h = sensor.humidity()
lcd.clear()
lcd.putstr("The Humidity: \n")
lcd.putstr(str(h))
# Check if temperature is above 40 degrees and control the LED accordingly
if t > 40:
led.on()
else:
led.off()
# Wait for 1 second
time.sleep(1)
# Clear the LCD display
lcd.clear()