MIcropython as MQTT broker

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
james_km69
Posts: 9
Joined: Wed Aug 15, 2018 8:31 pm

MIcropython as MQTT broker

Post by james_km69 » Wed Aug 15, 2018 8:43 pm

Hi, I am new to MQTT. I have it working with my ESP32 broad with my windows (or Rasb) as Mqtt broker.
wondering if it is possible to have the same ESP32 micro controller to be a mqtt broker (running Micro Python on it)
If so, any direction on how to do this?

Thanks,
James

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: MIcropython as MQTT broker

Post by pythoncoder » Thu Aug 16, 2018 8:20 am

I don't think this has been done. It would be quite involved and I'm not sure there would be much point. Why not just run mosquitto on a Raspberry Pi?
Peter Hinch
Index to my micropython libraries.

james_km69
Posts: 9
Joined: Wed Aug 15, 2018 8:31 pm

Re: MIcropython as MQTT broker

Post by james_km69 » Fri Aug 17, 2018 11:58 am

Thanks pythoncoder for the reply. I was just wondering. My goal was to eliminate at least one hardware from the system

OutoftheBOTS_
Posts: 847
Joined: Mon Nov 20, 2017 10:18 am

Re: MIcropython as MQTT broker

Post by OutoftheBOTS_ » Fri Aug 17, 2018 7:32 pm

If your connected to the internet then their is always cloud brokers :)

xenobius
Posts: 2
Joined: Fri Jan 22, 2021 8:04 am

Re: MIcropython as MQTT broker

Post by xenobius » Sat Jan 23, 2021 9:03 pm

But the thing is that you may want (like I actually do):

1. To have a system independent of cloud brokers simply because internet is not an option. For agriculture projects, you have situations where internet is not feasible because its either too far away from the nearest internet provider and they simply wont offer you internet - or because its just not worth it paying for internet only to be used for multiple tiny modules to communicate effectively together!

2. Custom boards with custom sizes for custom enclosures cannot always make use of the bulky raspberry pi. Sure enough you could now use the MicroPI but once again that a board on its own that needs to be "inserted" into your custom board. Perhaps its more ideal to have just 1 board and so raspberry pi is not an option either.

User avatar
tonazza
Posts: 2
Joined: Mon Feb 08, 2021 4:25 pm

Re: MIcropython as MQTT broker

Post by tonazza » Mon Feb 08, 2021 4:37 pm

Hello,

I was asking myself the same question that james_km69 asked :)
In my case I was actually searching for a MQTT broker library for the ESP-IDF SDK to be used with ESP32 hardware, but even micropython would have done the trick.
I've done some research and unfortunately there is no easy existing solution... :cry:

I'm working on a IoT project that has some restrictions (much like the latest xenobius post), and I'm thinking of going in the following direction: the devices will "speak" between them using websockets (and not MQTT), and in case there is an active internet connection, the system status will be backed up to a cloud MQTT broker by one "chosen " device, which will be the only MQTT client on the devices network.
In this way, the need for a local broker is removed.

Of course this is no elegant solution, but the alternative would be porting the Mosquitto code to ESP-IDF FreeRTOS.... :o

Andrea

User avatar
tonazza
Posts: 2
Joined: Mon Feb 08, 2021 4:25 pm

Re: MIcropython as MQTT broker

Post by tonazza » Tue Feb 09, 2021 2:39 pm

EDIT: just FYI, a starting solution does exist for ESP-IDF (not Micropython), using a library called mongoose
The company who develops mongoose also proposes a full embedded firmware called Mongoose OS, which is basically an alternative to micropython, having a limited free edition and a full paid commercial edition.

...still not tried the above, but at least a starting point exists!

User avatar
rubydesign
Posts: 3
Joined: Wed Mar 31, 2021 1:19 pm

Re: MIcropython as MQTT broker

Post by rubydesign » Tue Apr 27, 2021 1:43 pm

pythoncoder wrote:
Thu Aug 16, 2018 8:20 am
I don't think this has been done. It would be quite involved and I'm not sure there would be much point. Why not just run mosquitto on a Raspberry Pi?
So i am also new and trying to get a broker on an esp. But not because i am just wondering, because i have a larger network than a star. I am still evaluating how much it would cost to get internet access (pi based broker) to every esp, but at the moment i am assuming too much (600m2, 60m corridor, 20 rooms, 40cm stone walls, 40 esps, 1 pi4)

So if i actually manage the network connections in a mesh, and manage a station and AP on every esp (with a single root).
What is to stop me using the station to publish (upstream) and the AP to subscribe (downstream).
I could just pass messages that are meant to go down the mqtt tree down (from ap to station) and pass the ones that need to go up, up (ie from station to ap)

I guess this one goes to @pythoncoder , having written micropython-mqtt that could be used for this.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: MIcropython as MQTT broker

Post by pythoncoder » Tue Apr 27, 2021 5:08 pm

rubydesign wrote:
Tue Apr 27, 2021 1:43 pm
...
I guess this one goes to @pythoncoder , having written micropython-mqtt that could be used for this.
Writing a broker is a substantial task, emphatically not one I intend to undertake.

I simply can't see a point to the exercise. I have an original Raspberry Pi V1 running mosquitto. It uses 5W of power, runs 24/7 and never falls over. I don't believe ESP32 is capable of that level of reliability. The Pi connects via the wired network, but that's easily fixed if everything's to be on WiFi. Just use a Raspberry Pi Zero W.
Peter Hinch
Index to my micropython libraries.

User avatar
rubydesign
Posts: 3
Joined: Wed Mar 31, 2021 1:19 pm

Re: MIcropython as MQTT broker

Post by rubydesign » Fri Apr 30, 2021 8:46 pm

pythoncoder wrote:
Tue Apr 27, 2021 5:08 pm
I simply can't see a point to the exercise. I have an original Raspberry Pi V1 running mosquito. It uses 5W of power, runs 24/7 and never falls over. I don't believe ESP32 is capable of that level of reliability. The Pi connects via the wired network, but that's easily fixed if everything's to be on WiFi. Just use a Raspberry Pi Zero W.
The point is simply the size (see above). At the moment I can not see how to get a star topology )ie all esps connecting to the same PI) without buying 10 wifi routers. And for 40 esps i would probably need more than a pi zero, but that is ok.

I understand that writing a broker from scratch is too much. I was just trying to explore a sort of pass-through idea. A bit like the bridge that you coded. "Just" have both station and AP on each esp, and if the esp is not connected to the router (ie to another esp) then pass the traffic through.
I tough this could maybe be done by changing the init code of what you wrote a bit. And then connecting the "pipes", in to out and out to in.

But I guess this will be too difficult. And maybe especially in python pushing the limits of the esp.
I would even consider using the pi zero wifi, but with that weird 1 item per customer, with shipping, those cost 4 times more (always x 40), so that is not better than the many router option.

Thanks though

Post Reply