MQTT + HTTP Server simultaneously

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
Gudvin
Posts: 2
Joined: Tue Dec 24, 2019 8:24 pm

MQTT + HTTP Server simultaneously

Post by Gudvin » Tue Dec 24, 2019 8:53 pm

Hello
I run a mosquitto broker on a Raspberry Pi 3 board which can be turned "on" and "off" by a NodeMcu board. NodeMcu is a MQTT Client and uses this protocol to communicate with Raspberry Pi 3. Further I need a http server on my NodeMcu in order to turn on and off a Raspberry Pi board via "http" protocol. I faced a following problem: I have to run the both http server as well as MQTT Client at the same time on NodeMCU board. I cannot do it because they both enter a blocking infinite loop. In a PC world i would use a separate Thread for each instance, but i don't think it is a good idea to implement this way on a such resource-sensitive board like NodeMCU. Any idea how to get running both instances in "parallel" or "semi-parallel"?

kevinkk525
Posts: 969
Joined: Sat Feb 03, 2018 7:02 pm

Re: MQTT + HTTP Server simultaneously

Post by kevinkk525 » Wed Dec 25, 2019 7:30 am

You use a webserver that uses uasyncio and the async mqtt client https://github.com/peterhinch/micropython-mqtt or you use a webserver that uses _thread in which case you have blocking loops in different threads.
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

Gudvin
Posts: 2
Joined: Tue Dec 24, 2019 8:24 pm

Re: MQTT + HTTP Server simultaneously

Post by Gudvin » Wed Dec 25, 2019 4:58 pm

kevinkk525 wrote:
Wed Dec 25, 2019 7:30 am
You use a webserver that uses uasyncio and the async mqtt client https://github.com/peterhinch/micropython-mqtt or you use a webserver that uses _thread in which case you have blocking loops in different threads.
Thank you kevink525 uasyncio would be a perfect solution for me.

Post Reply