Examples

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:

    pip install paho-mqtt

Sample Integration for Consuming Messages

1. Import the Paho MQTT library

import paho.mqtt.client as mqtt
import 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}")

3. Define Connection Parameters

broker_address = '<BROKER_IP>'
broker_port = <BROKER_PORT>
client.connect(broker_address, port=broker_port)

4. Subscribe to Relevant Topics

# Subscribe to data output from the filter
client.subscribe(f"device/{device_id}/filter/{filter_id}/data")

# Subscribe to status updates from the filter
client.subscribe(f"device/{device_id}/filter/{filter_id}/status")

5. Implement Callbacks for Incoming Messages

def on_message(client, userdata, msg):
    topic = msg.topic
    payload = msg.payload.decode("utf-8")
    
    if "data" in topic:
        # Process incoming data from the filter
        print(f"Received data on topic {topic}: {payload}")
        # Add your custom logic for handling data messages
    elif "status" in topic:
        # Handle status updates from the filter
        print(f"Received status on topic {topic}: {payload}")
        # Add your custom logic for handling status messages

# Set callback for incoming messages
client.on_message = on_message

6. Loop to Handle Messages

client.loop_start()

7. Keep the Script Running

try:
    while True:
        pass
except KeyboardInterrupt:
    # 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.

Last updated