from machine import Pin, I2C, PWM, SPI
import dht
import time
import _thread
import network
import urequests
import ujson
import os
import LCD1602
# Initialize DHT22 sensor
dht_sensor = dht.DHT22(Pin(4))
# Initialize Relay
relay = Pin(5, Pin.OUT)
# Initialize Buzzer
buzzer = Pin(18, Pin.OUT)
# Initialize RGB LED
red_led = Pin(19, Pin.OUT)
green_led = Pin(21, Pin.OUT)
blue_led = Pin(22, Pin.OUT)
# Initialize State LED
state_led = Pin(25, Pin.OUT)
# Initialize I2C and LCD
i2c = I2C(scl=Pin(22), sda=Pin(21))
lcd = LCD1602(i2c, 0x27) # Use the correct I2C address
# Initialize Servo Motor
servo = PWM(Pin(23), freq=50)
# Initialize Push Buttons (only for egg count)
inc_button = Pin(13, Pin.IN, Pin.PULL_UP)
dec_button = Pin(14, Pin.IN, Pin.PULL_UP)
# Initialize MicroSD Card Module
spi = SPI(1, baudrate=1000000, sck=Pin(18), mosi=Pin(23), miso=Pin(19))
sd = SDCard(spi, Pin(5))
os.mount(sd, "/sd")
# Egg count variables
egg_count = 0
min_eggs = 5
max_eggs = 30
# Incubator state
incubator_on = True # Incubator is always on (no manual control)
# Wi-Fi Credentials
WIFI_SSID = "YourWiFiSSID"
WIFI_PASSWORD = "YourWiFiPassword"
# Webhook URL for notifications
WEBHOOK_URL = "https://your-webhook-url.com"
# Connect to Wi-Fi
def connect_wifi():
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.connect(WIFI_SSID, WIFI_PASSWORD)
while not sta_if.isconnected():
time.sleep(1)
print("Connected to Wi-Fi")
# Send notification
def send_notification(message):
data = ujson.dumps({"message": message})
urequests.post(WEBHOOK_URL, headers={"Content-Type": "application/json"}, data=data)
# Log data to SD card
def log_data(temp, hum):
with open("/sd/data.txt", "a") as f:
f.write("Temp: {:.1f}C, Hum: {:.1f}%\n".format(temp, hum))
# Function to set servo angle
def set_servo_angle(angle):
duty = int(40 + (angle / 180) * 115)
servo.duty(duty)
# Function to update LCD display
def update_lcd(temp, hum, message, state):
lcd.clear()
lcd.print("Temp: {:.1f}C".format(temp))
lcd.set_cursor(0, 1)
lcd.print("Hum: {:.1f}%".format(hum))
lcd.set_cursor(0, 2)
lcd.print(message)
lcd.set_cursor(0, 3)
lcd.print("State: {}".format(state))
# Function to control egg tray rotation
def rotate_egg_tray():
while True:
if incubator_on:
set_servo_angle(90)
time.sleep(5400) # 90 minutes
set_servo_angle(0)
time.sleep(5400) # 90 minutes
else:
time.sleep(1)
# Start egg tray rotation in a separate thread
_thread.start_new_thread(rotate_egg_tray, ())
# Debounce function for button press
def debounce_button(pin):
time.sleep(0.05)
return not pin.value()
# Main loop
while True:
dht_sensor.measure()
temp = dht_sensor.temperature()
hum = dht_sensor.humidity()
if incubator_on:
if temp < 37.5:
relay.on()
else:
relay.off()
if temp > 40:
buzzer.on()
send_notification("Critical: Temperature > 40°C")
else:
buzzer.off()
if 37.5 <= temp <= 40 and 40 <= hum <= 60:
red_led.off()
green_led.on()
blue_led.off()
elif temp < 37.5 or hum < 40:
red_led.off()
green_led.off()
blue_led.on()
else:
red_led.on()
green_led.off()
blue_led.off()
else:
relay.off()
buzzer.off()
red_led.off()
green_led.off()
blue_led.off()
state_led.value(1 if incubator_on else 0)
if egg_count < min_eggs:
message = "Add more eggs"
elif egg_count > max_eggs:
message = "Reduce eggs"
else:
message = "Egg count OK"
state = "ON" if incubator_on else "OFF"
update_lcd(temp, hum, message, state)
if not inc_button.value() and debounce_button(inc_button):
egg_count += 1
time.sleep(0.2)
if not dec_button.value() and debounce_button(dec_button):
egg_count -= 1
time.sleep(0.2)
egg_count = max(min_eggs, min(max_eggs, egg_count))
log_data(temp, hum)
time.sleep(1)