I am trying to connect my raspberry pi Pico W to a virtual machine which has a mqtt broker in it and a node red server. My MQTT broker is password protected. I have added the MQTT broker IP in the MQTT node on node red and the connection runs fine on node red. Now I am trying to write a micro-python code using thonny in the pi chip to publish a message to the server. I have been trying for day but my code is not able to connect my device to the mosquitto broker it is not able to get the CONNACK signal from the broker.
this is my main code
from mqtt_as import MQTTClient, config
import uasyncio as asyncio
import socket
# Local configuration
config['ssid'] = 'user'
config['wifi_pw'] = 'password'
config['server'] = '192.168.122.1' # Change to suit e.g. 'iot.eclipse.org'
async def messages(client): # Respond to incoming messages
async for topic, msg, retained in client.queue:
print((topic, msg, retained))
async def up(client): # Respond to connectivity being (re)established
while True:
await client.up.wait() # Wait on an Event
client.up.clear()
await client.subscribe('Nodered', 0) # renew subscriptions
async def main(client):
print("Hello")
await client.connect()
print("Hello")
for coroutine in (up, messages):
asyncio.create_task(coroutine(client))
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('Nodered', 'hello there', qos = 0)
n += 1
config["queue_len"] = 1 # Use event interface with default queue size
MQTTClient.DEBUG = True # Optional: print diagnostic messages
client = MQTTClient(config)
try:
asyncio.run(main(client))
finally:
client.close() # Prevent LmacRxBlk:1 errors
the mqtt_as python code I have downloaded from a website. the code is big, so here is the link
https://github.com/peterhinch/micropython-mqtt/blob/master/mqtt_as/mqtt_as.py
and I am getting this error
MPY: soft reboot
Hello
Checking WiFi integrity.
Got reliable connection
Connecting to broker.
Traceback (most recent call last):
File "<stdin>", line 38, 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 22, in main
File "/lib/mqtt_as.py", line 645, in connect
File "/lib/mqtt_as.py", line 314, in _connect
File "/lib/mqtt_as.py", line 213, in _as_read
OSError: (-1, 'Timeout on socket read')
I have followed every instruction from the above link and every other website I can find. please help me. I know there might be a silly mistake but please help I have spend a lot of time in it.