Thursday, April 18, 2024
ESPHow ToIoT HardwaresTutorials/DIY

ESP8266 as a MQTT Broker | How To Make ESP8266 as a MQTT Broker

In this tutorial i am telling to you ” How To use ESP8266 as a MQTT Broker”. uMQTTBroker is a MQTT Broker library for ESP8266 Arduino, available on GitHub.

Now we Arduino IDE, If you don’t familier with ESP8266 using Arduino IDE. Please Visit given below post.

Arduino Support for ESP8266 with simple test code

How To Make ESP8266 as a MQTT Broker

First Download uMQTT Broker library from Github. Download Now.  Create new sketch and paste given below code.

/*
 * uMQTTBroker demo for Arduino
 * 
 * Minimal Demo: the program simply starts a broker and waits for any client to connect.
 */

#include 
#include "uMQTTBroker.h"

uMQTTBroker myBroker;

/*
 * Your WiFi config here
 */
char ssid[] = "iotbyhvm";      // Replace with your network SSID (name)
char pass[] = "asdf7890"; // Replace with your network password

/*
 * WiFi init stuff
 */
void startWiFiClient()
{
  Serial.println("Connecting to "+(String)ssid);
  WiFi.begin(ssid, pass);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  
  Serial.println("WiFi connected");
  Serial.println("IP address: " + WiFi.localIP().toString());
}

void startWiFiAP()
{
  WiFi.softAP(ssid, pass);
  Serial.println("AP started");
  Serial.println("IP address: " + WiFi.softAPIP().toString());
}

void setup()
{
  Serial.begin(115200);
  Serial.println();
  Serial.println();

  // Connect to a WiFi network
  startWiFiClient();

  // Or start the ESP as AP
//startWiFiAP();

  // Start the broker
  Serial.println("Starting MQTT broker");
  myBroker.init();
}

void loop()
{   
  // do anything here
  delay(1000);
}

Now connect your ESP8266 development board with your PC. Select Board and Port. Flash this code.

Open Your Serial Monitor, Here you can found a IP address. this IP address is your MQTT Broker address.

If Serial Monitor is not displaying IP address then press RST button of ESP8266 Board.

What is uMQTTBroker ?

This is a MQTT Broker library for ESP8266 Arduino. You can start an MQTT broker in any ESP Arduino project. Just clone (or download the zip-file and extract it) into the libraries directory of your Arduino ESP8266 installation.

The broker does support:

  • MQTT protocoll versions v3.1 and v3.1.1 simultaniously
  • a smaller number of clients (at least 8 have been tested, memory is the issue)
  • retained messages
  • LWT
  • QoS level 0
  • username/password authentication

The broker does not yet support:

  • QoS levels other than 0
  • many TCP(=MQTT) clients
  • non-clear sessions
  • TLS

For API information Visit this: https://github.com/martin-ger/uMQTTBroker


If you are searching for a complete ready-to-run MQTT broker for the ESP8266 with additional features (persistent configuration, scripting support and much more) have a look at https://github.com/martin-ger/esp_mqtt .

esp_uMQTT_broker

This is a MQTT Broker/Client with scripting support on the ESP8266. This program enables the ESP8266 to become the central node in a small distributed IoT system. It implements an MQTT Broker and a simple scripted rule engine with event/action statements that links together the MQTT sensors and actors. It can act as STA, as AP, or as both and it can connect to another MQTT broker (i.e. in the cloud). Here it can also be bridge that forwards and rewrites topics in both directions. Also it can parse JSON structures, send basic HTTP GET requests and do basic I/O: i.e. read and write to local GPIO pins, react on timers and GPIO interrupts, drive GPIO pins with PWM, and read the ADC.


I hope you’ve found this post “ESP8266 as a MQTT Broker″useful. If have any query, Please write in comment box. You can share this post “ESP8266 as a MQTT Broker “.


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 “ESP8266 as a MQTT Broker | How To Make ESP8266 as a MQTT Broker

Leave a Reply

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