BlynkEdgent.h failed to read sensor

Hi, beginner here and I apologize in advance for my english as it’s not my first language. I’ve successfully implemented the dht11 code provided in https://examples.blynk.cc/, I even added bh1750 and it works perfectly fine on the esp8266 even the value pops up on the app and blynkconsole. But now I’m trying to use BlynkEdgent.h si I can connect it to a different wifi without having to change the coding. I tried using the BlynkEdgent.h example in arduino IDE and reused most of my already working code. I can connect it to my phone but it failed to read the sensor. Am I doing something wrong?

// Fill-in information from your Blynk Template here
#define BLYNK_TEMPLATE_ID "TMPLMEsYL1CB"
#define BLYNK_DEVICE_NAME "HP0nics"
#define BLYNK_FIRMWARE_VERSION        "0.1.0"

#define BLYNK_PRINT Serial
//#define BLYNK_DEBUG

#define APP_DEBUG

// Uncomment your board, or configure a custom board in Settings.h
//#define USE_SPARKFUN_BLYNK_BOARD
#define USE_NODE_MCU_BOARD
//#define USE_WITTY_CLOUD_BOARD
//#define USE_WEMOS_D1_MINI


#define DHTPIN D3
// Uncomment whatever type you're using!
#define DHTTYPE DHT11     // DHT 11
//#define DHTTYPE DHT22   // DHT 22, AM2302, AM2321
//#define DHTTYPE DHT21   // DHT 21, AM2301


#include <DHT.h>
#include <BH1750.h>
#include <Wire.h>
#include "BlynkEdgent.h"

DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer1;
BH1750 lightMeter;
float t,h,l;

// 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()
{
  t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
  h = dht.readHumidity();
  l = lightMeter.readLightLevel();
  if (isnan(t) || isnan(h)) {
    Serial.println("Failed to read from DHT11 sensor!");
    return;
    }
  if (isnan(l)){
    Serial.println("Failed to read from BH1750 sensor!");
    return;
    }
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V0, t);
  Blynk.virtualWrite(V1, h);
  Blynk.virtualWrite(V2, l);
}

void setup()
{
  Serial.begin(115200);
  delay(100);

  BlynkEdgent.begin();

  dht.begin();
  Wire.begin();
  lightMeter.begin();

  // Setup a function to be called every second
  timer1.setInterval(2000L, sendSensor);
}

void loop() {
  BlynkEdgent.run();
  timer1.run();

  t = dht.readTemperature(); 
  h = dht.readHumidity();
  l = lightMeter.readLightLevel();

  delay(800);
}

You need to start by removing these lines of code from your void loop.

You should also look at the Settings.h tab of your Edgent sketch and see if the pins you are using for the LED and button clash with any pins you have sensors connected to.

Pete.

Hi, thanks for the answer. I’ve removed the code lines and I’ve checked the pins used in settings.h tab, from what I see they’re not using the same pins as the sensors. (DHT11 use pin D3, BH1750 use pin
D1&D2)

#if defined(USE_NODE_MCU_BOARD) || defined(USE_WEMOS_D1_MINI)

  #define BOARD_BUTTON_PIN            0
  #define BOARD_BUTTON_ACTIVE_LOW     true

  #define BOARD_LED_PIN               2
  #define BOARD_LED_INVERSE           true
  #define BOARD_LED_BRIGHTNESS        255

I added D1,D2 inside wire.begin () and now the BH1750 sensor works.

Wire.begin(D1,D2);

But the DHT11 sensor still not working.

Take a look at this pinout diagram, and translate Pin D3 into a GPIO number…

Now can you see the conflict?

Pete.

2 Likes

Hello again, thank you for the answer Pete. Yes, I can see the conflict now and I decided to change the DHT11 pin to D5. But the serial monitor still shows “Failed to read from DHT11 sensor!”. I don’t know, it may sound dumb but I tried making another sketch to test just the DHT11 sensor using one of the pins for BH1750 (in this case D1) but it still didn’t work. Am I doing something wrong again???

