DHT22 传感器在上传到 MQTT 时显示 'nan'

DHT22 sensors show 'nan' when uploading to MQTT

我正在尝试使用 nodemcu-ESP8266 从 DHT22 向 mosquito 代理发送值,我目前正在使用 MQTT 资源管理器。但是,我的 DHT22 检测到 nan 值。我知道连线和电路板一般都没有问题,因为我后来用另一个程序验证了。 可能是什么问题?我知道名为 isnan() 的函数,它不是我要找的。

错误的程序,DHT22 显示 nan 值。

#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();
}

验证程序显示DHT22没有问题,可以忽略。

#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;
}

运行的程序包含以下相关代码:

  pinMode(DHTPin, INPUT);

  dht.begin();

没有的程序没有那几行代码。我不确定您使用的是哪个 DHT 库,所以我不能确定您是否需要 pinMode() 调用,但您肯定需要 dht.begin() 调用。在尝试使用传感器之前将这些行添加到 setup()

通过阅读这两个程序并进行比较,您可以很容易地自己发现这一点。