Home > Enterprise >  DHT22 sensors show 'nan' when uploading to MQTT
DHT22 sensors show 'nan' when uploading to MQTT

Time:04-13

I am trying to send value from DHT22 with nodemcu-ESP8266 to mosquito broker, and I am currently using MQTT explorer. However, my DHT22 detected nan value. I know that there are no problems with the wire connections and the the board in general, because I later verified with another program. What could be the problems? I know about the function called isnan(), it is not what I am looking for.

The faulty program, DHT22 shows nan value.

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


// WiFi
const char *ssid = "xxx"; // Enter your WiFi name
const char *password = "xxx";  // Enter WiFi password
// MQTT Broker
const char *mqtt_broker = "xxx"; // Enter your WiFi or Ethernet IP
const char *topic = "test/temperature & humidity";
const int mqtt_port = 1883;

WiFiClient espClient;
PubSubClient client(espClient);

#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
uint8_t DHTPin = D7;
DHT dht(DHTPin, DHTTYPE);

float temperature;
float humidity;
char msg[50];

void setup() {
  // Set software serial baud to 115200;
  Serial.begin(115200);

  // connecting to a WiFi network
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.println("Connecting to WiFi..");
  }

  Serial.println("Connected to the WiFi network");

  //connecting to a mqtt broker
  client.setServer(mqtt_broker, mqtt_port);
  client.setCallback(callback);

  while (!client.connected()) {
    String client_id = "esp8266-client-";
    client_id  = String(WiFi.macAddress());

    Serial.printf("The client %s connects to mosquitto mqtt broker\n", client_id.c_str());

    if (client.connect(client_id.c_str())) {
      Serial.println("Public mosquitto broker connected");
    } else {
      Serial.print("failed with state ");
      Serial.print(client.state());
      delay(2000);
    }
  }

  String msg = "real time temperature: ";
  msg = msg   dht.readTemperature();
  msg = msg   " C ;real time Humidity: " ;
  msg = msg   dht.readHumidity() ;
  msg = msg   "%";
  char message[58];
  msg.toCharArray(message, 58);

  Serial.println(message);

  // publish and subscribe
  client.publish(topic, message);
  client.subscribe(topic);
} /* end of void setup() */

void callback(char *topic, byte *payload, unsigned int length) {
  Serial.print("Message arrived in topic: ");
  Serial.println(topic);
  Serial.print("Message:");

  for (int i = 0; i < length; i  ) {
    Serial.print((char) payload[i]);
  }

  Serial.println();
  Serial.println(" - - - - - - - - - - - -");
}
void loop() {
  client.loop();
}

The verification program showed there are no problems with DHT22, can ignore.

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

// Uncomment one of the lines below 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

/*Put your SSID & Password*/
const char* ssid = "xxx";  // Enter SSID here
const char* password = "xxxx";  //Enter Password here

ESP8266WebServer server(80);

// DHT Sensor
uint8_t DHTPin = D7;

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

float Temperature;
float Humidity;
float temp;
float hum;

void setup() {
  Serial.begin(115200);
  delay(100);

  pinMode(DHTPin, INPUT);

  dht.begin();

  Serial.println("Connecting to ");
  Serial.println(ssid);

  //connect to your local wi-fi network
  WiFi.begin(ssid, password);

  //check wi-fi is connected to wi-fi network
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected..!");
  Serial.print("Got IP: ");  Serial.println(WiFi.localIP());

  server.on("/", handle_OnConnect);
  server.onNotFound(handle_NotFound);

  server.begin();
  Serial.println("HTTP server started");

  temp = temp_detect();
  hum = hum_detect();
  Serial.print(temp);
  Serial.println("degree celsius");
  Serial.print(hum);
  Serial.println("%");
}
void loop() {

  server.handleClient();

}

void handle_OnConnect() {

  Temperature = dht.readTemperature(); // Gets the values of the temperature
  Humidity = dht.readHumidity(); // Gets the values of the humidity
  server.send(200, "text/html", SendHTML(Temperature, Humidity));
  Serial.print(Temperature);
  Serial.print(Humidity);
}

float temp_detect() {
  float temperature1;
  temperature1 = dht.readTemperature();
  return temperature1;
}

float hum_detect() {
  float humidity1;
  humidity1 = dht.readHumidity();
  return humidity1;
}

void handle_NotFound() {
  server.send(404, "text/plain", "Not found");
}


/*
CAN IGNORE THIS PART, JUST HTML CODE

*/
String SendHTML(float Temperaturestat, float Humiditystat) {
  String ptr = "<!DOCTYPE html> <html>\n";
  ptr  = "<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
  ptr  = "<title>ESP8266 Weather Report</title>\n";
  ptr  = "<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n";
  ptr  = "body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;}\n";
  ptr  = "p {font-size: 24px;color: #444444;margin-bottom: 10px;}\n";
  ptr  = "</style>\n";
  ptr  = "</head>\n";
  ptr  = "<body>\n";
  ptr  = "<div id=\"webpage\">\n";
  ptr  = "<h1>ESP8266 NodeMCU Weather Report</h1>\n";

  ptr  = "<p>Temperature: ";
  ptr  = (int)Temperaturestat;
  ptr  = "°C</p>";
  ptr  = "<p>Humidity: ";
  ptr  = (int)Humiditystat;
  ptr  = "%</p>";

  ptr  = "</div>\n";
  ptr  = "</body>\n";
  ptr  = "</html>\n";
  return ptr;
}

CodePudding user response:

The program that worked contains this code which is relevant:

  pinMode(DHTPin, INPUT);

  dht.begin();

The program that didn't doesn't have those lines of code. I'm not sure which DHT library you're using so I can't say for sure if you need the pinMode() call but you definitely need the dht.begin() call. Add those lines to setup() before you try to use the sensor.

This is something you can easily discover for yourself by reading the two programs and comparing them.

  • Related