Clever way to connect to Mqtt server
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Clever way to connect to Mqtt server
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.
Index to my micropython libraries.
-
- Posts: 35
- Joined: Thu Feb 13, 2020 10:10 pm
Re: Clever way to connect to Mqtt server
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.
However, every time a message arrives it just disconnect.
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
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!
-
- Posts: 35
- Joined: Thu Feb 13, 2020 10:10 pm
Re: Clever way to connect to Mqtt server
I figure that QoS is the problem, however can't understand why QoS 1 broke the connection.
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Clever way to connect to Mqtt server
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.
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.
Index to my micropython libraries.
-
- Posts: 35
- Joined: Thu Feb 13, 2020 10:10 pm
Re: Clever way to connect to Mqtt server
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)
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)
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Clever way to connect to Mqtt server
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.
Index to my micropython libraries.