mqtt_as on Pico W - connection error to HiveMQ Cloud Broker

Questions and discussion about running MicroPython on a micro:bit board.
Target audience: MicroPython users with a micro:bit.
Post Reply
karl60
Posts: 2
Joined: Thu Aug 18, 2022 7:40 pm

mqtt_as on Pico W - connection error to HiveMQ Cloud Broker

Post by karl60 » Fri Aug 19, 2022 7:31 pm

(Updated and clarified question giving code)

I can pub/sub simultaneously using the HiveMQ Cloud broker using:
  • * Mosquitto_pub and _sub at the command line
    * Standard python on a laptop using HiveMQ example code
    * Windows MQTT Explorer app
In short, I'm happy the HiveMQ Broker side of things is working OK.

I can't get a Pico W using mqtt_as to connect to the HiveMQ Broker using this code:

Code: Select all

from mqtt_as import MQTTClient, config
import uasyncio as asyncio
from secrets import secrets

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

async def conn_han(client):
    await client.subscribe('foo_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('result', '{}'.format(n), qos = 1)
        n += 1

config['subs_cb'] = callback
config['connect_coro'] = conn_han
config['ssid'] = secrets['ssid']
config['wifi_pw'] = secrets['password']
config['server'] = secrets['hivemq_broker']
config['port'] = secrets['port']
config['user'] = secrets['hivemq_username']
config['password'] = secrets['hivemq_key']

MQTTClient.DEBUG = True  # Optional: print diagnostic messages
client = MQTTClient(config)
try:
    asyncio.run(main(client))
finally:
    client.close()  # Prevent LmacRxBlk:1 errors
This gives:

Code: Select all

>>> %Run -c $EDITOR_CONTENT
Checking WiFi integrity.
Got reliable connection
Connecting to broker.
Traceback (most recent call last):
  File "<stdin>", line 45, in <module>
  File "uasyncio/core.py", line 1, in run
  File "uasyncio/core.py", line 1, in run_until_complete
  File "uasyncio/core.py", line 1, in run_until_complete
  File "<stdin>", line 14, in main
  File "mqtt_as.py", line 589, in connect
  File "mqtt_as.py", line 270, in _connect
  File "mqtt_as.py", line 176, in _as_read
OSError: (-1, 'Connection closed by host')
Any suggestions as to what I'm doing wrong?
Thanks

User avatar
karfas
Posts: 192
Joined: Sat Jan 16, 2021 12:53 pm
Location: Vienna, Austria

Re: mqtt_as on Pico W - connection error to HiveMQ Cloud Broker

Post by karfas » Fri Aug 19, 2022 8:26 pm

When this happens initially - during connect(), I would check all network parameters and would analyze on network level:
- Firewall (and logs)
- Packet sniffer
- Broker logs
- Encryption required/enabled ?
- a few debug prints in MQTTClient

If the initial connection succeeds, there must be a reason the broker cuts the connection.
- Bad responses from your client (the different QOS settings ?)
- Network problems?
A few hours of debugging might save you from minutes of reading the documentation! :D
My repositories: https://github.com/karfas

Post Reply