ESP8266 and Node-RED with MQTT (Publish and Subscribe)

Learn how to publish and subscribe to MQTT topics with the ESP8266 NodeMCU board. In this tutorial, we’ll use the Node-RED dashboard to control the ESP8266 outputs and display sensor data from the ESP8266 on its interface. The Node-RED software is running on a Raspberry Pi, and the communication between the ESP8266 and the Node-RED software is done via MQTT communication protocol. We’ll program the ESP8266 using Arduino IDE.

ESP8266 and Node-RED with MQTT (Publish and Subscribe)

Updated 21 March 2023

Project Overview

The following figure shows an overview of what we’re going to do in this tutorial.

First, watch the video demonstration below

Node-RED and Node-RED Dashboard

To follow this tutorial, you need to have Node-RED and Node-RED Dashboard installed in your Raspberry Pi. Follow the next tutorials to install and get started with Node-RED and Node-RED dashboard:

MQTT Protocol

In this tutorial, we’ll establish a communication between a Raspberry Pi running the Node-RED software and an ESP8266 using MQTT.

MQTT stands for MQ Telemetry Transport and it is a nice lightweight publish and subscribe system where you can publish and receive messages as a client. It is a simple messaging protocol, designed for constrained devices and with low bandwidth. So, it’s the perfect solution for Internet of Things applications.

If you want to learn more about MQTT, watch the video below.

For a written version of this video and additional resources, read this blog post What is MQTT and How It Works.

Installing Mosquitto Broker

The MQTT broker is responsible for receiving all messages, filtering the messages, deciding who is interested in them, and then publishing the messages to all subscribed clients.

There are several brokers you can use. In this tutorial, we’re going to use the Mosquitto Broker.

You can install the Mosquitto MQTT broker locally on a Raspberry Pi, on your computer, or on the cloud. Follow one of the next tutorials to install Mosquitto broker:

To see if Mosquitto broker was successfully installed, run the next command:

pi@raspberry:~ $ mosquitto -v

This returns the Mosquitto version that is currently running on your Raspberry Pi. It should be 2.0.11 or above.

Checking Mosquitto Version

Note: the Mosquitto command returns the Mosquitto version that is currently installed, but it also tries to initialize Mosquitto again. Since Mosquitto is already running it prompts an error message. Don’t worry Mosquitto is properly installed and running if you see a similar message.

Establishing an MQTT communication with Node-RED

In this section, we’re going to establish an MQTT communication with Node-RED using the MQTT nodes.

Dashboard Layout

The first step is to create the dashboard layout. In this example, we’ll have a button to control an ESP8266 output, a chart, and a gauge to display temperature and humidity readings from the DHT11 sensor.

On the top right corner, click on the little arrow icon, and click on Dashboard.

On the Layout tab, create a tab called Room, and inside the Room tab, create two groups: Lamp and Sensor as shown in the figure below.

Node-RED Dashboard layout example

Creating the Flow

From the dashboard section at the left sidebar, drag a switch, a chart, and a gauge node to the flow. Then drag two MQTT in and one MQTT out node to the flow (they are under the network section)—see the figure below.

  • switch – it will control the ESP8266 output
  • mqtt output node – it will publish a message to the ESP8266 accordingly to the switch state
  • 2x mqtt input nodes – these nodes will be subscribed to the temperature and humidity topics to receive sensor data from the ESP
  • chart – will display the temperature sensor readings
  • gauge – will display the humidity sensor readings

Node-RED and the MQTT broker need to be connected. To connect the MQTT broker to Node-RED, double-click the MQTT output node. A new window pops up—as shown in the figure below.

1) Click the Add new mqtt-broker option.

Edit MQTT broker node node-red

2) Type localhost in the Server field. All the other settings are configured properly by default.

If you’re not running Node-RED on the same machine that you’re running Node-RED (Raspberry Pi), insert the MQTT IP address instead of localhost.

3) Click on the Security tab and insert your MQTT broker username and password if required.

4) Press Update and then Add. The MQTT output node automatically connects to your broker after deploying the Node-RED flow.

Edit all the other nodes’ properties as shown in the following instructions.

Switch Node

The switch sends a on string message when it’s on; and sends a off string message when it’s off. This node will publish on the room/lamp topic. Your ESP8266 will then be subscribed to this topic to receive its messages. Edit the node properties as shown in the picture below.

mqtt output node

This node is connected to the Mosquitto MQTT broker and it will publish in the room/lamp topic. Fill in the details as shown below. Insert the topic you want to subscribe to, set the QoS value and if you want to retain the messages or not.

MQTT Out node

What are retained messages in MQTT? retain (true or false) what does it mean?

A retained message is a normal MQTT message with the retained flag set to true. The broker stores the last retained message and the corresponding QoS for that topic. Each client that subscribes to a topic pattern that matches the topic of the retained message receives the retained message immediately after they subscribe. The broker stores only one retained message per topic.

Why is it useful? Retained messages help newly-subscribed clients get a status update immediately after they subscribe to a topic. This is particularly useful to get the status of a device. For example, GPIO 2 is currently HIGH, and the ESP32 suddenly resets. It doesn’t know what the last state of GPIO 2 was. However, if the state is a retained message, it will receive it right after subscribing to the topic and can update the state of GPIO 2 immediately.

