Just dropped some pseudo-code based on my initial risk threshold chart in Python. The idea here is that this can run on a PI Pico 1 or 2, serving as the direct interface for all the sensors. It’s built to evaluate environmental risks, with preset thresholds that determine when to warn the user of danger.
These warnings are recommendations, you can always choose to ignore them. However, if the AI spins up, a piezoelectric beeper will activate, and that means you should probably pay attention. At that point, the system has detected a high-risk event, like elevated CO₂ or radiation—stuff you don’t want to second-guess.
Right now, this is still rudimentary and missing some functions I’ll need to fill in. But you have to start somewhere.
I’ve also added the chart to the project's file base. Threshold values can be adjusted depending on how risk-averse you are.
from machine import I2C, Pin, UART
from ssd1306 import SSD1306_I2C
from time import sleep, ticks_ms
# --- Init Display & UART ---
i2c = I2C(0, scl=Pin(1), sda=Pin(0))
oled = SSD1306_I2C(128, 64, i2c)
uart = UART(0, baudrate=115200, tx=Pin(4), rx=Pin(5))
non_critical_interval = 60_000 # 1 minute
last_non_critical_check = 0
def warn_user(sensor, message):
oled.fill(0)
oled.text("⚠ {}".format(sensor), 0, 0)
oled.text(message, 0, 10)
oled.show()
def wake_ai(snapshot):
uart.write("SNAPSHOT:" + snapshot + "\n")
def handle_risk(level, sensor, med_msg, high_msg, snapshot):
if level == "LOW":
return
elif level == "MEDIUM":
warn_user(sensor, med_msg)
elif level == "HIGH":
warn_user(sensor, high_msg)
wake_ai(snapshot)
def eval_risk(value, thresholds):
low, med = thresholds
if value < low: return "LOW"
elif value <= med: return "MEDIUM"
return "HIGH"
# --- AI Message Handler ---
def process_ai():
if uart.any():
msg = uart.readline().decode().strip()
oled.fill(0)
if msg.startswith("CRITICAL:"):
oled.text("AI:", 0, 0)
oled.text(msg[9:], 0, 10)
elif msg.startswith("INFO:"):
oled.text("Info:", 0, 0)
oled.text(msg[5:], 0, 10)
oled.show()
# --- Sensor Read Functions (stubbed) ---
def read_all_critical():
return {
"CO2": 1600.0,
"VOC": 1.2,
"Pressure": 985.0,
"Radiation": 0.7
}
def read_all_non_critical():
return {
"Temp": 34.0,
"Humidity": 72.0,
"Light": 3100,
"Sound": 89,
"Mag": 350
}
# --- Loop ---
while True:
now = ticks_ms()
critical = read_all_critical()
snapshot = ",".join(f"{k}={v}" for k, v in critical.items())
handle_risk(eval_risk(critical["CO2"], (1000, 2000)), "CO2", "Ventilate", "High CO₂!", snapshot)
handle_risk(eval_risk(critical["VOC"], (0.5, 2.0)), "VOC", "Irritants", "Toxic VOCs", snapshot)
handle_risk(eval_risk(critical["Pressure"], (980, 1000)), "Pressure", "Storm risk", "Severe drop", snapshot)
handle_risk(eval_risk(critical["Radiation"], (0.5, 5.0)), "Radiation", "Elevated", "Evacuate!", snapshot)
if now - last_non_critical_check > non_critical_interval:
noncrit = read_all_non_critical()
snapshot_nc = ",".join(f"{k}={v}" for k, v in noncrit.items())
handle_risk(eval_risk(noncrit["Temp"], (15, 30)), "Temp", "Discomfort", "Heat/Hypo!", snapshot_nc)
handle_risk(eval_risk(noncrit["Humidity"], (30, 60)), "Humidity", "Uncomfortable", "Resp. risk", snapshot_nc)
handle_risk(eval_risk(noncrit["Light"], (500, 2000)), "Light", "Glare", "Overexposed", snapshot_nc)
handle_risk(eval_risk(noncrit["Sound"], (70, 85)), "Sound", "Noisy", "Hearing danger", snapshot_nc)
handle_risk(eval_risk(noncrit["Mag"], (100, 300)), "Mag Field", "Elevated", "Interference", snapshot_nc)
last_non_critical_check = now
process_ai()
sleep(5) # Sleep time between wake cycles
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.