Skip to content

can't await mqtt.simple publish method #357

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
alirezaimi opened this issue Oct 22, 2019 · 2 comments
Open

can't await mqtt.simple publish method #357

alirezaimi opened this issue Oct 22, 2019 · 2 comments

Comments

@alirezaimi
Copy link

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 ?

@2niuhe
Copy link

2niuhe commented Jan 6, 2020

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
Copy link
Author

@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 .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants