DHT22 Garbage Value

Hi, can anyone help figure what is wrong? constantly getting these garbage values despite changing the variable types
image

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include "DHT.h"
#define DHTTYPE DHT22
#define dht_dpin D4
DHT dht(dht_dpin, DHTTYPE);

//All variables with their data types
int temp2, setpoint2;

//Your Auth Token
char auth[] = "-";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "SINGTEL-FA54";
char pass[] = "zucheohaen";

WidgetBridge bridge1(V1);

BLYNK_CONNECTED() {
  // Place the AuthToken of the Device -1 here
  bridge1.setAuthToken("-");
}

BLYNK_WRITE(V12)
{
  temp2 = param.asFloat();
}



BLYNK_WRITE(V22)
{
  setpoint2 = param.asFloat();
}


void setup()
{
  Blynk.begin(auth, ssid, pass);
  dht.begin();
  Serial.begin(9600);
}

void loop()
{
  Blynk.run();

  temp2 = dht.readTemperature();
  Blynk.virtualWrite(V12, temp2);
  //bridge1.virtualWrite(V12, temp2);
  //bridge1.virtualWrite(V22, setpoint2);

}

Put this in a timed function that’s called no more tan once every 5 seconds.
Use a float variable type.

I don’t understand why you’re assigning a value to temp2 from the widget V12 and from the DHT22.

Pete.

I have tried the following code but no value appeared. changed my wemos, sensor and cable but still does not work.

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "-";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "SINGTEL-FA54";
char pass[] = "zucheohaen";

#define DHTPIN D4          // What digital pin we're connected to
#define DHTTYPE DHT22   // DHT 22, AM2302, AM2321

DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;

// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void sendSensor()
{
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

  if (isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  Blynk.virtualWrite(V12, t);
}

void setup()
{
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);
  dht.begin();

  timer.setInterval(1000L, sendSensor);
}

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

What does your serial monitor show?

Pete.

Also as Pete pointed out set your timer to min 3 seconds. Preferably 5.

It says fail to read sensor but I’m not too sure why. As mentioned, I have changed all components for multiple times. My device is online on Blynk App as well.

I have been trying to troubleshoot this issue for weeks… temperature on blynk app is either garbage value or no reading.

Add some serial print lines to see exactly what nan (not a number) values the sensor is giving.
Check that your DHT library is up to date, and maybe delete it and install a different one instead.

Pin D4 (GPIO2) isn’t a great choice of pin either:

Pete.

I have gotten the nan value multiple times but the serial monitor still reads failed to read sensor.

I have also tried alternating the pins of the WeMos D1 mini board.

You only get the “failed to read…” message if the value returned by the sensor is not a number.
I was suggesting that you add some serial print messages to see what was being returned.

And what about the suggestion to change the library?

Pete.