from machine import Pin, ADC, I2C, PWM
from time import sleep
from ssd1306 import SSD1306_I2C
# --- OLED ---
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
oled = SSD1306_I2C(128, 64, i2c)
# --- INPUTS ---
ph_sensor = ADC(Pin(34))
turbidity_sensor = ADC(Pin(35))
level_sensor = ADC(Pin(32)) # analog water level
leak_sensor = Pin(33, Pin.IN, Pin.PULL_UP) # pushbutton for leak
button = Pin(4, Pin.IN, Pin.PULL_UP) # manual stop
# --- OUTPUTS ---
pump_in = Pin(13, Pin.OUT)
pump_out = Pin(12, Pin.OUT)
filter_led = Pin(18, Pin.OUT)
oxygen_led = Pin(19, Pin.OUT)
buzzer = PWM(Pin(27))
buzzer.duty(0)
# --- OLED Display ---
def show_status(text):
oled.fill(0)
oled.text("AQUA MONITOR", 17, 5)
oled.text("STATUS:", 35, 25)
line1, *line2 = text.split("\n")
oled.text(line1, 18, 45)
if line2:
oled.text(line2[0], 18, 55)
oled.show()
def show_readings(ph, turb, lvl):
oled.fill(0)
oled.text("AQUA MONITOR", 17, 0)
oled.text("pH: {:.1f}".format(ph), 33, 20)
oled.text("Turb: {:.0f}".format(turb), 28, 35)
oled.text("Level: {:.0f}%".format(lvl), 23, 50)
oled.show()
# --- Buzzer ---
def beep(freq=1000, duration=0.2):
buzzer.freq(freq)
buzzer.duty(512)
sleep(duration)
buzzer.duty(0)
# --- OXYGEN LOW HANDLER ---
def handle_low_oxygen():
show_status("Oxygen Low!\nAerating...")
oxygen_led.on()
pump_in.on()
pump_out.on()
beep(1200, 0.2)
sleep(3)
pump_in.off()
pump_out.off()
oxygen_led.off()
# --- MAIN LOOP ---
while True:
ph = ph_sensor.read() / 4095 * 14
turb = turbidity_sensor.read() / 40
level_percent = (level_sensor.read() / 4095) * 100
leak = leak_sensor.value()
abnormal = False
status = "Normal"
# ==================================================
# 1. LEAK DETECTED (highest priority)
# ==================================================
if leak == 0:
pump_in.off()
pump_out.off()
filter_led.off()
oxygen_led.off()
show_status("Leak Detected")
for _ in range(2):
beep(1500, 0.2)
sleep(0.2)
sleep(2)
continue
# ==================================================
# 2. LOW OXYGEN (turbidity > 60 and ≤ 70)
# ==================================================
if turb > 60 and turb <= 70:
abnormal = True
handle_low_oxygen()
# ==================================================
# 3. HIGH TURBIDITY (turb > 70) → Replace Water
# ==================================================
if turb > 70:
abnormal = True
# Aerate briefly before replacing (realistic)
handle_low_oxygen()
# Filtering stage
show_status("Filtering...")
filter_led.on()
sleep(2)
filter_led.off()
# Replace water
show_status("Dirty Water\nReplacing...")
pump_out.on()
pump_in.on()
beep(1000, 0.4)
sleep(3)
pump_out.off()
pump_in.off()
# ==================================================
# 4. PH ABNORMAL → REFILL ONLY
# ==================================================
if ph < 6.5 or ph > 8:
abnormal = True
show_status("pH Abnormal\nRefilling...")
pump_in.on()
beep(800, 0.2)
sleep(3)
pump_in.off()
# ==================================================
# 5. LOW WATER LEVEL → REFILL
# ==================================================
if level_percent < 20:
abnormal = True
show_status("Low Level\nRefilling...")
pump_in.on()
beep(700, 0.2)
sleep(3)
pump_in.off()
# ==================================================
# 6. MANUAL STOP
# ==================================================
if button.value() == 0:
pump_in.off()
pump_out.off()
filter_led.off()
oxygen_led.off()
show_status("Manual Stop")
beep(500, 0.3)
sleep(2)
# ==================================================
# 7. NORMAL OR MAINTAINING
# ==================================================
if abnormal:
show_status("Maintaining...")
else:
show_readings(ph, turb, level_percent)
sleep(2)Loading
esp32-devkit-c-v4
esp32-devkit-c-v4
Loading
ssd1306
ssd1306