Send sensor values from NodeMCU to MQTT

Hi All,

This is a late blog actually. This is an extra credit for one of the assignments. I was not able to do it during that week and completed the week after. But I did not get the chance of writing the blog after the completion. I feel it is the time to write this blog.

I included a library <PubSubClient.h> in the aurdino code to publish and subscribe to a topic. Initially, I tried connecting to a local MQTT broker which put me in trouble. This is the reason I was not able to complete my task on time during the assignment week. It has given me the following error on the serial monitor.

I have tried alot of things to solve the issue, but I couldn’t do it. Then got the idea of connecting to a cloud MQTT broker which I had already setup on the Maqiatto and check if it connects to the MQTT on the cloud. This idea saved me. It got connected to the cloud MQTT and this is what the connection information I saw on the aurdino serial monitor.

The aurdino code:

#include <ESP8266WiFi.h>

#include <PubSubClient.h>

#include <DHT.h>

#define wifi_ssid “TP-LINK_F53E” //wifi name

#define wifi_password “60122421” //wwifi password

#define mqtt_server “” //mqtt server name

#define mqtt_user “username” //mqtt authentication

#define mqtt_password “password”

#define humidity_topic “sensor/humidity”

#define temperature_topic “sensor/temperature”

#define DHTTYPE DHT11

#define DHTPIN 14

WiFiClient espClient;

PubSubClient client(espClient);


void setup( ) {


dht.begin( );

setup_wifi( );

client.setServer(mqtt_server, 1883);


void setup_wifi( ) {


Serial.println( );

Serial.print(“Connecting to “);


WiFi.begin(wifi_ssid, wifi_password);

while (WiFi.status( ) != WL_CONNECTED) {





Serial.println(“WiFi connected”);

Serial.println(“IP address: “);

Serial.println(WiFi.localIP( );


void reconnect( ){

// Loop until we’re reconnected

while (!client.connected( )) {

Serial.print(“Attempting MQTT connection…”);

if (client.connect(“ESP8266Client”, mqtt_user, mqtt_password)) {


} else {

Serial.print(“failed, rc=”);

Serial.print(client.state( ));

Serial.println(“ try again in 5 seconds”);





bool checkBound(float newValue, float prevValue, float maxDiff) {

return !isnan(newValue) &&

(newValue < prevValue — maxDiff || newValue > prevValue + maxDiff);


long lastMsg = 0;

float temp = 0.0;

float hum = 0.0;

float diff = 1.0;

void loop( ) {

if (!client.connected( ) {

reconnect( );



client.loop( );

long now = millis( );

if (now — lastMsg > 2000) {

lastMsg = now;

float newTemp = dht.readTemperature( );

float newHum = dht.readHumidity( );

if (checkBound(newTemp, temp, diff)) {

temp = newTemp;

Serial.print(“New temperature:”);

Serial.println(String(temp).c_str( );

client.publish(temperature_topic, String(temp).c_str( ), true);


if (checkBound(newHum, hum, diff)) {

hum = newHum;

Serial.print(“New humidity:”);

Serial.println(String(hum).c_str( );

client.publish(humidity_topic, String(hum).c_str( ), true);




Thanks for reading the late post 😀