Mosquitto Mqtt Broker And ESP8266 Communcation In 6 Easy Steps

Tiago Horta
3 min readMay 8, 2021

--

Using one of the best messaging protocols for the Internet of Things (IoT), the MQTT allows us to exchange messages from a lot of divices and sensors.

In our specific case, we will use an ESP8266 module to subscribe and publish messages to a local Mosquitto MQTT broker.

Step 1

Downloading and installing the mosquitto mqtt broker.

Eclipse Mosquitto version 2.0.10

Step 2

Running the mosquitto broker.

$ mosquitto -v
The -v flag will display running information

The default config file will not allow communication from outside so we will need to configure the mosquitto.conf file to be able to communicate.

Adding the two frst lines in the begining of the file will do the work

Running the mosquitto broker using config file.

$ mosquitto -c mosquitto.conf -v

Step 3

List received messages within a specific topic.

$ mosquitto_sub -h localhost -p 1883 -t test/topic

Step 4

Send a message from the computer terminal to a specific topic.

$ mosquitto_pub -h localhost -p 1883 -t test/topic -m “Hello fom my pc!”
Received message from the last command

For now everything is working locally, just by establishing a communication from the local machine to itself.

We want for sure to integrate this with other IoT devices, so let’s get it working with a module!

ESP8266 with WiFi

Step 5

Installing Arduino IDE.

Arduino IDE version 1.8.13

Step 6

Code to pusblish and subscribe using ESP8266.

The ESP8266 will not only send messages but also list the received messages from the subscribed topic!

Arduino IDE serial monitoring
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
// WiFi
const char *ssid = ""; // Enter your WiFi name
const char *password = ""; // Enter WiFi password
// MQTT Broker
const char *mqtt_broker = ""; // Enter your WiFi or Ethernet IP
const char *topic = "test/topic";
const int mqtt_port = 1883;
WiFiClient espClient;
PubSubClient client(espClient);
void setup() {
// Set software serial baud to 115200;
Serial.begin(115200);

// connecting to a WiFi network
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("Connecting to WiFi..");
}

Serial.println("Connected to the WiFi network");

//connecting to a mqtt broker
client.setServer(mqtt_broker, mqtt_port);
client.setCallback(callback);

while (!client.connected()) {
String client_id = "esp8266-client-";
client_id += String(WiFi.macAddress());

Serial.printf("The client %s connects to mosquitto mqtt broker\n", client_id.c_str());

if (client.connect(client_id.c_str())) {
Serial.println("Public emqx mqtt broker connected");
} else {
Serial.print("failed with state ");
Serial.print(client.state());
delay(2000);
}
}

// publish and subscribe
client.publish(topic, "Hello From ESP8266!");
client.subscribe(topic);

}
void callback(char *topic, byte *payload, unsigned int length) {
Serial.print("Message arrived in topic: ");
Serial.println(topic);
Serial.print("Message:");

for (int i = 0; i < length; i++) {
Serial.print((char) payload[i]);
}

Serial.println();
Serial.println(" - - - - - - - - - - - -");
}
void loop() {
client.loop();
}
Received message from the ESP8266

And that’s it!

Following the steps, I was able to exchange messages from my local computer and the ESP8266 module.

Anyone can use this code base to build a specific use case, try the code and in case of any doubt feel free to get in touch with me!

Thank you for your reading time.

--

--

Tiago Horta
Tiago Horta

Responses (2)