This is the test sketch that didn’t work

// Fill-in information from your Blynk Template here
#define BLYNK_TEMPLATE_ID "TMPLMEsYL1CB"
#define BLYNK_DEVICE_NAME "HP0nics"
#define BLYNK_FIRMWARE_VERSION        "0.1.0"

#define BLYNK_PRINT Serial
//#define BLYNK_DEBUG

#define APP_DEBUG

// Uncomment your board, or configure a custom board in Settings.h
//#define USE_SPARKFUN_BLYNK_BOARD
#define USE_NODE_MCU_BOARD
//#define USE_WITTY_CLOUD_BOARD
//#define USE_WEMOS_D1_MINI

// Uncomment whatever type you're using!
#define DHTTYPE DHT11     // DHT 11
//#define DHTTYPE DHT22   // DHT 22, AM2302, AM2321
//#define DHTTYPE DHT21   // DHT 21, AM2301
#define DHTPIN D1

#include <DHT.h>

#include "BlynkEdgent.h"

DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer1;

float t,h;

// 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()
{
  t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
  h = dht.readHumidity();
  
  if (isnan(t) || isnan(h)) {
    Serial.println("Failed to read from DHT11 sensor!");
    return;
    }

  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V0, t);
  Blynk.virtualWrite(V1, h);
  
}

void setup()
{
  Serial.begin(115200);
  delay(100);

  BlynkEdgent.begin();

  dht.begin();
  

  // Setup a function to be called every second
  timer1.setInterval(2000L, sendSensor);
}

void loop() {
  BlynkEdgent.run();
  timer1.run();
}

Which type of DHT11 sensor do you have - one with 3 legs or 4?
How exactly is the sensor connected to your NodeMCU?

Pete.

Maybe the sensor is damaged, try a different one

3 legs, I use breadboard/project board and jumper.

But when I tried using example code from https://examples.blynk.cc/ :

#define BLYNK_TEMPLATE_ID "TMPLMEsYL1CB"
#define BLYNK_DEVICE_NAME "HP0nics"
#define BLYNK_AUTH_TOKEN "UxRaK39AFhChoGOxZw90JWU3LFuTygq5"

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


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

char auth[] = BLYNK_AUTH_TOKEN;

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

          
#define DHTPIN D1         // What digital pin we're connected to
// Uncomment whatever type you're using!
#define DHTTYPE DHT11     // DHT 11
//#define DHTTYPE DHT22   // DHT 22, AM2302, AM2321
//#define DHTTYPE DHT21   // DHT 21, AM2301

DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;
BH1750 lightMeter;

// 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
  float h = dht.readHumidity();
  float l = lightMeter.readLightLevel();
  if (isnan(t) || isnan(h)) {
    Serial.println("Failed to read from DHT11 sensor!");
    return;
    }
  if (isnan(l)){
    Serial.println("Failed to read from BH1750 sensor!");
    return;
    }
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V0, t);
  Blynk.virtualWrite(V1, h);
  Blynk.virtualWrite(V2, l);
}

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

  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);

  dht.begin();
  Wire.begin(D2,D3);
  lightMeter.begin();

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

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

It worked fine.

Do you think that provides me with any useable information which pins on your sensor are connected to which pins on your NodeMCU?

Pete.

Sorry,
This one is when i changed the DHT11 out pin to D5:

And this one is the test where I only use DHT11:

Can you post a photograph of the actual DHT11 that you are using, not a stock image of a DHT11

Pete.

Hi, did you solve your problem? I also have the same problem, while I use the blynk.run(), it was OK, but while I change to BlynkEdgent.run(), It seems measure failed in some time. I don’t know why~

Which pin are you using for your DHT11, and does it clash with any pins already in use in Settings.h ?

Read this for more info…

Pete.