Overview
YOLO-Pi publishes detection results to an MQTT broker, enabling real-time notification and integration with IoT systems, home automation, and monitoring dashboards. This guide explains how to configure MQTT, understand the message format, and subscribe to detection events.MQTT Setup
YOLO-Pi uses the Eclipse Paho MQTT client for Python to publish detection events.Environment Configuration
The MQTT server is configured via an environment variable:src/yolo-pi.py:17
Setting the MQTT Server
Before running YOLO-Pi, set theMQTT environment variable:
The script will exit if the
MQTT environment variable is not set.Client Initialization
YOLO-Pi initializes the MQTT client at startup:src/yolo-pi.py:19-29
Connection Parameters
- Client ID:
"yolo-pi"- Unique identifier for this MQTT client - Port:
1883- Standard MQTT port (non-TLS) - Topic:
"yolo"- All detections published to this topic
Detection Message Format
Each frame with detected objects publishes a JSON message to theyolo topic.
Message Structure
Field Descriptions
The detected object class name (e.g., “person”, “car”, “dog”)
Confidence score as a string (0.0 to 1.0). Higher values indicate greater confidence.
Publishing Detections
The detection results are published after each frame is processed:src/yolo-pi.py:64-102
Message Properties
- Topic:
"yolo" - Payload: UTF-8 encoded JSON string
- QoS: Default (0) - At most once delivery
- Retain: False - Messages are not retained on the broker
The
tkey variable (timestamp) is created but not currently published. You can extend the message format to include timestamps if needed.Subscribing to Detection Events
Create an MQTT subscriber to receive detection events in real-time.Python Subscriber Example
Node.js Subscriber Example
Command Line Testing
Usemosquitto_sub to test MQTT messages:
Detection Payload Details
Empty Frames
If no objects are detected in a frame, an empty array is published:Multiple Objects
Multiple detections are included in the same message:The order of detections is reversed (highest index first) due to the
reversed() call in the loop. This is an implementation detail and should not be relied upon for ordering.Integration Examples
Home Assistant
Integrate YOLO-Pi detections with Home Assistant:Node-RED Flow
Advanced Configuration
Custom MQTT Port
To use a non-standard MQTT port, modify the connection:Authentication
Add username/password authentication:TLS/SSL Encryption
Message Rate
YOLO-Pi publishes a message for every processed frame:- Raspberry Pi 3: ~0.5-1 message per second
- MacBook Pro: ~0.5 messages per second (with Tiny YOLO VOC)
Troubleshooting
Connection Refused
Error:Could not connect to mqtt stream [Errno 111] Connection refused
Solutions:
- Verify MQTT broker is running:
mosquitto -v - Check firewall rules allow port 1883
- Ensure
MQTTenvironment variable points to correct host
No Messages Received
Possible Causes:- No objects detected (check camera view and lighting)
- Detection threshold too high (lower
score_threshold) - MQTT subscriber on wrong topic (use
yolo) - Network issues between publisher and broker
JSON Decode Errors
Error:json.decoder.JSONDecodeError: Expecting value
Solutions:
- Ensure message payload is valid JSON
- Check for empty or malformed messages
- Verify UTF-8 encoding:
msg.payload.decode('utf-8')
Performance Considerations
Message Size
Typical message sizes:- Empty frame: ~2 bytes (
[]) - Single detection: ~40-50 bytes
- 10 detections: ~400-500 bytes
QoS Levels
YOLO-Pi uses QoS 0 (at most once). For critical applications, consider:Next Steps
Model Conversion
Learn how to convert different YOLO models
Running Inference
Configure and optimize object detection

