import machine
import dht
import time
from machine import Pin, SoftI2C
from servo import Servo
import network
from umqtt.simple import MQTTClient
from lcd_api import LcdApi
from i2c_lcd import I2cLcd
# WiFi credentials
ssid = 'Wokwi-GUEST'
password = ''
# MQTT settings
mqtt_server = 'broker.mqttdashboard.com'
mqtt_port = 1883
MQTT_BROKER = "broker.mqttdashboard.com"
client_id = ''
topic_sub_feed = b'home/chicken/feed'
topic_sub_water = b'home/chicken/water'
topic_sub_timer_feed = b'home/chicken/timer/feed'
topic_sub_timer_water = b'home/chicken/timer/water'
topic_pub_temp = b'home/chicken/temperature'
topic_pub_feed = b'home/chicken/feed_amount'
topic_pub_water = b'home/chicken/water_amount'
# Initialize WiFi
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.connect(ssid, password)
while not sta_if.isconnected():
time.sleep(1)
print('WiFi connected:', sta_if.ifconfig())
# Initialize MQTT client
client = MQTTClient(client_id, mqtt_server, mqtt_port)
# Initialize DHT22 sensor
dht_sensor = dht.DHT22(Pin(4))
I2C_ADDR = 0x27
totalRows = 4
totalColumns = 20
i2c = SoftI2C(scl=Pin(22), sda=Pin(21), freq=10000) #initializing the I2C method for ESP32
lcd = I2cLcd(i2c, I2C_ADDR, totalRows, totalColumns)
# Initialize servos
servo1 = Servo(pin = 18)
servo2 = Servo(pin = 19)
# Variables for tracking feed and water amounts
feed_amount = 0
water_amount = 0
# Timer objects for scheduling
timer1 = machine.Timer(0)
timer2 = machine.Timer(1)
feed_water_timer = machine.Timer(2) # Timer for 5-hour feed and water intervals
def feed_chicken():
global feed_amount
servo1.write_angle(0)
feed_amount += 1
client.publish(topic_pub_feed, str(feed_amount))
update_lcd()
def water_chicken():
global water_amount
servo2.write_angle(0)
water_amount += 1
client.publish(topic_pub_water, str(water_amount))
update_lcd()
def update_lcd():
lcd.move_to(0, 0)
lcd.putstr('Temp: {:.1f}C'.format(temperature))
lcd.move_to(0, 1)
lcd.putstr('Feed: {}'.format(feed_amount))
lcd.move_to(0, 2)
lcd.putstr('Water: {}'.format(water_amount))
def mqtt_callback(topic, msg):
pass # For this example, MQTT callback is not used
client.set_callback(mqtt_callback)
client.connect()
client.subscribe(topic_sub_feed)
client.subscribe(topic_sub_water)
client.subscribe(topic_sub_timer_feed)
client.subscribe(topic_sub_timer_water)
# Set up the feed_water_timer to call feed_and_water_chicken function every 5 hours (18000000 milliseconds)
# feed_water_timer.init(period=18000000, mode=machine.Timer.PERIODIC, callback=feed_and_water_chicken
UPDATE_INTERVAL = 10 # Update interval in seconds
previousMillis = 0
while True:
client.check_msg()
# Read temperature and humidity
dht_sensor.measure()
temperature = dht_sensor.temperature()
humidity = dht_sensor.humidity()
currentMillis = time.time()
print(currentMillis)
if currentMillis - previousMillis > UPDATE_INTERVAL:
print("Starting servo")
previousMillis = time.time()
# servo1.write_angle(90)
# servo2.write_angle(90)
# time.sleep(5)
# servo1.write_angle(0)
# servo2.write_angle(0)
# time.sleep(5)
feed_chicken()
water_chicken()
else:
servo1.write_angle(90)
servo2.write_angle(90)
# Publish temperature
client.publish(topic_pub_temp, str(temperature))
# Update LCD
update_lcd()
# Delay before the next loop