ESPHow ToIoT HardwaresTutorials/DIY

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

  1. Go to the uMQTTBroker GitHub page.
  2. Download the ZIP file.
  3. 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:

C
#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

  1. Connect your ESP8266 board to your PC.
  2. In Arduino IDE, select Tools > Board > Generic ESP8266 Module.
  3. Select the correct COM Port.
  4. Click the Upload button to flash the code.

Step 4: Get the MQTT Broker IP Address

  1. Open the Serial Monitor in Arduino IDE.
  2. Set the baud rate to 115200.
  3. 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:

  1. Use an MQTT client like MQTT Explorer, MQTT.fx, or Node-RED.
  2. Connect using the ESP8266’s IP address and port 1883.
  3. 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:

Harshvardhan Mishra

Hi, I'm Harshvardhan Mishra. Tech enthusiast and IT professional with a B.Tech in IT, PG Diploma in IoT from CDAC, and 6 years of industry experience. Founder of HVM Smart Solutions, blending technology for real-world solutions. As a passionate technical author, I simplify complex concepts for diverse audiences. Let's connect and explore the tech world together! If you want to help support me on my journey, consider sharing my articles, or Buy me a Coffee! Thank you for reading my blog! Happy learning! Linkedin

7 thoughts on “How to Use ESP8266 as an MQTT Broker

Leave a Reply

Your email address will not be published. Required fields are marked *