MQ-2 and Flame Sensor combined to make a Fire alarm System

Hi All,
I am trying to create a smart home Smoke/Fire Detector using Blynk.
I have used a ESP8266, a MQ-2 and Flame Sensor. I sort of added all the code in one place but I think I am missing something very important. Can you please help?

I am mainly having difficulty in compiling the MQ-2 Sensor Code, as it says 'digital' was not declared in this scope. Also I want to add a Buzzer option, and can’t find it’s code.

Here is the Code:

//Blynk Fire Alarm Notification with Flame Sensor
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
BlynkTimer timer;
char auth[] = "XXXX"; //Auth code sent via Email
char ssid[] = "XXXX"; //Wifi name
char pass[] = "XXXX";  //Wifi Password
int flag=0;

//Flame Sensor Code
void notifyOnFire() 
{
  int isButtonPressed = digitalRead(D1);
  if (isButtonPressed==1 && flag==0) {
    Serial.println("Fire in the House");
    Blynk.notify("Alert : Fire in the House");
    flag=1;
  }
  else if (isButtonPressed==0)
  {
    flag=0;
  }
}
void setup()
{
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
pinMode(D1,INPUT_PULLUP);
timer.setInterval(1000L,notifyOnFire); 
}

//Gas Sensor MQ-2 Action.
int gas_value;     
int gas_avalue;
void setup()
{
  pinMode(16, INPUT);
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
}
void loop() 
{
  gas_avalue = digitalRead(D2);
  if (gas_avalue < 1)
  {
    Serial.println("DANGER!!!!");
    digital.Write(D3,HIGH);
  }
  else
  {
    Serial.println("NO LEAKAGE");

    digital.Write(D3,LOW);
  }
  delay(100);

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

This should be digitalWrite(D3,HIGH);

We need to know more about your buzzer hardware, and how you’re connecting it.

Pete,

Thanks Pete, I’m gonna try that and let you know if this works.
About the Buzzer, i don’t plan to use anything fancy, just any plain Buzzer. Including a relay(connected to a big buzzer) might also work.

Sorry Pete, it didn’t work or I don’t know what I am missing here. I changed the to ‘‘digitalWrite(D3,HIGH);’’, but still getting errors, really can’t figure out pls help.

Here is the Error now.

C:\Program Files (x86)\Arduino\arduino-builder -dump-prefs -logger=machine -hardware C:\Program Files (x86)\Arduino\hardware -hardware C:\Users\Syedul Bashar\AppData\Local\Arduino15\packages -tools C:\Program Files (x86)\Arduino\tools-builder -tools C:\Program Files (x86)\Arduino\hardware\tools\avr -tools C:\Users\Syedul Bashar\AppData\Local\Arduino15\packages -built-in-libraries C:\Program Files (x86)\Arduino\libraries -libraries M:\Arduino\libraries -fqbn=esp8266:esp8266:nodemcuv2:CpuFrequency=80,VTable=flash,FlashSize=4M1M,LwIPVariant=v2mss536,Debug=Disabled,DebugLevel=None____,FlashErase=none,UploadSpeed=9600 -ide-version=10805 -build-path C:\Users\SYEDUL~1\AppData\Local\Temp\arduino_build_405914 -warnings=none -build-cache C:\Users\SYEDUL~1\AppData\Local\Temp\arduino_cache_659596 -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.esptool.path=C:\Users\Syedul Bashar\AppData\Local\Arduino15\packages\esp8266\tools\esptool\0.4.13 -prefs=runtime.tools.xtensa-lx106-elf-gcc.path=C:\Users\Syedul Bashar\AppData\Local\Arduino15\packages\esp8266\tools\xtensa-lx106-elf-gcc\1.20.0-26-gb404fb9-2 -prefs=runtime.tools.mkspiffs.path=C:\Users\Syedul Bashar\AppData\Local\Arduino15\packages\esp8266\tools\mkspiffs\0.2.0 -verbose M:\Arduino\FIRE ALARM\Test\Test.ino

M:\Arduino\FIRE ALARM\Test\Test.ino: In function 'void setup()':

Test:36: error: redefinition of 'void setup()'

 void setup()

      ^

Test:25: error: 'void setup()' previously defined here

 void setup()

      ^

M:\Arduino\FIRE ALARM\Test\Test.ino: In function 'void loop()':

Test:49: error: 'digital' was not declared in this scope

     digital.Write(D3,HIGH);

     ^

Test:55: error: 'digital' was not declared in this scope

     digital.Write(D3,LOW);

     ^

Test:60: error: a function-definition is not allowed here before '{' token

 {

 ^

Test:63: error: expected '}' at end of input

 }

 ^

Using library ESP8266WiFi at version 1.0 in folder: C:\Users\Syedul Bashar\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.2\libraries\ESP8266WiFi 
Using library Blynk at version 0.6.1 in folder: M:\Arduino\libraries\Blynk 
exit status 1
redefinition of 'void setup()'

As the error says, you have two void setups in your code, you’re only allowed one.

Pete.

Yes but if I remove one then only one sensor will work the other will not. Pete, I think I lack some basic instructions.

This is a Blynk forum, not basic C++ teaching forum.
You need to combine the code to into one void setup.

In addition, you can only have one void loop, so you need to do the same with that. But, doing digital reads thousands of times per second in the void loop isn’t conducive to good Blynking.
Read this for information about doing this with a timer:

Pete.

besides that, Blynk should not be used for mission critical situations like fire detection.

Any specific reason for this statement?

Thanks Pete. Sorry, if I put too much on you Brother, I’m a newbie.

A better way of looking at this is…

Don’t rely on Blynk or ANY IoT system for your mission critical control.

If your device setup controls a local detection, alarm and extinguishing system, then it needs to fully and timely function, regardless if there is an Internet connection and/or Blynk Server connection. This can easily be done in your code… here is one example of a way to keep your system running without a Blynk server link.

Other than that, Blynk is a perfectly good system to use for passing on the monitoring status and alerts to a remote local.

1 Like

Thanks Gunner…Mesmerizing stuff…as for me I have a lot of learning to do :frowning:

Guys I have done some research and learning, afterwards I have came up with the following code given below.
But it’s not correct and has a error. The Error is for using Void twice and I can’t think of a way to get around it. I have searched and looked up everywhere including Blynk and Arduino forums…
Pls help and suggest me…

//Blynk Fire Alarm Notification
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
BlynkTimer timer;
//Auth Code, Wi-Fi and others.
char auth[] = "XXXX"; //Auth code sent via Email
char ssid[] = "XXXX"; //Wifi name
char pass[] = "XXXX";  //Wifi Password
int flag=0;
int gas_value;
int gas_avalue;

//Flame Sensor Code.
void notifyOnFire();
{
  int isButtonPressed = digitalRead(D1);
 if (isButtonPressed==1 && flag==0) {
    Serial.println("Fire in the House");
    Blynk.notify("Alert : Fire in the House");
    flag=1;
  }
  else if (isButtonPressed==0)
  {
    flag=0;
  }

   void notifyOnSmoke() //SMOKE Sensor Code
  {
    int isButtonPressed = digitalRead(D2);
  if (isButtonPressed==1 && flag==0)
  {
    Serial.println("Alert!!! SMOKE is Detected.");
    Blynk.notify("Alert!!! SMOKE is Detected.");
    flag=1;
  }
  else if (isButtonPressed==0)
  {
    flag=0;
  } 
  }
}

void setup()
{
Serial.begin(115200);
Blynk.begin(auth, ssid, pass);
pinMode(D1,INPUT_PULLUP);
pinMode (D2,INPUT_PULLUP);
timer.setInterval(1000L,notifyOnFire);
timer.setInrerval(1000L,notifyOnSmoke); 
}
void loop()
{
  Blynk.run();
  timer.run();
}

You have a number of issues including:

An unwanted semicolon here:

The wrong number of closing curly brackets in your notifyOnFire() and notifyOnSmoke() functions.

A typo in the word “Interval” in your second timer:

and these two timers are trying to execute at exactly the same time, so they need a small delay between each.

Here’s the amended code:

//Blynk Fire Alarm Notification
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
BlynkTimer timer;
//Auth Code, Wi-Fi and others.
char auth[] = "XXXX"; //Auth code sent via Email
char ssid[] = "XXXX"; //Wifi name
char pass[] = "XXXX";  //Wifi Password
int flag=0;
int gas_value;
int gas_avalue;


void notifyOnFire() //Flame Sensor Code
{
  int isButtonPressed = digitalRead(D1);
 if (isButtonPressed==1 && flag==0)
 {
    Serial.println("Fire in the House");
    Blynk.notify("Alert : Fire in the House");
    flag=1;
 }
  else if (isButtonPressed==0)
  {
    flag=0;
  }
}

void notifyOnSmoke() //SMOKE Sensor Code
{
  int isButtonPressed = digitalRead(D2);
  if (isButtonPressed==1 && flag==0)
  {
    Serial.println("Alert!!! SMOKE is Detected.");
    Blynk.notify("Alert!!! SMOKE is Detected.");
    flag=1;
  }
  else if (isButtonPressed==0)
  {
    flag=0;
  } 
}


void setup()
{
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
  pinMode(D1,INPUT_PULLUP);
  pinMode (D2,INPUT_PULLUP);
  timer.setInterval(1000L,notifyOnFire);
  delay(100);
  timer.setInterval(1000L,notifyOnSmoke); 
}

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

Pete.

@PeteKnight PETE,
Straight up HUG :smiley:
U R the SUPER HERO !!!
Simply thanking you is not enough, but yet, I thank you from the bottom of my heart Brother…
Next stop adding @Gunner

2 Likes

A post was merged into an existing topic: Problem with Notifications