How to Use ESP8266 as an MQTT Broker
In this tutorial, I’ll guide you on how to set up ESP8266 as an MQTT Broker using the uMQTTBroker library. This is a lightweight MQTT Broker library designed specifically for ESP8266 using Arduino IDE.
If you’re not familiar with configuring ESP8266 using Arduino IDE, visit this guide: Arduino Support for ESP8266 with Simple Test Code.
What is uMQTTBroker?
uMQTTBroker is a library for ESP8266 that allows you to run an MQTT broker directly on your ESP8266 board. This eliminates the need for an external MQTT broker like Mosquitto for small-scale IoT projects.
Supported Features:
- MQTT Protocol versions v3.1 and v3.1.1
- Handles a limited number of clients (tested with at least 8 clients)
- Retained messages
- Last Will and Testament (LWT)
- QoS Level 0 (At most once delivery)
- Username/Password authentication
Limitations:
- Does not support QoS levels higher than 0
- Limited TCP connections
- Does not support TLS (SSL encryption)
For API details, visit the official uMQTTBroker GitHub repository.
Steps to Set Up ESP8266 as MQTT Broker
Step 1: Download uMQTTBroker Library
- Go to the uMQTTBroker GitHub page.
- Download the ZIP file.
- Extract the ZIP file and move the
uMQTTBroker
folder to your Arduino IDE’s libraries folder.
Step 2: Code for Setting Up MQTT Broker
Create a new sketch in Arduino IDE and paste the following code:
#include <ESP8266WiFi.h>
#include "uMQTTBroker.h"
uMQTTBroker myBroker;
// Wi-Fi Configuration
char ssid[] = "YOUR_SSID"; // Replace with your network SSID
char pass[] = "YOUR_PASSWORD"; // Replace with your network password
// Wi-Fi Initialization
void startWiFiClient() {
Serial.println("Connecting to " + (String)ssid);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi connected");
Serial.println("IP address: " + WiFi.localIP().toString());
}
void setup() {
Serial.begin(115200);
Serial.println();
// Connect to Wi-Fi network
startWiFiClient();
// Start the MQTT broker
Serial.println("Starting MQTT broker");
myBroker.init();
}
void loop() {
delay(1000); // Add your logic or additional functionality here
}
Step 3: Flash Code to ESP8266
- Connect your ESP8266 board to your PC.
- In Arduino IDE, select Tools > Board > Generic ESP8266 Module.
- Select the correct COM Port.
- Click the Upload button to flash the code.
Step 4: Get the MQTT Broker IP Address
- Open the Serial Monitor in Arduino IDE.
- Set the baud rate to 115200.
- Once connected, the Serial Monitor will display your ESP8266’s IP address. This IP address acts as your MQTT Broker’s address.
Note: If the IP address is not displayed, press the RST button on the ESP8266 board and check the Serial Monitor again.
Step 5: Connect MQTT Clients
To test your MQTT broker:
- Use an MQTT client like MQTT Explorer, MQTT.fx, or Node-RED.
- Connect using the ESP8266’s IP address and port 1883.
- Publish messages and observe the communication.
Alternative Solution for Enhanced MQTT Broker
If you need additional features like persistent configuration, scripting support, or JSON parsing, consider using ESP_MQTT_Broker instead.
ESP_MQTT_Broker is a robust MQTT broker/client with scripting support. It enables your ESP8266 to act as:
- MQTT Broker with JSON parsing
- IoT Controller with GPIO control
- HTTP Request handler for simple web integrations
- Bridge Mode for cloud MQTT brokers
Conclusion
Setting up ESP8266 as an MQTT Broker is a cost-effective solution for small IoT networks. Using the uMQTTBroker library simplifies the process, eliminating the need for additional broker services.
If you have any questions or run into issues, feel free to ask in the comments. Don’t forget to share this guide if you found it helpful!
You may also like:
- Dynamic WLAN configuration for ESP32 Board | AutoConnect
- ESP32 BLE on Arduino IDE with UART Test
- ESP32 Bluetooth Low Energy (BLE) on Arduino IDE
- ArduinoOTA ESP32: Wi-Fi (OTA) Wireless Update from the Arduino IDE
- ESP32 with LoRa using Arduino IDE
- How To Use Grove-LCD RGB Backlight with NodeMCU
- NodeMcu to DHT Interface in Blynk app
- Arduino IDE | Arduino | Open Source Hardware/Softawre | Arduino Vs RPi
- WiFi LoRA 32 (V2) ESP32 | Overview | Introduction
- DHT11 sensor with ESP8266/NodeMCU using Arduino IDE
Pingback: ESP8266 as a MQTT Broker | How To Make ESP8266 as a MQTT Broker — IoTbyHVM – Explore TechBytes – hashstacks
That’s not really an MQTT broker, you just copied the demo code that Martin Ger wrote, it’s a starting point to code from and not a working MQTT broker demo.
Hi Andy,
This demo works with my NodeMCU Board. Please write your error in comment box. This is Just Example code. umqttbroker library written by Martin Ger. I already mention link of github repo. I never say, this library written by me. Please read full post again.
thanks, trying this code. At least there is some issue with the copy/paste to this webpage. You forgot the library name after the first #include. I would guess it would be: #include “ESP8266WiFi.h”
Yes, i miss add to #include “ESP8266WiFi.h”
Thanks for tell me this issue
Pingback: Mosquitto broker | Install Mosquitto in AWS, Raspberry Pi and Android
Can you implement it on ESP32? It might be worth it.