← index #357Issue #112
Off-topic · high · value 2.029
QUERY · ISSUE

can't await mqtt.simple publish method

openby alirezaimiopened 2019-10-22updated 2020-01-07

Hi
I'm using umqtt.simple and there is problem when i await mqtt.publish in my code !
'NoneType' object isn't iterable
Is there any way to solve this issue ?
Or use other lib like https://github.com/peterhinch/micropython-mqtt/blob/master/mqtt_as ?

2 comments
2niuhe · 2020-01-06

The umqtt.simple module is sync.You'd better use peterhinch's uasync mqtt lib.

While you only need subscribe to some topic.
my solution is :

  • use check_msg() instead of wait_msg(),cause check_msg() return immediately.
import uasyncio
from umqtt.simple import MQTTClient

loop = uasyncio.get_event_loop()
async def my_callback(topic,msg):
   await uasyncio.sleep(1)
   
def sub_callback(topic, msg):
   global loop
   loop.create_task(my_callback(topic,msg))

async def my_main():
   c = MQTTClient("umqtt_client", server)
   c.set_callback(sub_callback)
   c.connect()
   c.subscribe(b'foo_con')
   pass

loop.creat_task(my_main())
loop.run_forever()
alirezaimi · 2020-01-07

@2niuhe thanks, but using umqtt_async have its own problem : https://forum.micropython.org/viewtopic.php?f=15&t=7144

thanks for your solution but i had problem in publish part not others .

CANDIDATE · ISSUE

Unconnected MQTT publish fails silently

closedby mkarlineropened 2016-10-30updated 2024-08-25

If I connect up an MQTT client and publish (sucessfully), then stop the broker, subsequent publishes fail without error. I'm not sure if this is the desired functionality or not. In fact, its a bit opaque to me how robust achieves its reconnection functionality.

connected with mihome, channel 6
dhcp client start...
ip:192.168.10.185,mask:255.255.255.0,gw:192.168.10.1

from simple import MQTTClient
c = MQTTClient("adcdd", "192.168.10.195") # Broker not running
c.connect()
0
c.publish("foo", "Hello MQTT")
..# Stop broker
c.publish("foo", "Hello MQTT")

No exception....

5 comments
pfalcon · 2016-10-30

In fact, its a bit opaque to me how robust achieves its reconnection functionality.

It doesn't have to be like that - code is at every MicroPython user's fingertips and simple to understand: https://github.com/micropython/micropython-lib/blob/master/umqtt.robust/umqtt/robust.py#L29

I'll look into the main topic of this issue when time permits.

mkarliner · 2016-10-30

Sorry, me being unclear. Looking at the code, I wonder how robust would reconnect in a situation other than a publish failure, ie: loss of connection with subscribe only?

pfalcon · 2016-10-30

Subscription requires running event loop (wait_msg() function). It's also protected in a similar fashion to publish(): https://github.com/micropython/micropython-lib/blob/master/umqtt.robust/umqtt/robust.py#L37

mkarliner · 2016-10-30

Thanks for the explanation, obvious now you say.

jonnor · 2024-08-25

The question seems to have been resolved. Proposing to close.

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied