IoT temperature sensor failing

I'm having an issue with an IoT air conditioner, where it's DHT11 sensor is failing.
I am using a NodeMCU.
The problem changes based on the library I use.
Some libraries cause it to report a very long number. Others return negative 999.
Here's my code:

#include <DHT.h>

#include <Adafruit_Sensor.h>

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#define DHTTYPE DHT11

const char* ssid = "**************";
const char* password = "****************";

int setTemp;
int temp;
int ledPin = 5; // GPIO13
DHT tempsens;
WiFiServer server(80);

void setup() {
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
  // Connect to WiFi network
  Serial.print("Connecting to ");
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
  Serial.println("WiFi connected");
  // Start the server
  Serial.println("Server started");
  // Print the IP address
  Serial.print("Use this URL to connect: ");
void loop() {
  setTemp = map(analogRead(A0),0,1024,50,100);
  delay(tempsens.getMinimumSamplingPeriod());  /* Delay of amount equal to sampling period */  
  temp = tempsens.getTemperature(); /* Get temperature value */

  Serial.print(tempsens.getStatusString());      /* Print status of communication */
  Serial.print(temp, 1);
  Serial.println(tempsens.toFahrenheit(temp), 1);
  // Check if a client has connected

  WiFiClient client = server.available();
  if (!client) {
  // Wait until the client sends some data
  Serial.println("new client");
  // Read the first line of the request
  String request = client.readStringUntil('\r');
  // Match the request
  int mde = 1;
  if (request.indexOf("/SYSTEM=FAN") != -1)  {
    digitalWrite(ledPin, HIGH);
    mde = 1;
  if (request.indexOf("/SYSTEM=AUTO") != -1)  {
    mde = 2;
  if (request.indexOf("/SYSTEM=OFF") != -1)  {
    digitalWrite(ledPin, LOW);
    mde = 3;
  if (mde == 2) {
    if (temp >= setTemp) {
      digitalWrite(ledPin, LOW);
    } else {
      digitalWrite(ledPin, LOW);
// Set ledPin according to the request
//digitalWrite(ledPin, value);
  // Return the response
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println(""); //  do not forget this one
  client.println("<!DOCTYPE HTML>");
  client.println("<head><title>Server</title><script>function refresh(refreshPeriod) {setTimeout('location.reload(true)', refreshPeriod);};window.onload = refresh(15000);</script></head>");
  client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n");
  client.println("body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;}\n");
  client.println("p {font-size: 24px;color: #444444;margin-bottom: 10px;}</style></head>\n");
  client.print("Status: ");
  if(mde == 1) {
    client.print("Fan on");
  } else if (mde == 2){
  } else {
  client.println("<p>Current temperature: ");
  client.println("<p>Set temperature: ");
  client.println("<a href=\"/SYSTEM=FAN\"\"><button>On</button></a>");
  client.println("<a href=\"/SYSTEM=AUTO\"\"><button>Auto</button></a><br />");
  client.println("<a href=\"/SYSTEM=OFF\"\"><button>Off</button></a><br />");
  Serial.println("Client disonnected");

String SendHTML(float Temperaturestat){
  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>Server</title><script>function refresh(refreshPeriod) {setTimeout('location.reload(true)', refreshPeriod);};window.onload = refresh(15000);</script>\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>";
  ptr +="</div>\n";
  ptr +="</body>\n";
  ptr +="</html>\n";
  return ptr;

Of course, the WiFi and password are replaced by my actual WiFi and password.

My wiring is correct, as I double-checked.

The NodeMCU is driving an optocoupler, which in turn is driving a relay controlling the air conditioner.
The control system works, since I can turn on and off the air conditioner manually, but the reported temperature is way off.

With the code I posted above, the returned value is 3865470720.00 C.
I'm absolutely sure that I'm not inside the Sun, so this value is very wrong.

What do you get if you use a library example or write a trivial example that just reads the temperature?

I get the same thing. Either a ridiculously high number or negative 999.

And you've tried this with several sensors?

Show a pic of your wiring and a schematic.

Your code looks a little odd - defining DHTTYPE but not using it. Can you post the test code you used.

No, I only have one sensor

I just used the DHT11 sample I found online - I'd have to find it again.
And DHTTYPE is defined for other libraries, for switching quickly.

Hi, we have already pinpointed the problem.

Can you show us a small sketch with has the wrong temperature ?
And please tell which DHT library you use, with a link to it.

When I look into my crystal ball, then you seem to combine two different DHT libraries and one of those libraries seems to be many years old. You might even have installed more than one DHT library. Select just one DHT library, and use an example from that library. Then carefully add the example code into your own sketch.

This is an example with a DHT library: That example only works with the Adafruit DHT library.

Actually, I think I may have just found the problem.
The sensor was wired with reverse polarity, and given my experience with reverse polarity, I doubt it will ever work again.
I guess I'd just be better off using a thermistor, anyway.

A BME280 would be a more accurate choice.

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