I'm running this code:
Code: Select all
def callback(topic, msg, retained):
print((topic, msg, retained))
async def conn_han(client):
await client.subscribe('test/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('test/topic', '{}'.format(n), qos = 1)
n += 1
settings.networking['mqtt']['subs_cb'] = callback
settings.networking['mqtt']['connect_coro'] = conn_han
MQTTClient.DEBUG = True # Optional: print diagnostic messages
client = MQTTClient(settings.networking['wifi'],settings.networking['mqtt'])
try:
asyncio.run(main(client))
finally:
client.close() # Prevent LmacRxBlk:1 errors
client=MQTTClient(settings.networking['wifi'],settings.networking['mqtt'])
It does connect to the wifi, connect to the broker, subscribes to the topic & publish messages.
Only thing it does NOT do is print the published messages on the topic in the callback function.
The broker (mosquitto) is working. Its running in the local wifi and I can connect, publish & subscribe with an app from my mobile.
The ESPs subscription & publishing are logged by the broker. I'm not getting any fault messages.
So everything works but the callback. I'm confused.
Thanks for your help.