DHT11 sensor cannot send data to BLYNK app

Hi, i am using Arduino Mega 2560 to read temperature and humidity value from my DHT11 sensor and display it on the BLYNK app. The sensor values can be displayed in Arduino Serial Monitor but cannot display inside BLYNK. Blynk will disconnect and i don’t know why. Tried run other programs using BLYNK and it works just fine so there must be something wrong with this coding. BTW i tot this coding from the Blynk examples. Can someone please help me?

#define BLYNK_PRINT Serial
#include <SPI.h>
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <Blynk.h>
#include "DHT.h"
// or Software Serial on Uno, Nano...
#include <SoftwareSerial.h>
SoftwareSerial EspSerial(10, 11); // RX, TX

// Your ESP8266 baud rate:
#define ESP8266_BAUD 9600
#define DHTPIN 11
#define DHTTYPE DHT11   
DHT dht(DHTPIN, DHTTYPE);
ESP8266 wifi(&EspSerial);
BlynkTimer timer;


char auth[] = "***************";

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

// Hardware Serial on Mega, Leonardo, Micro...
//#define EspSerial Serial1

float h =0;
float t =0;


//Fan Part
WidgetLED led3(V3);

BLYNK_WRITE(V3)
{
  // You'll get HIGH/1 at startTime and LOW/0 at stopTime.
  // this method will be triggered every day
  // until you remove widget or stop project or
  // clean stop/start fields of widget
  
  int value = param.asInt();
  digitalWrite(12,value);

  if (value == LOW){
    led3.off();
    Serial.println("AC OFF");
  } else {
    led3.on();
    Serial.println("AC ON");
  }
}

void tempsensor(){

 
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  Serial.println(h);
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  Serial.println(t);
 

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) ) {
    Serial.println(F("Failed to read from DHT sensor!"));
    
     return;
  }
   Blynk.virtualWrite(V4, t);
   Blynk.virtualWrite(V5, h);
}

//Setup
void setup() { // the setup function runs once when you press reset or power the board

// Debug console
  Serial.begin(9600);

  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(10);

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

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

void loop()
{
  Blynk.run();
  //tempsensor();
  timer.run();

}

The only thing I can really see is that you are using the same pin for you ESPSerial connection, and for your DHT11. Maybe try putting the DHT11 on a different pin.

1 Like

Yes, but if you go back to the original Sketch Builder example you’ll see that it says to use hardware serial for the Mega, but you’ve gone down the route of using Software Serial and you’ve changed the pins from 2 & 3 to 10 & 11 - which as @Toro_Blanco has pointed-out means that pin 10 is being used for two connections, which will never work.

Pete.

1 Like

Yes try changing it to PIN 2 and it works. Thank you!!

A DHT11-sensor with a timer set to 1 second?! Since when did that work flawlessly? :crazy_face:

1 Like

You have declared t and h as global and local variable.
Calling dht11 sensor every one second never works and makes no sense. :thinking:

Hi, i am using Arduino Mega 2560 with ESP8266 on a local server. Tried running the code below and my Blynk will disconnect. Is it because of flooding? BTW i tried running the functions separately and it works fine but when i compile them into a single code they do not work. Can someone please help me

#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <Blynk.h>
#include "DHT.h"
#include <SPI.h>
#include <SoftwareSerial.h>
SoftwareSerial EspSerial(10, 11); // RX, TX

// Your ESP8266 baud rate:
#define ESP8266_BAUD 9600
#define DHTPIN 11
#define DHTTYPE DHT11   // DHT 22  (AM2302), AM2321
DHT dht(DHTPIN, DHTTYPE);
ESP8266 wifi(&EspSerial);
BlynkTimer timer;

int PWM = 3;
boolean running_two = false;
int takeover2 = 0;


char auth[] = "***";
char ssid[] = "**";
char pass[] = "**";

//dht and fan
WidgetLED led3(V3);

BLYNK_WRITE(V3)
{
  int value = param.asInt();
  digitalWrite(12,value);

  if (value == LOW){
    led3.off();
    Serial.println("AC OFF");
  } else {
    led3.on();
    Serial.println("AC ON");
  }
}

void tempsensor(){
running_two = takeover2; 

  float h = dht.readHumidity();
  float t = dht.readTemperature();
 
  Blynk.virtualWrite(V4, t);
  Blynk.virtualWrite(V5, h);

  if (t > 35 && running_two == false){
  digitalWrite(PWM, HIGH);
  
  delay(10);
}
else if (t < 35 && running_two == false) {
  digitalWrite(PWM, LOW); 
 
}
  }

BLYNK_WRITE(V6) // Button Widget is writing to pin V1
{
  int fanSpeed = param.asInt();
  
   if (fanSpeed == 0) {
     analogWrite(PWM, 0);
    Serial.println("FAN OFF");
   } else if (fanSpeed == 1) {
    analogWrite(PWM, 190);
    Serial.println("LOW");
  }else if (fanSpeed == 2) {
    analogWrite(PWM, 255);
    Serial.println("HIGH"); 
  } 
}

BLYNK_WRITE(V7) {
  takeover2 = param.asInt();

  if (takeover2 == HIGH){
  running_two == true; 
  Serial.println("takeover fan");
   }
  else if (takeover2 == LOW) {
  running_two == false;
  Serial.println("back to DHT11");
  return running_two;
   }
 }

//ldr
const int ledPin = 13;   //the number of the LED pin
const int ldrPin = A0;  //the number of the LDR pin

int val = 0;
int takeover = 0;
int relay1State = 0;
boolean running = false; //


BLYNK_WRITE(V2) {
  relay1State = param.asInt();
  digitalWrite(ledPin, relay1State);
}

BLYNK_WRITE(V1) {
  takeover = param.asInt();

  if (takeover == HIGH){
  running == true; 
  Serial.println("taken over");
   }
  else if (takeover == LOW) {
  running == false;
  Serial.println("back to LDR sensor");
  return running;
   }
 }
 
void checkLDR() {
  int relay1Status = digitalRead(ledPin);
  int ldrStatus = analogRead(ldrPin);   //read the status of the LDR value
  running = takeover;
 
  //check if the LDR status is <= 300
  //if it is, the LED is HIGH
  
 if (ldrStatus <=300 && running == false) {
    digitalWrite(ledPin, HIGH);               //turn LED on
    Serial.println("Light Bulb is ON");
    Blynk.virtualWrite(V1, relay1Status);
    
   } 
  else if (ldrStatus >=300 && running == false  ){
    digitalWrite(ledPin, LOW);               //turn LED off
    Serial.println("Light Bulb is OFF");
    // Update Button Widget
      Blynk.virtualWrite(V1, relay1Status);
 }
 
} 

//Setup
void setup() { // the setup function runs once when you press reset or power the board

// Debug console
  Serial.begin(9600);

  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(10);

  Blynk.begin(auth, wifi, ssid, pass, "***", 8080);
  //lights
  pinMode(ledPin, OUTPUT);  //initialize the LED pin as an output
  pinMode(ldrPin, INPUT);   //initialize the LDR pin as an input
  digitalWrite(ledPin, relay1State);
  timer.setInterval(4000L, checkLDR);
  
  pinMode(12,OUTPUT);
  dht.begin();

  pinMode(PWM,OUTPUT);
  // Setup a function to be called every second
  timer.setInterval(2000L, tempsensor);


}





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

@m9506 I’ve merged your new topic into your previous one.

The answer is the same, you can’t use Pin 11 for both your DHT sensor and Software Serial.
You have a Mega, which has 3 serial ports, so you shouldn’t be using SoftwareSerial anyway.

Please don’t keep creating new topics about the same issue, especially when the problem has been answered already.

Pete.

Sorry, i forgot to change the pin number when compiling the codes. Thank you