ESP8266: Connecting to MQTT broker

The objective of this post is to explain how to connect the ESP8266 to a MQTT broker, hosted on CloudMQTT.


Introduction

The objective of this post is to explain how to connect the ESP8266 to a MQTT broker. If you are not familiar with the protocol, you can read more about here.

Although this example should work fine with other brokers, we will assume that the broker will be hosted on CloudMQTT.

Since CloudMQTT has a free plan, we can just create an account and test it. Setting an account is really simple and it’s outside the scope of this post. You can check here how to do it and how to create a broker instance.

After completing the procedure, check the instance information page, which should be similar to the one shown in figure 1. The important credentials that we will be using on the ESP8266 code are the server, the user, the password and the port.

ESP8266 CloudMQTT Credentials

Figure 1 – CloudMQTT instance information.

In the ESP8266 side, we will be using an MQTT that supports the ESP8266, called PubSubClient. The library can be installed via Arduino IDE library manager. The coding shown here is based on the examples provided in the library, which I encourage you to try.

Regarding the hardware, the tests shown on this tutorial were performed using a ESP8266 NodeMCU board.


The code

First, we start by including the libraries needed for all the functionality. We need the ESP8266WiFi library, in order to be able to connect the ESP8266 to a WiFi network, and the PubSubClient library, which allows us to connect to a MQTT broker and publish/subscribe messages in topics.

#include <ESP8266WiFi.h>
#include <PubSubClient.h>

Then, we will declare some global variables for our connections. Naturally, we need the WiFi credentials, to connect to the WiFi network.  You can check here a previous post explaining in detail how to connect to a WiFi network using the ESP8266.

const char* ssid = "YourNetworkName";
const char* password =  "YourNetworkPassword";

We will also need the information and credentials of the MQTT server. As explained in the introduction, you will need to know the server address, the port, the username and the password. Use your values in the variables bellow.

const char* mqttServer = "m11.cloudmqtt.com";
const int mqttPort = 12948;
const char* mqttUser = "YourMqttUser";
const char* mqttPassword = "YourMqttUserPassword";

After that, we will declare an object of class WiFiClient, which allows to establish a connection to a specific IP and port [1]. We will also declare an object of class PubSubClient, which receives as input of the constructor the previously defined WiFiClient.

The constructor of this class can receive other number of arguments, as can be seen in the API documentation.

WiFiClient espClient;
PubSubClient client(espClient);

Now, moving for the setup function, we open a serial connection, so we can output the result of our program. We also connect to the WiFi network.

Serial.begin(115200);
WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print("Connecting to WiFi..");
}

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

Next, we need to specify the address and the port of the MQTT server.  To do so, we call the setServer method on the PubSubClient object, passing as first argument the address and as second the port.  These variables were defined before, in constant strings.

client.setServer(mqttServer, mqttPort);

Then, we use the setCallback method on the same object to specify a handling function that is executed when a MQTT message is received. We will analyse the code for this function latter.

client.setCallback(callback);

Now, we will connect to the MQTT server, still in the setup function. As we did in the connection to the WiFi network, we connect to the server in a loop until we get success.

So, we do a while loop based on the output of the connected method called on the PubSubClient, which will return true if the connection is established or false otherwise.

To do the actual connection, we call the connect method, which receives as input the unique identifier of our client, which we will call “ESP8266Client”, and the authentication username and password, which we defined early. This will return true on connection success and false otherwise

In case of failure, we can call the state method on the the PubSubClient object, which will return a code with information about why the connection failled [2]. Check here the possible returning values.

Check bellow the whole connecting loop.

while (!client.connected()) {
    Serial.println("Connecting to MQTT...");

    if (client.connect("ESP8266Client", mqttUser, mqttPassword )) {

      Serial.println("connected");  

    } else {

      Serial.print("failed with state ");
      Serial.print(client.state());
      delay(2000);

    }
}

To finalize the setup function, we will publish a message on a topic. To do so, we call the publish method, which receives as input arguments the topic and the message. In this case, we will publish a “Hello from ESP8266” message on the “esp/test” topic.

client.publish("esp/test", "Hello from ESP8266");

Then, we will subscribe to that same topic, so we can receive messages from other publishers. To do so, we call the subscribe method, passing as input the name of the topic.

client.subscribe("esp/test");

Check the full setup function bellow.

void setup() {

  Serial.begin(115200);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.println("Connecting to WiFi..");
  }
  Serial.println("Connected to the WiFi network");

  client.setServer(mqttServer, mqttPort);
  client.setCallback(callback);

  while (!client.connected()) {
    Serial.println("Connecting to MQTT...");

    if (client.connect("ESP8266Client", mqttUser, mqttPassword )) {

      Serial.println("connected");  

    } else {

      Serial.print("failed with state ");
      Serial.print(client.state());
      delay(2000);

    }
  }

  client.publish("esp/test", "Hello from ESP8266");
  client.subscribe("esp/test");

}


The callback function

After the initialization, we need to specify the callback function, which will handle the incoming messages for the topics subscribed.

The arguments of this function are the name of the topic, the payload (in bytes) and the length of the message. It should return void.

In this function, we will first print the topic name to the serial port, and then print each byte of the message received. Since we also have the length of the message as argument of the function, this can be easily done in a loop.

Check bellow the whole function code.

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("-----------------------");

}

 

The main loop

In the main loop function, we will just call the loop method of the PubSubClient. This function should be called regularly to allow the client to process incoming messages and maintain its connection to the server [2].

void loop() {
  client.loop();
}

Check the full code bellow

#include <ESP8266WiFi.h>
#include <PubSubClient.h>

const char* ssid = "YourNetworkName";
const char* password =  "YourNetworkPassword";
const char* mqttServer = "m11.cloudmqtt.com";
const int mqttPort = 12948;
const char* mqttUser = "YourMqttUser";
const char* mqttPassword = "YourMqttUserPassword";

WiFiClient espClient;
PubSubClient client(espClient);

void setup() {

  Serial.begin(115200);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.println("Connecting to WiFi..");
  }
  Serial.println("Connected to the WiFi network");

  client.setServer(mqttServer, mqttPort);
  client.setCallback(callback);

  while (!client.connected()) {
    Serial.println("Connecting to MQTT...");

    if (client.connect("ESP8266Client", mqttUser, mqttPassword )) {

      Serial.println("connected");  

    } else {

      Serial.print("failed with state ");
      Serial.print(client.state());
      delay(2000);

    }
  }

  client.publish("esp/test", "Hello from ESP8266");
  client.subscribe("esp/test");

}

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();
}


Testing the code

First, make sure the MQTT server is running. Then, to test the code, just upload it and run it on your ESP8266. Open the Arduino IDE serial console, so the output gets printed.

Upon running, the ESP8266 will send the “Hello from ESP8266” message, which will not be printed on the serial. After that, the ESP8266 subscribes the same topic to which the hello message was sent.

If any other producer sends a message to the “esp/test” topic, then it will be printed in the serial console, as shown in figure 2.

ESP8266 MQTT test

Figure 2 – Messages sent to the “esp/test” topic.

For this tutorial, I used MQTTlens, a Google Chrome application, which connects to a MQTT broker and is able to subscribe and publish to MQTT topics [3]. This is a very useful application that I really recommend for this type of tests.

For the test, MQTTlens was subscribing the “esp/test” topic before connecting the ESP8266. As seen in figure 3, the “Hello from ESP8266” message was printed. After that, two hello messages were sent by MQTTlens, which can be seen in the same figure. The messages sent are the ones shown in figure 2, which were received by the ESP8266, as expected.

ESP8266 MQTT MQTTlens messages

Figure 3 – Testing the program with MQTTlens.


Related content


References

[1] https://www.arduino.cc/en/Reference/WiFiClient

[2] http://pubsubclient.knolleary.net/api.html

[3] https://chrome.google.com/webstore/detail/mqttlens/hemojaaeigabkbcookmlgmdigohjobjm


Technical details

  • ESP8266 libraries: v2.3.0
  • PubSubClient library: v2.6.0

111 Replies to “ESP8266: Connecting to MQTT broker”

  1. Hi! Very helping post!
    I’m having just one problem: with the MQTTLens.
    I can’t connect it. Can you please tell what information I need to fill in each space of the MQTTLens (Connection name, Hostname, tcp/ws, Port…)?
    Thanks for the help!

    1. As to rewriting the sentence, here’s what I’d do. My approach adds more information and uses “you” instead of “we.” Feel free to adapt this any way you choose.
      “After you include the four lines shown above, you will declare an object of the class WiFiClient, as shown below. That object lets you connect to a specific Internet provider (IP) and a specific port. Then you will declare a second object of the class PubSubClient that can receive information from the espClient object.
      Code here…
      The C++ constructor of the class PubSubClient provides overloaded functions in C++. Basically that means the function can receive several arrangements of arguments passed to it. For example, int publish (topic, payload) and int publish (topic, payload, length). For more information about function overloading, see: (add reference here)

      1. Thanks for the suggestion 🙂
        I’ll have to find some time to update some stuff in some older posts, I’ll take this in consideration when I do it and also in next posts.
        Best regards,
        Nuno Santos

        1. I wrote for two electrical-engineering magazines and was chief editor for most of my 18 years with them. Happy to proofread when you’re ready, at no cost. I am retired. You have good material. –Jon

          1. Thank you very much for the feedback 🙂
            Wow, that’s an impressive curriculum! That would be awesome, every feedback is important for me to learn and improve the content quality.
            Feel free to reach me at nuno_santos_slb@hotmail.com. I would definitely appreciate your suggestions.
            I’m always running around busy with my daily job so sometimes I take a while to catch up with comments / email 🙂
            Best regards,
            Nuno Santos

  2. This tutorial does not explain how to set up the MQTTlens application. I need to know what information goes in the MQTTlens “Connection Details” area so I can see how the Arduino software works. The small screen-shot diagram is impossible to read and it does not show the Connection Details the author used. Do I use the default MQTTlens settings or use settings for my CloudMQTT account? I’m at a dead stop here.

    This article could use better editing because some sections lead to confusion. For example, what does this mean? ” We will also declare an object of class PubSubClient, which receives as input of the constructor the previously defined WiFiClient.”

    1. Hi,

      Thanks for the feedback. Indeed the images are not very legible, I’ll try to be more careful in next publications.

      In the section MQTTLens of this post there’s a more detailed guide on how to setup MQTTLens:
      https://techtutorialsx.com/2018/07/23/uart-obloq-sending-message-to-mqtt-topic/

      The images are also a little bit low quality, but I think from the text description it should be straightforward to set things up.

      You should use the settings for your CloudMQTT account.

      The sentence has the following meaning: You instantiate (create) an object of class PubSubClient.

      The class PubSubClient has a constructor, which is what you call in order to create an object.

      That constructor receives as input an object of class WiFiClient.

      We have created a WiFiClient object in the line before, which is why I’ve said “the previously defined WiFiClient”.

      Just out of curiosity, how would you suggest to write the same sentence to be more clear?

      Best regards,
      Nuno Santos

  3. This tutorial does not explain how to set up the MQTTlens application. I need to know what information goes in the MQTTlens “Connection Details” area so I can see how the Arduino software works. The small screen-shot diagram is impossible to read and it does not show the Connection Details the author used. Do I use the default MQTTlens settings or use settings for my CloudMQTT account? I’m at a dead stop here.
    This article could use better editing because some sections lead to confusion. For example, what does this mean? ” We will also declare an object of class PubSubClient, which receives as input of the constructor the previously defined WiFiClient.”

    1. Hi,
      Thanks for the feedback. Indeed the images are not very legible, I’ll try to be more careful in next publications.
      In the section MQTTLens of this post there’s a more detailed guide on how to setup MQTTLens:
      https://techtutorialsx.com/2018/07/23/uart-obloq-sending-message-to-mqtt-topic/
      The images are also a little bit low quality, but I think from the text description it should be straightforward to set things up.
      You should use the settings for your CloudMQTT account.
      The sentence has the following meaning: You instantiate (create) an object of class PubSubClient.
      The class PubSubClient has a constructor, which is what you call in order to create an object.
      That constructor receives as input an object of class WiFiClient.
      We have created a WiFiClient object in the line before, which is why I’ve said “the previously defined WiFiClient”.
      Just out of curiosity, how would you suggest to write the same sentence to be more clear?
      Best regards,
      Nuno Santos

  4. Hi I have used your code and tried connecting to cloud mqtt from esp8266 board but there seems to be some problem which I couldn’t get. In cloud mqtt logs I’m finding socket issue. could you help me out in this. The below is my code.

    #include
    #include

    const char* ssid = “***********”;
    const char* password = “************”;
    const char* mqttServer = “m24.cloudmqtt.com”;
    const int mqttPort = 12091;
    const char* mqttUser = “rdpzxbnw”;
    const char* mqttPassword = “HN4fLROkszDV”;

    WiFiClient espClient;
    PubSubClient client(espClient);

    void setup() {

    Serial.begin(115200);

    WiFi.begin(ssid, password);

    while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.println(“Connecting to WiFi..”);
    }
    Serial.println(“Connected to the WiFi network”);

    client.setServer(mqttServer, mqttPort);
    client.setCallback(callback);

    while (!client.connected()) {
    Serial.println(“Connecting to MQTT…”);

    if (client.connect(“ESP8266Client”, mqttUser, mqttPassword )) {

    Serial.println(“connected”);

    } else {

    Serial.print(“failed with state “);
    Serial.print(client.state());
    delay(2000);

    }
    }

    client.publish(“esp/test”, “Hello from ESP8266”);
    client.subscribe(“esp/test”);

    }

    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();
    }

    1. client.publish(“esp/test”, “Hello from ESP8266”);
      client.subscribe(“esp/test”);

      Swap these two statements. You want to subscribe first, then publish. Try that and let us know if it solves your problem.

  5. Excellent tutorial antepher. Very useful and working fine. – Thanks.

    I would like to publish the temperature and humidity of DHT11 connected to ESP8266. Can you suggest.

    Thanks in advance.

Leave a Reply