This guide should help you connect and consume messages from your computer vision filter.
Python Example
In this guide we are using the Paho MQTT client in Python.
Prerequisites
Paho MQTT library installed:
pipinstallpaho-mqtt
Sample Integration for Consuming Messages
1. Import the Paho MQTT library
import paho.mqtt.client as mqttimport json
2. Set up MQTT client
# Replace 'your_device_id' and 'your_filter_id' with actual device and filter identifiers.device_id ='your_device_id'filter_id ='your_filter_id'client = mqtt.Client(client_id=f"{device_id}_{filter_id}")
# Subscribe to data output from the filterclient.subscribe(f"device/{device_id}/filter/{filter_id}/data")# Subscribe to status updates from the filterclient.subscribe(f"device/{device_id}/filter/{filter_id}/status")
5. Implement Callbacks for Incoming Messages
defon_message(client,userdata,msg): topic = msg.topic payload = msg.payload.decode("utf-8")if"data"in topic:# Process incoming data from the filterprint(f"Received data on topic {topic}: {payload}")# Add your custom logic for handling data messageselif"status"in topic:# Handle status updates from the filterprint(f"Received status on topic {topic}: {payload}")# Add your custom logic for handling status messages# Set callback for incoming messagesclient.on_message = on_message
6. Loop to Handle Messages
client.loop_start()
7. Keep the Script Running
try:whileTrue:passexceptKeyboardInterrupt:# Disconnect from the broker when done client.loop_stop() client.disconnect()
Additional Tips
Adjust QoS levels based on message importance.
Customize topics and payloads according to your application's needs.
Explore advanced features like metrics publication based on your naming conventions.