Skip to content

Commit e028975

Browse files
authored
feat(blog): Add article about ESP32 Mosquitto port (#483)
* feat(blog): Add article about ESP32 mosquitto port * fix(authors): Update authors details
1 parent 7fe2bc5 commit e028975

File tree

4 files changed

+182
-3
lines changed

4 files changed

+182
-3
lines changed
17 KB
Loading
59.8 KB
Loading
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
---
2+
title: "Lightweight MQTT Broker for ESP32: Mosquitto ported to ESP-IDF"
3+
date: 2025-05-28
4+
showAuthor: false
5+
summary: "Mosquitto -- the industry-standard MQTT broker -- has been ported to ESP-IDF. Its lightweight version retains Mosquitto's core functionality and security features to run on resource-constrained IoT devices. This MQTT broker is ideal for edge computing, testing, and standalone IoT deployments. In this article, we will do an overview and show you how to get started."
6+
authors:
7+
- david-cermak
8+
tags: [ "ESP-IDF", "MQTT", "Broker", "IoT", "ESP32"]
9+
---
10+
11+
12+
## Introduction
13+
Traditionally, MQTT brokers run in the cloud or on powerful servers. But what if we could run them directly on IoT devices?
14+
15+
The lightweight implementation of the popular Eclipse Mosquitto™ broker makes this possible. Ported to ESP-IDF, this version brings MQTT broker functionality to edge devices and reduces the need for always-on external infrastructure.
16+
17+
Optimized for the limited resources of the ESP32, this port maintains the core features of Eclipse Mosquitto and benefits from its feature completeness, configurability, extensibility, and portability. Running this MQTT broker directly on the device reduces latency, enhances privacy by keeping data local, and allows for fully autonomous IoT networks -- even in the absence of internet connectivity.
18+
19+
20+
### Key features
21+
- Supports TLS transport (based on ESP-TLS)
22+
- Supports plain TCP transport
23+
- Single listener configuration (only one transport at a time)
24+
25+
26+
## Use cases of running an MQTT broker on IoT devices
27+
While it is possible to run an MQTT broker on IoT devices, what are the use cases and considerations? Let's explore three interesting scenarios where running the broker on an IoT device is particularly beneficial.
28+
29+
### Local broker for private IoT networks
30+
One powerful use case is running a local Mosquitto broker on the ESP32 to serve local IoT devices within a private network segment:
31+
- Create an independent home automation hub that works even when internet connectivity fails
32+
- Integrate with platforms like ESP-Home, Home Assistant, or custom IoT devices
33+
- Enhance privacy by keeping all data within the local network
34+
35+
36+
### Testing on target
37+
Another use-case is testing MQTT applications directly on the ESP32. By running both the client and broker on the same chip, developers can:
38+
- Develop and test MQTT-based applications without external dependencies
39+
- Simulate complex MQTT interactions in a controlled environment
40+
- Debug communication issues in isolation before deploying to a production environment
41+
- Create self-contained test fixtures for IoT product validation
42+
43+
44+
### Bridged brokers over P2P networks
45+
The [serverless MQTT example](https://github.com/espressif/esp-protocols/tree/master/components/mosquitto/examples/serverless_mqtt) demonstrates how two ESP32 devices running Mosquitto can create a synchronized broker system over peer-to-peer connections:
46+
- Connect IoT devices across separate private networks without requiring cloud infrastructure
47+
- Synchronize two local MQTT networks across the internet using secure peer-to-peer connections
48+
- Create resilient IoT networks that can operate across geographical locations
49+
- Enable MQTT communication between networks separated by NATs or firewalls
50+
51+
52+
## Technical specifications
53+
- **Memory Footprint**:
54+
- Program memory: ~60 kB
55+
- Stack size: Minimum 5 kB (recommended)
56+
- Heap usage: ~2 kB on startup, ~4 kB per connected client
57+
- **Connectivity**: TCP or TLS transport
58+
- **API**: Simple C API with broker configuration structure
59+
- **Dependencies**:
60+
- ESP-IDF components: newlib, esp_timer, esp_tls (for secure TLS connections)
61+
- Managed components: sock_utils (for porting)
62+
63+
64+
## Implementation details
65+
The MQTT broker is implemented as a port of the original Mosquitto codebase. It follows a single-threaded, event-driven architecture that efficiently handles multiple connections through socket multiplexing. The core of this implementation consists of a number of key components covered below.
66+
67+
### Socket multiplexing
68+
The broker uses an abstracted multiplexing layer that utilizes polling implementations with:
69+
- **Mux Layer**: An abstraction layer (`mux.c`) that provides common interface functions for socket multiplexing
70+
- **Poll Implementation**: Uses standard `poll()` mechanisms for handling multiple file descriptors
71+
72+
### Event loop
73+
The main broker loop (`loop.c`) performs the following operations:
74+
1. Processes incoming client connections
75+
2. Handles socket read/write events
76+
3. Manages client keepalive tracking
77+
4. Processes message queues and subscriptions
78+
5. Handles disconnections and cleanup
79+
80+
### ESP-IDF integration
81+
The port includes a custom implementation (`net__esp_tls.c`) that integrates ESP-TLS for secure connections, which handles TLS context initialization and manages secure socket connections.
82+
ESP-TLS is an internal component of ESP-IDF providing simplified and abstracted access to security related APIs, you can read more about it in the official [ESP-IDF documentation](https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/protocols/esp_tls.html).
83+
84+
### Simplified message handling
85+
```mermaid
86+
graph TD
87+
A[Client Connection Request] --> B[Socket Accept]
88+
B --> C{TLS Enabled?}
89+
C -->|Yes| D[ESP-TLS Context Creation]
90+
C -->|No| E[Plain TCP Connection]
91+
D --> F[TLS Handshake]
92+
F --> G[Connection Ready]
93+
E --> G
94+
G --> H[Socket Multiplexing]
95+
96+
subgraph "Event Loop"
97+
H --> I[Poll for Events]
98+
I --> J[Process Socket Events]
99+
J --> K[Handle Client Messages]
100+
K --> L[Route Messages to Subscribers]
101+
L --> I
102+
end
103+
104+
M[Client Disconnect] --> N[Socket Cleanup]
105+
N --> O[Release Resources]
106+
```
107+
108+
## How to use the MQTT broker in your project
109+
To use the ESP32 Mosquitto Port in your projects, you'll need to download the [mosquitto component](https://components.espressif.com/components/espressif/mosquitto) and initialize the broker with appropriate configuration settings. It is important to provide sufficient stack space for the running task.
110+
111+
### Start from an existing example
112+
Use the below command to create a new project from the simple broker example
113+
```
114+
$ idf.py create-project-from-example "espressif/mosquitto:broker"
115+
```
116+
117+
Configure the project with:
118+
```
119+
$ idf.py menuconfig
120+
```
121+
122+
In the appeared configuration menu:
123+
124+
- Go to `Connection` and choose the preferred connection
125+
- Go to `Example` and choose the example transport (TLS or plain TCP)
126+
Then build and flash normally with:
127+
```
128+
$ idf.py build flash monitor
129+
```
130+
131+
### Use the broker in your project
132+
Add mosquitto component to your project dependency with:
133+
```
134+
$ idf.py add-dependency "espressif/mosquitto"
135+
```
136+
137+
Configure the broker and start it in your application:
138+
139+
```c
140+
struct mosq_broker_config config = {
141+
.host = "0.0.0.0", // Listen on all interfaces
142+
.port = 1883, // Standard MQTT port
143+
.tls_cfg = NULL // No TLS in this example
144+
};
145+
146+
// Start the broker (runs in the current task)
147+
mosq_broker_run(&config);
148+
```
149+
150+
## Future development
151+
The ESP32 Mosquitto Port is designed to be expanded with additional features in future releases, supporting:
152+
- Plugins and configuration files to extend broker functionality
153+
- WebSocket and Websocket Secure transports
154+
155+
## License information
156+
157+
The ESP32 Mosquitto Port is licensed under the dual Eclipse Distribution License (EDL) and Eclipse Public License (EPL). These licenses ensure that the software remains free and open-source while providing clear terms for usage, modification, and distribution.
158+
159+
- **Eclipse Distribution License (EDL)**: A BSD-style license that allows for commercial use, modification, distribution, and private use with minimal restrictions.
160+
- **Eclipse Public License (EPL)**: A copyleft license that requires any distributed modifications to also be licensed under the EPL.
161+
162+
For full license details, please refer to the LICENSE files included in the source code repository:
163+
- [edl-v10](https://github.com/espressif/esp-protocols/blob/mosq-v2.0.20_2/components/mosquitto/edl-v10) - Eclipse Distribution License version 1.0
164+
- [epl-v20](https://github.com/espressif/esp-protocols/blob/mosq-v2.0.20_2/components/mosquitto/epl-v20) - Eclipse Public License version 2.0
165+
166+
## Resources
167+
168+
- [Official Mosquitto documentation](https://mosquitto.org/)
169+
- [ESP32 Mosquitto component](https://components.espressif.com/components/espressif/mosquitto)
170+
- [ESP32 Mosquitto port development repository](https://github.com/espressif/esp-protocols/tree/master/components/mosquitto)
171+
- [Brokerless MQTT example](https://github.com/espressif/esp-protocols/tree/master/components/mosquitto/examples/serverless_mqtt)
172+
173+
<div style="font-size: 0.8em; color: #888; margin-top: 2em;">
174+
Eclipse, Eclipse Mosquitto, and the Eclipse Foundation logos are trademarks of Eclipse Foundation, Inc.
175+
</div>

data/authors/david-cermak.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
{
22
"name": "David Cermak",
3-
"bio": "",
4-
"image": ""
5-
}
3+
"image" : "img/authors/david-cermak.webp",
4+
"bio": "Embedded Software Engineer at Espressif",
5+
"social": [
6+
{ "linkedin": "https://www.linkedin.com/in/david-cermak-016282158/" },
7+
{ "github": "https://github.com/david-cermak" }
8+
]
9+
}

0 commit comments

Comments
 (0)