Sunday, November 6, 2016

Mqtt broker on ESP8266 #5

[Later edit]. On you will find instructions on how to install the ESP8266 MQTT broker on your EPS8266 and how to use the MQTT service provided by

I've accepted Anthony's challenge to squeeze more from the ESP8266 as MQTT broker so I managed to increase the maximum number of simultaneous subscribers on a single ESP8266. 

Now instead of 9 like I had before ( plus bridge to/from cloud and websockets to connect my mobile app to ESP8266 MQTT broker) I have 20 stable subscribers.

Top was 30 but the heap memory was too low to have flawless connections to all subscribers.

Also a good news is that this is not limiting the number of topics a device can subscribe to the broker ( in a reasonable number of 4 or 5 topics per device).

A picture with 25 connections and one with websocket.

ESP8266 as MQTTbroker 

At the beginning I have 35Kb and after 25+1+1 connections I am ending with 11 to 12 Kb of heap memory which is plenty even for bigger payloads.

Related links:
Mqtt broker on ESP8266 #4
Mqtt broker on ESP8266 #3
Mqtt broker on ESP8266 #2
Mqtt broker on ESP8266 #1


  1. Sounds like a interesting project with a lot of potential if you would build on it. So I was wondering if you have thought anything about using a Mesh networking concept to setup a distributed system of MQTT brokers that could share the load and that way both get redundant MQTT brokers and allow more clients to connect to that MQTT mesh?

    That is, I don’t like the idea of relying of a cloud service as I want everything to always be 100% locally controlled on my local network, however I think that it would be very cool if you or someone else made a firmware for ESP8266 devices that allowed several devices such as Sonoff to automagically setup their own self-sustained and self-healing mesh-network nodes of MQTT brokers and clients so that all devices talked each other, working totally on their own without the need for a cloud service or a central server with MQTT broker on a Raspberry or PC.

    With a such distributed system you could in theory support many more clients if you could get several of them to act as a MQTT brokers to share the load with each other. That way each device would as a “mesh node” act as both as a server and as a client in the distributed system and the more devices you add then the more numbers of clients you could connect. As even if each ESP8266 had a MQTT broker that could only connect to 3 MQTT clients and another 2 MQTT brokers then adding more devices would exponentially grow the amount of MQTT client you can connect. Having for example 20 devices would allow you to connect 60 MQTT clients in total and that mesh-network would have 20 MQTT brokers for redundancy, all sharing the total load of the mesh.

    If you could also create add some kind of roaming master-slave high availability or cluster service that could keep track of which device currently acts as MQTT broker master coordinator/controller and which are MQTT broker slaves then the distributed system would have redundancy so that one of the devices would always be the MQTT broker master coordinator/controller , and if that goes down then the other MQTT broker slaves would re-elect a new master coordinator/controller . Just like in a standard high availability or cluster service. You then normally have one roaming VIP (Virtual IP) that the current master gets as a second IP so that you do not need to hunt for which one is the current master.

    That way users would not have to rely on a cloud service or setup their own server with MQTT broker as all devices would act as both servers and clients, with all devices.

  2. Replies
    1. YEs, go to and make an account.

  3. Hi.. been following your work for quite some time. Is it possible to make this mqtt broker library public.

    1. Hi, Even better, the full application is at Just flash the binary to a new 4Mb ESP8266 and you have a functional MQTT broker in 1 minute. If you go to you a have code and binary for a ESP8266 socket(plug) that integrates with the and Bondar ESP8266 broker. You can use the demoapp as a starter for any device type you want. In 30 minutes you have a fully functional solution.

  4. Any Chance to port this project to ESP32 ??

    1. This project actually started on ESP31B then was moved to ESP8266. If I will have some time I will ported back to ESP32.

    2. Thanks for the reply.. this will be a good improvement. I think the hardware from esp32 will handle with more connections and clients...

  5. Can you provide the source code of bondar ? I search on your github account, but we can find only BLOB.
    I have tried another MQTT broker (, but I have some stability problems (no more responding after 1 day, and the watchdog doesn't trig).
    On my MQTT broker, I am using a BMP680 sensor, and the MQTT broker can publish the temperature and pressure values. I don't want another esp8266 device for the sensor.
    I have also a SONOFF Pro 4 CH with the tasmota firmware, and it is working very well.
    Depending the temperature, I publish some order to from my MQTT broker to my SONOFF MQTT client.