"""
1. Go to http://www.hivemq.com/demos/websocket-client/
"test.mosquitto.org"
"broker.mqttdashboard.com"
"""
import network
import time
from machine import Pin, PWM
import dht
import ujson
from umqtt.simple import MQTTClient
from hx711 import HX711
from neopixel import NeoPixel
#DHT config
sensor = dht.DHT22(Pin(15))
# neopixel config
pixels = NeoPixel(Pin(27), 16)
pixels.fill((0, 0, 0))
pixels.write()
#hx711 pin
capteur_hx711 = HX711(19,18,1)
# Set up PWM Pin for servo control
servo_pin = Pin(0)
servo = PWM(servo_pin)
#Set Duty Cycle for Different Angles
max_duty = 7864
min_duty = 1802
half_duty = int(max_duty/2)
#Set PWM frequency
frequency = 50
servo.freq (frequency)
#Open door
servo.duty_u16(max_duty)
time.sleep(0.2)
# MQTT Server Parameters
MQTT_CLIENT_ID = "chicken-separator"
MQTT_BROKER = "broker.mqttdashboard.com"
MQTT_USER = ""
MQTT_PASSWORD = ""
MQTT_TOPIC_TEMP = "sep-temp"
MQTT_TOPIC_HEAT = "sep-heat"
x = 0
prev_weather = ""
def mqtt_message(topic, msg):
global x
print("Incoming message:", msg)
try:
if msg == b'OFF':
print("OFF")
x = 0
elif msg == b'ON':
print("ON")
pixels.fill((255, 255, 255))
pixels.write()
x = 1
except Exception as e:
print("Error:", e)
print("Connecting to WiFi", end="")
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.connect('Wokwi-GUEST', '')
while not sta_if.isconnected():
print(".", end="")
time.sleep(0.1)
print(" Connected!")
print("Connecting to MQTT server... ", end="")
client = MQTTClient(MQTT_CLIENT_ID, MQTT_BROKER, user=MQTT_USER, password=MQTT_PASSWORD)
client.set_callback(mqtt_message)
client.connect()
client.subscribe("sep-heat")
print("Connected!")
while True:
client.check_msg()
capteur_hx711.power_on()
while (capteur_hx711.is_ready()) :
break
mesure = capteur_hx711.read(False)
while (capteur_hx711.is_ready()) :
break
mesure = capteur_hx711.read(True)
#Calibration
mesure = mesure/420
print ('Total Weight of Chicken is :', mesure)
if mesure >= 1 :
servo.duty_u16(min_duty)
print("Door Closed")
else :
servo.duty_u16(max_duty)
print("Door Opened")
sensor.measure()
if x == 0 :
if sensor.temperature() <= 32 :
pixels.fill((255, 255, 255))
pixels.write()
else :
pixels.fill((0, 0, 0))
pixels.write()
message = str(sensor.temperature())
if message != prev_weather:
client.publish(MQTT_TOPIC_TEMP, message)
prev_weather = message
else :
pass
time.sleep(1)