Could someone look at my code and see why Blynk isn't reading my DHT11 please?

I’m trying to get my ESP32 to show humidity and temperature to Blynk. Blynk is recognizing that the device is online but has no data on the gauges. I’ve triple-checked that V5 and V6 are correctly assigned to the gauges. Does anyone see anything wrong with this code?

• Hardware model: Spark Fun Thing Plus ESP32
• Web server
• Blynk server
• Blynk Library version v1.0.1


/*************************************************************

  This is a simple demo of sending and receiving some data.
  Be sure to check out other examples!
 *************************************************************/

// Template ID, Device Name and Auth Token are provided by the Blynk.Cloud
// See the Device Info tab, or Template settings
#define BLYNK_TEMPLATE_ID           "****"
#define BLYNK_DEVICE_NAME           "Quickstart Device"
#define BLYNK_AUTH_TOKEN            "****"


// Comment this out to disable prints and save space
#define BLYNK_PRINT Serial


#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <dht.h>
#include <SPI.h>

#define DHT11_PIN A1
dht DHT;

char auth[] = BLYNK_AUTH_TOKEN;

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "****";
char pass[] = "****";

BlynkTimer timer;

// This function is called every time the Virtual Pin 0 state changes
BLYNK_WRITE(V0)
{
  // Set incoming value from pin V0 to a variable
  int value = param.asInt();

  // Update state
  Blynk.virtualWrite(V1, value);
}

// This function is called every time the device is connected to the Blynk.Cloud
BLYNK_CONNECTED()
{
  // Change Web Link Button message to "Congratulations!"
  Blynk.setProperty(V3, "offImageUrl", "https://static-image.nyc3.cdn.digitaloceanspaces.com/general/fte/congratulations.png");
  Blynk.setProperty(V3, "onImageUrl",  "https://static-image.nyc3.cdn.digitaloceanspaces.com/general/fte/congratulations_pressed.png");
  Blynk.setProperty(V3, "url", "https://docs.blynk.io/en/getting-started/what-do-i-need-to-blynk/how-quickstart-device-was-made");
}

// This function sends Arduino's uptime every second to Virtual Pin 2.
void myTimerEvent()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V2, millis() / 1000);
}


void sendSensor()
{
  float h = DHT.humidity;
  float t = DHT.temperature; // or dht.readTemperature(true) for Fahrenheit

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t);
}


void setup()
{
  // Debug console
  Serial.begin(9600);

  pinMode(DHT11_PIN,INPUT);

  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk.cloud", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);

  // Setup a function to be called every second
  timer.setInterval(1000L, myTimerEvent);
}

void loop()
{
  Serial.println(DHT.temperature);
  delay(500);
  Serial.println(DHT.humidity);
  delay(500);
  Blynk.run();
  timer.run();
  // You can inject your own code or combine it with other sketches.
  // Check other examples on how to communicate with Blynk. Remember
  // to avoid delay() function!
}


Which pin have you attached your DHT sensor to?

You should increase this to 5000L, the DHT sensor is very slow (and inaccurate).

Remove this from your void loop.
If you want to print the readings then do this in your sendSensor function, and use the h and t variables, rather than re-reading your sensor.

You can’t use delays in Blynk, and that’s why you’re using a timer.

Pete.

1 Like

Thank you for the reply! I’m using pin 13 on the ESP32 (the code shoes A1 but that was just trying to troubleshoot, it’s normally on 13).

I’ve changed the write to 5000L. Now the Uptime widget is climbing slower.

The serial prints are just for debugging purposes to make sure it wasn’t the DHT sensor being bad. I’ve removed it from the code just in case but that didn’t work.

Do you see any other reason why Blynk might not be reading the sensor?

Also, anything with **** in the code has been replaced just for posting on a public forum. In the IDE, there’s actual values.

Sorry, I didn’t study your code closely enough. You should be using g a timer to call the sendSensor function, every 5000ms or more.

