from machine import Pin, I2C
from time import sleep
import dht
import esp
import ssd1306
# Disable debug output
esp.osdebug(None)
# Inisialisasi sensor DHT22
dht_sensor = dht.DHT22(Pin(14))
# Inisialisasi I2C untuk LCD
i2c = I2C(scl=Pin(22), sda=Pin(21), freq=400000)
lcd_width = 128
lcd_height = 64
oled = ssd1306.SSD1306_I2C(lcd_width, lcd_height, i2c)
# Fungsi untuk membaca suhu dan kelembaban dari sensor DHT22
def read_dht_sensor():
try:
dht_sensor.measure()
temp = dht_sensor.temperature()
hum = dht_sensor.humidity()
return temp, hum
except Exception as e:
print("Error reading DHT22 sensor:", e)
return None, None
# Fungsi untuk menampilkan teks di OLED
def display_on_oled(temp, hum):
oled.fill(0)
oled.text("Suhu:{:.2f}".format(temp), 0, 0)
oled.text("Kelembaban:{:.2f}%".format(hum), 0, 20)
oled.show()
while True:
temp, hum = read_dht_sensor()
if temp is not None and hum is not None:
print("Suhu: {:.2f} C, Kelembaban: {:.2f} %".format(temp, hum))
display_on_oled(temp, hum)
else:
print("Failed to read from DHT22 sensor")
sleep(2)
Loading
esp32-devkit-c-v4
esp32-devkit-c-v4
Loading
ssd1306
ssd1306