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
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
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')
Thanks