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(1, baudrate=9600, tx=Pin(17), rx=Pin(16))
# -------------------------------
# Display Function (Values + Emoji)
# -------------------------------
def show_reading(temp, hum):
oled.fill(0)
# Display sensor values
oled.text("T:{:.1f}C".format(temp), 0, 0)
oled.text("H:{:.1f}%".format(hum), 0, 10)
# Display emoji based on conditions
if temp > 35:
oled.text("(>_<) Hot", 20, 40) # Hot
elif hum > 70:
oled.text("(o_o) Wet", 20, 40) # Humid
else:
oled.text("(^_^) Happy", 20, 40) # Normal
oled.show()
# -------------------------------
# Startup Screen
# -------------------------------
def show_waiting():
oled.fill(0)
oled.text("Waiting data...", 9, 15)
oled.text("(^_^)", 40, 40)
oled.show()
show_waiting()
# -------------------------------
# Main Loop (UART Reading)
# -------------------------------
while True:
if uart.any():
line = uart.readline() # Read until newline
if not line:
continue
try:
text = line.decode().strip()
print("Received:", text)
# Expected format: temp,hum
t_str, h_str, l_str = text.split(",")
temp = float(t_str)
hum = float(h_str)
show_reading(temp, hum)
except Exception as e:
print("Parse error:", e, "Data:", line)
time.sleep_ms(20)