Clever way to connect to Mqtt server

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Clever way to connect to Mqtt server

Post by pythoncoder » Sun Feb 28, 2021 5:26 pm

That is how to do it. But beware of calling it at a high rate, especially at qos==1, otherwise you will instantiate large numbers of concurrent tasks.
Peter Hinch
Index to my micropython libraries.

thalesmaoa
Posts: 35
Joined: Thu Feb 13, 2020 10:10 pm

Re: Clever way to connect to Mqtt server

Post by thalesmaoa » Sun May 30, 2021 2:55 pm

Hi Peter, I notice some behaviour that I can't find a way to fix. I've cleared all my code to the provided example.

Code: Select all

# MQTT
def callback(topic, msg, retained):
    #print((topic, msg, retained))
    if topic == b'door/front':
        if msg == b'password':
            print('ESP received hello message')

async def conn_han(client):
    await client.subscribe(topic_sub, 1)

# Main
async def main(client):
    await client.connect()
    while True:
        await asyncio.sleep(2)

# Start flow here
config['subs_cb'] = callback
config['connect_coro'] = conn_han
config['server'] = SERVER

MQTTClient.DEBUG = True  # Optional: print diagnostic messages
client = MQTTClient(config)
try:
    asyncio.run(main(client))
finally:
    client.close()  # Prevent LmacRxBlk:1 errors
However, every time a message arrives it just disconnect.

Code: Select all

RAM free 93776 alloc 17392
RAM free 93776 alloc 17392
ESP received hello message
Checking WiFi integrity.
Got reliable connection
Connecting to broker.
Connected to broker.
Reconnect OK!

thalesmaoa
Posts: 35
Joined: Thu Feb 13, 2020 10:10 pm

Re: Clever way to connect to Mqtt server

Post by thalesmaoa » Sun May 30, 2021 3:44 pm

I figure that QoS is the problem, however can't understand why QoS 1 broke the connection.

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

Re: Clever way to connect to Mqtt server

Post by pythoncoder » Mon May 31, 2021 5:39 am

QoS == 1 should not have this effect. We have run QoS == 1 communications for periods measured in weeks with only rare reconnections. However reconnections will occur if operating at or near the limit of radio range.

The first thing I would try is to determine the quality of the physical WiFi link. I would run the ESP32 in the same room as the access point and see if the fault still occurs. This would indicate whether there are wireless issues.
Peter Hinch
Index to my micropython libraries.

thalesmaoa
Posts: 35
Joined: Thu Feb 13, 2020 10:10 pm

Re: Clever way to connect to Mqtt server

Post by thalesmaoa » Mon May 31, 2021 12:57 pm

That was my first try. I was sure that the problem was signal and defect ESP32, but is not.
I also followed issue #51 over github, but I think is not the same.

ESP32 / MQTT Server (whatever) => QoS = 0 (no disconnect)
ESP32 QoS = 1 / MQTT Server QoS = 1 (no disconect)
ESP32 QoS = 1 / MQTT Server QoS = 2 (disconnect)

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

Re: Clever way to connect to Mqtt server

Post by pythoncoder » Tue Jun 01, 2021 9:03 am

QOS == 2 is not supported by mqtt_as. If a client running mqtt_as subscribes to a QOS == 2 publication the outcome is undefined. We have not tested this case.
Peter Hinch
Index to my micropython libraries.

Post Reply