ESP32 MQTT – Publish DHT11/DHT22 Temperature and Humidity Readings (Arduino IDE)

Learn how to publish temperature and humidity readings from a DHT11 or DHT22 sensor via MQTT with the ESP32 to any platform that supports MQTT or any MQTT client. As an example, we’ll publish sensor readings to Node-RED Dashboard and the ESP32 will be programmed using Arduino IDE.

ESP32 MQTT Publish DHT22 or DHT11 Sensor Readings Arduino IDE

Recommended reading: What is MQTT and How It Works

Project Overview

The following diagram shows a high-level overview of the project we’ll build.

DHT11/DHT22 Publish Readings Node-Red MQTT How it works and project overview
  • The ESP32 requests temperature and humidity readings from the DHT11 or DHT22 sensor;
  • Temperature readings are published in the esp32/dht/temperature topic;
  • Humidity readings are published in the esp32/dht/humidity topic;
  • Node-RED is subscribed those topics;
  • Node-RED receives the sensor readings and displays them on gauges;
  • You can receive the readings in any other platform that supports MQTT and handle the readings as you want.

Prerequisites

Before proceeding with this tutorial, make sure you check the following prerequisites.

Arduino IDE

We’ll program the ESP32 using Arduino IDE, so make sure you have the ESP32 add-on installed.

MQTT Broker

Installing Mosquitto MQTT broker Raspberry Pi

To use MQTT, you need a broker. We’ll be using Mosquitto broker installed on a Raspberry Pi. Read How to Install Mosquitto Broker on Raspberry Pi.

You can use any other MQTT broker, including a cloud MQTT broker. We’ll show you how to do that in the code later on.

If you’re not familiar with MQTT make sure you read our introductory tutorial: What is MQTT and How It Works.

MQTT Libraries

To use MQTT with the ESP32 we’ll use the Async MQTT Client Library.

Installing the Async MQTT Client Library

  1. Click here to download the Async MQTT client library. You should have a .zip folder in your Downloads folder
  2. Unzip the .zip folder and you should get async-mqtt-client-master folder
  3. Rename your folder from async-mqtt-client-master to async_mqtt_client
  4. Move the async_mqtt_client folder to your Arduino IDE installation libraries folder
  5. Finally, re-open your Arduino IDE

Alternatively, you can go to Sketch > Include Library > Add . ZIP library and select the library you’ve just downloaded.

Installing the Async TCP Library

To use MQTT with the ESP, you also need the Async TCP library.

  1. Click here to download the Async TCP client library. You should have a .zip folder in your Downloads folder
  2. Unzip the .zip folder and you should get AsyncTCP-master folder
  3. Rename your folder from AsyncTCP-master to AsyncTCP
  4. Move the AsyncTCP folder to your Arduino IDE installation libraries folder
  5. Finally, re-open your Arduino IDE

Alternatively, you can go to Sketch > Include Library > Add . ZIP library and select the library you’ve just downloaded.

DHT Sensor Libraries

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

After installing the libraries, restart your Arduino IDE.

To learn more about the DHT11 or DHT22 temperature sensor, read our guide: ESP32 with DHT11/DHT22 Temperature and Humidity Sensor using Arduino IDE

Parts Required

For this tutorial you need the following parts:

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!

Schematic Diagram

Wire the DHT11 or DHT22 to the ESP32 as shown in the following schematic diagram with the data pin connected to GPIO 4.

DHT11 DHT22 wiring to ESP32 schematic diagram

Note: if you have a DHT sensor in a breakout board, it comes with only three pins and with an internal pull-up resistor on pin 2, so you don’t need to connect the resistor. You just need to wire VCC, data and GND.

In this example, we’re connecting the DHT data pin to GPIO 4. However, you can use any other suitable digital pin.

Learn how to use the ESP32 GPIOs with our guide: ESP32 Pinout Reference: Which GPIO pins should you use?

Code

Copy the following code to your Arduino IDE. To make it work for you, you need to insert your network credentials as well as the MQTT broker details.

/*
  Rui Santos
  Complete project details at https://RandomNerdTutorials.com/esp32-mqtt-publish-dht11-dht22-arduino/
  
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files.
  
  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.
*/

#include "DHT.h"
#include <WiFi.h>
extern "C" {
  #include "freertos/FreeRTOS.h"
  #include "freertos/timers.h"
}
#include <AsyncMqttClient.h>

#define WIFI_SSID "REPLACE_WITH_YOUR_SSID"
#define WIFI_PASSWORD "REPLACE_WITH_YOUR_PASSWORD"

// Raspberry Pi Mosquitto MQTT Broker
#define MQTT_HOST IPAddress(192, 168, 1, XXX)
// For a cloud MQTT broker, type the domain name
//#define MQTT_HOST "example.com"
#define MQTT_PORT 1883

// Temperature MQTT Topics
#define MQTT_PUB_TEMP "esp32/dht/temperature"
#define MQTT_PUB_HUM  "esp32/dht/humidity"

// Digital pin connected to the DHT sensor
#define DHTPIN 4  

