BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Problem with DHT Reading with blynk. Test Code Works


#1

Hello

I cant find a problem with my code. test Code from DHT Library works good, every read every second have values.
But in my code i have like 1 succesful over 50/100 reads.
Maybe someone have that problem or see some error in my code.


#include <FS.h>
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>
#include <Wire.h>
#include "SSD1306.h"
#include <NTPClient.h>
#include <WiFiUdp.h>
#include "font.h"
#include <WiFiManager.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <ArduinoJson.h>
#define DHTTYPE DHT11 
#define dht_dpin 2

DHT dht(dht_dpin, DHTTYPE); 

char blynk_token[34] = "";

bool shouldSaveConfig = false;

//callback notifying us of the need to save config
void saveConfigCallback () {
  Serial.println("Should save config");
  shouldSaveConfig = true;
}

WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "185.207.104.70", 3600, 60000);

SSD1306  display(0x3c, D3, D5);

BlynkTimer timer;

void sendSensor()
{
  String formattedDate;
  String dayStamp;
  String timeStamp;

  timeClient.update();

  formattedDate = timeClient.getFormattedDate();
  int splitT = formattedDate.indexOf("T");
  dayStamp = formattedDate.substring(0, splitT);
  timeStamp = formattedDate.substring(splitT + 1, formattedDate.length() - 1);

  Serial.println("day = " + String(dayStamp) + " and time = " + String(timeStamp));

  float h = dht.readHumidity();
  float t = dht.readTemperature();

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read DHT " + String(h) + " " + String(t));
    return;
  }

  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t);

  Serial.println("humidity = " + String(h) + "% and temperature = " + String(t) + "°C");

//display code removed

}

void setup()
{
  dht.begin();
  delay(700);
  Serial.begin(115200);

  //SPIFFS.format();

  display.init();

  if (SPIFFS.begin()) {
    Serial.println("mounted file system");
    if (SPIFFS.exists("/config.json")) {
      //file exists, reading and loading
      Serial.println("reading config file");
      File configFile = SPIFFS.open("/config.json", "r");
      if (configFile) {
        Serial.println("opened config file");
        size_t size = configFile.size();
        // Allocate a buffer to store contents of the file.
        std::unique_ptr<char[]> buf(new char[size]);
        configFile.readBytes(buf.get(), size);
        DynamicJsonBuffer jsonBuffer;
        JsonObject& json = jsonBuffer.parseObject(buf.get());
        json.printTo(Serial);
        if (json.success()) {
          Serial.println("\nparsed json");
          strcpy(blynk_token, json["blynk_token"]);
        } else {
          Serial.println("failed to load json config");
        }
      }
    }
  } else {
    Serial.println("failed to mount FS");
  }

  WiFiManagerParameter custom_blynk_token("blynk", "blynk token", blynk_token, 33);
  WiFiManager wifiManager;
  wifiManager.setSaveConfigCallback(saveConfigCallback);
  wifiManager.addParameter(&custom_blynk_token);

//display code removed

  //wifiManager.resetSettings();
  if (!wifiManager.autoConnect("Monitor", "password")) {
    Serial.println("WIFI connection failed");
//display code removed
    delay(3000);
    //reset and try again, or maybe put it to deep sleep
    ESP.reset();
    delay(5000);
  }

  Serial.println("WIFI Connected");
//display code removed

  strcpy(blynk_token, custom_blynk_token.getValue());

  if (shouldSaveConfig) {
    Serial.println("saving config");
    DynamicJsonBuffer jsonBuffer;
    JsonObject& json = jsonBuffer.createObject();

    json["blynk_token"] = blynk_token;

    File configFile = SPIFFS.open("/config.json", "w");
    if (!configFile) {
      Serial.println("failed to open config file for writing");
    }
    json.printTo(Serial);
    json.printTo(configFile);
    configFile.close();
  }

  Blynk.config(blynk_token, IPAddress(139, 59, 206, 133), 8080);
  //Blynk.begin(auth, ssid, pass, IPAddress(139,59,206,133), 8080);
  bool result = Blynk.connect();

  if (result != true)
  {
    Serial.println("Blynk connection faile");
//display code removed
    Serial.println(blynk_token);
    wifiManager.resetSettings();
    ESP.reset();
    delay (5000);
  }
  else
  {
    Serial.println("Blynk connection succesful");
//display code removed
    delay (5000);
  }

  timeClient.begin();
  timeClient.setTimeOffset(7200);
  timer.setInterval(1000L, sendSensor);
}

void loop()
{
  Blynk.run();
  timer.run();
}


#2

To solve this you need to add dht.begin(); just before reading :slight_smile:

  dht.begin();
  float h = dht.readHumidity();
  float t = dht.readTemperature();

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read DHT " + String(h) + " " + String(t));
    return;
  }

#3

The DHT11 is a very low quality… recommended reading rate 5+ seconds