BLYNK
BLYNK.IO       📲 GETTING STARTED       📗 DOCS       👉 SKETCH BUILDER

I need a help.I cannot use dht 11 with mq2 and blynk

Hello Guys
I need your help.
I am using esp 32 together with mq2 sensor.
When I added dht11 to my circuit,the connection broken and lcd was stop.
.Dht 11 is working but if I use together with circuit I see a peoblem .What can I do .I dont see the measures on blynk.


#include <LiquidCrystal_I2C.h>
#include "DHT.h"
int esikdegeri=1500;
int toggleState_1=0;
int toggleState_2=0;
int toggleState_3=0;
int toggleState_4=0;
//int buzzer=18;
int deger=0;
int gazokuma=36; 
int lcdColumns = 16;
int lcdRows = 2;
int role_1=27;
int role_2=26;
int role_3=25;
int role_4=33;
int buzzer=18;
int ok=32;
//int sayac=0;
int yukari=35; 
int asagi=34;
//Set Degerleri Belirleme 
int ayarsayac=1;  
int gerilimset=500;
int akimset=500;
int gazsensorset=1000;
int sicaklikset=500;
int nemset=500;
bool acildurum=false;
bool anaekran=false; 
bool ayarlar=false;
bool gazsetbelirle=false;
bool ayarsayackontrol=false;
bool gerilimsetbelirle=false;
bool akimsetbelirle=false;
bool sicakliksetbelirle=false;
bool nemsetbelirle=false;
int yukarisayac=0;
int asagisayac=0;
unsigned long acilsimdi=0;
unsigned long acilsonra=0;
unsigned long eskizaman=0;
unsigned long simdikizaman;
unsigned long ayarlarsimdi=0;
unsigned long ayarlareski=0;
unsigned long butonsimdi=0;
unsigned long butoneski=0;
LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows);  
// Fill-in information from your Blynk Template here
#define BLYNK_TEMPLATE_ID "TMPLebZli4eo"
#define BLYNK_DEVICE_NAME "SmartBox"
#define DHTPIN 13 //D5 pin of Node MCU 
#define DHTTYPE DHT11

//#include <LiquidCrystal_I2C.h>

#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_WROVER_BOARD
//#define USE_TTGO_T7
#include "BlynkEdgent.h"
DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer1;
BlynkTimer timer;
float h , t;
int sayac=1;
#include <PZEM004Tv30.h>

/* Hardware Serial2 is only available on certain boards.
 * For example the Arduino MEGA 2560
*/
#if defined(ESP32)
PZEM004Tv30 pzem(Serial2,16,17);
#else
PZEM004Tv30 pzem(Serial2);
#endif

//int esikdegeri=1000;
//int buzzer=18;
//int deger;
//int gazokuma=4; 
void sendSensor()
{
float voltage = pzem.voltage();
float current = pzem.current();
float h = dht.readHumidity();
float t = dht.readTemperature();
deger=analogRead(gazokuma);
Blynk.virtualWrite(V1, h);
Blynk.virtualWrite(V0, t);
Blynk.virtualWrite(V2, voltage);
Blynk.virtualWrite(V3, current);
Blynk.virtualWrite(V4, deger);
Blynk.virtualWrite(V9,sicaklikset);
Blynk.virtualWrite(V10,gazsensorset);
Blynk.virtualWrite(V11,akimset);
Blynk.virtualWrite(V12,gerilimset);
Blynk.virtualWrite(V13,nemset);
//butonkontrol();
//gaz_olcum();
//if((acildurum!=true)&&(ayarlar!=true)) 
//{
//anaekran_menu();
////gaz_olcum();
//} 
}
void anadongu() 
{
simdikizaman=millis();
acilsimdi=millis();
ayarlarsimdi=millis();
butonsimdi=millis();
if(simdikizaman-eskizaman>=2500) 
{
if((acildurum!=true)&&(ayarlar!=true)) 
{
anaekran_menu();
Serial.println("Ana EKRAN Cagırıldı...");


}
eskizaman=simdikizaman;
}
//Acil Durumda Cagrılcak Fonksiyon
if(acilsimdi-acilsonra>100)
{
acil_durum();
acilsimdi=acilsonra;  
}
if(ayarlarsimdi-ayarlareski>500)
{

if(digitalRead(ok)==true) 
{
ayarlar=true;
ayarsayackontrol=true;
}
if(ayarlar==true)
{
ayarlarmenu(); 
}
ayarlareski=ayarlarsimdi; 
}
if(butonsimdi-butoneski>100) 
{

butonkontrol();
butoneski=butonsimdi;  
}
}    