// Uncomment whatever DHT sensor type you're using
//#define DHTTYPE DHT11   // DHT 11
#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
//#define DHTTYPE DHT21   // DHT 21 (AM2301)   

// Initialize DHT sensor
DHT dht(DHTPIN, DHTTYPE);

// Variables to hold sensor readings
float temp;
float hum;

AsyncMqttClient mqttClient;
TimerHandle_t mqttReconnectTimer;
TimerHandle_t wifiReconnectTimer;

unsigned long previousMillis = 0;   // Stores last time temperature was published
const long interval = 10000;        // Interval at which to publish sensor readings

void connectToWifi() {
  Serial.println("Connecting to Wi-Fi...");
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
}

void connectToMqtt() {
  Serial.println("Connecting to MQTT...");
  mqttClient.connect();
}

void WiFiEvent(WiFiEvent_t event) {
  Serial.printf("[WiFi-event] event: %d\n", event);
  switch(event) {
    case SYSTEM_EVENT_STA_GOT_IP:
      Serial.println("WiFi connected");
      Serial.println("IP address: ");
      Serial.println(WiFi.localIP());
      connectToMqtt();
      break;
    case SYSTEM_EVENT_STA_DISCONNECTED:
      Serial.println("WiFi lost connection");
      xTimerStop(mqttReconnectTimer, 0); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi
      xTimerStart(wifiReconnectTimer, 0);
      break;
  }
}

void onMqttConnect(bool sessionPresent) {
  Serial.println("Connected to MQTT.");
  Serial.print("Session present: ");
  Serial.println(sessionPresent);
}

void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
  Serial.println("Disconnected from MQTT.");
  if (WiFi.isConnected()) {
    xTimerStart(mqttReconnectTimer, 0);
  }
}

/*void onMqttSubscribe(uint16_t packetId, uint8_t qos) {
  Serial.println("Subscribe acknowledged.");
  Serial.print("  packetId: ");
  Serial.println(packetId);
  Serial.print("  qos: ");
  Serial.println(qos);
}
void onMqttUnsubscribe(uint16_t packetId) {
  Serial.println("Unsubscribe acknowledged.");
  Serial.print("  packetId: ");
  Serial.println(packetId);
}*/

void onMqttPublish(uint16_t packetId) {
  Serial.print("Publish acknowledged.");
  Serial.print("  packetId: ");
  Serial.println(packetId);
}

void setup() {
  Serial.begin(115200);
  Serial.println();

  dht.begin();
  
  mqttReconnectTimer = xTimerCreate("mqttTimer", pdMS_TO_TICKS(2000), pdFALSE, (void*)0, reinterpret_cast<TimerCallbackFunction_t>(connectToMqtt));
  wifiReconnectTimer = xTimerCreate("wifiTimer", pdMS_TO_TICKS(2000), pdFALSE, (void*)0, reinterpret_cast<TimerCallbackFunction_t>(connectToWifi));

  WiFi.onEvent(WiFiEvent);

  mqttClient.onConnect(onMqttConnect);
  mqttClient.onDisconnect(onMqttDisconnect);
  //mqttClient.onSubscribe(onMqttSubscribe);
  //mqttClient.onUnsubscribe(onMqttUnsubscribe);
  mqttClient.onPublish(onMqttPublish);
  mqttClient.setServer(MQTT_HOST, MQTT_PORT);
  // If your broker requires authentication (username and password), set them below
  //mqttClient.setCredentials("REPlACE_WITH_YOUR_USER", "REPLACE_WITH_YOUR_PASSWORD");
  connectToWifi();
}

void loop() {
  unsigned long currentMillis = millis();
  // Every X number of seconds (interval = 10 seconds) 
  // it publishes a new MQTT message
  if (currentMillis - previousMillis >= interval) {
    // Save the last time a new reading was published
    previousMillis = currentMillis;
    // New DHT sensor readings
    hum = dht.readHumidity();
    // Read temperature as Celsius (the default)
    temp = dht.readTemperature();
    // Read temperature as Fahrenheit (isFahrenheit = true)
    //temp = dht.readTemperature(true);

    // Check if any reads failed and exit early (to try again).
    if (isnan(temp) || isnan(hum)) {
      Serial.println(F("Failed to read from DHT sensor!"));
      return;
    }
    
    // Publish an MQTT message on topic esp32/dht/temperature
    uint16_t packetIdPub1 = mqttClient.publish(MQTT_PUB_TEMP, 1, true, String(temp).c_str());                            
    Serial.printf("Publishing on topic %s at QoS 1, packetId: %i", MQTT_PUB_TEMP, packetIdPub1);
    Serial.printf("Message: %.2f \n", temp);

    // Publish an MQTT message on topic esp32/dht/humidity
    uint16_t packetIdPub2 = mqttClient.publish(MQTT_PUB_HUM, 1, true, String(hum).c_str());                            
    Serial.printf("Publishing on topic %s at QoS 1, packetId %i: ", MQTT_PUB_HUM, packetIdPub2);
    Serial.printf("Message: %.2f \n", hum);
  }
}

