Subscription to topic with Paho python not getting messages


#1

Hi:

I’m making a small app to send messages from a remote location to a home server. An decided to go with python scripts in both ends. So created a subscription and a publish script. Publish script is working fine. I can even see the publish messages in the panel.

But the subscription script is just connecting to MQTT and nothing more. Once i was able to receive the messages from the publish script, but no more.

I have 2 different users with a TX and RX client-id and they should"crosstalk" to each other (TX-user1 to RX-user2) But so far can’t seem to find the reason why it is not happening any more. They are communicating to a common topic: test/dummy

#!/usr/bin/python
# coding: utf8

import keyring
import paho.mqtt.client as mqttc

broker_url = "node02.myqtthub.com"
broker_port = 1883
clean_session = True
topic = "test/dummy"
client_id = "mqtt-user2-rx"
username = 'mqtt-user2'
password = keyring.get_password("system", username)


def on_connect(client, userdata, flags, rc):
    if rc == 0:
        print("Connected to MQTT Broker!")
    else:
        print("Failed to connect, return code %d\n", rc)


def on_subscribe(client, userdata, mid, granted_qos):
    print("Subscribed: "+str(mid)+" "+str(granted_qos))


def on_message(client, userdata, msg):
    print(msg.topic+" "+str(msg.qos)+" "+str(msg.payload))


def on_log(client, userdata, level, string):
    print(string)


client = mqttc.Client(client_id, clean_session)
client.username_pw_set(username, password)
client.on_message = on_message
client.on_connect = on_connect
client.on_subscribe = on_subscribe
client.on_log = on_log
client.connect(broker_url, broker_port, 15)
client.subscribe(topic, qos=1)

client.loop_forever()

Could you guys help me here? Have being struggling with this code for a week now.

Regards,
acaminiti


#2

Hi:

So found my answer in paho documentation:

Simple string and subscribe options (MQTT v5.0 only)
e.g. subscribe(“my/topic”, options=SubscribeOptions(qos=2))
topic: A string specifying the subscription topic to subscribe to. qos: Not used. options: The MQTT v5.0 subscribe options. properties: a Properties instance setting the MQTT v5.0 properties to be included. Optional - if not set, no properties are sent.

Good to know that aspl MyQttHub service is on MQTT v5.0

PS: Can provide better python code examples when i have it ready. Let me know if interested.

Regards,
acaminiti


#3

Hi there,

Brief summary

You have to subscribe inside on_connect to ensure you receive CONNACK before sending SUBSCRIBE.

Long description:

If you check how your code runs, you will see the following:

    image

That is, the example cannot work because client code is sending SUBSCRIBE before having received CONNACK. Though there is work to support this MQTT-4, we believe current MQTT v3.1.1 does not allows this (and that’s the approach our MQTT engine has followed).

This also reflects in example shown in PAHO client documentation where they subscribe inside on_connect (right after receiving CONNACK indication): https://pypi.org/project/paho-mqtt/#id4:

Best Regards,
With this identified, we updated code example to move client.subscribe as indicated:

    image

With this change, log for code example now looks like this:

    image

After this change you can see how subscription is working by taking a look at the subscription tab. You should see your subscription (and the rest):


#4

Hello:

Indeed, changing the location of the subscription works. But then i wonder why it worked with the SubscribeOption that day. Thanks!

Regards,
acaminiti