import network
import ujson
from utime import sleep_ms
from umqtt.simple import MQTTClient
from hx711 import HX711
from machine import Pin
import dht
#hx711 pin
capteur_hx711 = HX711(19,18,1)
#DHT pin
sensor = dht.DHT22(Pin(15))
# MQTT Server Parameters
MQTT_CLIENT_ID = "project-chicken-separator"
MQTT_BROKER = "broker.mqttdashboard.com"
MQTT_USER = ""
MQTT_PASSWORD = ""
MQTT_TOPIC_WEIGHT = "weight-chicken"
prev_data = ""
prev_mess = 0
prev_weather = ""
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="")
sleep_ms(100)
print(" Connected!")
print("Connecting to MQTT server... ", end="")
client = MQTTClient(MQTT_CLIENT_ID, MQTT_BROKER, user=MQTT_USER, password=MQTT_PASSWORD)
client.connect()
print(" Connected!")
while True :
capteur_hx711.power_on()
while (capteur_hx711.is_ready()) :
pass
mesure = capteur_hx711.read(False)
print(mesure)
while (capteur_hx711.is_ready()) :
pass
mesure = capteur_hx711.read(True)
print(mesure)
#Calibration
mesure = mesure/420
print ('Total Weight of Chicken is :', mesure)
if message != prev_data:
client.publish(MQTT_TOPIC_WEIGHT, "Fence movement")
prev_data = message
prev_mess = 0
play_buzzer_and_sleep(784, 5000)
sleep_ms(1000)
else:
if prev_mess == 0:
client.publish(MQTT_TOPIC_WEIGHT, "No fence movement")
prev_mess = 1
else:
continue
sleep_ms(100)
# https://github.com/SergeyPiskunov/micropython-hx711/tree/master
# https://www.youtube.com/watch?v=RQgRCFXAM3E&ab_channel=ChristianDucros
# https://www.instructables.com/Arduino-Bathroom-Scale-With-50-Kg-Load-Cells-and-H/