View raw code

How the Code Works

The following section imports all the required libraries.

#include "DHT.h"
#include <WiFi.h>
extern "C" {
  #include "freertos/FreeRTOS.h"
  #include "freertos/timers.h"
}
#include <AsyncMqttClient.h>

Include your network credentials on the following lines.

#define WIFI_SSID "REPLACE_WITH_YOUR_SSID"
#define WIFI_PASSWORD "REPLACE_WITH_YOUR_PASSWORD"

Insert the Raspberry Pi IP address, so that the ESP32 connects to your broker.

#define MQTT_HOST IPAddress(192, 168, 1, 106)

If you’re using a cloud MQTT broker, insert the broker domain name, for example:

#define MQTT_HOST "example.com"

Define the MQTT port.

#define MQTT_PORT 1883

The temperature and humidity will be published on the following topics:

#define MQTT_PUB_TEMP "esp32/dht/temperature"
#define MQTT_PUB_HUM  "esp32/dht/humidity"

Define the GPIO that the DHT sensor data pin is connected to. In our case, it is connected to GPIO 4.

Uncomment the DHT sensor type you’re using. In our example, we’re using the DHT22.

//#define DHTTYPE DHT11   // DHT 11
#define DHTTYPE DHT22     // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

Initialize the DHT sensor on the pin and type defined earlier.

DHT dht(DHTPIN, DHTTYPE);

The temp and hum variables will hold the temperature and humidity values from the DHT22 sensor.

float temp;
float hum;

Create an AsyncMqttClient object called mqttClient to handle the MQTT client and timers to reconnect to your MQTT broker and router when it disconnects.

AsyncMqttClient mqttClient;
TimerHandle_t mqttReconnectTimer;
TimerHandle_t wifiReconnectTimer;

Then, create some auxiliary timer variables to publish the readings every 10 seconds. You can change the delay time on the interval variable.

unsigned long previousMillis = 0;  // Stores last time temperature was published
const long interval = 10000;       // Interval at which to publish sensor readings

Note: the DHT11 and DHT22 have a low sampling rate. You can only request DHT11 readings every second, or every two seconds for the DHT22.

MQTT functions: connect to Wi-Fi, connect to MQTT, and Wi-Fi events

We haven’t added any comments to the functions defined in the next code section. Those functions come with the Async Mqtt Client library. The function’s names are pretty self-explanatory.

For example, the connectToWifi() connects your ESP32 to your router:

void connectToWifi() {
  Serial.println("Connecting to Wi-Fi...");
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
}

The connectToMqtt() connects your ESP32 to your MQTT broker:

void connectToMqtt() {
  Serial.println("Connecting to MQTT…");
  mqttClient.connect();
 }

The WiFiEvent() function is responsible for handling the Wi-Fi events. For example, after a successful connection with the router and MQTT broker, it prints the ESP32 IP address. On the other hand, if the connection is lost, it starts a timer and tries to reconnect.

void WiFiEvent(WiFiEvent_t event) {
  Serial.printf("[WiFi-event] event: %d\n", event);
  switch(event) {
    case SYSTEM_EVENT_STA_GOT_IP:
      Serial.println("WiFi connected");
      Serial.println("IP address: ");
      Serial.println(WiFi.localIP());
      connectToMqtt();
      break;
    case SYSTEM_EVENT_STA_DISCONNECTED:
      Serial.println("WiFi lost connection");
      xTimerStop(mqttReconnectTimer, 0);
      xTimerStart(wifiReconnectTimer, 0);
      break;
  }
}

The onMqttConnect() function runs after starting a session with the broker.

void onMqttConnect(bool sessionPresent) {
  Serial.println("Connected to MQTT.");
  Serial.print("Session present: ");
  Serial.println(sessionPresent);
}

MQTT functions: disconnect and publish

If the ESP32 loses connection with the MQTT broker, it calls the onMqttDisconnect function that prints that message in the serial monitor.

void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
  Serial.println("Disconnected from MQTT.");
  if (WiFi.isConnected()) { 
    xTimerStart(mqttReconnectTimer, 0);
  }
}

When you publish a message to an MQTT topic, the onMqttPublish() function is called. It prints the packet id in the Serial Monitor.

void onMqttPublish(uint16_t packetId) {
  Serial.println("Publish acknowledged.");
  Serial.print("  packetId: ");
  Serial.println(packetId);
}

Basically, all these functions that we’ve just mentioned are callback functions. So, they are executed asynchronously.

setup()

Now, let’s proceed to the setup(). Initialize the DHT sensor.

dht.begin();

The next two lines create timers that will allow both the MQTT broker and Wi-Fi connection to reconnect, in case the connection is lost.

mqttReconnectTimer = xTimerCreate("mqttTimer", pdMS_TO_TICKS(2000), pdFALSE, (void*)0, reinterpret_cast<TimerCallbackFunction_t>(connectToMqtt));
wifiReconnectTimer = xTimerCreate("wifiTimer", pdMS_TO_TICKS(2000), pdFALSE, (void*)0, reinterpret_cast<TimerCallbackFunction_t>(connectToWifi));

