How to deal with mqtt_as and multiple subscriptions?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
werner
Posts: 3
Joined: Wed Jul 06, 2022 5:44 pm

How to deal with mqtt_as and multiple subscriptions?

Post by werner » Wed Jul 06, 2022 6:25 pm

I want to extend the unclean.py example and added some subscriptions. This is working fine so far. My question is how to proceed with additional subscriptions? Is it possible to have multiple sub_cbs for each or is a message queue the better solution to process the messages?

Thanks in advance, Werner

ChrisO
Posts: 48
Joined: Mon Apr 06, 2020 6:16 pm

Re: How to deal with mqtt_as and multiple subscriptions?

Post by ChrisO » Thu Jul 07, 2022 6:34 am

I don’t think that’s how mqtt_as was built.
Generally you subscribe with a broad enough topic with the use of wildcard and you let the subs_cb filter the messages based on topic. If you process messages right when they come in, it usually doesn’t take that long, so I’m not sure what you want to use the queue for. Let’s just say that the Queue is not necessary per se and depends on your use case.
Chris

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

Re: How to deal with mqtt_as and multiple subscriptions?

Post by pythoncoder » Thu Jul 07, 2022 7:43 am

It is possible to subscribe to multiple topics but there can only be one subscription callback. To demo this, take the script range.py and adapt the connection handler as follows

Code: Select all

async def conn_han(client):
    await client.subscribe('foo_topic', 1)
    await client.subscribe('bar_topic', 1)
Then on your PC publish to both - I modified the pubtest script:

Code: Select all

#! /bin/bash
# mosquitto_sub -h 192.168.0.10 -t result
while :
do
    mosquitto_pub -h 192.168.0.10 -t foo_topic -m "gordon bennett" -q 1
    sleep 5
    mosquitto_pub -h 192.168.0.10 -t bar_topic -m "pete was here" -q 1
    sleep 5
done
The callback runs as expected:

Code: Select all

publish 50
Topic: "foo_topic" Message: "gordon bennett" Retained: False
publish 51
Topic: "bar_topic" Message: "pete was here" Retained: False
publish 52
Topic: "foo_topic" Message: "gordon bennett" Retained: False
Peter Hinch
Index to my micropython libraries.

werner
Posts: 3
Joined: Wed Jul 06, 2022 5:44 pm

Re: How to deal with mqtt_as and multiple subscriptions?

Post by werner » Thu Jul 07, 2022 8:26 am

Thank you for the fast response. I found also an starting point to deal with multiple topics and one subscription callback.
The question for me still is not to lose a message while processing another one. Is it possible to run these processes in the background?

Code: Select all

# multiple topics
def callback(topic, msg): 
    print((topic, msg))
    if topic == b"username/feeds/Temperature control":
        sub_cb_temp(topic, msg)
    else
        sub_cb_weight(topic, msg)

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

Re: How to deal with mqtt_as and multiple subscriptions?

Post by pythoncoder » Thu Jul 07, 2022 8:35 am

It might be worth you looking at the uasyncio tutorial as this question is a generic query about asynchronous programming.

In short, the callback should run to completion quickly but it can launch asynchronous tasks using uasyncio.create_task(). These will run "in the background".
Peter Hinch
Index to my micropython libraries.

werner
Posts: 3
Joined: Wed Jul 06, 2022 5:44 pm

Re: How to deal with mqtt_as and multiple subscriptions?

Post by werner » Thu Jul 07, 2022 9:44 am

This looks like I was searching for. I will check out this tutorial. Thank you.

Post Reply