DHT Sensor Values not populating Gauge

Working on my first project with Node-Red. I am trying to show Temperature and Humidity values from my DHT sensor onto the Node-Red Dashboard. Data populates the serial monitor of my ESP32. The MQTT node shows the connection. I haven't tried other browsers other than Microsoft Edge. Not sure what other issue it can be. Below are some screenshots and supportive info. If I missed something please let me know.

Sensor: DHT22
Microprocessor: ESP32S
MQTT Mosquito Broker: Installed on Raspberri Pi

[
    {
        "id": "5aaaa13.ece6d6",
        "type": "tab",
        "label": "Flow 1",
        "disabled": false,
        "info": ""
    },
    {
        "id": "6e4f621f.b2f1e4",
        "type": "mqtt out",
        "z": "5aaaa13.ece6d6",
        "name": "",
        "topic": "Hydroponic/Lights",
        "qos": "",
        "retain": "",
        "respTopic": "",
        "contentType": "",
        "userProps": "",
        "correl": "",
        "expiry": "",
        "broker": "bbe256ccf519929b",
        "x": 630,
        "y": 80,
        "wires": []
    },
    {
        "id": "31c609d9.aa546e",
        "type": "mqtt out",
        "z": "5aaaa13.ece6d6",
        "name": "",
        "topic": "Hydroponic/Pump",
        "qos": "",
        "retain": "",
        "respTopic": "",
        "contentType": "",
        "userProps": "",
        "correl": "",
        "expiry": "",
        "broker": "bbe256ccf519929b",
        "x": 630,
        "y": 160,
        "wires": []
    },
    {
        "id": "a08b7ab1db1fea4f",
        "type": "bigtimer",
        "z": "5aaaa13.ece6d6",
        "outtopic": "",
        "outpayload1": "0",
        "outpayload2": "1",
        "name": "Big Timer",
        "comment": "",
        "lat": 0,
        "lon": 0,
        "starttime": "360",
        "endtime": "1200",
        "starttime2": 0,
        "endtime2": 0,
        "startoff": 0,
        "endoff": 0,
        "startoff2": 0,
        "endoff2": 0,
        "offs": 0,
        "outtext1": "",
        "outtext2": "",
        "timeout": 1440,
        "sun": true,
        "mon": true,
        "tue": true,
        "wed": true,
        "thu": true,
        "fri": true,
        "sat": true,
        "jan": true,
        "feb": true,
        "mar": true,
        "apr": true,
        "may": true,
        "jun": true,
        "jul": true,
        "aug": true,
        "sep": true,
        "oct": true,
        "nov": true,
        "dec": true,
        "day1": 0,
        "month1": 0,
        "day2": 0,
        "month2": 0,
        "day3": 0,
        "month3": 0,
        "day4": 0,
        "month4": 0,
        "day5": 0,
        "month5": 0,
        "day6": 0,
        "month6": 0,
        "day7": 0,
        "month7": 0,
        "day8": 0,
        "month8": 0,
        "day9": 0,
        "month9": 0,
        "day10": 0,
        "month10": 0,
        "day11": 0,
        "month11": 0,
        "day12": 0,
        "month12": 0,
        "d1": 0,
        "w1": 0,
        "d2": 0,
        "w2": 0,
        "d3": 0,
        "w3": 0,
        "d4": 0,
        "w4": 0,
        "d5": 0,
        "w5": 0,
        "d6": 0,
        "w6": 0,
        "xday1": 0,
        "xmonth1": 0,
        "xday2": 0,
        "xmonth2": 0,
        "xday3": 0,
        "xmonth3": 0,
        "xday4": 0,
        "xmonth4": 0,
        "xday5": 0,
        "xmonth5": 0,
        "xday6": 0,
        "xmonth6": 0,
        "xday7": 0,
        "xmonth7": 0,
        "xday8": 0,
        "xmonth8": 0,
        "xday9": 0,
        "xmonth9": 0,
        "xday10": 0,
        "xmonth10": 0,
        "xday11": 0,
        "xmonth11": 0,
        "xday12": 0,
        "xmonth12": 0,
        "xd1": 0,
        "xw1": 0,
        "xd2": 0,
        "xw2": 0,
        "xd3": 0,
        "xw3": 0,
        "xd4": 0,
        "xw4": 0,
        "xd5": 0,
        "xw5": 0,
        "xd6": 0,
        "xw6": 0,
        "suspend": false,
        "random": false,
        "randon1": false,
        "randoff1": false,
        "randon2": false,
        "randoff2": false,
        "repeat": true,
        "atstart": true,
        "odd": false,
        "even": false,
        "x": 300,
        "y": 80,
        "wires": [
            [
                "6e4f621f.b2f1e4"
            ],
            [],
            []
        ]
    },
    {
        "id": "feef21fde929a231",
        "type": "cronplus",
        "z": "5aaaa13.ece6d6",
        "name": "",
        "outputField": "payload",
        "timeZone": "",
        "persistDynamic": false,
        "commandResponseMsgOutput": "output1",
        "outputs": 1,
        "options": [
            {
                "name": "schedule1",
                "topic": "topic1",
                "payloadType": "num",
                "payload": "0",
                "expressionType": "cron",
                "expression": "0 0 6 * * * *",
                "location": "",
                "offset": "0",
                "solarType": "all",
                "solarEvents": "sunrise,sunset"
            },
            {
                "name": "schedule2",
                "topic": "topic2",
                "payloadType": "num",
                "payload": "1",
                "expressionType": "cron",
                "expression": "0 5 6 * * * *",
                "location": "",
                "offset": "0",
                "solarType": "all",
                "solarEvents": "sunrise,sunset"
            },
            {
                "name": "schedule3",
                "topic": "topic3",
                "payloadType": "num",
                "payload": "0",
                "expressionType": "cron",
                "expression": "0 0 7 * * * *",
                "location": "",
                "offset": "0",
                "solarType": "all",
                "solarEvents": "sunrise,sunset"
            },
            {
                "name": "schedule4",
                "topic": "topic4",
                "payloadType": "num",
                "payload": "1",
                "expressionType": "cron",
                "expression": "0 5 7 * * * *",
                "location": "",
                "offset": "0",
                "solarType": "all",
                "solarEvents": "sunrise,sunset"
            },
            {
                "name": "schedule5",
                "topic": "topic5",
                "payloadType": "num",
                "payload": "0",
                "expressionType": "cron",
                "expression": "0 0 8 * * * *",
                "location": "",
                "offset": "0",
                "solarType": "all",
                "solarEvents": "sunrise,sunset"
            },
            {
                "name": "schedule6",
                "topic": "topic6",
                "payloadType": "num",
                "payload": "1",
                "expressionType": "cron",
                "expression": "0 5 8 * * * *",
                "location": "",
                "offset": "0",
                "solarType": "all",
                "solarEvents": "sunrise,sunset"
            },
            {
                "name": "schedule7",
                "topic": "topic7",
                "payloadType": "num",
                "payload": "0",
                "expressionType": "cron",
                "expression": "0 0 9 * * * *",
                "location": "",
                "offset": "0",
                "solarType": "all",
                "solarEvents": "sunrise,sunset"
            },
            {
                "name": "schedule8",
                "topic": "topic8",
                "payloadType": "num",
                "payload": "1",
                "expressionType": "cron",
                "expression": "0 5 9 * * * *",
                "location": "",
                "offset": "0",
                "solarType": "all",
                "solarEvents": "sunrise,sunset"
            },
            {
                "name": "schedule9",
                "topic": "topic9",
                "payloadType": "num",
                "payload": "0",
                "expressionType": "cron",
                "expression": "0 * 10 * * * *",
                "location": "",
                "offset": "0",
                "solarType": "all",
                "solarEvents": "sunrise,sunset"
            },
            {
                "name": "schedule10",
                "topic": "topic10",
                "payloadType": "num",
                "payload": "1",
                "expressionType": "cron",
                "expression": "0 5 10 * * * *",
                "location": "",
                "offset": "0",
                "solarType": "all",
                "solarEvents": "sunrise,sunset"
            },
            {
                "name": "schedule11",
                "topic": "topic11",
                "payloadType": "num",
                "payload": "0",
                "expressionType": "cron",
                "expression": "0 0 11 * * * *",
                "location": "",
                "offset": "0",
                "solarType": "all",
                "solarEvents": "sunrise,sunset"
            },
            {
                "name": "schedule12",
                "topic": "topic12",
                "payloadType": "num",
                "payload": "1",
                "expressionType": "cron",
                "expression": "0 5 11 * * * *",
                "location": "",
                "offset": "0",
                "solarType": "all",
                "solarEvents": "sunrise,sunset"
            },
            {
                "name": "schedule13",
                "topic": "topic13",
                "payloadType": "num",
                "payload": "0",
                "expressionType": "cron",
                "expression": "0 0 12 * * * *",
                "location": "",
                "offset": "0",
                "solarType": "all",
                "solarEvents": "sunrise,sunset"
            },
            {
                "name": "schedule14",
                "topic": "topic14",
                "payloadType": "num",
                "payload": "1",
                "expressionType": "cron",
                "expression": "0 5 12 * * * *",
                "location": "",
                "offset": "0",
                "solarType": "all",
                "solarEvents": "sunrise,sunset"
            },
            {
                "name": "schedule15",
                "topic": "topic15",
                "payloadType": "num",
                "payload": "0",
                "expressionType": "cron",
                "expression": "0 0 13 * * * *",
                "location": "",
                "offset": "0",
                "solarType": "all",
                "solarEvents": "sunrise,sunset"
            },
            {
                "name": "schedule16",
                "topic": "topic16",
                "payloadType": "num",
                "payload": "1",
                "expressionType": "cron",
                "expression": "0 5 13 * * * *",
                "location": "",
                "offset": "0",
                "solarType": "all",
                "solarEvents": "sunrise,sunset"
            },
            {
                "name": "schedule17",
                "topic": "topic17",
                "payloadType": "num",
                "payload": "0",
                "expressionType": "cron",
                "expression": "0 0 14 * * * *",
                "location": "",
                "offset": "0",
                "solarType": "all",
                "solarEvents": "sunrise,sunset"
            },
            {
                "name": "schedule18",
                "topic": "topic18",
                "payloadType": "num",
                "payload": "1",
                "expressionType": "cron",
                "expression": "0 5 14 * * * *",
                "location": "",
                "offset": "0",
                "solarType": "all",
                "solarEvents": "sunrise,sunset"
            },
            {
                "name": "schedule19",
                "topic": "topic19",
                "payloadType": "num",
                "payload": "0",
                "expressionType": "cron",
                "expression": "0 0 15 * * * *",
                "location": "",
                "offset": "0",
                "solarType": "all",
                "solarEvents": "sunrise,sunset"
            },
            {
                "name": "schedule20",
                "topic": "topic20",
                "payloadType": "num",
                "payload": "1",
                "expressionType": "cron",
                "expression": "0 5 15 * * * *",
                "location": "",
                "offset": "0",
                "solarType": "all",
                "solarEvents": "sunrise,sunset"
            },
            {
                "name": "schedule21",
                "topic": "topic21",
                "payloadType": "num",
                "payload": "0",
                "expressionType": "cron",
                "expression": "0 0 16 * * * *",
                "location": "",
                "offset": "0",
                "solarType": "all",
                "solarEvents": "sunrise,sunset"
            },
            {
                "name": "schedule22",
                "topic": "topic22",
                "payloadType": "num",
                "payload": "1",
                "expressionType": "cron",
                "expression": "0 5 16 * * * *",
                "location": "",
                "offset": "0",
                "solarType": "all",
                "solarEvents": "sunrise,sunset"
            },
            {
                "name": "schedule23",
                "topic": "topic23",
                "payloadType": "num",
                "payload": "0",
                "expressionType": "cron",
                "expression": "0 0 17 * * * *",
                "location": "",
                "offset": "0",
                "solarType": "all",
                "solarEvents": "sunrise,sunset"
            },
            {
                "name": "schedule24",
                "topic": "topic24",
                "payloadType": "num",
                "payload": "1",
                "expressionType": "cron",
                "expression": "0 5 17 * * * *",
                "location": "",
                "offset": "0",
                "solarType": "all",
                "solarEvents": "sunrise,sunset"
            },
            {
                "name": "schedule25",
                "topic": "topic25",
                "payloadType": "num",
                "payload": "0",
                "expressionType": "cron",
                "expression": "0 0 18 * * * *",
                "location": "",
                "offset": "0",
                "solarType": "all",
                "solarEvents": "sunrise,sunset"
            },
            {
                "name": "schedule26",
                "topic": "topic26",
                "payloadType": "num",
                "payload": "1",
                "expressionType": "cron",
                "expression": "0 5 18 * * * *",
                "location": "",
                "offset": "0",
                "solarType": "all",
                "solarEvents": "sunrise,sunset"
            },
            {
                "name": "schedule27",
                "topic": "topic27",
                "payloadType": "num",
                "payload": "0",
                "expressionType": "cron",
                "expression": "0 0 19 * * * *",
                "location": "",
                "offset": "0",
                "solarType": "all",
                "solarEvents": "sunrise,sunset"
            },
            {
                "name": "schedule28",
                "topic": "topic28",
                "payloadType": "num",
                "payload": "1",
                "expressionType": "cron",
                "expression": "0 5 19 * * * *",
                "location": "",
                "offset": "0",
                "solarType": "all",
                "solarEvents": "sunrise,sunset"
            },
            {
                "name": "schedule29",
                "topic": "topic29",
                "payloadType": "num",
                "payload": "0",
                "expressionType": "cron",
                "expression": "0 0 20 * * * *",
                "location": "",
                "offset": "0",
                "solarType": "all",
                "solarEvents": "sunrise,sunset"
            },
            {
                "name": "schedule30",
                "topic": "topic30",
                "payloadType": "num",
                "payload": "1",
                "expressionType": "cron",
                "expression": "0 5 20 * * * *",
                "location": "",
                "offset": "0",
                "solarType": "all",
                "solarEvents": "sunrise,sunset"
            }
        ],
        "x": 320,
        "y": 160,
        "wires": [
            [
                "31c609d9.aa546e"
            ]
        ]
    },
    {
        "id": "26bfdfbcd2df6cbc",
        "type": "ui_gauge",
        "z": "5aaaa13.ece6d6",
        "name": "Air Temperature",
        "group": "f937c3d364ce5930",
        "order": 0,
        "width": 0,
        "height": 0,
        "gtype": "gage",
        "title": "Air Temperature",
        "label": "F",
        "format": "{{msg.payload}}",
        "min": 0,
        "max": "100",
        "colors": [
            "#00b500",
            "#e6e600",
            "#ca3838"
        ],
        "seg1": "",
        "seg2": "",
        "diff": false,
        "className": "",
        "x": 620,
        "y": 320,
        "wires": []
    },
    {
        "id": "d8cef8415fd7815a",
        "type": "ui_gauge",
        "z": "5aaaa13.ece6d6",
        "name": "Humidity",
        "group": "37de8fe8.46846",
        "order": 0,
        "width": 0,
        "height": 0,
        "gtype": "gage",
        "title": "Humidity",
        "label": "%",
        "format": "{{msg.payload}}",
        "min": 0,
        "max": "100",
        "colors": [
            "#00b500",
            "#e6e600",
            "#ca3838"
        ],
        "seg1": "",
        "seg2": "",
        "diff": false,
        "className": "",
        "x": 600,
        "y": 380,
        "wires": []
    },
    {
        "id": "27b3e9cbfa66c58b",
        "type": "mqtt in",
        "z": "5aaaa13.ece6d6",
        "name": "Air Temperature",
        "topic": "Hydroponic/temperature",
        "qos": "2",
        "datatype": "auto-detect",
        "broker": "bbe256ccf519929b",
        "nl": false,
        "rap": true,
        "rh": 0,
        "inputs": 0,
        "x": 340,
        "y": 320,
        "wires": [
            [
                "26bfdfbcd2df6cbc"
            ]
        ]
    },
    {
        "id": "d08d52248e46d75b",
        "type": "mqtt in",
        "z": "5aaaa13.ece6d6",
        "name": "Humidity",
        "topic": "Hydroponic/humidity",
        "qos": "2",
        "datatype": "auto-detect",
        "broker": "bbe256ccf519929b",
        "nl": false,
        "rap": true,
        "rh": 0,
        "inputs": 0,
        "x": 320,
        "y": 380,
        "wires": [
            [
                "d8cef8415fd7815a"
            ]
        ]
    },
    {
        "id": "bbe256ccf519929b",
        "type": "mqtt-broker",
        "name": "",
        "broker": "localhost",
        "port": "1883",
        "clientid": "",
        "autoConnect": true,
        "usetls": false,
        "protocolVersion": "4",
        "keepalive": "60",
        "cleansession": true,
        "birthTopic": "",
        "birthQos": "0",
        "birthPayload": "",
        "birthMsg": {},
        "closeTopic": "",
        "closeQos": "0",
        "closePayload": "",
        "closeMsg": {},
        "willTopic": "",
        "willQos": "0",
        "willPayload": "",
        "willMsg": {},
        "userProps": "",
        "sessionExpiry": ""
    },
    {
        "id": "f937c3d364ce5930",
        "type": "ui_group",
        "name": "Air Temp",
        "tab": "f57a184e.4f4fa",
        "order": 3,
        "disp": true,
        "width": "6",
        "collapse": false,
        "className": ""
    },
    {
        "id": "37de8fe8.46846",
        "type": "ui_group",
        "name": "Lights",
        "tab": "f57a184e.4f4fa",
        "order": 1,
        "disp": true,
        "width": "6",
        "collapse": false,
        "className": ""
    },
    {
        "id": "f57a184e.4f4fa",
        "type": "ui_tab",
        "name": "Hydroponic",
        "icon": "dashboard",
        "disabled": false,
        "hidden": false
    }
]
#include "DHT.h"
#include <WiFi.h>
extern "C" {
  #include "freertos/FreeRTOS.h"
  #include "freertos/timers.h"
}
#include <AsyncMqttClient.h>

