mqtt_as subscribe problem

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
poesel
Posts: 22
Joined: Sun Oct 20, 2019 4:58 pm

mqtt_as subscribe problem

Post by poesel » Sun Jun 27, 2021 9:26 am

Hi,

I'm running this code:

Code: Select all

def callback(topic, msg, retained):
    print((topic, msg, retained))

async def conn_han(client):
    await client.subscribe('test/topic', 1)

async def main(client):
    await client.connect()
    n = 0
    while True:
        await asyncio.sleep(5)
        print('publish', n)
        # If WiFi is down the following will pause for the duration.
        await client.publish('test/topic', '{}'.format(n), qos = 1)
        n += 1

settings.networking['mqtt']['subs_cb'] = callback
settings.networking['mqtt']['connect_coro'] = conn_han

MQTTClient.DEBUG = True  # Optional: print diagnostic messages
client = MQTTClient(settings.networking['wifi'],settings.networking['mqtt'])
try:
    asyncio.run(main(client))
finally:
    client.close()  # Prevent LmacRxBlk:1 errors


client=MQTTClient(settings.networking['wifi'],settings.networking['mqtt'])
on an ESP32. Its more or less straight from Peters tutorial (I've only split mqtt & wifi settings).
It does connect to the wifi, connect to the broker, subscribes to the topic & publish messages.
Only thing it does NOT do is print the published messages on the topic in the callback function.

The broker (mosquitto) is working. Its running in the local wifi and I can connect, publish & subscribe with an app from my mobile.
The ESPs subscription & publishing are logged by the broker. I'm not getting any fault messages.

So everything works but the callback. I'm confused.

Thanks for your help.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: mqtt_as subscribe problem

Post by pythoncoder » Sun Jun 27, 2021 9:40 am

Just guessing here but I wonder if the problem somehow relates to 'test/topic'? It might be worth trying a name without a '/'.
Peter Hinch
Index to my micropython libraries.

poesel
Posts: 22
Joined: Sun Oct 20, 2019 4:58 pm

Re: mqtt_as subscribe problem

Post by poesel » Sun Jun 27, 2021 10:48 am

Ok, switched the topic to 'qwe' and found out, that, opposed to what I wrote before, the subscription does not work.
Code:

Code: Select all

def callback(topic, msg, retained):
    print((topic, msg, retained))

async def conn_han(client):
    print('subscribing...')
    await client.subscribe('qwe', 1)

async def main(client):
    await client.connect()
    n = 0
    while True:
        await asyncio.sleep(5)
        print('publish', n)
        # If WiFi is down the following will pause for the duration.
        await client.publish('qwe', '{}'.format(n), qos = 1)
        n += 1

settings.networking['mqtt']['subs_cb'] = callback
settings.networking['mqtt']['connect_coro'] = conn_han

MQTTClient.DEBUG = True  # Optional: print diagnostic messages
client = MQTTClient(settings.networking['wifi'],settings.networking['mqtt'])
try:
    asyncio.run(main(client))
finally:
    client.close()  # Prevent LmacRxBlk:1 errors


client=MQTTClient(settings.networking['wifi'],settings.networking['mqtt'])
This line 'print('subscribing...')' is never executed. Did I forget something?

poesel
Posts: 22
Joined: Sun Oct 20, 2019 4:58 pm

Re: mqtt_as subscribe problem

Post by poesel » Sun Jun 27, 2021 6:19 pm

Ok, it was of course my fault. I had messed up the configuration and with it the assignment of the coros.
Now everything works.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: mqtt_as subscribe problem

Post by pythoncoder » Mon Jun 28, 2021 9:05 am

Good! Out of interest, do topic names with a "/" work OK? I have never tested these although they are legal in MQTT.
Peter Hinch
Index to my micropython libraries.

kevinkk525
Posts: 969
Joined: Sat Feb 03, 2018 7:02 pm

Re: mqtt_as subscribe problem

Post by kevinkk525 » Mon Jun 28, 2021 10:25 am

I have topics like home/binary_sensor/77c0b900/status/config and everything works just fine.
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

poesel
Posts: 22
Joined: Sun Oct 20, 2019 4:58 pm

Re: mqtt_as subscribe problem

Post by poesel » Mon Jun 28, 2021 5:41 pm

'/' works for me, too.

kevinkk525
Posts: 969
Joined: Sat Feb 03, 2018 7:02 pm

Re: mqtt_as subscribe problem

Post by kevinkk525 » Mon Jun 28, 2021 6:13 pm

The only thing one has to be careful about is to use bytes and not strings. If the topics/messages are short, it will work either way. But if they are longer then the topics/messages will break the communication because they might get sent in multiple operations and therefore split into multiple parts which will not work correctly in some cases (e.g. special characters). So as long as one uses bytes as topics, any topic should work.
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: mqtt_as subscribe problem

Post by pythoncoder » Tue Jun 29, 2021 12:25 pm

Thanks both. It's one of those things I intended to test, but never got around to doing.
Peter Hinch
Index to my micropython libraries.

Post Reply