print("\tProgram: AUTOMATIC FOOD DISPENSER AND WATER TEMPERATURE MONITORING FOR AQUARIUM")
print("\tDate: 17/11/2024")
print("\tCreated by BLACKY")
print("\t==============================================")
from machine import Pin, PWM, SoftI2C
from time import sleep, time
import dht
import ssd1306
# Pin Assignments
dht_pin = Pin(27) # DHT22 data pin
red_led = Pin(18, Pin.OUT) # Red LED
blue_led = Pin(19, Pin.OUT) # Blue LED
green_led = Pin(5, Pin.OUT) # Green LED
servo_pin = Pin(4) # Servo motor pin
buzzer = PWM(Pin(2)) # Buzzer with PWM for sound modulation
i2c = SoftI2C(scl=Pin(13), sda=Pin(12)) # I2C for OLED display
# Initialize peripherals
servo = PWM(servo_pin, freq=50) # Servo motor
oled = ssd1306.SSD1306_I2C(128, 64, i2c) # OLED display
sensor = dht.DHT22(dht_pin) # DHT22 temperature and humidity sensor
# Ensure buzzer is off at initialization
buzzer.duty(0)
# Constants
HOT_THRESHOLD = 40.0 # Temperature threshold for "hot"
WARM_THRESHOLD = 35.0 # Temperature threshold for "warm"
COLD_THRESHOLD = 10.0 # Temperature threshold for "cold"
CYCLE_DURATION = 0.3 * 60 # 30 seconds
start_time = time() # Start time for servo cycles
# Functions
def set_servo_angle(angle):
"""Set the servo motor to a specific angle (0 to 180 degrees)."""
angle = max(0, min(180, angle)) # Clamp angle between 0 and 180
duty = int(40 + (angle / 180) * 115) # Map angle to duty cycle (40-155)
servo.duty(duty)
def sound_buzzer(frequency, duration):
"""Sound the buzzer at a specific frequency for a duration."""
buzzer.freq(frequency)
buzzer.duty(512) # Activate the buzzer with 50% duty cycle
sleep(duration)
buzzer.duty(0) # Ensure buzzer is turned off
def read_dht_sensor():
"""Attempt to read from the DHT sensor with retries."""
for _ in range(3): # Retry up to 3 times
try:
sensor.measure()
return sensor.temperature(), sensor.humidity()
except Exception as e:
print(f"Retrying DHT sensor read: {e}")
sleep(1) # Wait before retrying
raise RuntimeError("DHT sensor failed after 3 retries")
# Main loop
while True:
try:
# Measure temperature and humidity
temp, hum = read_dht_sensor()
# Clear OLED and display temperature and humidity
oled.fill(0)
oled.text(f"Temp: {temp:.1f}C", 0, 0)
oled.text(f"Hum: {hum:.1f}%", 0, 10)
# Control LEDs and buzzer based on temperature
if temp >= HOT_THRESHOLD:
red_led.on()
blue_led.off()
green_led.off()
oled.text("Status: HOT", 0, 20)
sound_buzzer(1000, 0.2) # Buzzer beeps at 1kHz for hot alert
elif temp >= WARM_THRESHOLD:
red_led.off()
blue_led.off()
green_led.on()
oled.text("Status: WARM", 0, 20)
elif temp >= COLD_THRESHOLD:
red_led.off()
blue_led.on()
green_led.off()
oled.text("Status: COLD", 0, 20)
else:
red_led.off()
blue_led.off()
green_led.off()
oled.text("Status: VERY COLD", 0, 20)
sound_buzzer(500, 0.5) # Buzzer beeps at 500Hz for very cold alert
# Servo control every 30 minutes
elapsed_time = time() - start_time
if elapsed_time >= CYCLE_DURATION:
oled.text("Servo Active", 0, 30)
set_servo_angle(0)
sleep(1)
set_servo_angle(90)
sleep(1)
set_servo_angle(180)
sleep(1)
set_servo_angle(90)
start_time = time() # Reset cycle time
# Refresh OLED display
oled.show()
# Short delay
sleep(1)
except Exception as e:
oled.fill(0)
oled.text("Error occurred", 0, 0)
oled.text(str(e), 0, 10)
oled.show()
print(f"Error: {e}")
sleep(2)
Loading
esp32-devkit-v1
esp32-devkit-v1