Create Push Notification over Flame Sensor

Hi, all
I want to make a Flame Sensor project with Push Notification when only the fire appear.
Here is my code:

    #define BLYNK_PRINT SwSerial


    #include <SoftwareSerial.h>
    SoftwareSerial SwSerial(10, 11); // RX, TX
    
    #include <BlynkSimpleStream.h>

    // You should get Auth Token in the Blynk App.
    // Go to the Project Settings (nut icon).
    char auth[] = "*****************************************";

    int buzzer = 13 ;// define buzzer Interface
    int pin = 2; // define the flame sensor interface
    int analoog = A0; // define the flame sensor interface
 
    int val ;// define numeric variables val
    float sensor; //read analoog value

    BlynkTimer timer;

    void notifyUptime()
    {
      sensor = analogRead(analoog);
      Serial.println(sensor);  // display temperature
      long uptime = millis() / 60000L;

      val = digitalRead (pin) ;// digital interface will be assigned a value of 3 to read val
      if (val == HIGH) // When the flame sensor detects a signal, buzzer beep
      {
         digitalWrite (buzzer, HIGH);
      }
         else
     {
         digitalWrite (buzzer, LOW);
     }
        delay(1000);
     }

    void setup()
    {
      pinMode (buzzer, OUTPUT) ;// define buzzer as output interface
      pinMode (pin, INPUT) ;// output interface defines the flame sensor
      pinMode (analoog, INPUT) ;// output interface defines the flame sensor
  
     // Debug console
     SwSerial.begin(9600);

    // Blynk will work through Serial
    // Do not read or write this serial manually in your sketch
    Serial.begin(9600);
    Blynk.begin(Serial, auth);

    // Notify immediately on startup
    if(val==HIGH){
    Blynk.notify("Flame!!!");
    }

    // Setup a function to be called every minute
    timer.setInterval(60000L, notifyUptime);
    }

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

When I upload the code, there is no notif appear in my app, could you help me to fix it?
Thanks

hello!

some points to consider:

1 - where are you using sw serial in your sketch? i think nowhere. you should give some detail what board / mcu / communication module / flame sensor are you using.

2 - you shouldn’t use so long delays in blynk sketch like 1000 millis. remove that. (ideally you shouldn’t use delays at all)

3 - you are checking the val variable in setup, but you do not assign value to it only in notifyUptime, which is called after // Notify immediately on startup part. this way the value of val will always be 0 on first read.

4 - if(val==HIGH) Blynk.notify("Flame!!!"); is called only once, in setup, but never called in main loop. you should put this in the notifyUptime function, or a dedicated function, and call say every 15 seconds… or, probably better, you can hook up the digital output of the sensor to an interrupt pin, and get alarms in real time.

I use arduino uno over usb serial.
How to make interrupt pin?

study this:
https://www.arduino.cc/en/Reference/AttachInterrupt

note, however, that interrupts are not beginner stuff. you have to study and understand very carefully what is written in that article. look for the details!

1 Like

Hi,
I have modified the code:

    #define BLYNK_PRINT SwSerial

    #include <SoftwareSerial.h>
    SoftwareSerial SwSerial(10, 11); // RX, TX
    
    #include <BlynkSimpleStream.h>

    // You should get Auth Token in the Blynk App.
    // Go to the Project Settings (nut icon).
    char auth[] = "*************************";

    int buzzer = 13 ;// define buzzer Interface
    int pin = 2; // define the flame sensor interface
    int analoog = A0; // define the flame sensor interface
 
    int val ;// define numeric variables val
    float sensor; //read analoog value

    const byte interruptPin = 2;
    volatile byte state = LOW;

    //BlynkTimer timer;

    void blink()
    {
      sensor = analogRead(analoog);
      Serial.println(sensor);  // display temperature
      //long uptime = millis() / 

      state = !state;
      val = digitalRead (pin) ;// digital interface will be assigned a value of 3 to read val
      if (val == HIGH) // When the flame sensor detects a signal, buzzer beep
      {
        digitalWrite (buzzer, HIGH);
        Blynk.notify("Flame is detected");
      }
        else
     {
        digitalWrite (buzzer, LOW);
     }
        delay(1000);
     }

    void setup()
    {
       pinMode (buzzer, OUTPUT) ;// define buzzer as output interface
       pinMode (pin, INPUT) ;// output interface defines the flame sensor
       pinMode (interruptPin, INPUT_PULLUP) ;
       attachInterrupt(digitalPinToInterrupt(interruptPin), blink, CHANGE);
       pinMode (analoog, INPUT) ;// output interface defines the flame sensor
   
       // Debug console
       SwSerial.begin(9600);

       // Blynk will work through Serial
       // Do not read or write this serial manually in your sketch
       Serial.begin(9600);
       Blynk.begin(Serial, auth);
    }

    void loop()
    {
       Blynk.run();
       digitalWrite(buzzer,state);
       //timer.run();
    }

