from machine import Pin, I2C, UART
import ssd1306
import time
# -------------------------------
# OLED Setup (I2C)
# -------------------------------
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
devices = i2c.scan()
if not devices:
raise RuntimeError("No I2C devices found")
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
# -------------------------------
# UART Setup
# -------------------------------
uart = UART(2, baudrate=9600, tx=Pin(17), rx=Pin(16))
def show_reading(temp, hum, lux):
oled.fill(0)
oled.text("T:{:.1f}C".format(temp), 0, 0)
oled.text("H:{:.1f}%".format(hum), 0, 10)
oled.text("L:{}".format(lux), 0, 20)
if temp > 30:
oled.text("(>_<) Hot", 20, 40)
elif hum > 70:
oled.text("(O_O) Wet", 20, 40)
elif lux > 3000:
oled.text("(-_-) Zzz", 20, 40)
else:
oled.text("(^_^) Happy", 20, 40)
oled.show()
while True:
if uart.any():
line = uart.readline()
if not line:
continue
try:
text = line.decode("utf-8").strip()
print("Recieved: ", text)
print(text)
if len(text.split(",")) == 3:
t_str, h_str, l_str = text.split(",")
temp = float(t_str)
hum = float(h_str)
lux = int(l_str)
show_reading(temp, hum, lux)
except Exception as e:
print(e)
time.sleep_ms(2000) # Give it more time