The following line assigns a callback function, so when the ESP32 connects to your Wi-Fi, it will execute the WiFiEvent() function to print the details described earlier.

WiFi.onEvent(WiFiEvent);

Finally, assign all the callback functions. This means that these functions will be executed automatically when needed. For example, when the ESP32 connects to the broker, it automatically calls the onMqttConnect() function, and so on.

mqttClient.onConnect(onMqttConnect);
mqttClient.onDisconnect(onMqttDisconnect);
//mqttClient.onSubscribe(onMqttSubscribe);
//mqttClient.onUnsubscribe(onMqttUnsubscribe);
mqttClient.onPublish(onMqttPublish);
mqttClient.setServer(MQTT_HOST, MQTT_PORT);

Broker Authentication

If your broker requires authentication, uncomment the following line and insert your credentials (username and password).

mqttClient.setCredentials("REPlACE_WITH_YOUR_USER", "REPLACE_WITH_YOUR_PASSWORD");

Finally, connect to Wi-Fi.

connectToWifi();

loop()

In the loop(), you create a timer that will allow you to get new temperature and humidity readings from the DHT sensor and publishing them on the corresponding topic every 10 seconds.

unsigned long currentMillis = millis();
// Every X number of seconds (interval = 10 seconds) 
// it publishes a new MQTT message
if (currentMillis - previousMillis >= interval) {
  // Save the last time a new reading was published
  previousMillis = currentMillis;
  // New DHT sensor readings
  hum = dht.readHumidity();
  // Read temperature as Celsius (the default)
  temp = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  //temp = dht.readTemperature(true);

Learn more about getting readings from the DHT11 or DHT22 sensors: ESP32 with DHT11/DHT22 Temperature and Humidity Sensor using Arduino IDE.

Publishing to topics

To publish the readings on the corresponding MQTT topics, use the next lines:

uint16_t packetIdPub1 = mqttClient.publish(MQTT_PUB_TEMP, 1, true, String(temp).c_str());
uint16_t packetIdPub2 = mqttClient.publish(MQTT_PUB_HUM, 1, true, String(hum).c_str());

Basically, use the publish() method on the mqttClient object to publish data on a topic. The publish() method accepts the following arguments, in order:

  • MQTT topic (const char*)
  • QoS (uint8_t): quality of service – it can be 0, 1 or 2
  • retain flag (bool): retain flag
  • payload (const char*) – in this case, the payload corresponds to the sensor reading

The QoS (quality of service) is a way to guarantee that the message is delivered. It can be one of the following levels:

  • 0: the message will be delivered once or not at all. The message is not acknowledged. There is no possibility of duplicated messages;
  • 1: the message will be delivered at least once, but may be delivered more than once;
  • 2: the message is always delivered exactly once;
  • Learn about MQTT QoS.

Uploading the code

With your Raspberry Pi powered on and running the Mosquitto MQTT broker, upload the code to your ESP32.

Open the Serial Monitor at a baud rate of 115200 and you’ll see that the ESP32 starts publishing messages on the topics we’ve defined previously.

ESP32 Publish MQTT DHT11 DHT22 Temperature Humidity Serial Monitor

Preparing Node-RED Dashboard

The ESP32 is publishing temperature readings every 10 seconds on the esp32/dht/temperature and esp32/dht/humidity topics. Now, you can use any dashboard that supports MQTT or any other device that supports MQTT to subscribe to those topics and receive the readings.

As an example, we’ll create a simple flow using Node-RED to subscribe to those topics and display the readings on gauges.

If you don’t have Node-RED installed, follow the next tutorials:

Having Node-RED running on your Raspberry Pi, go to your Raspberry Pi IP address followed by :1880.

http://raspberry-pi-ip-address:1880

The Node-RED interface should open. Drag two MQTT in nodes, and two gauge nodes to the flow.

ESP32 DHT11 DHT22 Arduino IDE

Click the MQTT node and edit its properties.

MQTT In Node ESP32 Publish Temperature Humidity Node-RED Flow

The Server field refers to the MQTT broker. In our case, the MQTT broker is the Raspberry Pi, so it is set to localhost:1883. If you’re using a Cloud MQTT broker, you should change that field.

Insert the topic you want to be subscribed to and the QoS. This previous MQTT node is subscribed to the esp32/dht/temperature topic.

Click on the other MQTT in node and edit its properties with the same server, but for the other topic: esp32/dht/humidity.

Click on the gauge nodes and edit its properties for each reading. The following node is set for the temperature readings. Edit the other chart node for the humidity readings.

ESP32 Gauge Temperature Humidity Node-RED Flow

Wire your nodes as shown below:

ESP32 MQTT Publish Temperature Humidity Node-RED Flow

Finally, deploy your flow (press the button on the upper right corner).

Deploy Node-RED button

Alternatively, you can go to Menu > Import and copy the following to your Clipboard to create your Node-RED flow.

[{"id":"5a45b8da.52b0d8","type":"mqtt in","z":"b01416d3.f69f38","name":"","topic":"esp32/dht/temperature","qos":"1","datatype":"auto","broker":"8db3fac0.99dd48","x":300,"y":60,"wires":[["3042e15e.80a4ee"]]},{"id":"3042e15e.80a4ee","type":"ui_gauge","z":"b01416d3.f69f38","name":"","group":"37de8fe8.46846","order":2,"width":0,"height":0,"gtype":"gage","title":"Temperature","label":"ºC","format":"{{value}}","min":0,"max":"40","colors":["#00b500","#f7df09","#ca3838"],"seg1":"","seg2":"","x":590,"y":60,"wires":[]},{"id":"8ff168f0.0c74a8","type":"mqtt in","z":"b01416d3.f69f38","name":"","topic":"esp32/dht/humidity","qos":"1","datatype":"auto","broker":"8db3fac0.99dd48","x":290,"y":140,"wires":[["29251f29.6687c"]]},{"id":"29251f29.6687c","type":"ui_gauge","z":"b01416d3.f69f38","name":"","group":"37de8fe8.46846","order":2,"width":0,"height":0,"gtype":"gage","title":"Humidity","label":"%","format":"{{value}}","min":"30","max":"100","colors":["#53a4e6","#1d78a9","#4e38c9"],"seg1":"","seg2":"","x":580,"y":140,"wires":[]},{"id":"8db3fac0.99dd48","type":"mqtt-broker","z":"","name":"","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"37de8fe8.46846","type":"ui_group","z":"","name":"DHT","tab":"53b8c8f9.cfbe48","order":1,"disp":true,"width":"6","collapse":false},{"id":"53b8c8f9.cfbe48","type":"ui_tab","z":"","name":"Home","icon":"dashboard","order":2,"disabled":false,"hidden":false}]

View raw code

Demonstration

Go to your Raspberry Pi IP address followed by :1880/ui.

http://raspberry-pi-ip-address:1880/ui

You should get access to the current DHT temperature and humidity readings on the Dashboard. You can use other dashboard-type nodes to display the readings on different ways.

ESP32 MQTT Publish Temperature Humidity Node-RED Dashboard

That’s it! You have your ESP32 board publishing DHT temperature and humidity readings to Node-RED via MQTT.

Wrapping Up

MQTT is a great communication protocol to exchange small amounts of data between devices. In this tutorial you’ve learned how to publish temperature and humidity readings from a DHT sensor with the ESP32 to different MQTT topics. Then, you can use any device or home automation platform to subscribe to those topics and receive the readings.

Instead of a DHT11 or DHT22 sensor, you can use any other sensor like a DS18B20 temperature sensor or BME280 sensor:

We hope you’ve found this tutorial useful. If you want to learn more about the ESP32, take a look at our resources:

Thanks for reading.



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!

52 thoughts on “ESP32 MQTT – Publish DHT11/DHT22 Temperature and Humidity Readings (Arduino IDE)”

    • did you ever get an answer? why have you changed the MQTT lib, from “PubSubClient” to “Async MQTT client” ?

      Any good reason for that ?

      Reply
        • Hi Joe.
          I’m sorry.
          We received many comments on different tutorials every day. It is very difficult to keep track of all the questions.
          I’m sorry that I missed your question.
          I’ve already answered your question.

          We’re using the AsyncMQTT library because we like it. We specially like the callback functions of the Async MQTT Client library. So, it is easy to make something when an event happens, like disconnecting from broker or disconnecting from Wi-Fi.

          I’ve used the PubSubClient library in the past and it also worked well.

          Regards,
          Sara

          Reply
  1. Obrigado!
    Não estou com materiais para testar, estamos em casa devido ao Covid 19. Encaminhei para outros Colegas.
    Carlos bruni

    Nota: Sara e Rui Santos , cuidem-se!

    Reply
  2. Hello
    can I change the IP address for esp32 when connected to the router because when using esp32 as the server doesn’t work on my router at home and when changed the router it’s work … why that happened do you think from the same IP the router give every time which has problem 192.168.1.7
    or channel or what ???
    the new router give 192.168.43.107 as IP and work correctly.
    how can I fix the old router ???

    Reply
    • Hi Angel.
      It is just a matter of preference.
      We specially like the callback functions of the Async MQTT Client library. So, it is easy to make something when an event happens, like disconnecting from broker or disconnecting from Wi-Fi.
      Regards,
      Sara

      Reply
  3. Hi, Thanks for the tutorial. I’m one of your clients who purchased your course. I setup everything as per the instructions and I had it working and reading temperature and humidity in the serial panel. There were several iterations of the following, for a minute or two before it stabilized and started to show the values. Nevertheless, it now iterates and doesn’t get to show the temp and humidity values. Seems like an intermittent thing.

    “Rebooting…
    ets Jun 8 2016 00:22:57

    rst:0xc (SW_CPU_RESET),boot:0x17 (SPI_FAST_FLASH_BOOT)
    configsip: 0, SPIWP:0xee
    clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
    mode:DIO, clock div:1
    load:0x3fff0018,len:4
    load:0x3fff001c,len:1044
    load:0x40078000,len:8896
    load:0x40080400,len:5816
    entry 0x400806ac

    Connecting to Wi-Fi…
    [WiFi-event] event: 0
    [WiFi-event] event: 2
    [WiFi-event] event: 4
    [WiFi-event] event: 7
    WiFi connected
    IP address:
    192.168.86.41
    Connecting to MQTT…
    Publish acknowledged. packetId: 21584
    Publish acknowledged. packetId: 29556
    Guru Meditation Error: Core 1 panic’ed (LoadProhibited). Exception was unhandled.
    Core 1 register dump:
    PC : 0x400d2d4a PS : 0x00060230 A0 : 0x800d2df5 A1 : 0x3ffcfd40
    A2 : 0x3ffc1010 A3 : 0x00000000 A4 : 0x3ffc1110 A5 : 0x00000003
    A6 : 0x3ffd069e A7 : 0x0000002f A8 : 0x0000000a A9 : 0x0000000a
    A10 : 0x00000000 A11 : 0x00000001 A12 : 0x0000002f A13 : 0x00000000
    A14 : 0x0000002a A15 : 0x3ffb1f90 SAR : 0x0000000a EXCCAUSE: 0x0000001c
    EXCVADDR: 0x00000000 LBEG : 0x400014fd LEND : 0x4000150d LCOUNT : 0xffffffff

    Backtrace: 0x400d2d4a:0x3ffcfd40 0x400d2df2:0x3ffcfdb0 0x400d4f51:0x3ffcfdd0 0x400d4f95:0x3ffcfe10 0x400d53f2:0x3ffcfe30 0x40088b81:0x3ffcfe60

    Rebooting…
    ets Jun 8 2016 00:22:57”

    Reply
  4. Never mind, I had changed the MQTT_PORT to 1880 thinking that it needed to be the same as the Rasp Berry Pi port. I changed it back to 1883 and now it works perfectly.

    Reply
    • Hi.
      In this particular example, you need to make Node-RED (Raspberry Pi) accessible from anywhere.
      We don’t have a tutorial about that. But you can search “access Raspberry Pi from anywhere” or something similar.
      I hope this helps.
      Regards,
      Sara

      Reply
  5. Hello, thank you for great article.

    May I ask you, I just can not get it running in PlatformIO as:
    src/main.cpp: At global scope:
    src/main.cpp:44:16: error: variable or field ‘WiFiEvent’ declared void
    void WiFiEvent(WiFiEvent_t event) {
    ^
    src/main.cpp:44:16: error: ‘WiFiEvent_t’ was not declared in this scope

    Building it in Arduino IDE works 🙁

    Reply
  6. Thanks for the good explanation on this tutorial. I have followed step by step. However, it cannot be connected to the MQTT broker (Raspberry). The serial monitor prints like this continuously.

    11:27:42.919 -> Connecting to MQTT…
    11:27:47.696 -> Publishing on topic esp32/dht/temperature at QoS 1, packetId: 0Message: 34.60
    11:27:47.696 -> Publishing on topic esp32/dht/humidity at QoS 1, packetId 0: Message: 55.00
    11:27:57.669 -> Publishing on topic esp32/dht/temperature at QoS 1, packetId: 0Message: 34.60
    11:27:57.669 -> Publishing on topic esp32/dht/humidity at QoS 1, packetId 0: Message: 55.00
    11:28:01.444 -> Disconnected from MQTT.
    11:28:03.447 -> Connecting to MQTT…
    11:28:07.677 -> Publishing on topic esp32/dht/temperature at QoS 1, packetId: 0Message: 34.60
    11:28:07.677 -> Publishing on topic esp32/dht/humidity at QoS 1, packetId 0: Message: 55.10
    11:28:17.680 -> Publishing on topic esp32/dht/temperature at QoS 1, packetId: 0Message: 34.60
    11:28:17.680 -> Publishing on topic esp32/dht/humidity at QoS 1, packetId 0: Message: 55.10
    11:28:21.964 -> Disconnected from MQTT.
    11:28:23.953 -> Connecting to MQTT…

    Why did this happen and how to solve it? I’ve made sure that Mosquitto is working properly using this tutorial https://randomnerdtutorials.com/testing-mosquitto-broker-and-client-on-raspbbery-pi/

    Thankyou

    Reply
    • Hi Ibnuh,.,.,
      I also have this issue. Trying to figure out why.
      Did you solve the issue?.,. Could you please give me a hand with that?.,.,

      Thank you.,..,

      Reply
  7. When testing the temperature and humidity data from the ESP32 attached to a laptop running the Arduino IDE the output provides two sets of readings then disconnects from the MQTT. It reconnects and provides four additional readings – repeating this process.

    As regards the gauges on node-red – both remain at zero. The node-red mqtt in shows connection has been made. I have placed the USER and Password into the Arduino code – made no difference.

    Any suggestions welcome.

    Reply
  8. Hi. I would like to run DHT22 on ESP32-CAM together with a working Camera web server. Do you think it is possible?

    I noticed that the connection to the wifi network for the web server and the program with mqtt communication is solved differently.

    The idea is to view the room, read the temperature and turn on the light on mqtt.

    Reply
  9. Hello, I am new to the world of ESP32, the tutorial worked correctly but I need to add 5 more temperature sensors, would you help me with a reference or tutorial please

    Reply
    • Hi.
      I never tried multiple DHT sensors on the ESP32.
      But, I think you just need to create multiple instances of the DHT object on different pins and then use the methods to get temperature on each of them.

      For example:
      DHT dht1(DHTPIN1, DHTTYPE);
      DHT dht2(DHTPIN2, DHTTYPE);
      DHT dht3(DHTPIN3, DHTTYPE);

      Then, init all sensors:
      dht1.begin();
      dht2.begin();
      dht3.begin();

      And read temperature for each sensor:
      temp1 = dht1.readTemperature();
      temp2 = dht2.readTemperature();
      temp3 = dht3.readTemperature();

      I hope this helps.
      Regards,
      Sara

      Reply
  10. could you please make a tutorial ”how to publish sensors value in thingspeak through mosquitto broker installed in raspberry pi”

    Reply
  11. Hello, I’m new to MQTT and Arduino. After uploading the sketch to ESP32, I don’t see anything on the serial monitor. Can someone help me please?

    Reply
    • Hi.
      After uploading a new code to the ESP32, open the Serial Monitor. Make sure you have the right Baud rate selected. Then, press the ESP32 on-board RESET button to restart the board and start running the code.
      I hope this helps.
      Regards,
      Sara

      Reply
  12. Hi, I have tried this example above, however the follow message showed :

    variable or field ‘WiFiEvent’ declared void

    I installed all libraries and the message showed again.
    Please could you help me?

    Reply
    • Hi.
      What version of the ESP32 boards version do you have?
      Maybe you need to downgrade to version 2.0.1. Tools > Boards > Boards Manager > ESP32.
      Regards,
      Sara

      Reply
  13. Hi, I have tried this example above, however the follow message showed :

    93_16_ error_ ‘WiFiEvent_t’ was not declared in this scope
    variable or field ‘WiFiEvent’ declared void

    I installed all libraries and the message showed again.
    Please could you help me?

    Reply
  14. i am trying to upload the code to my board and i am getting the following error:
    Build options changed, rebuilding all
    sketch_nov8a_DHT11_MQTT_node_red:16:12: error: freertos/FreeRTOS.h: No such file or directory
    16 | #include “freertos/FreeRTOS.h”
    | ^~~~~~~~~~~~~~~~~~~~~
    compilation terminated.

    exit status 1
    freertos/FreeRTOS.h: No such file or directory

    Any ideas please?

    Reply
  15. hi, in my case the code works for a day and after then i can see blinking the esp32 for every 2 seconds (so i think it operates) but nothing appears on my mqtt broker. wireless is working but the esp is not sending data. after a reset it works again (but everyday reset the device is not what it is about, isn´t it?)

    thx tom

    Reply
      • Hi Sara,

        i have to check it with my notebook with serial monitor, at the moment i´ve just connect the esp to a 5v power supply. problem is that the error appears sometimes a day after power on so it´s a bit tricky to get errors on the serial monitor. give me a few days. i´ll get back to you soon, thx so far. tom

        Reply
        • Hi!

          below see the serial monitor. so no error, just stopped to work. the esp is blinking every 2 seconds seems it does something, it is pingable so the wlan is still there.

          Publishing on topic esp32/dht/temperature at QoS 1, packetId: 3093Message: 22.20
          Publishing on topic esp32/dht/humidity at QoS 1, packetId 3094: Publish acknowledged. packetId: 3093
          Message: 50.90
          Publish acknowledged. packetId: 3094
          Publishing on topic esp32/dht/temperature at QoS 1, packetId: 3095Message: 22.30
          Publishing on topic esp32/dht/humidity at QoS 1, packetId 3096: Publish acknowledged. packetId: 3095
          Message: 50.90
          Publish acknowledged. packetId: 3096
          Publishing on topic esp32/dht/temperature at QoS 1, packetId: 3097Message: 22.30
          Publishing on topic esp32/dht/humidity at QoS 1, packetId 3098: Publish acknowledged. packetId: 3097
          Message: 50.90
          Publish acknowledged. packetId: 3098

          any ideas about that?

          Reply
  16. Hi I am trying to use my laptop hotspot as a static IP however, when ever I attpemt to connect to the ESP, it says “Wifi lost connection” I am not sure how to troubleshoot. Thank you

    Reply
  17. Hello, First of all thanks for such a vast content on these topics.
    My question is: Is it necessary to use MQTT broker if I just want to show readings on Node Red? I don’t want other devices to communicate with each other.

    Reply
  18. Thank you for this recipe. I’m trying to run it on a normal ESP32 dev board, sending to the mqtthq public service.
    Testing w/o DHT11, faking input, it seems to constantly show losing wifi and re-establishing. Very occasionally, it does publish a message and get a response.

    16:57:50.948 -> WiFi connected
    16:57:50.948 -> IP address:
    16:57:50.948 -> 192.168.1.195
    16:57:51.645 -> Connecting to Wi-Fi…
    16:57:55.926 -> Connecting to MQTT…
    16:57:55.926 -> [WiFi-event] event: 5
    16:57:55.926 -> WiFi lost connection
    16:57:55.926 -> [WiFi-event] event: 4
    16:57:55.926 -> [WiFi-event] event: 7
    16:57:55.926 -> WiFi connected
    16:57:55.994 -> IP address:
    16:57:55.994 -> 192.168.1.195
    16:57:57.922 -> Connecting to Wi-Fi…
    16:57:57.922 -> Disconnected from MQTT.
    16:57:59.967 -> Connecting to MQTT…
    16:58:00.442 -> Connected to MQTT.
    16:58:00.442 -> Session present: 0
    16:58:00.968 -> Connecting to MQTT…
    16:58:00.968 -> [WiFi-event] event: 5
    16:58:00.968 -> WiFi lost connection
    16:58:00.968 -> [WiFi-event] event: 4
    16:58:00.968 -> [WiFi-event] event: 7
    16:58:00.968 -> WiFi connected
    16:58:00.968 -> IP address:
    16:58:00.968 -> 192.168.1.195
    16:58:02.927 -> Connecting to Wi-Fi…
    16:58:02.962 -> Disconnected from MQTT.
    16:58:04.950 -> Connecting to MQTT…
    16:58:05.789 -> Connected to MQTT.
    16:58:05.789 -> Session present: 0
    16:58:05.962 -> Connecting to MQTT…
    16:58:05.962 -> [WiFi-event] event: 5

    I’ve tried running the stock Arduino example SimpleWifiServer.ino, and it does connect to the local WIFI, and the router does show a stable connection.
    What is Event 5?

    Reply
  19. Hmmm. Using my phone hotspot, instead of the AT&T Uverse Guest Wifi, it appears to work near flawlessly.
    Connecting to MQTT…
    Disconnected from MQTT.
    Connecting to MQTT…
    Connected to MQTT.
    Session present: 0
    Publishing on topic esp32/dht/temperature at QoS 1, packetId: 1Message: 73.00
    Publishing on topic esp32/dht/humidity at QoS 1, packetId 2: Message: 51.00
    Publish acknowledged. packetId: 1
    Publish acknowledged. packetId: 2
    Publishing on topic esp32/dht/temperature at QoS 1, packetId: 3Message: 74.00
    Publishing on topic esp32/dht/humidity at QoS 1, packetId 4: Message: 52.00
    Publish acknowledged. packetId: 3
    Publish acknowledged. packetId: 4
    Publishing on topic esp32/dht/temperature at QoS 1, packetId: 5Message: 75.00
    Publishing on topic esp32/dht/humidity at QoS 1, packetId 6: Message: 53.00
    Publish acknowledged. packetId: 5
    Publish acknowledged. packetId: 6
    Publishing on topic esp32/dht/temperature at QoS 1, packetId: 7Message: 76.00
    Publishing on topic esp32/dht/humidity at QoS 1, packetId 8: Message: 54.00
    Publish acknowledged. packetId: 7
    Publish acknowledged. packetId: 8
    Publishing on topic esp32/dht/temperature at QoS 1, packetId: 9Message: 77.00
    Publishing on topic esp32/dht/humidity at QoS 1, packetId 10: Message: 55.00
    Publish acknowledged. packetId: 9
    Publish acknowledged. packetId: 10
    Publishing on topic esp32/dht/temperature at QoS 1, packetId: 11Message: 78.00
    Publishing on topic esp32/dht/humidity at QoS 1, packetId 12: Message: 56.00
    Publish acknowledged. packetId: 11
    Publish acknowledged. packetId: 12
    Publishing on topic esp32/dht/temperature at QoS 1, packetId: 13Message: 79.00
    Publishing on topic esp32/dht/humidity at QoS 1, packetId 14: Message: 57.00
    Publish acknowledged. packetId: 13
    Publish acknowledged. packetId: 14
    Publishing on topic esp32/dht/temperature at QoS 1, packetId: 15Message: 80.00
    Publishing on topic esp32/dht/humidity at QoS 1, packetId 16: Message: 58.00
    Publish acknowledged. packetId: 15
    Publish acknowledged. packetId: 16
    Publishing on topic esp32/dht/temperature at QoS 1, packetId: 17Message: 72.00
    Publishing on topic esp32/dht/humidity at QoS 1, packetId 18: Message: 59.00
    Publish acknowledged. packetId: 17
    Publish acknowledged. packetId: 18
    Publishing on topic esp32/dht/temperature at QoS 1, packetId: 19Message: 73.00
    Publishing on topic esp32/dht/humidity at QoS 1, packetId 20: Message: 60.00
    Publish acknowledged. packetId: 19
    Publish acknowledged. packetId: 20

    Reply
  20. Hmm. Using the primary WIFI account on my AT&T Uverse router seems to work fine also. So, this failure is confined to the secondary guest account.

    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.