BLYNK_WRITE(V5) {
  toggleState_1 = param.asInt();
  if(toggleState_1 == 1){
    digitalWrite(role_1, LOW);
  }
  else { 
    digitalWrite(role_1, HIGH);
  }
}

BLYNK_WRITE(V6) {
  toggleState_2 = param.asInt();
  if(toggleState_2 == 1){
    digitalWrite(role_2, LOW);
  }
  else { 
    digitalWrite(role_2, HIGH);
  }
}

BLYNK_WRITE(V7) {
  toggleState_3 = param.asInt();
  if(toggleState_3 == 1){
    digitalWrite(role_3, LOW);
  }
  else { 
    digitalWrite(role_3, HIGH);
  }
}

BLYNK_WRITE(V8) {
  toggleState_4 = param.asInt();
  if(toggleState_4 == 1){
    digitalWrite(role_4, LOW);
  }
  else { 
    digitalWrite(role_4, HIGH);
  }
}
void setup()
{
Serial.begin(115200);
  // initialize LCD
 lcd.begin();                   
 lcd.backlight();
 pinMode(buzzer,OUTPUT);
 pinMode(role_1,OUTPUT);
 pinMode(role_2,OUTPUT);
 pinMode(role_3,OUTPUT);
 pinMode(role_4,OUTPUT);
 pinMode(buzzer,OUTPUT);
 pinMode(ok,INPUT);
 pinMode(yukari,INPUT); 
 pinMode(asagi,INPUT); 
 Serial.println(F("DHTxx test!"));
 dht.begin();
 pinMode(gazokuma, INPUT);
 lcd.clear(); 
 lcd.setCursor(0,0); 
 lcd.print("Haydar"); 
 lcd.setCursor(0,1); 
 lcd.print("Gungor"); 
 delay(1500);
 lcd.clear(); 
 lcd.setCursor(0,0); 
 lcd.print("Dogus UNI"); 
 lcd.setCursor(0,1); 
 lcd.print("Bitirme Projesi"); 
 delay(1500);
 lcd.clear(); 
 BlynkEdgent.begin();
 timer1.setInterval(1000L,sendSensor);
 timer1.setInterval(2500L,anadongu);
 timer1.setInterval(500L,kontrols);
 Serial.begin(115200);
}
void kontrols()
{
  butonkontrol();
  if(digitalRead(ok)==true) 
{
ayarlar=true;
ayarsayackontrol=true;
}
if(ayarlar==true)
{
ayarlarmenu(); 
}
  
}
void loop() 
{
 BlynkEdgent.run();
 timer1.run();
 //timer.run();
}
void dht11_olcum()
{
 lcd.clear();
  float h = dht.readHumidity();
  float t = dht.readTemperature();
//  float f = dht.readTemperature(true);
//  if (isnan(h) || isnan(t) || isnan(f)) 
//  {
//    Serial.println(F("Failed to read from DHT sensor!"));
//    return;
//  }  
  //lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Sicaklik:"); 
  lcd.setCursor(11,0);
  lcd.print(t); 
  lcd.setCursor(0,1);
  lcd.print("Nem:"); 
  lcd.setCursor(11,1);
  lcd.print(h); 
}
void gaz_olcum()
{
lcd.clear();
deger=analogRead(gazokuma);
//Serial.println("Deger :");
//Serial.println(deger);
lcd.setCursor(0,0);
lcd.print("Gaz  Deger:");
lcd.setCursor(10,1);
lcd.print(deger);
}
void elektrikselolcum()
{
lcd.clear();
float voltage = pzem.voltage();
if(voltage != NAN)
{
Serial.print("Voltage:"); 
Serial.print(voltage); 
Serial.println("V");
}
else 
{
//Serial.println(“Error reading voltage”);
}
float current = pzem.current();
if(current != NAN)
{
Serial.print("Current: ");
Serial.print(current); 
Serial.println("A");
}
else 
{
Serial.println("Error reading current");
}
float power = pzem.power();
if(current != NAN)
{
Serial.print("Power: "); 
Serial.print(power); 
Serial.println("W");
}
else 
{
Serial.println("Error reading power");
}
float energy = pzem.energy();
if(current != NAN)
{
Serial.print("Energy: ");
Serial.print(energy,3); 
Serial.println("kWh");
} 
else 
{
Serial.println("Error reading energy");
}
float frequency = pzem.frequency();
if(current != NAN)
{
Serial.print("Frequency: "); 
Serial.print(frequency, 1); 
Serial.println("Hz");
} else 
{
Serial.println("Error reading frequency");
}
float pf = pzem.pf();
if(current != NAN)
{
Serial.print("PF: "); 
Serial.println(pf);
} else {
Serial.println("Error reading power factor");
}
lcd.setCursor(0,0);
lcd.print("Voltage:");
//Gerilim
lcd.setCursor(11,0);
lcd.print(voltage);
lcd.setCursor(0,1);
lcd.print("Current:");
//Akım
lcd.setCursor(11,1);
lcd.print(current);
//delay(3000);
//lcd.clear();
//lcd.setCursor(0,0);
//lcd.print("Power:");
//Güç
//lcd.setCursor(11,0);
//lcd.print(power);
//lcd.setCursor(0,1);
//lcd.print("Frequency :");
////Frekans
//lcd.setCursor(11,1);
//lcd.print(frequency);
//
//  float v = pzem.voltage();
//  if (v < 0.0) v = 0.0;
//   Serial.println(v);Serial.println("V; ");
//
//  float i = pzem.current();
//   if(i >= 0.0){ Serial.println(i);Serial.println("A; "); }
//
//  float p = pzem.power();
//   if(p >= 0.0){ Serial.println(p);Serial.println("W; "); }
//
//  float e = pzem.energy();
//   if(e >= 0.0){ Serial.println(e);Serial.println("Wh; "); }
}
void acil_durum() 
{
float voltage = pzem.voltage();
float current = pzem.current();
float h = dht.readHumidity();
float t = dht.readTemperature();
deger=analogRead(gazokuma);
if((voltage>gerilimset)||(current>akimset)||(h>nemset)||(t>sicaklikset)||(deger>gazsensorset))
{
lcd.clear();
acildurum=true;
anaekran=false;
ayarlar=false;
lcd.setCursor(0,0); 
lcd.print("Acil");
lcd.setCursor(0,1); 
lcd.print("Durum!!!!");
digitalWrite(buzzer,HIGH);
digitalWrite(role_1,LOW);
digitalWrite(role_2,LOW);
digitalWrite(role_3,LOW);
digitalWrite(role_4,LOW);
}
else
{
acildurum=false; 
digitalWrite(buzzer,LOW);
} 
}
void ayarlarmenu() 
{

////////////////////////////////////////////////////////////Ayar Sayac İçeerisindki Kullanım bitimi. ......
if(ayarsayac==1)
{
lcd.clear();
lcd.setCursor(0,0); 
lcd.print("Gaz Sensor");
lcd.setCursor(0,1); 
lcd.print("Set Belirleme");
} 
/////Gaz sensor Menusu Kontrol ediliyor............
if((sayac==2)&&(ayarsayac==1)) 
{
ayarsayackontrol=false;
gazsetbelirle=true;
gerilimsetbelirle=false; 
akimsetbelirle=false;
sicakliksetbelirle=false;
nemsetbelirle=false;
lcd.clear();  
}
if(gazsetbelirle==true) 
{
if(digitalRead(yukari)==1) 
{
gazsensorset++;
}
if(digitalRead(asagi)==1) 
{
gazsensorset--;
}
lcd.setCursor(0,0);
lcd.print("Gaz Set:"); 
lcd.setCursor(10,1);
lcd.print(gazsensorset);
if((sayac==3)&&(gazsetbelirle==true))
{
////////Sınır Belirleme ..............
ayarsayackontrol=false;
ayarlar=false; 
gazsetbelirle=false;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Gaz Sensor Set"); 
lcd.setCursor(0,1);
lcd.print("Belirlendi....");
digitalWrite(buzzer,HIGH);
delay(2000); 
digitalWrite(buzzer,LOW);
sayac=1;
}
}
if(ayarsayac==2)
{
lcd.clear();
lcd.setCursor(0,0); 
lcd.print("Gerilim");
lcd.setCursor(0,1); 
lcd.print("Set Belirleme"); 
}
/////////////////////////////////////Gerilim Set Belirleme       -------------------------
if((sayac==2)&&(ayarsayac==2)) 
{
ayarsayackontrol=false;
gazsetbelirle=false;
gerilimsetbelirle=true; 
akimsetbelirle=false;
sicakliksetbelirle=false;
nemsetbelirle=false;
lcd.clear();  
}
if(gerilimsetbelirle==true) 
{
if(digitalRead(yukari)==1) 
{
gerilimset++;
}
if(digitalRead(asagi)==1) 
{
gerilimset--;
}
lcd.setCursor(0,0);
lcd.print("Gerilim Set:"); 
lcd.setCursor(10,1);
lcd.print(gerilimset);
if((sayac==3)&&(gerilimsetbelirle==true))
{
////////Sınır Belirleme ..............
ayarsayackontrol=false;
ayarlar=false; 
gazsetbelirle=false;
gerilimsetbelirle=false;
sicakliksetbelirle=false;
nemsetbelirle=false;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Gerilim Set"); 
lcd.setCursor(0,1);
lcd.print("Belirlendi....");
digitalWrite(buzzer,HIGH);
delay(2000); 
digitalWrite(buzzer,LOW);
sayac=1;
}
}
//////////////////////////////////// Gerilim Set Bitiş           -------------------------
if(ayarsayac==3)
{
lcd.clear();
lcd.setCursor(0,0); 
lcd.print("Akim");
lcd.setCursor(0,1); 
lcd.print("Set Belirleme"); 
}
//////////////////////////////////// Akım Set Başlangıç          -------------------------
if((sayac==2)&&(ayarsayac==3)) 
{
ayarsayackontrol=false;
gazsetbelirle=false;
gerilimsetbelirle=false;
akimsetbelirle=true;
nemsetbelirle=false;
sicakliksetbelirle=false; 
lcd.clear();  
}
if(akimsetbelirle==true) 
{
if(digitalRead(yukari)==1) 
{
akimset++;
}
if(digitalRead(asagi)==1) 
{
akimset--;
}
lcd.setCursor(0,0);
lcd.print("Akim Set:"); 
lcd.setCursor(10,1);
lcd.print(akimset);
if((sayac==3)&&(akimsetbelirle==true))
{
////////Sınır Belirleme ..............
ayarsayackontrol=false;
ayarlar=false; 
gazsetbelirle=false;
gerilimsetbelirle=false;
akimsetbelirle=false;
sicakliksetbelirle=false;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Akim Set"); 
lcd.setCursor(0,1);
lcd.print("Belirlendi....");
digitalWrite(buzzer,HIGH);
delay(2000); 
digitalWrite(buzzer,LOW);
sayac=1;
}
}



//////////////////////////////////// Akım Set Bitiş           -------------------------
if(ayarsayac==4)
{
lcd.clear();
lcd.setCursor(0,0); 
lcd.print("Sicaklik");
lcd.setCursor(0,1); 
lcd.print("Set Belirleme"); 
}
///////////////////////////////////Sicaklik Set Başlangıç     -------------------------
if((sayac==2)&&(ayarsayac==4)) 
{
ayarsayackontrol=false;
gazsetbelirle=false;
gerilimsetbelirle=false; 
akimsetbelirle=false;
sicakliksetbelirle=true;
nemsetbelirle=false;
lcd.clear();  
}
if(sicakliksetbelirle==true) 
{
if(digitalRead(yukari)==1) 
{
sicaklikset++;
}
if(digitalRead(asagi)==1) 
{
sicaklikset--;
}
lcd.setCursor(0,0);
lcd.print("Sicaklik Set:"); 
lcd.setCursor(10,1);
lcd.print(sicaklikset);
if((sayac==3)&&(sicakliksetbelirle==true))
{
////////Sınır Belirleme ..............
ayarsayackontrol=false;
ayarlar=false; 
gazsetbelirle=false;
gerilimsetbelirle=false;
akimsetbelirle=false;
nemsetbelirle=false;
sicakliksetbelirle=false;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Sicaklik Set"); 
lcd.setCursor(0,1);
lcd.print("Belirlendi....");
digitalWrite(buzzer,HIGH);
delay(2000); 
digitalWrite(buzzer,LOW);
sayac=1;
}
}
///////////////////////////////////Sicaklik Set Bitiş     -------------------------
if(ayarsayac==5)
{
lcd.clear();
lcd.setCursor(0,0); 
lcd.print("Nem");
lcd.setCursor(0,1); 
lcd.print("Set Belirleme"); 
}
///////////////////////////////////Nem Set Başlangıç     -------------------------
if((sayac==2)&&(ayarsayac==5)) 
{
ayarsayackontrol=false;
gazsetbelirle=false;
gerilimsetbelirle=false;
akimsetbelirle=false; 
sicakliksetbelirle=false;
nemsetbelirle=true;
lcd.clear();  
}
if(nemsetbelirle==true) 
{
if(digitalRead(yukari)==1) 
{
nemset++;
}
if(digitalRead(asagi)==1) 
{
nemset--;
}
lcd.setCursor(0,0);
lcd.print("Nem Set:"); 
lcd.setCursor(10,1);
lcd.print(nemset);
if((sayac==3)&&(nemsetbelirle==true))
{
////////Sınır Belirleme ..............
ayarsayackontrol=false;
ayarlar=false; 
gazsetbelirle=false;
gerilimsetbelirle=false;
akimsetbelirle=false;
nemsetbelirle=false;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Nem Set"); 
lcd.setCursor(0,1);
lcd.print("Belirlendi....");
digitalWrite(buzzer,HIGH);
delay(2000); 
digitalWrite(buzzer,LOW);
sayac=1;
}
}
///////////////////////////////////Nem Set Bitiş         -------------------------

}
void anaekran_menu() 
{
if(sayac==1)
{
dht11_olcum();  
//Serial.println("Fonksiyon Çalıştı");
}
if(sayac==2) 
{
gaz_olcum();
}
if(sayac==3) 
{
elektrikselolcum();
}
sayac++;
if(sayac==4)
{
sayac=1;
//lcd.clear();
}  
}
void butonkontrol() 
{
///////Ayar Menüsü İçerisindeki Kullanım Kontrol Mekanizması
////////////////////////////////////////////////////////////
if((digitalRead(ok)==1)&&(ayarlar==true))
{
sayac++;
delay(300);
if(sayac==5)
{
sayac=1;  
}
}
if(ayarsayackontrol==true)
{
if(digitalRead(yukari)==1)
{
digitalWrite(buzzer,HIGH);
ayarsayac++; 
delay(300);  
digitalWrite(buzzer,LOW);
}
if(digitalRead(asagi)==1)
{
digitalWrite(buzzer,HIGH);
ayarsayac--; 
delay(300);
digitalWrite(buzzer,LOW);
}
/////////////////////////////////////////////////////////////Ayar Menüsü içerisindeki döngü koşulu
if(ayarsayac>5) 
{
ayarsayac=5;  
}
if(ayarsayac<1) 
{
ayarsayac=1; 
}
}  
}

These two lines of code need to be at the top of your sketch…

In fact, I’d move all of the code that you’ve dumped at the top of your sketch and place it after #include "BlynkEdgent.h" and see what happens.

Pete.

… and don’t poll the DHT sensor every second! :wink: