In this blog post, let’s make a NodeMCU MQTT client.

As you all know, NodeMCU + MQTT is the perfect starting point for IoT. So I started working on a small project. So I wanted to have an MQTT client on a NodeMCU to get sensor data. So Let’s checkout, how to do it. I hope this might help you with your projects too.

At end of this blog, you will be able,

What Do You Need?

If you have a problem with MQTT, please check the below links

What am I using to program this?

Let’s start…

Before start programming,

  1. You need to install Arduino software into your computer (Depending on Linux or Windows OS, your installation process will be changed, there are many tutorials on how to do this, so no need to talk about this 😉 )

  2. You need to install Node MCU via Boards Manager. (This Tutorial shows, how to install Node MCU board to your Arduino IDE)

  3. Install PubSubClient library. You can install this library from here. Library Link:

Let’s start coding…

First I have explained each code block below. Or you can see the source code from Github.

Setup Network Info

First, you need to add your wifi SSID, password, and MQTT server address as follows.

Setup WiFi function

This function will be used to set up your wifi connection. It will connect to the wifi connection that you defined in the above section. Finally, it will display the IP address of the NodeMCU on the serial monitor.

MQTT subscribe callback function

This function will be run when an MQTT message is received, on a subscribed topic. You can do set digital write on digital pins or set values on analog pins depending on the MQTT message. Currently, in this program, the message will be printed on the serial monitor.

MQTT reconnect function

This function will be used to connect to the MQTT server. When the MQTT server is disconnected, then we can use this function to reconnect with the MQTT server.

Void setup function

This function is used to initialize the NodeMCU.  Currently, I will be using bound rate as 115200. Then I will connect to wifi and set the default MQTT server port as 1883 (This is the default port of the MQTT broker). Finally, I will register the callback to event execute when a message is received to a subscribed topic.

Void Loop function

As you all know, the void loop function will keep running over and over again. Following is our void loop function. This function will first check if an MQTT client is connected to a broker. If not, it will reconnect. Then, once in 2 seconds, a message will be published to topic outTopic with the message saying hello world #count.

So let’s run this code and check it out.

Demo – NodeMCU Client publish messages

The following image shows how the NodeMCU client publishes messages and the Ubuntu terminal can subscribe to that topic and receive the messages.

Demo – NodeMCU Client Subscribe to a topic data

The following image when an MQTT publisher publishes a message to a topic to which a nodeMCU client is subscribed to. As you can see, the serial monitor prints the message through the callback function we discussed above. So we can change that function to turn on/off devices accordingly. 

To catch this kind of small tutorials, and things I do for fun, don’t forget to subscribe to my BloggerYouTube & follow me on GitHub.
Do you face any issues while trying this? Let’s discuss in bellow…

***Please note that all of the content on this post was originally published on We have migrated these posts to for your convenience and continued access.

Leave a Reply

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