print("Hello, ESP32!")
from machine import Pin, I2C
from time import sleep
import ssd1306
import dht
# Cấu hình chân
led = Pin(15, Pin.OUT)
dht_sensor = dht.DHT22(Pin(4)) # Thay Pin(4) bằng chân GPIO thực tế
# LED nhấp nháy
for _ in range(5):
led.value(1)
sleep(0.5)
led.value(0)
sleep(0.5)
# Cấu hình OLED
oled_width = 128
oled_height = 64
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
# Hiển thị ban đầu
oled.fill(0)
oled.text("Hello, ESP32!", 5, 10)
oled.text("Hello World!", 5, 20)
oled.show()
sleep(2)
# Vòng lặp chính
while True:
try:
dht_sensor.measure()
Nhietdo = dht_sensor.temperature()
Doam = dht_sensor.humidity()
print("Nhiet do: {}°C, Do am: {}%".format(Nhietdo, Doam))
# Hiển thị lên OLED với nhãn tiếng Việt không dấu
oled.fill(0)
oled.text("Nhietdo: {}C".format(Nhietdo), 0, 10)
oled.text("Doam: {}%".format(Doam), 0, 30)
oled.show()
# Điều khiển LED
led.value(1 if Nhietdo > 30 else 0)
sleep(2)
except OSError as e:
print("Loi doc DHT22:", e)
oled.fill(0)
oled.text("Sensor Error!", 10, 20)
oled.show()
sleep(2)