Bruno Cerk
Bruno Cerk

Reputation: 355

Paho MQTT module not returning full message from subscribed topic

i'm currently working with a sensor that sends bytes through MQTT and i'm using Paho MQTT module to make a testing environment so i can learn the back-end process of it. Here is my code just to continuously print the messages that the topic sends.

def on_message(sensorclt, userdata, msg):
    print(msg.payload)

def on_connect(sensorclt, userdata, flags, rc):
    if rc == 0:
        print('Connected')
        sensorclt.subscribe([(Topics[0], 0), (Topics[1], 0), (Topics[2], 0)])
    else:
        print("Failed to connect")
sensorclt = mqtt.Client()
sensorclt.connect(host='iot.eclipse.org', port=1883)
sensorclt.on_connect = on_connect
sensorclt.on_message = on_message
sensorclt.loop_forever()

Here is the output of a few seconds working:

b'\x01\x01\x00\x1f\xa4\x94Z\xe3\x03\x00'
b'\x01\x01\x01\x1f\xa4\x94Z\n\x00\x80'
b'\x01\x01\x02\x1f\xa4\x94Z\x06\x00\x00'
b'\x01\x01\x00 \xa4\x94Z\xe4\x03\x00'
b'\x01\x01\x01 \xa4\x94Z\n\x00\x80'
b'\x01\x01\x02 \xa4\x94Z\x06\x00\x00'
b'\x01\x01\x00!\xa4\x94Z\xe3\x03\x00'
b'\x01\x01\x01!\xa4\x94Z\t\x00\x80'

The thing is that the sensor manufacturer tutorial says that there should be 10 bytes, not 8 or even sometimes 7 as the output is pointing. I can prove that information using a simple node-red environment to show that the topic is really sending 10 bytes and never less then that and node-red always receives 10, and again, nothing less .

Node-red Debug node with "Complete Message Object" as Output

Why is this happening? How can i counter it?

Upvotes: 0

Views: 157

Answers (1)

hardillb
hardillb

Reputation: 59608

All the examples you have shown have exactly 10 bytes:

'\x01' '\x01' '\x00' '\x1f' '\xa4' '\x94' 'Z' '\xe3' '\x03' '\x00'
'\x01' '\x01' '\x01' '\x1f' '\xa4' '\x94' 'Z' '\n' '\x00' '\x80'
'\x01' '\x01' '\x02' '\x1f' '\xa4' '\x94' 'Z' '\x06' '\x00' '\x00'
'\x01' '\x01' '\x00' ' ' '\xa4' '\x94' 'Z' '\xe4' '\x03' '\x00'
'\x01' '\x01' '\x01' ' ' '\xa4' '\x94' 'Z' '\n' '\x00' '\x80'
'\x01' '\x01' '\x02' ' ' '\xa4' '\x94' 'Z' '\x06' '\x00' '\x00'
'\x01' '\x01' '\x00' '!' '\xa4' '\x94' 'Z' '\xe3' '\x03' \x00'
'\x01' '\x01' '\x01' '!' '\xa4' '\x94' 'Z' '\t' '\x00' '\x80'

It just happens that some of those bytes fall with in the normal character range e.g. !, Z, space, new line (\n) or tab (\t)

Upvotes: 1

Related Questions