from machine import Pin, ADC, I2C, PWM
import dht
import time
import ssd1306
# ================= BUTTON =================
btn_new = Pin(12, Pin.IN, Pin.PULL_UP)
btn_expiry = Pin(13, Pin.IN, Pin.PULL_UP)
btn_power = Pin(14, Pin.IN, Pin.PULL_UP)
# ================= SENSOR =================
pot = ADC(Pin(34))
pot.atten(ADC.ATTN_11DB)
ldr = ADC(Pin(35))
ldr.atten(ADC.ATTN_11DB)
dht_sensor = dht.DHT22(Pin(4))
# ================= OUTPUT =================
red = Pin(23, Pin.OUT)
yellow = Pin(18, Pin.OUT)
green = Pin(19, Pin.OUT)
neon = Pin(2, Pin.OUT)
orange = Pin(27, Pin.OUT)
buzzer = PWM(Pin(5))
buzzer.duty(0)
oled = ssd1306.SSD1306_I2C(
128, 64, I2C(0, scl=Pin(22), sda=Pin(21))
)
# ================= SYSTEM =================
food_status = ""
food_status_val = 0
show_status = False
status_timer = 0
door_open = False
door_timer = 0
system_on = False
booting = False
boot_timer = 0
# ================= THINGSPEAK =================
# ================= BUZZER =================
def beep():
buzzer.freq(2000)
buzzer.duty(500)
time.sleep(0.1)
buzzer.duty(0)
def buzzer_expiry():
buzzer.freq(1500)
buzzer.duty(700)
time.sleep(0.4)
buzzer.duty(0)
def buzzer_alarm():
buzzer.freq(2000)
buzzer.duty(700)
def buzzer_off():
buzzer.duty(0)
# ================= READ =================
def read_quantity():
return int(pot.read() * 100 / 4095)
def read_light():
return 100 - int(ldr.read() * 100 / 4095)
# ================= MAIN LOOP =================
while True:
# ----- POWER BUTTON -----
if btn_power.value() == 0:
system_on = not system_on
beep()
if system_on:
booting = True
boot_timer = time.time()
time.sleep(0.1)
if not system_on:
oled.fill(0)
oled.text("SYSTEM OFF", 23, 28)
oled.show()
buzzer_off()
red.off(); green.off(); yellow.off(); neon.off(); orange.off()
continue
# ----- BOOT SCREEN -----
if booting:
oled.fill(0)
oled.text("SYSTEM ON", 23, 28)
oled.show()
if time.time() - boot_timer > 0.5:
booting = False
continue
# ----- SENSOR READ -----
try:
dht_sensor.measure()
temp = dht_sensor.temperature()
hum = dht_sensor.humidity()
except:
continue
qty = read_quantity()
light = read_light()
# ----- BUTTONS -----
if btn_new.value() == 0:
beep()
food_status = "NEW STOCK"
food_status_val = 1
show_status = True
status_timer = time.time()
if btn_expiry.value() == 0:
buzzer_expiry()
food_status = "NEAR EXPIRY"
food_status_val = 2
show_status = True
status_timer = time.time()
if show_status and time.time() - status_timer > 1:
show_status = False
# ----- FAULT CHECK -----
temp_fault = temp > 35
hum_low = hum < 40
hum_high = hum > 75
stock_fault = qty < 20
if light > 30:
if not door_open:
door_open = True
door_timer = time.time()
else:
door_open = False
door_fault = door_open and (time.time() - door_timer > 1)
any_fault = temp_fault or hum_low or hum_high or stock_fault or door_fault
# ----- OUTPUT -----
yellow.off(); neon.off(); orange.off()
red.off(); green.off()
buzzer_off()
if any_fault:
buzzer_alarm()
if door_fault:
yellow.on()
if stock_fault:
orange.on()
if temp_fault or hum_low or hum_high:
neon.on()
if show_status:
if food_status == "NEW STOCK":
green.on()
elif food_status == "NEAR EXPIRY":
red.on()
# ----- OLED -----
oled.fill(0)
if any_fault:
oled.text("STORAGE ALERT", 12, 5)
oled.text("--------------", 8, 17)
# ===== TEMP & HUM SELANG-SELI =====
if temp_fault and (hum_low or hum_high):
if int(time.time()) % 4 < 2:
oled.text("TEMP HIGH", 28, 40)
oled.text(str(temp) + "C", 45, 56)
else:
if hum_low:
oled.text("HUM TOO LOW", 18, 40)
else:
oled.text("HUM TOO HIGH", 16, 40)
oled.text(str(hum) + "%", 45, 56)
elif temp_fault:
oled.text("TEMP HIGH", 28, 40)
oled.text(str(temp) + "C", 45, 56)
elif hum_low:
oled.text("HUM TOO LOW", 18, 40)
oled.text(str(hum) + "%", 45, 56)
elif hum_high:
oled.text("HUM TOO HIGH", 16, 40)
oled.text(str(hum) + "%", 45, 56)
elif stock_fault:
oled.text("LOW STOCK", 28, 40)
oled.text(str(qty) + "%", 55, 56)
elif door_fault:
oled.text("DOOR OPEN", 28, 40)
oled.text(str(light) + "%", 50, 56)
elif show_status:
oled.text("FOOD STATUS", 18, 7)
oled.text("--------------", 8, 17)
oled.text(food_status, 20, 45)
else:
oled.text("SYSTEM READY", 15, 0)
oled.text("---------------", 5, 10)
oled.text("Temp : " + str(temp) + "C", 12, 20)
oled.text("Hum : " + str(hum) + "%", 12, 32)
oled.text("Qty : " + str(qty) + "%", 12, 44)
oled.text("Light : " + str(light) + "%", 12, 56)
oled.show()
time.sleep(0.5)