micropython / micropython-lib

Core Python libraries ported to MicroPython
Other
2.43k stars 1k forks source link

can't await mqtt.simple publish method #357

Open alirezaimi opened 5 years ago

alirezaimi commented 5 years ago

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 commented 4 years ago

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 :

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 commented 4 years ago

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