import time
from machine import I2C, Pin
from dht import DHT22
from ssd1306 import SSD1306_I2C
# 1. Initialize I2C bus for SSD1306 OLED
i2c = I2C(1, scl=Pin(27), sda=Pin(26), freq=400000)
#SCREEN_WIDTH = 128, SCREEN_HEIGHT = 64
oled = SSD1306_I2C(128, 64, i2c, addr=0x3c)
dht_sensor = DHT22(Pin(28))
# Startup message
oled.fill(0)
oled.rect(0, 0, 128, 64, 1)
oled.text("AeroSense OLED", 8, 15)
oled.text("System Ready...", 8, 38)
oled.show()
print("--- AeroSense OLED System Initialized ---")
time.sleep(2)
# 3. Main loop
while True:
try:
dht_sensor.measure()
temp = dht_sensor.temperature()
hum = dht_sensor.humidity()
# Update OLED Display
oled.fill(0)
oled.rect(0, 0, 128, 64, 1)
oled.text("AeroSense Node", 8, 6)
oled.line(0, 18, 128, 18, 1)
oled.text(f"Temp: {temp:.1f} C", 10, 28)
oled.text(f"Hum: {hum:.1f} %", 10, 44)
oled.show()
print(f"Temperature: {temp:.1f}°C | Humidity: {hum:.1f}%")
except OSError:
oled.fill(0)
oled.rect(0, 0, 128, 64, 1)
oled.text("Sensor Error!", 12, 28)
oled.show()
print("[ERROR] Failed to read from DHT22 sensor!")
time.sleep(2)