DHT 22 Sensor Data pushing to Firebase

Hi, all i’ve been trying to upload the DHT22 data to the Firebase using the firebase arduino library and ESP8266.

Initially it will transmit the temperature and humidity as expected but after some time, it would be pushing to the database the last updated value instead of the realtime value.

the weird thing is i tried serial.print of the variable I am pushing and seems it’s being updated with the latest humidity and temperature value…

i tried another testing where tried sending a count value incrementing to Database and yes it’s working as expected… I am wondering how to debug the DHT22 outdated value pushing ?

/*
changes that had to make 
*/


#include "ntpUtils.h"
#include "authUtils.h"

#include <FirebaseArduino.h>
#include <DHT.h>

#define DHTPIN D4
#define FANPIN D6
#define LIGHTPIN D5
#define SENSORPIN D8
#define DHTTYPE DHT22
#define DELAY_TIME_TO_SEND  5 // 5 min
#define TABLE_LEITURA "leitura"
#define TABLE_MOTION "motion"
#define CLIENT_EMAIL "manojkumar"
#define DEVICE_PATH "/devices/1/"

DHT dht(DHTPIN, DHTTYPE);

StaticJsonBuffer<2000> jsonBuffer;
JsonObject &data = jsonBuffer.createObject();
//JsonObject &motion = jsonBuffer.createObject();
int val = 0;
int count = 15000;
String temperatura = "";
bool motionSensorRead = false;

void setup() {
    Serial.begin(9600);
    WiFi.mode(WIFI_STA);
    WiFi.disconnect();
    WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
    Serial.print("connecting");
    while (WiFi.status() != WL_CONNECTED) {
        Serial.print(".");
        delay(500);
    }
    Serial.println();
    Serial.print("connected: ");
    Serial.println(WiFi.localIP());
    Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
    udp.begin(UDP_NTS_PORT);
    pinMode(LED_BUILTIN, OUTPUT);
    pinMode(FANPIN, OUTPUT);
    pinMode(LIGHTPIN, OUTPUT);
    pinMode(SENSORPIN, INPUT);
    dht.begin();
    //motionSensorRead = !digitalRead(SENSORPIN);
    //digitalWrite(LED_BUILTIN, !Firebase.getBool(String(DEVICE_PATH) + "light"));

}

inline String getErrorString(String table) {
    return String(TABLE_LEITURA) + " failed to send:";
}

void inline readMotionSensorAndSendToFirebase() {
    if (digitalRead(SENSORPIN) != motionSensorRead) {
        motionSensorRead = digitalRead(SENSORPIN);
        //motion["state"] = String(motionSensorRead);
        //motion["date"] = getEpoch();
        //Firebase.push(TABLE_MOTION, motion);
        Serial.println("sensor=" + String(motionSensorRead));
        delay(1000);
    }

    if (Firebase.failed()) {
        Serial.print(getErrorString(TABLE_MOTION));
        Serial.println(Firebase.error());
        return;
    }
}

void setLeitura(String temperatura, String umidade ) {
    
    if(val >=100)
    {
    val=0;
    }
    else
    {
      val++;
    }
    data["temp"] = temperatura;
    data["hum"] = umidade;
    data["pH"] = val;
    

    
    
}

void readDataToSend() {
    temperatura = String(dht.readTemperature());
    String umid = String(dht.readHumidity());
    String cliente = CLIENT_EMAIL;
    setLeitura(temperatura, umid);
    //String data = getEpoch();
    //String motion = String(digitalRead(SENSORPIN));
    delay(1000);
    
    Serial.println(
            "temp=" + temperatura + " hummidity=" + umid + " email=" +  " dateTime=" +  " ");
}

void  readLeituraAndSendToFirebase() {
    
    readDataToSend();

    if (count > DELAY_TIME_TO_SEND) {

        if (data["temp"] == "nan" || data["hum"] == "nan" ) {
            Serial.println("fuckedUp");
            return;
        }

        count = 0;
        Firebase.push("D1", data); // parent node is D1
    }

    if (Firebase.failed()) {
        Serial.print(getErrorString(TABLE_LEITURA));
        Serial.println(Firebase.error());
        return;
    }
}

void inline updateLighAndFan() {
    digitalWrite(FANPIN, !Firebase.getBool(String(DEVICE_PATH) + "fan"));
    digitalWrite(LIGHTPIN, !Firebase.getBool(String(DEVICE_PATH) + "light"));
    digitalWrite(LED_BUILTIN, !Firebase.getBool(String(DEVICE_PATH) + "light"));
}

//TODO verify wheter is this okay or not
void inline updateNtp() {
    WiFi.hostByName(ntpServerName, timeServer);
    setSyncProvider(getNtpTime);
    setSyncInterval(300);
}

void inline updateCountAndPrint() {
    delay(100);
    count++;
    Serial.println("/-----Count-------");
    Serial.println(count);
    Serial.println("------Count------/");
}

void loop() {
    //updateNtp();
    //readMotionSensorAndSendToFirebase();
    readLeituraAndSendToFirebase();
    //updateLighAndFan();
    updateCountAndPrint();
}

It doesn’t seem this is your problem, but when doing a similar project I had a problem with the DHT library. It seems there’s some small, microsecond sized, timing error when working with the ESP8266 boards. It could be possible that using the serial monitor takes just enough time to fix this?

In any case, if you find it useful, here’s the library I found that worked: Arduino/libraries/DHTNEW at master · RobTillaart/Arduino · GitHub

can you show the results of the data from ESP8266 to firebase?