mqtt input node

This node is subscribed to the room/temperature topic to receive temperature sensor data from the ESP8266. The ESP8266 will be publishing the temperature readings on this topic.

Node-RED subscribe to temperature topic

chart node

The chart will display the readings received on the room/temperature topic.

mqtt input node

This node is subscribed to the room/humidity topic to receive humidity sensor data from the ESP8266. The ESP8266 will be publishing the humidity readings on this exact topic.

gauge node

The gauge will display the readings received on the room/humidity topic. Edit the gauge properties as shown below. You can adjust the colour gradient to your liking.

Wire your nodes as shown in the figure below.

Your Node-RED application is ready. Click the Deploy button on the top right corner.

The Node-RED application is ready. To see how your dashboard looks go to  http://your-pi-ip-address:1880/ui.

Now, follow the next sections to prepare your ESP8266.

Building the Circuit

The following sections show you the needed parts and schematics to build the circuit for this project.

Parts required

These are the parts required to build the circuit (click the links below to find the best price at Maker Advisor):

You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!

Schematics

Here are the schematics for this project’s circuit.

Preparing your Arduino IDE

We’ll program the ESP8266 using the Arduino IDE. In order to upload code to your ESP8266 using the Arduino IDE, you need to install the ESP8266 add-on (How to Install the ESP8266 Board in Arduino IDE). You’ll also need to install two additional libraries to have everything ready for your ESP8266.

Installing the PubSubClient Library

The PubSubClient library provides a client for doing simple publish/subscribe messaging with a server that supports MQTT (basically allows your ESP8266 to talk with Node-RED).

1) Click here to download the PubSubClient library. You should have a .zip folder in your Downloads folder

2) In the Arduino IDE, go to Sketch > Include Library > Add .ZIP library and select the library .zip folder you’ve just downloaded.

3) Restart your Arduino IDE.

The library comes with a number of examples. See File >Examples > PubSubClient within the Arduino IDE software.

Installing the DHT Sensor Library

To read from the DHT sensor, we’ll use the DHT library from Adafruit. To use this library you also need to install the Adafruit Unified Sensor library. Follow the next steps to install those libraries.

1) Open your Arduino IDE and go to Sketch Include Library > Manage Libraries. The Library Manager should open.

2) Search for “DHT” on the Search box and install the DHT library from Adafruit.

Installing Adafruit DHT library

3) After installing the DHT library from Adafruit, type “Adafruit Unified Sensor” in the search box. Scroll all the way down to find the library and install it.

Installing Adafruit Unified Sensor driver library

4) After installing the libraries, restart your Arduino IDE.

For more information about the DHT11 sensor and the ESP8266, read ESP8266 DHT11/DHT22 Temperature and Humidity Web Server with Arduino IDE.

Selecting the right board on Arduino IDE

You also need to select the right board on Arduino IDE:

1) Go to Tools > Board and select the ESP8266 board you’re using.

2) Select the right COM port in Tools > Port.

Uploading code

Now, you can upload the following code to your ESP8266. This code publishes messages with the temperature and humidity from the DHT11 sensor on the room/temperature and room/humidity topics via MQTT protocol.

The ESP8266 is subscribed to the room/lamp topic to receive the messages published on that topic by the Node-RED application, to turn the lamp on or off.

