Sheep Counter with LoRa / Meshtastic — Deployment Guide

View source on GitHub
Preset

LoRa Gateway (Full System)

Count sheep at the gate and see real-time totals on your phone and in a Home Assistant dashboard — even on an off-grid farm with no WiFi in the field.

DevicePurpose
reCamera 2002Runs YOLO sheep detector and gate-crossing counter
XIAO ESP32-S3 + Wio-SX1262LoRa transmitter node (Meshtastic firmware)
reComputer R1100 / Raspberry PiGateway — bridges LoRa messages to MQTT and Home Assistant

What you'll get:

  • Automatic IN / OUT / INSIDE sheep counts every time an animal crosses the gate
  • LoRa heartbeat every 15 minutes to keep all displays in sync
  • Real-time Home Assistant dashboard with daily totals and trend history
  • Works off-grid — only the gateway computer needs a LAN connection to HA

Requirements: reCamera connected via USB or Ethernet · XIAO ESP32-S3 with Wio-SX1262 LoRa board · reComputer or Raspberry Pi running Home Assistant on the local network


1

Deploy Sheep Counter to reCamera

Deploy the sheep counter to your reCamera; it starts automatically on boot.

Wiring
  1. Connect reCamera to your computer via USB-C or Ethernet
  2. Connect the XIAO ESP32-S3 LoRa node to reCamera's UART port (/dev/ttyS3):
    • XIAO TX → reCamera RX
    • XIAO RX → reCamera TX
    • Shared GND
  3. Enter your reCamera IP address (USB default: 192.168.42.1) and SSH password
Troubleshooting
IssueSolution
SSH connection failedCheck cable; try password recamera.2 if recamera doesn't work
Counter exits immediatelyCheck /var/log/sheep_counter.log, then run /etc/init.d/S85sscma-keepalive restart
No log output after 2 minEnsure SSCMA inference daemon is running: /etc/init.d/S85sscma-keepalive start
No UART events reaching XIAOConfirm XIAO GND is shared with reCamera; check wiring on /dev/ttyS3

After Deployment

Deployment stops the reCamera's default Node-RED service. After deployment, SSH into the camera and tail the log to confirm it's running:

ssh recamera@192.168.42.1
tail -f /var/log/sheep_counter.log

You should see detection events like EVT,IN,1,0,1,123 appearing when sheep cross the gate line.

2

Configure the XIAO LoRa Transmitter

Flash Meshtastic firmware to the XIAO ESP32-S3 and configure it to act as a Serial + Detection Sensor node.

Wiring
  1. Connect the XIAO ESP32-S3 to your computer via USB-C
  2. Open the Meshtastic Web Flasher in Chrome/Edge
  3. Select XIAO ESP32-S3 as the target device and flash the latest stable Meshtastic firmware
  4. After flashing, open the Meshtastic Web Client to configure:
    • Serial Module → Enable → Baud Rate: 115200 → Receive mode: passthrough
    • Detection Sensor Module → Enable → Monitor GPIO: 490 → Detection high → Alert message prefix: SHEEP
    • Set a unique Node name (e.g. SheepGate1) so you can identify it on the mesh
  5. Pair the XIAO with a Meshtastic-compatible LoRa receiver (phone app or local node)
Troubleshooting
IssueSolution
Flasher can't detect XIAOHold BOOT while plugging in USB; release after the port appears
No messages on the meshCheck RF frequency matches all mesh nodes; confirm channel preset is the same
Serial data arrives garbledConfirm baud rate is 115200 on both the XIAO Serial Module and the reCamera UART
GPIO alerts not firingVerify the sheep_counter binary is sending a ~0.5 s pulse on GPIO 490 on each crossing

After Deployment

Once configured, trigger a test crossing by briefly pulling GPIO 490 HIGH. You should see:

  • A "SHEEP" alert on your Meshtastic phone app within 30 seconds
  • The raw EVT,IN,... line appearing in your Meshtastic channel as a text message
3

Deploy Gateway Bridge to reComputer

Deploy the bridge services to your gateway computer over SSH as two Docker containers (sheep-meshtastic-bridge, sheep-ha-bridge). They publish the sheep counts received over LoRa to Home Assistant via MQTT.

Before you start, ensure Home Assistant is running on your LAN and its MQTT integration is connected to the same broker with discovery enabled. You will enter the gateway SSH connection, MQTT broker IP, and Meshtastic receiver serial port in the deploy form.

