# ตัวอย่าง การเขียนโปรแกรม Micropython ควบคุมการทำงาน ESP32
# LAB08 DHT22 LCD I2C B
# ครูวิบูลย์ กัมปนาวราวรรณ อาทิตย์ 14 กรกฏาคม 2567
from machine import Pin, SoftI2C
import dht
import time
from lcd_api import LcdApi
from i2c_lcd import I2cLcd
i2c = SoftI2C(scl=Pin(22), sda=Pin(21), freq=100000)
lcd = I2cLcd(i2c, 0x27, 2, 16)
sensor = dht.DHT22(Pin(23))
led = Pin(13, Pin.OUT)
def scroll_text(text, line, lcd):
# ฟังก์ชันเลื่อนข้อความจากขวาไปซ้ายบนจอ LCD
if len(text) <= 16:
lcd.move_to(0, line)
lcd.putstr(text)
return
for i in range(len(text) - 15):
lcd.move_to(0, line)
lcd.putstr(text[i:i+16])
time.sleep(0.3)
scroll_text('LAB08 DHT22 LCD I2C', 0, lcd)
scroll_text('77Viboon Kumpanavarawan', 1, lcd)
time.sleep(1)
while True:
lcd.clear()
sensor.measure()
temp = sensor.temperature()
hum = sensor.humidity()
lcd.move_to(0, 0)
lcd.putstr('Temp: {:.1f}C'.format(temp))
lcd.move_to(0, 1)
lcd.putstr('Hum: {:.1f}%'.format(hum))
if temp > 40:
led.value(1)
else:
led.value(0)
time.sleep(2)