Yes, the sendSensor function isn’t currently being called, so never executes.

Pete.

You can try this example

Thank you for the reply. That’s the code that I’m working off of. I’ve tried running that code straight and it doesn’t work (or at least I can’t get it to work after 6-7 hours working on it). The code I have now is a hybrid of that code and some example code I had from a previous semester in school.

So I changed the line in the setup function to
timer.setInterval(1000L, sendSensor);

and in the loop function I added
sendSensor();

Sadly, still no input on the gauges, and now the uptime widget isn’t counting up. I also tried it without the call in the loop function but nothing changed.

So you didn’t listen to what I was saying about a 5000ms minimum read time for the sensor?

You either need two timers, or to put the uptime code in the sendSensor function along with the DHT code.

Having serial prints of the readings is a good idea, just not in the void loop, and not re-reading the sensors rather than printing the values that you are trying to send to Blynk.

Pete.

Okay, so, I changed some things around with the DHT calls and I think I’m making progress. Sorry, I did listen, I just forgot to change it after I pasted it into the IDE. Here’s my code as of now and a screenshot of the Blynk dashboard. Before I wasn’t getting anything on the gauges. Now, I’m at least getting something. It’s maxing out at 1 degree and 1% for some reason. Any ideas?

/*************************************************************

  This is a simple demo of sending and receiving some data.
  Be sure to check out other examples!
 *************************************************************/

// Template ID, Device Name and Auth Token are provided by the Blynk.Cloud
// See the Device Info tab, or Template settings
#define BLYNK_TEMPLATE_ID           "****"
#define BLYNK_DEVICE_NAME           "Quickstart Device"
#define BLYNK_AUTH_TOKEN            "****"


// Comment this out to disable prints and save space
#define BLYNK_PRINT Serial


#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <DHT.h>
#include <SPI.h>

#define DHT11_PIN 13
#define DHTTYPE DHT11


char auth[] = BLYNK_AUTH_TOKEN;

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "****";
char pass[] = "****";

DHT dht(DHT11_PIN,DHTTYPE);
BlynkTimer timer;

// This function is called every time the Virtual Pin 0 state changes
BLYNK_WRITE(V0)
{
  // Set incoming value from pin V0 to a variable
  int value = param.asInt();

  // Update state
  Blynk.virtualWrite(V1, value);
}

// This function is called every time the device is connected to the Blynk.Cloud
BLYNK_CONNECTED()
{
  // Change Web Link Button message to "Congratulations!"
  Blynk.setProperty(V3, "offImageUrl", "https://static-image.nyc3.cdn.digitaloceanspaces.com/general/fte/congratulations.png");
  Blynk.setProperty(V3, "onImageUrl",  "https://static-image.nyc3.cdn.digitaloceanspaces.com/general/fte/congratulations_pressed.png");
  Blynk.setProperty(V3, "url", "https://docs.blynk.io/en/getting-started/what-do-i-need-to-blynk/how-quickstart-device-was-made");
}

// This function sends Arduino's uptime every second to Virtual Pin 2.
void myTimerEvent()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V2, millis() / 5000L);
}


void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t);
}


void setup()
{
  // Debug console
  Serial.begin(9600);

  pinMode(DHT11_PIN,INPUT);

  dht.begin();
  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk.cloud", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);

  // Setup a function to be called every second
  timer.setInterval(5000L, sendSensor);
  timer.setInterval(5000L, myTimerEvent);
}

void loop()
{
//  Serial.println(DHT.temperature);
 // delay(500);
 // Serial.println(DHT.humidity);
 // delay(500);
  sendSensor();
  Blynk.run();
  timer.run();
  // You can inject your own code or combine it with other sketches.
  // Check other examples on how to communicate with Blynk. Remember
  // to avoid delay() function!
}

How did you configure your datastream ?

That’s it!!! That’s all I needed, thank you. It was set to 1 max. Now it’s working properly! Thank you to both of you so much!

1 Like