BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Combined code not working

#1

hi blynkers,
I need to detect the room temperature and turn on the fan and need detect the smoke and give a notification included I need to detect the motion and turn on the lights I am also using the eventer to turn on the buzzer all this happen with the blynk app but the code runs well separately but they are not working altogather at the same time temperature works fine only once but it does not show the reading when the temperature is rised
for gas sensor the notification is not coming I am using Arduino uno dht11 t temperature sensor and mq 6 gas sensor the code is here

//code for serial usb

#define BLYNK_PRINT SwSerial
#include <SoftwareSerial.h>
SoftwareSerial SwSerial(10, 11); // RX, TX
#include <BlynkSimpleStream.h>
char auth[] = "dfe8ce156c5a4a1990fb55bb91326ded";

//temperature system initiallization

#define Gate 9
#include <DHT.h>
#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

//gas system initialization

int D1 = 7;
int gas_value;
int gas_avalue;

//medical and sprinkler system

BlynkTimer timer;
boolean flag = true;

//lighting system

int led = 13;                
int sensor = 3;             
int state = LOW;            
int val = 0;




//initialization part completed

//temperature operation function

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

  if (isnan(h) || isnan(t)) {
    SwSerial.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 fancontrol()
{ float t = dht.readTemperature();
  if(t <26 )
    { 
      analogWrite(Gate,0);
     
          }
    
    else if(t==26)
    {
      analogWrite(Gate, 51);
    
    }
    
     else if(t==27)
    {
      analogWrite(Gate, 100);
      
    }
    
     else if(t==28)
    {
      analogWrite(Gate, 800);
       
    }
    
    else if(t==29)
    {
      analogWrite(Gate, 900);
      
    }
     else if(t>29)
    {
      analogWrite(Gate, 1000);
   
    } 
    
 }

 //gas operating function

 void getsensordata()
{
  gas_avalue = digitalRead(7);
  if (gas_avalue < 1)
  {
   
    Blynk.notify("Danger LPG leak detected");
    
  }
}

//medical and sprinkler system operating function

void sendFlagToServer() {
  if (flag) {
    Blynk.virtualWrite(V0, 1);
  } else {
    Blynk.virtualWrite(V0, 0);
  }
  flag = !flag;
}

BLYNK_WRITE(V1) {
  //here you'll get 0 or 255
  int ledValue = param.asInt();
}


// pir system working funtion

void lighting()
{
 val = digitalRead(sensor);   // read sensor value
  if (val == HIGH) {           // check if the sensor is HIGH
    digitalWrite(led, HIGH);   // turn LED ON
                // delay 100 milliseconds 
    
    
      Serial.println("Motion detected!"); 
      delay(1000);
     
  } 
  else {
      digitalWrite(led, LOW); // turn LED OFF
       
      
        Serial.println("Motion stopped!");
  }}

  // working function completed

  void setup()
{
  
  // setup
  
  pinMode(Gate,OUTPUT);
  digitalWrite(Gate,LOW);
  analogWrite(Gate, 0);
  pinMode(7, INPUT);
  pinMode(led, OUTPUT);      
  pinMode(sensor, INPUT);    

   
  //common

  
 Serial.begin(9600);
 SwSerial.begin(9600);
Serial.begin(9600);
Blynk.begin(Serial, auth);

  dht.begin();

  // Setup a function to be called every second
  timer.setInterval(1000L, sendSensor);//temperature function wiil be called
  timer.setInterval(1000L, fancontrol);// with temp input fan willl be controlled
  timer.setInterval(1000L, getsensordata );//with the the gas sensor input notification will be sent
  timer.setInterval(1000L, sendFlagToServer);//with timer in blynk we can set the time for the sprinkler and medicine buzzer
  timer.setInterval(1000L,lighting);//pir works 
}
       
  void loop()
{
   Blynk.run();
  timer.run();
  
}```
0 Likes

#2

You have all your timed functions trying to run at the exact same time every second…

0 Likes

#3

Thanks for the reply gunner,
I have made changes to the timer yet the same result. I have to upload the code every single time to see the change in room temperature and still not getting the notification from gas sensor part.


#define BLYNK_PRINT SwSerial
#include <SoftwareSerial.h>
SoftwareSerial SwSerial(10, 11); // RX, TX
#include <BlynkSimpleStream.h>
char auth[] = "dfe8ce156c5a4a1990fb55bb91326ded";

//temperature system initiallization

#define Gate 9
#include <DHT.h>
#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

//gas system initialization

int D1 = 7;
int gas_value;
int gas_avalue;

//medical and sprinkler system

BlynkTimer timer;
boolean flag = true;

//lighting system

int led = 13;                
int sensor = 3;             
int state = LOW;            
int val = 0;




//initialization part completed

//temperature operation function

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

  if (isnan(h) || isnan(t)) {
    SwSerial.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 fancontrol()
{ float t = dht.readTemperature();
  if(t <26 )
    { 
      analogWrite(Gate,0);
     
          }
    
    else if(t==26)
    {
      analogWrite(Gate, 51);
    
    }
    
     else if(t==27)
    {
      analogWrite(Gate, 100);
      
    }
    
     else if(t==28)
    {
      analogWrite(Gate, 800);
       
    }
    
    else if(t==29)
    {
      analogWrite(Gate, 900);
      
    }
     else if(t>29)
    {
      analogWrite(Gate, 1000);
   
    } 
    
 }

 //gas operating function

 void getsensordata()
{
  gas_avalue = digitalRead(7);
  //Blynk.notify("danger");
  
  if (gas_avalue < 1)
  {
   
    Blynk.notify("Danger LPG leak detected");
    
  }
}

//medical and sprinkler system operating function

void sendFlagToServer() {
  if (flag) {
    Blynk.virtualWrite(V0, 1);
  } else {
    Blynk.virtualWrite(V0, 0);
  }
  flag = !flag;
}

BLYNK_WRITE(V1) {
  //here you'll get 0 or 255
  int ledValue = param.asInt();
}


// pir system working funtion

void lighting()
{
 val = digitalRead(sensor);   // read sensor value
  if (val == HIGH) {           // check if the sensor is HIGH
    digitalWrite(led, HIGH);   // turn LED ON
                // delay 100 milliseconds 
    
    
      Serial.println("Motion detected!"); 
      delay(1000);
     
  } 
  else {
      digitalWrite(led, LOW); // turn LED OFF
       
      
        Serial.println("Motion stopped!");
  }}

  // working function completed

  void setup()
{
  
  // setup
  
  pinMode(Gate,OUTPUT);
  digitalWrite(Gate,LOW);
  analogWrite(Gate, 0);
  pinMode(7, INPUT);
  pinMode(led, OUTPUT);      
  pinMode(sensor, INPUT);    

   
  //common

  
 Serial.begin(9600);
 SwSerial.begin(9600);
Serial.begin(9600);
Blynk.begin(Serial, auth);

  dht.begin();

  // Setup a function to be called every second
  timer.setInterval(1000L, sendSensor);//temperature function wiil be called
  timer.setInterval(1000L, fancontrol);// with temp input fan willl be controlled
  timer.setInterval(2000L, getsensordata );//with the the gas sensor input notification will be sent
  timer.setInterval(2500L, sendFlagToServer);//with timer in blynk we can set the time for the sprinkler and medicine buzzer
  timer.setInterval(1000L,lighting);//pir works 
}
       
  void loop()
{
   Blynk.run();
  timer.run();
  
}```
0 Likes

#4

You can’t use your hardware serial for both your Blynk connection and debug messages.
In other areas you’re using software serial for your debugging.

Pete.

0 Likes

#5

thanks for the reply pete,
I removed that one still it doesn’t help
:slightly_frowning_face:

0 Likes

#6

but the motion sensor works properly without any disturbance only the temperature reading is not getting refreshed and when the gas is detected the notification is not coming. but I am able to get the notification for the gas sensor when I tried the program separately.

0 Likes

#7

Both of these functions take a temperature reading, and both are being called at exactly the same time. The DHT sensors are quite slow, and don’t like to be polled that often, so this may be the issue.

Are you getting any “Failed to read from DHT sensor!” messages in your serial monitor?

You should try adding more SwSerial.println commands to track the flow of your program and the variable values at each stage of the process.

Pete.

0 Likes

#8

@Ashwin_Vvz I guess you didn’t read or understand my linked post… changing a couple of timers from 1000 to 2000 or 2500 still causes them to interfere with each other every 2nd and 5th iteration

1s - timer 1
2s - timer 1 & 2
2.5s - timer 3
3s - timer 1
4s - timer 1 & 2
5s - timer 1 & 2 & 3
6s - timer 1

0 Likes

#9

thanks gunner

0 Likes