#define WIFI_SSID "**********"
#define WIFI_PASSWORD "***********"

// Raspberry Pi Mosquitto MQTT Broker
#define MQTT_HOST IPAddress(192, 168, ***, ***)
// 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 "Hydroponic/temperature"
#define MQTT_PUB_HUM  "Hydroponic/humidity"

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

// 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("My User", "My 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 Hydroponic/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 Hydroponic/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);
  }
}

Welcome to the forum !

Have you connected a debug node to the MQTT in node to see what if anything it receives ?

Is Node-RED all running on the Pi?

A great tool for looking at your mqtt data it 'MQTT Explorer'.

Depending on the IDE you are using to flash the ESP32, you should have a way to look at its log. Have you tried that to see what the ESP32 is doing" i.e. is is actually sending the data? (The 'MQTT Explorer' tool would make it easy to answer this question.)

Thank you guys for the replies thus far!

@smcgann99, I connected a debug node and I am not getting any errors. However, I tried to restart my RPI and got the following error "systemctl status mosquitto.service" and " journalctl -xe". I unplugged the RPI and started it back up and the gauges now populate correctly. I should have went through the basics before posting, being hard headed does that.

@zenofmud, thanks for the heads up on the MQTT Explorer. That will come in handy in the future. In the process of downloading it now.

Thank you again!

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.