This example connects to an MQTT broker and subscribes to the OCR result from "camera" 5 and prints out the Container Code and if the checksum is correct or not (1 or 0).



import datetime
import json
import paho.mqtt.client as mqtt

# MQTT client
client = mqtt.Client()
# Callback for when the client receives a CONNACK response from the server
def on_connect(clientuserdataflagsrc):
    if rc == 0:
        print("Connected to the broker")
        #Subscribe to OCR results        
        client.subscribe("ConPDS/ocr/39a9ea8af7389b55b24000d27152602fdc178a98/cameras/5/results")
    
    else:
        print("Failed to connect to the broker. Return code:"rc)
# Callback for when a message is received from the server
def on_message(clientuserdatamsg):
    # Parse the JSON object
    data = json.loads(msg.payload)

    # Extract variables from the JSON object
    container_code = data.get("result")
    confidence = data.get("confidence")
    checksum = data.get("checksum")
    # Do relevant stuff with the results
    # Eg. print results to screen, Call API, Update DB, etc.
    # Here we print to screeen
    print (datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), container_codechecksum)

# Set up the MQTT client
client.on_connect = on_connect
client.on_message = on_message

# Connect to the MQTT broker
client.connect("connection_broker"188360)

# Start the MQTT client loop to listen for messages
client.loop_forever()