DHT11 Sensor Frequent Data Spikes

Hi All,

(Edited to tidy up my original post as per comments below)

I'm trying to monitor temperature using a DHT11. I have it reporting the temperature in Serial Monitor every 2secs but it frequently spikes with huge readings (2147483647ºC). This happens every 10-readings or so, and I'll get a cluster of these spiked readings before normal readings resume.

I've tried increasing the delay between readings but this doesn't do away with the spikes. Does anyone have any ideas on how I might reduce the frequency of these data spikes?

Thanks for your help.

Here's the Sketch:

const int refresh=3;//read every X seconds
boolean showSerial =true;//true or false

unsigned int unit=0;//0=C, 1=F, 2=Humidity
char *title[]={"Temperature","Temperature","Humidity"};
char *unitText[]={"°C","°F","%"};

#include "DHT.h"
#define DHTPIN 2 
#define DHTTYPE DHT11   // DHT 11
//#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
//#define DHTTYPE DHT21   // DHT 21 (AM2301)
DHT dht(DHTPIN, DHTTYPE);
float temperatureValue,temperatureFValue, humidityValue;// 
// ****** DHT settings end (Robojax.com )

#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <ESPmDNS.h>

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

WebServer server(80);


void sendTemp() {
//see video https://youtu.be/JXCcmZUmzy8
  String page = "<!DOCTYPE html>\n";
  page +="<html>\n";  
  page +="<head>\n";
  page +="<title>LivingRoomTemp</title>\n";
  page +="    <meta http-equiv='refresh' content='";
  page += String(refresh);// how often temperature is read
  page +="'/>\n";  

  page +="<head>\n";  
  page +="<body>\n"; 
  page +="<h1>Living Room Temperature</h1>\n";    
  page +="<p style=\"font-size:50px;\">";
  page +=title[unit];
  page +="<br/>";  
  page +="<p style=\"color:red; font-size:50px;\">";
 if (DHTTYPE ==DHT11){
  page += String((int)temperatureValue);  
}else{
  page += String(temperatureValue, 1);
}

  page +=unitText[unit]; 
  page +="</p>\n</body>";  
  page +="</html>\n";  
 server.send(200,  "text/html",page);

}


void handleNotFound() {

  String message = "File Not Found\n\n";
  message += "URI: ";
  message += server.uri();
  message += "\nMethod: ";
  message += (server.method() == HTTP_GET) ? "GET" : "POST";
  message += "\nArguments: ";
  message += server.args();
  message += "\n";

  for (uint8_t i = 0; i < server.args(); i++) {
    message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  }

  server.send(404, "text/plain", message);

}

void setup(void) {
  // Robojax.com code for ESP32 DHT11 DHT22
   dht.begin();

  Serial.begin(115200);
  WiFi.mode(WIFI_STA); 
  WiFi.begin(ssid, password);
  Serial.println("");

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("Open: http://");
  Serial.print(WiFi.localIP());
  Serial.println(" to read temperature");

  if (MDNS.begin("robojaxDHT")) {
    Serial.println("MDNS responder started");
    Serial.println("or open http://robojaxDHT");
  }

  server.on("/", sendTemp);
  
  server.on("/inline", []() {
    server.send(200, "text/plain", "this works as well");
  });
  server.onNotFound(handleNotFound);
  server.begin();
  Serial.println("HTTP server started");
  //see video https://youtu.be/JXCcmZUmzy8
}


void loop(void) {
  //Robojax.com code for ESP32 DHT11 DHT22
  server.handleClient();
  temperatureValue = dht.readTemperature();// Read temperature as Celsius (the default)
  humidityValue = dht.readHumidity();// Reading humidity 
  temperatureFValue = dht.readTemperature(true);// Read temperature as Fahrenheit (isFahrenheit = true)
  if(unit ==1)
  {
   temperatureValue =temperatureFValue; //
  }else if(unit==3)
  {
    temperatureValue =humidityValue;   
  }else{
    temperatureValue =temperatureValue;
  }
  if(showSerial){
      Serial.print(title[unit]);
      Serial.print(": ");
      if (DHTTYPE ==DHT11){
     Serial.println((int)temperatureValue);  
      }else{
       Serial.print(temperatureValue,1);
      }
      
  }
  Serial.println();//just adds new line

  delay(2000);// change this to larger value (1000 or more) if you don't need very often reading
  // Robojax.com code for ESP32 and DHT11 DHT22  
  //see video https://youtu.be/JXCcmZUmzy8

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

The problem is the code you posted doesn't just read the temperature and report it, it does loads of other stuff. After you have read the introductory tutorial write a simple function that reads the temperature and reports it in the serial monitor and does nothing else. When you get that working correctly write other functions to do the other stuff you need.

Don't forget the code tags </> when posting code.

Learn to be a data detective. What is 2147483647 in binary? :slight_smile:

Also I doubt that you've posted a complete sketch. I can't find definitions for some of the variables...

Thanks for this. Hmm, I'm clearly not good enough at this detective work.. I've converted the number to binary but am not sure what that is telling me, is this helpful to my problem?

00110010 00110001 00110100 00110111 00110100 00111000 00110011 00110110 00110100 00110111

Maybe I'm being dumb here... that is quite likely. Apologies if I need this spelling out a little more.

Good idea. I'll do as you suggest and strip out the code.

Got it! Sorry about that. I've edited my original post.

I can't comment on that, but you didn't succeed in converting it to binary... the digital string you posted is not even close... you just printed the binary values of the ASCII digits. Those are irrelevant for the value of a variable in a program.

It would provide a "clue". You can decide for yourself whether clues would be useful in solving your problem.

OK, but while you are at it learn to write separate functions, not cram everything into the loop function. Once you do that finding problems becomes a whole lot easier. Consider this problem, if you had a function to read the sensor and another one to display the data then a few carefully placed // would disable all your other code and enable you to find the problem.

The only things in the loop function should be calls to other functions.

1 Like