PicoMQTT – An MQTT Client/Broker library for ESP8266 and ESP32
PicoMQTT is a lightweight MQTT library for Arduino/PlatformIO optimized for ESP8266 and ESP32. It not only supports the MQTT Client mode like most existing solutions but also the MQTT Broker mode which transforms an ESP8266 or ESP32 board into an MQTT gateway replacing a Raspberry Pi board or an IoT gateway typically used for this task.
The library follows MQTT 3.1.1 specification, supports the publishing and consuming of arbitrary-sized messages, can deliver thousands of messages per second, and supports easy integration with the ArduinoJson library to publish and consume JSON messages.
MQTT Broker code example:
#include <Arduino.h>
#include <PicoMQTT.h>
PicoMQTT::Server mqtt;
void setup() {
// Usual setup
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin("MyWiFi", "password");
// Subscribe to a topic pattern and attach a callback
mqtt.subscribe("#", [](const char * topic, const char * payload) {
Serial.printf("Received message in topic '%s': %s\n", topic, payload);
});
// Start the broker
mqtt.begin();
}
void loop() {
// This will automatically handle client connections. By default, all clients are accepted.
mqtt.loop();
if (random(1000) == 0)
mqtt.publish("picomqtt/welcome", "Hello from PicoMQTT!");
}
There are some limitations to the implementation due to constrained resources in the ESP8266 and ESP32, notably:
- Client only supports MQTT QoS levels 0 and 1
- Broker only supports MQTT QoS level 0, ignores will and retained messages.
Another downside highlighted by the developer is that only ESP8266 and ESP32 boards are supported.
Developer Michał Leśniewski further told Hackster.io that PicoMQTT doesn’t have all the features that Mosquitto has and it’s much slower than a Raspberry Pi, but it’s good enough for a few devices that do not send a large number of messages. He found his own setup with a few ESP boards to be very stable. You’ll find the source code, documentation, and a benchmark sample on GitHub with everything licensed under GNU LGPLv3.
The benchmark consists of ESP boards in Broker mode, and a PC sending a flood of messages with different parameters. What’s really odd is that the ESP8266 can handle several thousand messages per second for small payloads (<=10 bytes) using up to 5 consumers, but the ESP32 struggles much more only being able to handle several hundred messages per second, or even less, under the same conditions.
Read More: PicoMQTT – An MQTT Client/Broker library for ESP8266 and ESP32 – CNX Software