← index #948Issue #357
Off-topic · high · value 2.210
QUERY · ISSUE

umqtt.simple using websockets

openby Anonymouse83opened 2024-12-12updated 2024-12-16

I have an existing - working - MQTT application using Paho-MQTT and Websockets.
I am working on a new device, which lacks the memory to use Paho-MQTT, but I cannot get umqtt.simple to work with Websockets.
The only example I could find showed setting up the websocket, then setting e.g. cleitn=MQTTClient(<clientid>, <server>, sock=websocket, port=<port>), which fails irrespective of how "sock" is set.
Is there a way to use the websocket protocol with umqtt.simple?
Many thanks,

1 comment
jomasnash · 2024-12-16

The official umqtt.simple from micropython-lib does not support setting the 'sock'.
Only client_id,server, port, user, password, keepalive, ssl is possible.

Also umqtt.simple uses a normal socket. When using Websockets, all MQTT packets are wrapped in a Websocket packet so it is not possible, without significant modifications, to use it in umqtt.simple.

I have made a async MQTT-client using Websockets. I use a patched version of aiohttp. The modifications to aiohttp were necessary because aiohttp does not accept headers (which are needed for almost every broker). Even then, aiohttp can still be blocking because the used getaddrinfo is still a blocking function, therefore I use my own developed, fully async, version of getaddrinfo.

CANDIDATE · 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 .

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