But, still nothing notif when upload it. Is there something wrong in my code?

hi!

i’m on phone right now, but later today i will try to find out what could be the problem with the code.

@Maria, could you specify what kind of flame sensor are you using? a link from the webshop you’ve bought would be the best. it has analog or digital output? because in the sketch you hooked up to a digital pin, i assume it has a digital output.
if digital, please confirm:

  • it was correctly set the threshold value with the trimpot?
  • on flame present, it has low or high output? (this should be in specification, or you can simply test with a multimeter)

with your current code, at least the buzzer sounds correctly on flame event? if not:

  1. first you should write a minimalist, non blynk sketch, and test if it works correctly:
#define FLAME   2
#define BUZZER 13

void setup()
{
  // we should know the sensor output level on flame detection is low or high?
  // if low: use INPUT_PULLUP
  // if high: use INPUT + a phisical pulldown resistor, say 10k
  // in the following sketch i will assume that the sensor has a low signal on flame detection and threshold is set correctly with trimpot

  pinMode(FLAME, INPUT_PULLUP);
  pinMode(BUZZER, OUTPUT);

  digitalWrite(BUZZER, LOW);
}

void loop()
{
  if (!digitalRead(FLAME)) digitalWrite(BUZZER, HIGH);  // if sensor output is high, remove "!" from the if statement
  else digitalWrite(BUZZER, LOW);
}

please confirm if this works correctly before going further!

  1. in online mode, first of all, you have to be sure that you have a solid connection to blynk server, both on phone and mcu. (you can test this with sending just the mcu uptime in every second and see on a display widget in blynk app if increments regularly)

  2. after all the above points are working reliable, you can implement the code for the flame sensor.

I use KY-026, and here is my reference code:
https://tkkrlab.nl/wiki/Arduino_KY-026_Flame_sensor_module
It has 4 pin: VCC, A0, D0, & GND. I have test the sensor, it has run correctly. If there are no flame, then serial monitor will show value about about 1000 and the alarm is off. Whereas, if there are flame, the serial monitor will show value under 500 depending on the distance the flame from sensor. If the distance is very close to sensor, then the value under 50 and the alarm sound so loud.

Sensor is analog with a digital interface for the LED.

@Maria looks like your very first sketch was the most suitable. Look at the mods marked as “Costas” in this one and see if it works. You might need to increase the 200L for timer.setInterval as you are using one of those slow Arduino devices.

 #define BLYNK_PRINT SwSerial


    #include <SoftwareSerial.h>
    SoftwareSerial SwSerial(10, 11); // RX, TX
    
    #include <BlynkSimpleStream.h>

    // You should get Auth Token in the Blynk App.
    // Go to the Project Settings (nut icon).
    char auth[] = "*****************************************";

    int buzzer = 13 ;// define buzzer Interface
    int pin = 2; // define the flame sensor interface
    int analoog = A0; // define the flame sensor interface
 
    int val ;// define numeric variables val
    float sensor; //read analoog value

    BlynkTimer timer;

    void readSensor()   // Costas
    {
      sensor = analogRead(analoog);
      Serial.println(sensor);  // display temperature
      // long uptime = millis() / 60000L;  Costas

      val = digitalRead (pin) ;// digital interface will be assigned a value of 3 to read val
      if (val == HIGH) // When the flame sensor detects a signal, buzzer beep
      {
         digitalWrite (buzzer, HIGH);
      }
         else
     {
         digitalWrite (buzzer, LOW);
     }
        //delay(1000);  // Costas
     }

    void setup()
    {
      pinMode (buzzer, OUTPUT) ;// define buzzer as output interface
      pinMode (pin, INPUT) ;// output interface defines the flame sensor
      pinMode (analoog, INPUT) ;// output interface defines the flame sensor
  
     // Debug console
     SwSerial.begin(9600);

    // Blynk will work through Serial
    // Do not read or write this serial manually in your sketch
    Serial.begin(9600);
    Blynk.begin(Serial, auth);

    // Notify immediately on startup
    if(val==HIGH){
      Blynk.notify("Flame!!!");
    }

    // Setup a function to be called every 200ms Costas
    timer.setInterval(200L, readSensor);  // Costas
    }

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

