import machine
import time
from mfrc522 import MFRC522
from hx711 import HX711
# Pin Definitions (SPI0 + PIR + Chute + HX711)
sck = machine.Pin(18)
mosi = machine.Pin(19)
miso = machine.Pin(16)
sda = machine.Pin(17, machine.Pin.OUT)
rst = machine.Pin(22, machine.Pin.OUT)
pir_pin = machine.Pin(20, machine.Pin.IN)
# Egg Rollout Break-Beam Sensor on GP21 (LOW / 0 when an egg blocks the beam)
egg_beam = machine.Pin(21, machine.Pin.IN, machine.Pin.PULL_UP)
# Initialize Interfaces matching your diagram pins
spi = machine.SPI(0, baudrate=2500000, polarity=0, phase=0, sck=sck, mosi=mosi, miso=miso)
reader = MFRC522(spi, sda, rst)
scale = HX711(d_out=26, pd_sck=27)
# --- COMPLETE HEN REGISTRY DICTIONARY ---
HEN_REGISTRY = {
"01030400": "Hen 1 (Blue Card - Delaware)",
"11334400": "Hen 2 (Green Card - Delaware)",
"5577CC00": "Hen 3 (Yellow Card - New Hampshire)",
"AACC0000": "Hen 4 (Red Card - New Hampshire)",
"04220400": "Hen 5 (NFC Tag - Runner Mix)",
"C0EE4800": "Hen 6 (Key Fob - Runner Mix)"
}
LOOKBACK_SCANS = 3
weight_history = []
last_known_empty_weight = 0
print("==============================================")
print(" 🐔 NESTING BOX MONITORING PROGRAM RUNNING ")
print("==============================================")
current_bird = None
presence_start_time = None
is_confirmed_present = False
dynamic_tare = 0
confirmed_entry_weight = 0
entry_weight_captured = False
egg_laid_this_session = False
while True:
current_raw_reading = scale.read()
weight_history.append(current_raw_reading)
if len(weight_history) > 30:
weight_history.pop(0)
# 1. RFID Sweep Check
(stat, tag_type) = reader.request(reader.REQIDL)
rfid_detected = (stat == reader.OK)
if rfid_detected:
(stat, raw_uid) = reader.anticoll()
if stat == reader.OK:
uid_str = "".join([f"{x:02X}" for x in raw_uid])
detected_bird = HEN_REGISTRY.get(uid_str, f"Unregistered Tag [HEX: {uid_str}]")
# Entry Event
if current_bird is None:
current_bird = detected_bird
presence_start_time = time.time()
is_confirmed_present = False
entry_weight_captured = False
confirmed_entry_weight = 0
egg_laid_this_session = False
dynamic_tare = last_known_empty_weight
print(f"⏳ [PENDING] {current_bird} detected. Waiting 3 seconds...")
# Confirmation Check
elif not is_confirmed_present:
if (time.time() - presence_start_time) >= 3:
print(f"📥 [ENTRY] {current_bird} CONFIRMED inside nesting box.")
is_confirmed_present = True
# Live Weight Capture
elif is_confirmed_present and not entry_weight_captured:
calculated_live_weight = int((current_raw_reading - dynamic_tare) / 100)
if abs(calculated_live_weight) > 5:
confirmed_entry_weight = calculated_live_weight
print(f"⚖️ [WEIGHT] Captured bird weight: {confirmed_entry_weight} units.")
entry_weight_captured = True
# 2. Live Rollout Detection (Triggers when the egg breaks the beam line on GP21)
if is_confirmed_present and egg_beam.value() == 0 and not egg_laid_this_session:
egg_laid_this_session = True
print(f"🥚 [EGG DROP] An egg is rolling into the collection funnel!")
else:
# Exit Event
if current_bird is not None:
if is_confirmed_present:
lookback_index = max(0, len(weight_history) - 1 - LOOKBACK_SCANS)
raw_exit_reading = weight_history[lookback_index]
confirmed_exit_weight = int((raw_exit_reading - dynamic_tare) / 100)
weight_difference = confirmed_exit_weight - confirmed_entry_weight
print(f"📤 [EXIT] {current_bird} left the nesting box.")
print(f"⚖️ [WEIGHT] Entry: {confirmed_entry_weight} | Exit: {confirmed_exit_weight} | Diff: {weight_difference}")
if egg_laid_this_session:
print("🎉 [RESULT] Session Status: SUCCESSFUL EGG LAID")
else:
print("❌ [RESULT] Session Status: No egg detected.")
else:
print(f"○ [FALSE ALARM] {current_bird} left before 3 seconds.")
# Reset tracking states cleanly
current_bird = None
is_confirmed_present = False
entry_weight_captured = False
egg_laid_this_session = False
presence_start_time = None
else:
if current_raw_reading != 0:
last_known_empty_weight = current_raw_reading
# 3. PIR General Movement check (Only prints if you actively use the PIR simulator box)
if is_confirmed_present and pir_pin.value() == 1:
print("🏃 [PIR] Activity: Hen shifted inside box.")
time.sleep(0.4)
time.sleep(0.2)
Loading
mfrc522
mfrc522