DHT22- shows only one value

Hi there
I am new to this and trying to get Blynk working with my DHT22/AM2302 module, Arduino UNO and Ethernet Shield . It’s a class project for my students…

Anyway, both my Blynk Widgets show the humidity value, even though I have temperature and humidity on separate virtual pins.

Please have a look at my code…What am I missing? Any help would be great.

#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>

#define DHTPIN            2
#define DHTTYPE           DHT22
DHT_Unified dht(DHTPIN, DHTTYPE);
uint32_t delayMS;

char auth[] = "29cc7f6141894c02bafcaadfc067cc2a";

void setup()
{
  
  Serial.begin(9600);
  dht.begin();
  delayMS = sensor.min_delay / 1000;
}

void loop() {
  Blynk.begin(auth);
  // Delay between measurements.
  delay(delayMS);
  // Get temperature event and print its value.
  sensors_event_t event;  
  dht.temperature().getEvent(&event);
  if (isnan(event.temperature)) {
    Serial.println("Error reading temperature!");
  }
  else {
    Serial.print("Temperature: ");
    Serial.print(event.temperature);
    Serial.println(" *C");
  }
  // Get humidity event and print its value.
  dht.humidity().getEvent(&event);
  if (isnan(event.relative_humidity)) {
    Serial.println("Error reading humidity!");
  }
  else {
    Serial.print("Humidity: ");
    Serial.print(event.relative_humidity);
    Serial.println("%");
  }
  
  Blynk.virtualWrite(1, event.temperature);
  Blynk.virtualWrite(2, event.relative_humidity);

 Blynk.run();
}

Much appreciated

First you are running everything in your void loop() and while the ethernet connection will handle it without disconnecting much, it is still bad practice with Blynk based sketches.

You can check out the Sketch Builder (link at the upper right of this page) to see a DHT example with timed routines.

https://examples.blynk.cc/?board=Arduino%20Uno&shield=Ethernet%20Shield%20W5100&example=More%2FDHT11

Secondly, I found that I needed to run any Blynk.virtualWrite(1, event.temperature); command within the same process as it’s relevant dht.temperature().getEvent(&event); otherwise the data seemed to be forgotten when calling the other event data.

e.g.

  dht.temperature().getEvent(&event);
  if (isnan(event.temperature)) {
    Serial.println("Error reading temperature!");
  }
  else {
    Serial.print("Temperature: ");
    Serial.print(event.temperature);
    Serial.println(" *C");
    Blynk.virtualWrite(1, event.temperature);
  }
1 Like