Ok, I will try. But, if I use ESP8266-01, how is the circuit diagram for push notif? Because I just use USB serial for the connection, but it’s not stable connection.

I would recommend against using the ESP-01 as it is very limited.
For almost the same price you can now buy a decent ESP i.e. one that is plug and play with a USB port.

Just to be clear the decent ESP’s only use USB for power / flashing and not the poor USB internet connection method.

See how you go with the latest sketch, consider which ESP you should be using and then someone will advise the required connections if you are still unsure.

Then, how about Wemos D1 r2? Is it decent?

For me the WeMos authentic ESP’s are #1.
I wouldn’t consider the clones unless you want a few hundred of them and have a good returns policy with the vendor.
If you decide to use WeMos you then just need to work out which one:

  1. WeMos D1 - full size but AFAIK no benefit over the Mini
  2. WeMos D1 Mini - I believe they are no longer produced or sold by WeMos
  3. WeMos D1 Mini Pro - like the D1 mini but 16MB flash (rather than 4MB) and provisioned to accept an external antenna.

16MB use is still not available in the Arduino core so it works as a 4MB device. Work has been done for 16MB flash and with some manual tweaks it can be used but I have only ever used 4MB. Hopefully 16MB flash will appear in the core some time soon.

Then, I’ll use WeMos D1 because I just have WeMos D1 and ESP8266-01. But, the operating voltage max.3,3 V. Must I use breadboard power supply to increase the voltage?

NO!!!

You will be looking to reduce any 5V feeds down to the WeMos’ 3.3V.
Power the sensor with 3.3V as it might work at 3.3V. If it doesn’t use a voltage divider between sensor and the pins on the WeMos.

I have change the connection to WeMos, here is the sketch:

     #define BLYNK_PRINT Serial

    #include <ESP8266WiFi.h>
    #include <BlynkSimpleEsp8266.h>
    
    // You should get Auth Token in the Blynk App.
    // Go to the Project Settings (nut icon).
    char auth[] = "*****************************************";
    // Your WiFi credentials.
    // Set password to "" for open networks.
    char ssid[] = "Redmi 3s";
    char pass[] = "69AEE473";

    int buzzer = 8 ;// define buzzer Interface
    int pin = 4; // define the flame sensor interface
    int analoog = A0; // define the flame sensor interface
 
    int val ;// define numeric variables val
    float sensor; //read analoog value

    BlynkTimer timer;

    void readSensor()   // Costas
    {
      sensor = analogRead(analoog);
      Serial.println(sensor);  // display temperature
      // long uptime = millis() / 6000L;  Costas

     val = digitalRead (pin) ;// digital interface will be assigned a value of 3 to read val
     if (val == HIGH) // When the flame sensor detects a signal, buzzer beep
     {
        digitalWrite (buzzer, HIGH);
     }
        else
    {
        digitalWrite (buzzer, LOW);
     }
        //delay(1000);  // Costas
     }

    void setup()
    {
       pinMode (buzzer, OUTPUT) ;// define buzzer as output interface
       pinMode (pin, INPUT) ;// output interface defines the flame sensor
       pinMode (analoog, INPUT) ;// output interface defines the flame sensor
  
       // Debug console
      Serial.begin(9600);

      Blynk.begin(auth, ssid, pass);
      // You can also specify server:
      //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8442);
      //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8442);

      // Notify immediately on startup
      if(val==HIGH){
      Blynk.notify("Flame!!!");
       }

      // Setup a function to be called every 200ms Costas
      timer.setInterval(200L, readSensor);  // Costas
      }

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

But, when I upload, there are error:
warning: espcomm_sync failed
error: espcomm_open failed
error: espcomm_upload_mem failed

Could you help me to fix it?

What does Serial Monitor show now?

I set the Board: “Wemos D1 & R2 mini”, CPU Frequency: “80 MHz”, Flash Size: “4M (3M SPIFFS)”, and Upload Speed: “115200”. Is it correct?
If not, the “CPU Frequency” is for which CPU? Is it my computer CPU? And what the mean of Flash Size: "4M (3M SPIFFS)?

Yes

It’s the CPU of the WeMos not your PC.