Page 2 of 2

Re: Clever way to connect to Mqtt server

Posted: Sun Feb 28, 2021 5:26 pm
by pythoncoder
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.

Re: Clever way to connect to Mqtt server

Posted: Sun May 30, 2021 2:55 pm
by thalesmaoa
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!

Re: Clever way to connect to Mqtt server

Posted: Sun May 30, 2021 3:44 pm
by thalesmaoa
I figure that QoS is the problem, however can't understand why QoS 1 broke the connection.

Re: Clever way to connect to Mqtt server

Posted: Mon May 31, 2021 5:39 am
by pythoncoder
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.

Re: Clever way to connect to Mqtt server

Posted: Mon May 31, 2021 12:57 pm
by thalesmaoa
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)

Re: Clever way to connect to Mqtt server

Posted: Tue Jun 01, 2021 9:03 am
by pythoncoder
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.