← index #357Issue #908
Related · high · value 1.943
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

AttributeError: 'NoneType' object has no attribute 'write'

closedby Ugthakopened 2024-08-14updated 2024-08-14

Hello there,

I'm currently trying to send a message from my Raspberry Pi Pico W to my Node-Red through MQTT.simple with the following snippet of code:

while True:
pin_state = gpio_pin.value()
print(f"Pin state: {pin_state}")
client.publish(MQTT_TOPIC, str(pin_state))
time.sleep(1)

"pin_state" is working as intended so I have no problems collecting the data I wish to but when I run the program I always get the error:
AttributeError: 'NoneType' object has no attribute 'write'

there was a issue previously where someone was suffering from the same issue and the two replies stated that you needed to connect to the broker first, which I have confirmed I am doing, and that it's possible that there isn't a connection to the broker at all; I believe there to be a connection between my Pi and the Broker because no errors are presented and the node within Node-Red does react to the connection.

If any more information is needed to help me, please do ask and I look forward to any help given!

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