Prerequisites
  • Docker and the Docker Compose plugin installed on the gateway (docker compose version prints a version).

  • The gateway can pull python:3.11-slim and install Python packages (paho-mqtt, meshtastic) from PyPI on first start. A PyPI mirror is applied automatically on networks in China.

  • The MQTT broker IP is reachable from the gateway on port 1883. Use the broker's LAN IP; 127.0.0.1 points to the container itself.

  • If the gateway was set up with the earlier install.sh of this solution, stop and disable its two systemd services first. They run from /opt/sheep-gateway, restart automatically, and hold the Meshtastic serial port that the new container needs:

    sudo systemctl disable --now meshtastic-bridge ha-bridge
    sudo rm /etc/systemd/system/meshtastic-bridge.service /etc/systemd/system/ha-bridge.service
    sudo systemctl daemon-reload
    

    /opt/sheep-gateway is no longer used and can be removed with sudo rm -rf /opt/sheep-gateway.

Wiring
  1. Connect the LoRa receiver node (a second Meshtastic device, e.g. reComputer serial or a plugged-in USB Meshtastic radio) to the gateway computer
Troubleshooting
IssueSolution
sheep-meshtastic-bridge restartingConfirm the Meshtastic USB radio is plugged into the gateway and the serial port path matches (ls /dev/ttyACM*); check docker logs sheep-meshtastic-bridge
sheep-ha-bridge fails to connectConfirm the MQTT broker IP is reachable on port 1883 and accepts the gateway connection
No MQTT messagesVerify the MQTT broker IP and that the broker accepts unauthenticated connections on port 1883
sheep-meshtastic-bridge cannot open the serial port on a gateway set up with the earlier install.shThe old meshtastic-bridge systemd service still holds the port; check with systemctl is-active meshtastic-bridge ha-bridge and disable both as described in Prerequisites
Containers missing after rebootContainers use restart: unless-stopped; confirm Docker starts at boot with systemctl is-enabled docker
pip install fails in the container logThe gateway cannot reach PyPI; check its network or DNS, then run docker compose -p sheep_gateway restart in ~/sheep-gateway/gateway (remote target) or redeploy this step
After Deployment

After deployment, verify both containers are running:

ssh recomputer@<gateway-ip>
docker ps --filter name=sheep- --format '{{.Names}}\t{{.Status}}'

sheep-meshtastic-bridge and sheep-ha-bridge should both show Up. The first start installs Python packages and takes about a minute longer than later restarts.

Import the ha_dashboard.yaml file into Home Assistant:

  1. Copy assets/gateway/ha_dashboard.yaml from this solution package to your HA config directory (with the remote target, the deployment also uploads it to ~/sheep-gateway/gateway/ha_dashboard.yaml on the gateway)
  2. In HA: Settings → Dashboards → Import → select the file
Deployment method

Deploy over SSH from this machine to the gateway. Enter the gateway IP address and SSH credentials, then the MQTT broker IP and serial port.

What you will be asked for(filled in the SenseCraft Solution app)
  • Local MQTT Broker IP*

    IP address of your MQTT broker (often same as Home Assistant).

  • Meshtastic Serial Port*default /dev/ttyACM0

    Serial device path of the USB-connected Meshtastic receiver.

4

Open Sheep Counter Dashboard

The full system is now running. Click below to open the Home Assistant sheep counter dashboard and confirm live counts are coming in.

Troubleshooting
IssueSolution
Dashboard page shows no dataConfirm ha-bridge is running, MQTT discovery is enabled in HA, and the sensor.sheep_* entities exist
Counts not updatingTrigger a manual crossing; check MQTT traffic with mosquitto_sub -t sheep/# -v
HA entities show unavailableRe-import the ha_dashboard.yaml and restart the ha-bridge service
After Deployment

Your sheep counter is live! On the dashboard you will see:

  • IN / OUT / INSIDE — real-time counters since the last dashboard reset
  • Last event time — timestamp of the most recent gate crossing
  • Daily history — 24-hour trend graph

Quick Verification

  1. Wave your hand in front of the camera (or walk past the gate line) to simulate a crossing
  2. Watch the EVT,IN,... line appear in /var/log/sheep_counter.log on the reCamera
  3. Within ~30 s, the IN counter should increment on the HA dashboard

Tips

  • Mount reCamera at an angle to the gate so sheep cross the virtual line cleanly
  • Adjust the gate-line position in the counter config if your crossing rate is low
  • The LoRa heartbeat (HB messages every 15 min) keeps displays in sync after radio gaps

Next Steps