("Project: Food Storage Monitoring System")
("This program will monitor temperature and humidity of the food in the storage")
("Trigger alerts on an OLED screen and with a buzzer if values go beyond thresholds.")
("Develop by: Ikhwan Munib")
("Date: 15/4/2026")
# Import library / module
from machine import Pin, PWM, SoftI2C, ADC
import oled_library
import dht
from time import sleep
# Pin Declaration
dht_sensor = dht.DHT22(Pin(4))
red_led = Pin(14, Pin.OUT)
green_led = Pin(27, Pin.OUT)
buzzer = PWM(Pin(18, Pin.OUT))
buzzer.freq(1000)
SCL1_Pin = Pin(23)
SDA1_Pin = Pin(22)
OLED1_Pin = SoftI2C(scl=SCL1_Pin, sda=SDA1_Pin)
# Treshold
TEMP_LIMIT = 30 #'C
HUM_LIMIT = 70 # %
GAS_LIMIT = 2000 #ADC value
LIGHT_LIMIT = 2000 #ADC value
#Push Button
button = Pin(25, Pin.IN, Pin.PULL_UP)
# Potentiometers
gas_sensor = ADC(Pin(34))
ldr_sensor = ADC(Pin(35))
gas_sensor.atten(ADC.ATTN_11DB)
ldr_sensor.atten(ADC.ATTN_11DB)
# Object Declaration
screen1 = oled_library.SSD1306_I2C(width=128, height=64, i2c=OLED1_Pin)
# Main Program Loop
while True:
try:
dht_sensor.measure()
temp = dht_sensor.temperature()
hum = dht_sensor.humidity()
except:
temp = 0
hum = 0
# Read simulated sensors
gas = gas_sensor.read()
light = ldr_sensor.read()
# Adjustable threshold (optional scaling)
adjust = threshold_pot.read()
GAS_LIMIT = 1000 + (adjust / 4095 * 2000)
# --------------------------
# CHECK CONDITIONS
# --------------------------
unsafe = False
if temp > TEMP_LIMIT or hum > HUM_LIMIT:
unsafe = True
if gas > GAS_LIMIT:
unsafe = True
if light > LIGHT_LIMIT:
unsafe = True
# --------------------------
# BUTTON (ACKNOWLEDGE)
# --------------------------
if button.value() == 0:
alert_ack = True
buzzer.duty(0)
# --------------------------
# OUTPUT CONTROL
# --------------------------
if unsafe and not alert_ack:
# UNSAFE
red_led.on()
green_led.off()
buzzer.duty(512)
status = "UNSAFE!"
else:
# SAFE
red_led.off()
green_led.on()
buzzer.duty(0)
status = "SAFE"
alert_ack = False
# --------------------------
# OLED DISPLAY
# --------------------------
oled.fill(0)
oled.text("Temp: {:.1f}C".format(temp), 0, 0)
oled.text("Hum : {:.1f}%".format(hum), 0, 10)
oled.text("Gas : {}".format(gas), 0, 20)
oled.text("Light: {}".format(light), 0, 30)
oled.text(status, 0, 50)
oled.show()
# --------------------------
# SERIAL MONITOR
# --------------------------
print("Temp:", temp, "Hum:", hum,
"Gas:", gas, "Light:", light,
"Status:", status)
sleep(1)
Loading
esp32-devkit-v1
esp32-devkit-v1