The code is well commented on where you need to make changes. You need to edit the code with your network credentials (SSID and password), and broker details (Raspberry Pi IP address, mqtt broker username and password.

This code is also compatible with other DHT sensors – you just need to uncomment and comment the right lines of code to choose your sensor.

/*****
 
 All the resources for this project:
 https://randomnerdtutorials.com/
 
*****/

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

// Uncomment one of the lines bellow for whatever DHT sensor type you're using!
#define DHTTYPE DHT11   // DHT 11
//#define DHTTYPE DHT21   // DHT 21 (AM2301)
//#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321

// Change the credentials below, so your ESP8266 connects to your router
const char* ssid = "REPLACE_WITH_YOUR_SSID";
const char* password = "REPLACE_WITH_YOUR_PASSWORD";

// MQTT broker credentials (set to NULL if not required)
const char* MQTT_username = "REPLACE_WITH_MQTT_USERNAME"; 
const char* MQTT_password = "REPLACE_WITH_MQTT_PASSWORD"; 

// Change the variable to your Raspberry Pi IP address, so it connects to your MQTT broker
const char* mqtt_server = "YOUR_BROKER_IP_ADDRESS";
//For example
//const char* mqtt_server = "192.168.1.106";

// Initializes the espClient. You should change the espClient name if you have multiple ESPs running in your home automation system
WiFiClient espClient;
PubSubClient client(espClient);

// DHT Sensor - GPIO 5 = D1 on ESP-12E NodeMCU board
const int DHTPin = 5;

// Lamp - LED - GPIO 4 = D2 on ESP-12E NodeMCU board
const int lamp = 4;

// Initialize DHT sensor.
DHT dht(DHTPin, DHTTYPE);

// Timers auxiliar variables
long now = millis();
long lastMeasure = 0;

// This functions connects your ESP8266 to your router
void setup_wifi() {
  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("WiFi connected - ESP IP address: ");
  Serial.println(WiFi.localIP());
}

// This function is executed when some device publishes a message to a topic that your ESP8266 is subscribed to
// Change the function below to add logic to your program, so when a device publishes a message to a topic that 
// your ESP8266 is subscribed you can actually do something
void callback(String topic, byte* message, unsigned int length) {
  Serial.print("Message arrived on topic: ");
  Serial.print(topic);
  Serial.print(". Message: ");
  String messageTemp;
  
  for (int i = 0; i < length; i++) {
    Serial.print((char)message[i]);
    messageTemp += (char)message[i];
  }
  Serial.println();

  // Feel free to add more if statements to control more GPIOs with MQTT

  // If a message is received on the topic room/lamp, you check if the message is either on or off. Turns the lamp GPIO according to the message
  if(topic=="room/lamp"){
      Serial.print("Changing Room lamp to ");
      if(messageTemp == "on"){
        digitalWrite(lamp, HIGH);
        Serial.print("On");
      }
      else if(messageTemp == "off"){
        digitalWrite(lamp, LOW);
        Serial.print("Off");
      }
  }
  Serial.println();
}

// This functions reconnects your ESP8266 to your MQTT broker
// Change the function below if you want to subscribe to more topics with your ESP8266 
void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    /*
     YOU MIGHT NEED TO CHANGE THIS LINE, IF YOU'RE HAVING PROBLEMS WITH MQTT MULTIPLE CONNECTIONS
     To change the ESP device ID, you will have to give a new name to the ESP8266.
     Here's how it looks:
       if (client.connect("ESP8266Client")) {
     You can do it like this:
       if (client.connect("ESP1_Office")) {
     Then, for the other ESP:
       if (client.connect("ESP2_Garage")) {
      That should solve your MQTT multiple connections problem
    */
    if (client.connect("ESP8266Client", MQTT_username, MQTT_password)) {
      Serial.println("connected");  
      // Subscribe or resubscribe to a topic
      // You can subscribe to more topics (to control more LEDs in this example)
      client.subscribe("room/lamp");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

// The setup function sets your ESP GPIOs to Outputs, starts the serial communication at a baud rate of 115200
// Sets your mqtt broker and sets the callback function
// The callback function is what receives messages and actually controls the LEDs
void setup() {
  pinMode(lamp, OUTPUT);
  
  dht.begin();
  
  Serial.begin(115200);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);

}

// For this project, you don't need to change anything in the loop function. Basically it ensures that you ESP is connected to your broker
void loop() {

  if (!client.connected()) {
    reconnect();
  }
  if(!client.loop())
    client.connect("ESP8266Client", MQTT_username, MQTT_password);

  now = millis();
  // Publishes new temperature and humidity every 30 seconds
  if (now - lastMeasure > 30000) {
    lastMeasure = now;
    // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
    float humidity = dht.readHumidity();
    // Read temperature as Celsius (the default)
    float temperatureC = dht.readTemperature();
    // Read temperature as Fahrenheit (isFahrenheit = true)
    float temperatureF = dht.readTemperature(true);

    // Check if any reads failed and exit early (to try again).
    if (isnan(humidity) || isnan(temperatureC) || isnan(temperatureF)) {
      Serial.println("Failed to read from DHT sensor!");
      return;
    }

    // Publishes Temperature and Humidity values
    client.publish("room/temperature", String(temperatureC).c_str());
    client.publish("room/humidity", String(humidity).c_str());
    //Uncomment to publish temperature in F degrees
    //client.publish("room/temperature", String(temperatureF).c_str());
    
    Serial.print("Humidity: ");
    Serial.print(humidity);
    Serial.println(" %");
    Serial.print("Temperature: ");
    Serial.print(temperatureC);
    Serial.println(" ÂșC");
    Serial.print(temperatureF);
    Serial.println(" ÂșF");
  }
} 

View raw code

After uploading the code, and with the Raspberry Pi running your Node-RED application and the Mosquitto broker, you can open the Arduino IDE serial monitor at a baud rate of 115200 and see what’s happening in real-time.

This is helpful to check if the ESP8266 has established a successful connection to your router and to the Mosquitto broker. You can also see the messages the ESP8266 is receiving and publishing.

Demonstration

Congratulations! Your project is now completed.

Go to http://your-pi-ip-address/ui to control the ESP with the  Node-RED application. You can access your application in any browser on the same network that your Pi (watch the video demonstration below).

The application should look something the figure below.

Wrapping up

In this tutorial, we’ve shown you the basic concepts that will allow you to turn on lights and monitor sensors on your ESP8266 using Node-RED and MQTT communication protocol. You can follow these basic steps to build more advanced projects.

We have other MQTT tutorials using other sensors that you may find useful:

We hope you’ve found this tutorial useful.

If you like this project and Home Automation make sure you check our eBook: SMART HOME with Raspberry Pi, ESP32, and ESP8266.



Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »
Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »

Enjoyed this project? Stay updated by subscribing our newsletter!

124 thoughts on “ESP8266 and Node-RED with MQTT (Publish and Subscribe)”

  1. Great intro. I love it! You guys are awesome.
    One note of caution to new users though regarding using charts; I had a dashboard set up with about 6 charts and after a day or two the pi became VERY slow. I’m not sure why or if this problem was fixed in the recent node-red update but be aware that if you have a lot of charts and you pi gets very sluggish you need to remove the charts or reboot the pi.
    Bruster

    Reply
  2. Very nice Tutorial and very well commented Sketch. Thank You! (P.S.: some Mosquitto Servers, like mine, expect Username and Password to get connected to. In that case change line 108 of the sketch to:
    if (client.connect(“ESP8266Client”,[username],[password])) {

    Reply
  3. connected…yeey 🙂
    local ip
    172.20.1.78
    Attempting MQTT connection…failed, rc=-2 try again in 5 seconds
    Attempting MQTT connection…failed, rc=-2 try again in 5 seconds
    Attempting MQTT connection…failed, rc=-2 try again in 5 seconds
    Attempting MQTT connection…

    how to solve upword trouble ,itz didn’t connct to mqtt

    Reply
    • Hi.
      You either need to:
      – start the mosquitto broker, or
      – you have the wrong IP address for the mosquitto broker in your Arduino code, or
      – you have multiple ESPs in your network with the same name, causing a conflict with the mosquitto broker.

      Reply
      • Hi Sara, I appreciated your tutorial, but i have same the problem with Mr.SAM, and then i tried your suggested but all the steps not work, FYI, i using Mosquitto broker with IP localhost:1883. So i hope you can help me how to fix it problem. Thank you in advance

        Reply
    • I also got the same error. Reason is spelling mistake in MQTT configuration file entries. This is the place where u have to check.

      pi@raspberry:~$ sudo nano /etc/mosquitto/mosquitto.conf

      This will open the configuration file and check whether u proper spelling used to enter below entries.

      listener 1883
      allow_anonymous true

      Then press Ctrl+X press Y and then Enter key.
      After that reboot the raspberrypi using following command.

      pi@raspberry:~$ sudo reboot

      Problem solved…… Good Luck!!

      For more details refer – https://randomnerdtutorials.com/how-to-install-mosquitto-broker-on-raspberry-pi/#comment-710666

      Reply
    • Hi James.
      If you want to use the temperature sensor with the Rapsberry Pi, without Node-RED, you can use the DHT python library.
      Take a look at look at the library here: github.com/adafruit/Adafruit_Python_DHT
      I hope this helps.
      Regards,
      Sara 🙂

      Reply
  4. Great tutorial! Thanks very much, really helped get me started! Only issue i had was with the lamp turning on and off, after a bit of investigation it turned out that the NodeRed output default of the switch node is “true” and “false” not “on” and “off”, after minor code change everything worked!

    Reply
  5. Hi Rui,
    I have a general sort of question:
    – It looks to me like all projects, either using ESP8266 or Raspberry Pi rely on using a home LAN, Wifi, etc. However, what if you want to build a, say, robot to use away from Wifi out in the field what do you use then? or what if you build a project and you want to demo at your friend’s place who has a different LAN. How do you become independent of the local LAN? can you use an access point with some private IP to link all your devices to it? Thanks.

    Reply
    • Good question!

      When I ask this question to myself, a couple of months ago, I ended with 2 approaches:

      1) I defined an array of ssid/passwords of some wifi networks (home, office, and so on…). The ESP8266 will try to connect to first one available. PROBLEM: you can be on a different place and so… no knew wifi to connect to…!

      2) I programmed the ESP8266 as a DHCP server with it’s own sub-net (let’s say, for example: 192.168.5.x) and the other devices will be DHCP clients of the ESP8266. PROBLEM: wired devices; if you want to connect your home desktop computer to the ESP, you’ll need to do a lot of changes…

      Reply
  6. Hi maam,
    A very nice tutorial maam.
    Im having a problem with the temperature reading.
    Im not getting a reading for temperature but im getting a reading for humidity.
    Ive connected a messsage payload at the output and it returns an empty string.
    Awaiting Your reply ASAP.
    Thanking you.

    Reply
    • Hi.
      That is very weird.
      Maybe the sensor is failing to get temperature readings.
      Can you try the basic example and see if the sensor is working properly: github.com/adafruit/DHT-sensor-library/blob/master/examples/DHTtester/DHTtester.ino
      Regards,
      Sara

      Reply
  7. Hi.I have problem with this code when I upload this code to Node MCU .Like this .I don’t know what I need to do .
    Error: Exist status 1.Error compiling for board node MCU 1.0 🙂

    Reply
    • Hi.
      Please make sure that you’ve installed all the necessary libraries.
      You also need to install this library (not mentioned in the tutorial, we need to update it): github.com/adafruit/Adafruit_Sensor
      Regards,
      Sara

      Reply
  8. Hi,

    I created the dashboard on node-red step by step and burned the code into nodemcu as well. But NodeMCU and node-RED are not communicating with each other. I checked the arduino serial monitor and its giving this error.
    Attempting MQTT connection…failed, rc=5 try again in 5 seconds
    Kindly help me how to fix this bug. I really need your help mam.

    Reply
  9. Excellent tutorial!!!! Thanks a lot! I follow you from “starting with raspberry PI” until turn on the LED with the dashboard from node-red

    Reply
  10. really nice tutorial..it covers a lot of concepts, like mosquito broker, node-red and broker at the same device, esp as mqtt client..
    I have only one but important question..is it necessary to establish a wifi connection?
    i want to make a secured local mqtt network. Do i need the wifi and the router, because i think i can establish a connection from raspberry to esp or vice versa and bypass all the internet dangers..
    what do you think about it?

    Reply
  11. You people are great!!! Thank you so much for guiding us through the entire process. It’s unique and content is amazing,detailed. No confusion at any point.

    Reply
  12. Hey,
    I have been enjoying your tutorials very much, has helped me get up to speed wit IoT.
    It would be cool if you had time to do this with a nodeMCU running micropython rather than a Arduino IDE, to see the python approach.

    Reply
  13. Do you have any idea of how much you can speed up the data transfer rate? for instance, in this project the sensors data are refreshed in every 30 sec. Would it be possible to refresh in every 0,1sec?

    Reply
    • Hi Carlos.
      You can speed up the data transfer. But I never tried 0.1 seconds. You have to experiment and see if it works. Then, let me know your results.
      Regards,
      Sara

      Reply
  14. hallo, i have a problem with your code.
    my monitor give feedback
    Attampting mqtt connection…… failed, rc=2 try again in 5 seconds
    Attampting mqtt connection…… failed, rc=2 try again in 5 seconds
    Attampting mqtt connection…… failed, rc=2 try again in 5 seconds
    Attampting mqtt connection…… failed, rc=2 try again in 5 seconds
    and still so.
    I use all your code, just change the ssid and password, please help me

    Reply
    • In my case I changed my router’s channel from automatic to 11 (static), this solved my connection problems, another thing is that I use the mosquito on HomeAssistant, his IP being 172.30.32.1, but the connection was accepted only on IP HOST (ie HomeAssistant- 192.168.XXX.XXX)

      Reply
  15. Hi Sara & Rui
    I had always wanted to try Node Red but was apprehensive. This tutorial has worked wonders for me. Thanks for the great project. Keep it up.
    Vasant

    Reply
  16. Sara, Rui.
    Excelente trabajo. Segui este un par mas de los tutoriales y ya estoy enviando datos a la pi desde mis sensores en una ESP8266. Tambien estoy controlando varios actuadores conectados al esp8266.
    Solo me falta, ahora, ver como manejar la pi por nombre y como poder usarla de una red externa la wifi de mi casa.

    Reply
  17. Thank you very much for the contribution, I was already hitting my head against the wall for a solution to the constant drops of the MQTT connection of my ESP82266 arduino Mega onBoard (Wemos), in your Skecth you have the path of the stones, I changed the name of ESP to a shorter and the connection remained stable, your tutorial was the most complete that I found so far on this huge web. It was perfect in ESP communication with the mosquitto broker on HomeAssistant linked to NodeRed. I am Brazilian, so forgive any mistake in my English.

    Reply
  18. Hi there,
    many thanks for the tutorials, really helped to get me started. However, I have an issue that mu ESP doesn’t connect to Mosquitto Broker. Similar to SAM (comment on January 23, 2018) I get “Attempting MQTT connection…failed, rc=-2 try again in 5 seconds” on the monitor. Does the -2 help to narrow down the problem? SAM got 3 potential issues, but I’m sure the MQTT broker is running, I checked with your 2 terminal method and also from terminal to debug in Node-RED works. Also, I have only one ESP running in my network (I nonetheless changed the name here:

    YOU MIGHT NEED TO CHANGE THIS LINE, IF YOU’RE HAVING PROBLEMS WITH MQTT MULTIPLE CONNECTIONS
    To change the ESP device ID, you will have to give a new name to the ESP8266.
    Here’s how it looks:
    if (client.connect(“ESP8266Client”)) {
    to something else, but still no luck.

    So I was thinking the only option is the IP address. But I triple checked and the number match to those I use to log in to the Raspberry pi. Do I also need to specify the channel here? I don’t think so but I’m a bit lost what else to test/check. Also budiman (March 25 2020) had a similar issue, but I think the rc=2 (positive value) means something different? Or should I also try to specify the WLAN channel in the router, as recommended by somebody else in response there?

    Could it play a role that the ESP8266 is using 2.4GHz network but the Pi is in 5GHz?

    Any help will be greatly appreciated! 🙂

    Reply
  19. In node-red, when I double-click on my switch function, I don’t get what the tutorial shows. The node-red version (on a Pi-ZW) is 1.0.6, freshly installed. In particular, I don’t see any way to set On Payload or Off Payload under “Edit switch node”.

    Reply
  20. I keep getting “Failed to read from DHT sensor!” in my serial monitor in the Arduino IDE. Can anyone tell me what I’m doing wrong? Any help would be appreciated!
    Thanks in advance,
    Lucas

    Reply
      • Hi,
        Thank you very much for your reply. I already fixed the issue; I found that the wiring schematics were ‘wrong’ in a way that the sensor should not be connected with Vin and a resistor but directly with the 3v3 pin. Also the sequence that is shown in this tutorial didn’t work for me. I finally made it work following the schematic in this picture: circuits4you.com/wp-content/uploads/2019/01/NodeMCU-DHT11-DHT22.png
        Thanks anyways,
        Lucas

        Reply
        • Hi again.
          In your case, if you’re using a DHT sensor that is on a breakout board, it already has the resistor built-in. So, there’s no need to add another one.
          Regards,
          Sara

          Reply
  21. Hello Rui. Great project, i always get the most relevant and comprehensive guide from your website.
    i followed along and got everything work so far. It’s just that the gauge is not updating. i triple checked the connections and dashboard settings, but only humidity is showing. Any guesses what am i missing?
    Thank you

    Reply
    • Hi.
      In this particular example only the humidity is displayed on the gauge.
      The temperature is being displayed on the chart.
      Regards,
      Sara

      Reply
  22. Hi,

    what a great pleasure having found this website! I have tried quite some of your tutorials (including some privat changes to fit in my projects) NOT ONE DID FAIL! Big, big compliment.

    Just a small question to this tutorial. I have tried to publish the temperature of one room to let it show via oled display show in another room (example: temperature on the balcony, displayed on oled in the kitchen). So; – no problem to publish from ESP (here balcony) via MQTT (raspi, nodered, etc.). Even on my mobile perfectly running. Subscribe to ESP in kitchen: No problem with serial monitor (via the callback void function). But I cannot get the data into a variable stored outside the callback function to put it on the oled display (together with other data available in the void loop). Sorry for such a stupid question (could not find an answer in google).

    Again; – Thanks a lot!

    Konrad

    Reply
    • Hi.
      For example, the messageTemp variable is declared inside the
      void callback(String topic, byte* message, unsigned int length) function as follows:
      String messageTemp;
      So, it is declared as a local variable.
      You need to create a global variable that you can use anywhere in your code.
      For example, before the setup(), declare a variable, for example:
      String myTemp;
      Then, in the callback function, you just need to set the value of the myTemp variable(global):
      void callback(String topic, byte* message, unsigned int length) {
      Serial.print(“Message arrived on topic: “);
      Serial.print(topic);
      Serial.print(“. Message: “);
      String messageTemp;
      (…)
      myTemp = messageTemp;
      }
      I hope you understand my explanation.
      Regards,
      Sara

      Reply
    • Hi.
      Double-check that you’ve copied the code properly.
      That means that the reconnect() function was declared twice, which doesn’t happen in the code.
      Regards,
      Sara

      Reply
  23. Hi Sara and Rui,

    first of all: thanks for all your excellent work here, I’ve started some month ago with the ESPs and learned a lot from this source.
    RecentIy I struggled with an MQTT-project with random wired behavior, and I had no idea whats going on (I did some other projects before without any problems). The key was, that I used the same client-ID name like an existing project, and thanks of the remarks in this code I figured out what the problem was… perfect. I would never had thought in this direction…

    So please keep up this excellent work,

    Cheers, Giogio

    Reply
  24. Hi Guys,
    Loved this tutorial. Except for the minor visual changes to the the newer node-red (v1.0.6) i used, it was just as explained. I did find an issue with the sketch when you want to view temp in Fahrenheit and uncomment this section, an error is thrown due to hif not being declared. Changed hic to hif and all is good.

    // Computes temperature values in Celsius
    //float hic = dht.computeHeatIndex(t, h, false);
    //static char temperatureTemp[7];
    //dtostrf(hic, 6, 2, temperatureTemp);

    // Uncomment to compute temperature values in Fahrenheit
    float hif = dht.computeHeatIndex(f, h);
    static char temperatureTemp[7];
    dtostrf(hic, 6, 2, temperatureTemp); //THIS SHOULD BE hif, not hic

    Thanks for all your much appreciated work
    Terry

    Reply
  25. Hello,

    Thanks for this tutorial!
    I have a problem between the esp8266 client and node-red, node-red is not receiving data from the esp.
    I am receiving data from the esp on an android client but not on node-red.
    I also tested with MQTT.fx, I get on nodered but not on the android and esp client.

    Can you help me please.

    Thank you

    Nebtreak

    Reply
  26. hi, Sara, I have a small problem. I only have a bme280 sensor for the temp and humidity I am trying to make the code fit the sensor but I don’t get it to work do you know what I should change to make it work with the bme280.
    thx already

    Reply
  27. Sara,
    Can and will you help me
    I follow this to get the broker working.
    but my node wont give an output to the ui
    Perhaps you can add an import a node-red file with this internetpage?
    thanks Dick

    my node-red will not show the ui display
    [{“id”:”3eec258f.3679fa”,”type”:”comment”,”z”:”5f822691.6c8b48″,”name”:””,”info”:”switch – this will control the ESP8266 output\nmqtt output node – this will publish a message to the ESP8266 accordingly to the switch state\n2x mqtt input nodes – this nodes will be subscribed to the temperature and humidity topics to receive sensor data from the ESP\nchart – will display the temperature sensor readings\ngauge – will display the humidity sensor readings\n\nhttps://randomnerdtutorials.com/esp8266-and-node-red-with-mqtt/\n\nhttp://192.168.1.152:1880/ui\n\n”,”x”:390,”y”:60,”wires”:[]}]

    Reply
  28. Hello,

    Excellent introduction and tutorial on this exciting subject. Just I have a question. Will it be possible to deploy outside the normal Wifi network i.e on anywhere on the internet and control and acquire the data? Request your valuable input on this matter.

    Regards,

    Reply
  29. Hola buen dĂ­a, querĂ­a preguntar ya que me sirviĂł muchĂ­simo este tutorial, mi director de tesis me pide un Dashboard con node red para que el mismo sea visto dentro de mi red local como si estoy fuera de mi red. Se puede hacer…..digo…si levantĂł el Dashboard dentro de mi red local de la misma forma puedo ver mi Dashboard desde fuera de mi red local, agradecerĂ­a si puedes responder a mi inquietud. Saludos desde Argentina.

    Reply
  30. Hi Sara,
    a very nice project. It’s my first project to learn MQTT and Node-Red.
    The sketch was compiled without errors and I get the following output from the serial monitor:

    Connecting to UE
    …..
    WiFi connected – ESP IP address: 192.168.178.35
    Attempting MQTT connection … connected
    Humidity: 41.80% Temperature: 29.10 * C 84.38 * F Heat index: 28.88 * C
    Humidity: 41.40% Temperature: 29.10 * C 84.38 * F Heat index: 28.84 * C
    Humidity: 41.30% Temperature: 29.10 * C 84.38 * F Heat index: 28.83 * C
    Humidity: 41.30% Temperature: 29.20 * C 84.56 * F Heat index: 28.94 * C
    Humidity: 41.40% Temperature: 29.10 * C 84.38 * F Heat index: 28.84 * C
    Humidity: 41.40% Temperature: 29.10 * C 84.38 * F Heat index: 28.84 * C
    Humidity: 42.00% Temperature: 29.10 * C 84.38 * F Heat index: 28.90 * C
    Humidity: 41.80% Temperature: 29.10 * C 84.38 * F Heat index: 28.88 * C
    Humidity: 41.70% Temperature: 29.00 * C 84.20 * F Heat index: 28.77 * C
    Humidity: 41.70% Temperature: 29.00 * C 84.20 * F Heat index: 28.77 * C
    Humidity: 41.60% Temperature: 29.10 * C 84.38 * F Heat index: 28.86 * C
    Humidity: 41.60% Temperature: 29.10 * C 84.38 * F Heat index: 28.86 * C

    Unfortunately, the values ​​are not displayed in the node dashboard. I only get the switch of the lamp, where I can switch between on and off, and the empty temperature and humidity display. But no led-light on by breadboard.
    I went through the flows carefully and couldn’t find any fault. There was no debug message.
    Unfortunately I cannot attach a screenshot.
    Do you have an idea? Or you can email me the flow / json

    Many greetings Ulli

    Reply
  31. Hello Sara, I have the problem that the data of the DHT22 is displayed in the serial monitor, but not in the dashboard. Although I have already looked through the individual nodes of the flow several times. Since this is my first MQTT project, I can’t find the bug yet.
    Serial Monitor:
    Connecting to UE
    .
    WiFi connected – ESP IP address: 192.168.178.35
    Attempting MQTT connection…connected
    Humidity: 56.60 % Temperature: 29.80 *C
    Humidity: 38.70 % Temperature: 29.90 *C
    Humidity: 39.40 % Temperature: 29.70 *C
    Humidity: 39.00 % Temperature: 29.70 *C

    Many greetings Ulli

    Reply
  32. I was able to reproduce your project which allowed me to learn a ton about MQTT and NodeRed. However, I could not determine how to implement a user/password for MQTT on the client side, in PubSubClient. In order to make the code work, I had to disable security in the Mosquitto installation on the RRI. (I had enabled security during the Mosquitto installation).

    Is this possible?
    Thanks.

    Reply
  33. This looked to be a great project for my needs, so I dusted off my Rpi 0 w, and proceeded to update the software, which took quite a while (pi 0’s are a bit slow, but it’s what I wanted to use in my project).Node Red was also updated, and worked nothing like what I see here, AAMOF I dont see the tabs refered to. Only the info and debug appear. Since I am totally new to Node Red, I’m lost. Is there an update to this somewhere? Thanks

    Reply
    • John, I too ran into this problem when I did this project last year. The Node red interface is different form Rui’s and I believe another update (2.0) is out or coming. I ended up doing a lot of searching for tutorials that were recent, and there weren’t a lot of screenshots of the interface I was using. By googling, I was able to figure out how it worked for using this tutorial. Node Red is awesome and more powerful than I imagined. Here is a video, though integrating with home assistant, shows the interface I was faced with. https://www.youtube.com/watch?v=hBEb_FCLRU8. Also I spent a lot of time hovering over and pushing buttons to see what they did. Good luck and hope you get there as this is a a lot of fun,

      Reply
      • Thanks so much Terry I’ll check it out. I’ve loaded a brand new image (buster) with lots of differences. Much of what was loaded with Jessie was not there. I finally got Node Red installed, but getting warnings when trying to install Node Red Dashboard. I think it has to do with the version of node.js (if there is one installed LOL). I am quite the novice on Linux based systems, I came up through DOS thru all windows.

        Reply
  34. Yes Sara, I did that after fighting with the install process. Many warnings/errors there, especually when doing npm install, but I pushed ahead to find the UI had considerable differences

    Reply
  35. I don’t understand the reconnect function. Where does the “ESP8266Client” come from? You already give the client a different name at the beginning of the script (espClient)?

    Reply
  36. Hi team,
    I am using online broker(hive) for the connection between node-red and esp32 with no raspberry pi inbetween. I don’t know why led is not working. I am using two different network, esp32 is connected with hotspot and node-red is on localhost, both are using public hive broker.
    Can you suggest something….

    Reply
  37. Not sure what benefit of using Raspri PI in this tutorial. can NODE-RED installed on Windows PC achieve the same output in controlling ESP8266?

    Reply
    • Yes.
      But, you would need to have your computer always powered on for the mqtt broker to be active.
      So, it makes sense to use a Rpi. But, you can also use a computer, if you prefer.
      Regards,
      Sara

      Reply
  38. Hello Team Santos,
    Very nice guide – Thank you.
    Everything works as expected, except the Node Red UI displays Heat Index instead of Temperature. Is there a cure for this?
    Thanks for your work, rr

    Reply
  39. hello. i get this wierd problem when i tried verifiied the sketch in arduino ide. is there anybody that knows why it wont accept it

    sketch_mar19a:22:13: error: expected unqualified-id before numeric constant
    22 | const char* 200,100,0,200 = “REPLACE_WITH_YOUR_RPI_IP_ADDRESS”;
    | ^~~

    Reply
      • You can query your router for connections. Each router is different,so check your router manufacturer for details. A few years ago I downloaded an app called “who’s on my wifi” which lists connections to the wifi network,it helps a lot.

        Reply
  40. Great tutorial! Many thanks. My intention is to use the code with home control systems like HomeAssistant and Domoticz. I found that publishing strings are different between those applications. Because of that there is lot of users strugling ESP integrations with those apps it should be nice if tutorial can cover MQTT integration with those applications also. The main difference seems to be that one uses directory structue and the other (Domoticz) IDX numbers for connecting MQTT messages into application. The other difference is that messages can carry one or several sensor values depending implementation. Anyhow your tutorial is exellent, as always.

    Reply
  41. thank you your tutorial!
    I’m using ESP8266 in a home automation system with RPi and Node-Red.
    I set-up some ESP module to comunicate with RPi using MQTT.
    For diagnostic reason, I would like each ESP module acts also as a web-server publishing some information on a web page.
    Should be possible to host both configuration on a single ESP module?
    If positive, should I just “merge” the two sketches? Or…?
    thank you for your help!

    Reply
        • Did you get this to work?

          I have just started to do the same thing,, I am currently reading a temperature sensor , publishing the data to a web server so I can also do OTA updates.
          Next step is to send a MQTT message.
          but the following
          // Create AsyncWebServer object on port 80
          AsyncWebServer server(80);

          // try and create mqtt client
          const char* mqtt_server = “192.168.4.183”;
          PubSubClient client(server);

          generates an error
          no matching function for call to ‘PubSubClient::PubSubClient(AsyncWebServer&)’

          I have had a quick look at the PubSubClient documentation and the only example it gives is for a WifiClient.

          If you managed to sort this it would save me having to do some further investigation.

          Cheers
          Steve

          Reply
          • You’re passing the server variable which refers to the web server instead of the MQTT client.

            const char* mqtt_server = “192.168.4.183”;
            PubSubClient client(server); –> this is wrong

            Regards,
            Sara

  42. Firstly – Thanks for this guys. I have to admit I didn’t read the majority of the comments so please excuse if there is an answer above. Can this project be done with just 1 raspberry pi running node red and mosquito (so without the esp8266 – the sensor readings are usb serial ported into the pi from weatherbit on microbit) or 2 raspberry pis or one pi and the node red and mosquito on my Linus machine I have for home assistant? Does it always have to have an esp board? I have the spare esp boards but for the life of my I can’t figure out how to get the microbit data (they export radio/Bluetooth and serial rx/TX and usb serial) into the esp or then parse the data to then make sense of it. I keep finding half tutorials from forever ago randomly on the Web and nothing is working for me. I managed to get the serial data into node red from pi but I can’t figure out the function call process. It’s proving to be the most complex weather station in the world…. You’d think I was sampling Mars 😀 if there is a particular book or course that covers this, please let me know. I’m pulling my hair out atm.

    Reply
    • Hi.
      No, you don’t need an ESP8266 or ESP32.
      You can also connect peripherals directly to the Raspberry Pi without the need of other microcontroler.
      Unfortunately, we don’t have any tutorials with the microbit.
      Does it have Wi-Fi? If so, there’s probably a sample code for MQTT with microbit.
      If not, you’ll need to send the data via RX/TX, but unfortunately, we don’t have any tutorials about the microbit.
      Regards,
      Sara

      Reply
  43. Hi,
    thank you very much for this great and detailed tutorial. Could you please help with my question? I have mqtt broker running on my windows laptop not on a raspberry. Everything works fine: Esp is connected to the broker on localhost. The Node red is set up as you described it and is connected too. But there are no messages arriving. Is there something to change in my windows installation?
    Thank you very much,
    best regards, Petra

    Reply
    • Hi.
      You cannot use localhost to refer to the broker in your ESP8266 code.
      You need to use the broker IP address, in this case, it will be your computer IP address on your local network.
      Regards,
      Sara

      Reply
  44. Serial monitor does not show the outputs inside the callback funtions such as “Message arrived on topic:”. That means callback function is not invoked.what could be the reasons for that.My all connection are correct.

    Reply

Leave a Comment

Download Our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.