import machine
import time
import ssd1306
import dht
# ---------------- I2C OLED Setup ----------------
# GP4 = SDA, GP5 = SCL
i2c = machine.I2C(0, sda=machine.Pin(4), scl=machine.Pin(5), freq=400000)
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
# ---------------- DHT22 Setup -------------------
# DHT22 DATA connected to GP15
sensor = dht.DHT22(machine.Pin(15))
print("I2C OLED + DHT22 Started")
# ---------------- Main Loop ---------------------
while True:
try:
sensor.measure()
temperature = sensor.temperature()
humidity = sensor.humidity()
oled.fill(0)
oled.text("IOT MONITOR", 0, 0)
oled.hline(0, 10, 128, 1)
oled.text("Status: ONLINE", 0, 20)
oled.text(f"Temp: {temperature:.1f} C", 0, 35)
oled.text(f"Hum: {humidity:.1f} %", 0, 55)
oled.show()
except OSError:
oled.fill(0)
oled.text("Sensor Error!", 20, 30)
oled.show()
time.sleep(2)