NodeMCU with Ultra Sonic & Infrared flame detector send Gmail from Blynk

Hi Pete & Expert

I am having issue with my project, basically sending notification to my gmail when condition is match

NodeMCU to detect distance and send email
The same Node MCU to send email if fire is detected.

hardware that i used

NodeMCU
IR Flame sensor
Ultra sonic

If I use the code separately Ultra sonic (distance alert) & Fire sensor both are works well.

When I combined both code together I only get Blynk app alert & gmail for distance and not for fire alert.

Here is my code.

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

#define  trig  D2
#define  echo  D1

long duration;
int distance;

// You should get Auth Token in the Blynk App.

char auth[] = "mycode";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "mywifi";
char pass[] = "mypwd";
int flag=0;
void notifyOnFire()
{
  int isButtonPressed = digitalRead(D3);
  if (isButtonPressed==1 && flag==0) {
    Serial.println("Fire in the House");
    Blynk.email("mymail@gmail.com", "Fire Alert", "Please Wear Your Safety Helmet!");
    Blynk.notify("Alert : Fire in the House");
    flag=1;
  }
  else if (isButtonPressed==0)
  {
    flag=0;
  }
}
BlynkTimer timer;

void setup()
{
  // Debug console
  pinMode(trig, OUTPUT);  // Sets the trigPin as an Output
  pinMode(echo, INPUT);   // Sets the echoPin as an Inpu
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  pinMode(D3,INPUT_PULLUP);
  timer.setInterval(1000L,notifyOnFire); 
  // Setup a function to be called every second
  timer.setInterval(15000L, sendSensor);
}

void loop()
{
  Blynk.run();
  timer.run();
}
void sendSensor()
{
  digitalWrite(trig, LOW);   // Makes trigPin low
  delayMicroseconds(2);       // 2 micro second delay

  digitalWrite(trig, HIGH);  // tigPin high
  delayMicroseconds(10);      // trigPin high for 10 micro seconds
  digitalWrite(trig, LOW);   // trigPin low

  duration = pulseIn(echo, HIGH);   //Read echo pin, time in microseconds
  distance = duration * 0.034 / 2;   //Calculating actual/real distance

  Serial.print("Distance = ");        //Output distance on arduino serial monitor
  Serial.println(distance);
  
  if(distance >= 5)
  {
    Blynk.tweet("My Arduino project is tweeting using @blynk_app and it’s awesome!\n #arduino #IoT #blynk");
     Blynk.email("mymail@gmail.com", "Distance Alert", "Disntance reached!");
    Blynk.notify("Disntance reached! ");

  
  }
  Blynk.virtualWrite(V0, distance);
  delay(1000);                        //Pause for 1 seconds and start measuring distance again
}

Thank you.

Regards
KM

Are you using Blynk library version 0.6.1 ?

Pete.

1 Like

Do you see “fire in the house” in Serial monitor when D3 is active?

1 Like

I Bazzio,

Thanks for reminded me to check this out,
I dont see fire in the house after several time i torch up near my IR flame sensor

only once it showed during starting time.
at the sensor i can see a green light, (PIN is ok)

00:19:00.214 → [11134] Connecting to blynk-cloud.com:80
00:19:00.385 → [11416] Ready (ping: 14ms).
00:19:01.438 → Fire in the House
00:19:15.439 → Distance = 4
00:19:30.560 → Distance = 1902
00:19:45.782 → Distance = 184
00:20:01.035 → Distance = 188
00:20:16.288 → Distance = 2
00:20:31.375 → Distance = 184

Hi Pete,

Its been a long time, I hope you are doing well. Thanks for helping me here.

the Blynk library version is 1.0.1.

That’s your problem.
For Blynk legacy notifications etc to work you need to be using 0.6.1

Pete.

1 Like

Great, I will downgrade then

@PeteKnight Thank you Pete, as you said, it works now after I downgrade that to 0.6.1,
the same version 0.6.1 would work if i add another sensor in this case DHT22 correct?

@bazzio, Thanks for stepping in. below is what i got now from serial monitor.

Regards
KM

01:22:24.953 → Distance = 293
01:22:39.918 → Distance = 14
01:22:54.914 → Distance = 15
01:23:03.941 → Fire in the House
01:23:09.918 → Distance = 7
01:23:11.917 → Fire in the House
01:23:25.015 → Distance = 1482
01:23:40.017 → Distance = 1552
01:23:54.912 → Distance = 7
01:23:59.936 → Fire in the House
01:24:08.925 → Fire in the House
01:24:09.909 → Distance = 6
01:24:24.927 → Distance = 6
01:24:39.908 → Distance = 7
01:24:54.935 → Distance = 53

Of course, the Blynk library has nothing to do with how sensors like that work.
Just don’t try to read the sensor too frequently. About once every 5 seconds is about all the DHT22 can handle.

Pete.

Thanks Pete for your suggestion.

Hi Pete & Expert,

I am receiving notification from Blynk app when the condition is match for distance & fire.
but i dont receive a email from dispatcher@blynk.io.

i did receive emails until 13th August but not now.

Am I right to say 100 email per day?

Here is my sketch

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

#define  trig  D2
#define  echo  D1

long duration;
int distance;

// You should get Auth Token in the Blynk App.
                                                        
char auth[] = "my32bittoken";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "mywifi";
char pass[] = "wifipwd";
int flag=0;
void notifyOnFire()
{
  int isButtonPressed = digitalRead(D3);
  if (isButtonPressed==1 && flag==0) {
    Serial.println("Fire in the House");
    Blynk.email("myemail@gmail.com", "Fire Alert", "Fire detected!");
    Blynk.notify("Alert : Fire in the House now");
    flag=1;
  }
  else if (isButtonPressed==0)
  {
    flag=0;
  }
}
BlynkTimer timer;

void setup()
{
  // Debug console
  pinMode(trig, OUTPUT);  // Sets the trigPin as an Output
  pinMode(echo, INPUT);   // Sets the echoPin as an Inpu
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  pinMode(D3,INPUT_PULLUP);
  timer.setInterval(1000L,notifyOnFire); 
  // Setup a function to be called every second
  timer.setInterval(15000L, sendSensor);
}

void loop()
{
  Blynk.run();
  timer.run();
}
void sendSensor()
{
  digitalWrite(trig, LOW);   // Makes trigPin low
  delayMicroseconds(2);       // 2 micro second delay

  digitalWrite(trig, HIGH);  // tigPin high
  delayMicroseconds(10);      // trigPin high for 10 micro seconds
  digitalWrite(trig, LOW);   // trigPin low

  duration = pulseIn(echo, HIGH);   //Read echo pin, time in microseconds
  distance = duration * 0.034 / 2;   //Calculating actual/real distance

  Serial.print("Distance = ");        //Output distance on arduino serial monitor
  Serial.println(distance);
  
  if(distance >= 5)
  {
    Blynk.tweet("My Arduino project is tweeting using @blynk_app and it’s awesome!\n #arduino #IoT #blynk");
     Blynk.email("myemail@gmail.com", "Safety Alert", "Please Wear Your Safety Helmet!");
    Blynk.notify("Disntance reached! ");

  
  }
  Blynk.virtualWrite(V0, distance);
  delay(1000);                        //Pause for 1 seconds and start measuring distance again
}

Any clue?

Regards
Raja

Limitations:

  • Maximum allowed email + subject + message length is 120 symbols. However you can increase this limit if necessary by adding #define BLYNK_MAX_SENDBYTES XXX to you sketch. Where XXX is desired max length of your email. For example for ESP you can set this to 1200 max length #define BLYNK_MAX_SENDBYTES 1200. The #define BLYNK_MAX_SENDBYTES 1200 must be included before any of the Blynk includes.
  • Only 1 email per 5 seconds is allowed
  • In case you are using gmail on the Local Server you are limited with 500 mails per day (by google). Other providers may have similar limitations, so please be careful.
  • User is limited with 100 messages per day in the Blynk Cloud;

Pete.

1 Like

Thanks Pete,
I just checked my email message is less than 120 Characters.

Let me also make some changes (5 sec per email)

I will share the outcome later.

Thanks
Regards
Km

@PeteKnight & Expert,

same project & component

issue facing : Adding new DHT11 in existing sketch as below

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>


#define  trig  D2
#define  echo  D1


long duration;
int distance;

// You should get Auth Token in the Blynk App.
                                                        
char auth[] = "74lW8XQGwBITywFjt3V05BwcOZl78cmA";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "wifi";
char pass[] = "pwd";
int flag=0;
void notifyOnFire()
{
  int isButtonPressed = digitalRead(D3);
  if (isButtonPressed==1 && flag==0) {
    Serial.println("Warning Fire Detected");
    Blynk.email("mymail@gmail.com", "Fire Alert", "Warning Fire Detected!");
    Blynk.notify("Alert : Fire Detected");
    flag=1;
  }
  else if (isButtonPressed==0)
  {
    flag=0;
  }
}
BlynkTimer timer;

void setup()
{
  // Debug console
  pinMode(trig, OUTPUT);  // Sets the trigPin as an Output
  pinMode(echo, INPUT);   // Sets the echoPin as an Inpu
  Serial.begin(9600);
  
  Blynk.begin(auth, ssid, pass);
  pinMode(D3,INPUT_PULLUP);
  timer.setInterval(1000L,notifyOnFire); 
  // Setup a function to be called every second
  timer.setInterval(15000L, sendSensor);
}

void loop()
{
  Blynk.run();
  timer.run();
}
void sendSensor()
{
  digitalWrite(trig, LOW);   // Makes trigPin low
  delayMicroseconds(2);       // 2 micro second delay

  digitalWrite(trig, HIGH);  // tigPin high
  delayMicroseconds(10);      // trigPin high for 10 micro seconds
  digitalWrite(trig, LOW);   // trigPin low

  duration = pulseIn(echo, HIGH);   //Read echo pin, time in microseconds
  distance = duration * 0.034 / 2;   //Calculating actual/real distance

  Serial.print("Distance = ");        //Output distance on arduino serial monitor
  Serial.println(distance);
  
  if(distance >= 5)
  {

    Blynk.tweet("My Arduino project is tweeting using @blynk_app and it’s awesome!\n #arduino #IoT #blynk");
     Blynk.email("mymail@gmail.com", "Distance alert!", "Distance alert!");
    Blynk.notify("Distance alert ! ");

  
  }
  Blynk.virtualWrite(V0, distance);
  delay(1000);                        //Pause for 1 seconds and start measuring distance again
}

The above sketch is perfectly working fine for the ultrasonic & Fire

Next :-

I would like to add the DHT11 sensor at pin 5 and I want Blynk to sent notifications & email when
temperature is reach to 50

below is the example sketch that i have for DHT11


#define BLYNK_PRINT Serial


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>

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

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

#define DHTPIN D5          // What digital pin we're connected to

// Uncomment whatever type you're using!
#define DHTTYPE DHT11     // DHT 11
//#define DHTTYPE DHT22   // DHT 22, AM2302, AM2321
//#define DHTTYPE DHT21   // DHT 21, AM2301

DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;

// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

  if (isnan(h) || isnan(t)) {
    Serial.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 setup()
{
  // Debug console
  Serial.begin(9600);

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

  dht.begin();

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

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

Please May I know how can I combine these important DHT11 codes to my primary sketch at the very top?

Please take your time.

Regards
KM

Simply copy/paste the lines from the DHT sketch in to your original sketch.
Do this one line at a time, and omit the lines that already exist.

When it comes to your sendSensor function, either combine your existing code and the DHT code into one sendSensor function, or rename the DHT one as something else (sendDHTSensor) maybe.

Trying to read the DHT sensor every 1 second won’t work well though, change this to every 5-10 seconds.

You’ll also need to add an if statement to check if the DHT temperature reading is over your 50° threshold and send your email and notification if it is.
You’ll also need an additional flag variable to avoid sending multiple emails and notifications.

Pete.

1 Like

Thanks @PeteKnight

Sure I’ll create something like sendDHTSensor
as your suggestion.

Also I’ll set the DHT to read every 10sec.

I believe this is the code to add, but I’ll check that later,


float h = dht.readHumidity();
float t = dht.readTemperature();

if (t > 50.0) {

Blynk.email("mymail@gmail.com", "high Temperature", "High Temp!");
    Blynk.notify("Alert : High Temp!");


else if (isButtonPressed==0)
  {
    flag=0;

About the flag variable, I already got one like below.

Can a use the same?

I will share the outcome Pete, thank you very much.

Regards,
KM

@kernelmaker99a please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Pete.

Hi Pete,

My apology, was using my phone to reply and I cant find that back tics :rofl:

anyway here is the complete code.

I follow you instruction and its working,
Just that i need a help on the email part, when the temperature reach 50 Celsius, an email should trigger

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>

#define  trig  D2
#define  echo  D1


long duration;
int distance;

// You should get Auth Token in the Blynk App.
                                                        
char auth[] = "AUTHTOKEN";

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


#define DHTPIN D5          // What digital pin we're connected to
#define DHTTYPE DHT11     // DHT 11
DHT dht(DHTPIN, DHTTYPE);

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

  if (isnan(h) || isnan(t)) {
    Serial.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);
}

int flag=0;
void notifyOnFire()
{
  int isButtonPressed = digitalRead(D3);
  if (isButtonPressed==1 && flag==0) {
    Serial.println("Warning Fire Detected");
    Blynk.email("mymail@gmail.com", "Fire Alert", "("distance! ");
    Blynk.notify("Alert : ("distance!");
    flag=1;
  }
  else if (isButtonPressed==0)
  {
    flag=0;
  }
}
BlynkTimer timer;

void setup()
{
  // Debug console
  pinMode(trig, OUTPUT);  // Sets the trigPin as an Output
  pinMode(echo, INPUT);   // Sets the echoPin as an Inpu
  Serial.begin(9600);
  
  Blynk.begin(auth, ssid, pass);
  pinMode(D3,INPUT_PULLUP);
  timer.setInterval(1000L,notifyOnFire); 
  // Setup a function to be called every second
  timer.setInterval(15000L, sendSensor);
    dht.begin();
    timer.setInterval(1000L, sendDHTSensor);
}

void loop()
{
  Blynk.run();
  timer.run();
}
void sendSensor()
{
  digitalWrite(trig, LOW);   // Makes trigPin low
  delayMicroseconds(2);       // 2 micro second delay

  digitalWrite(trig, HIGH);  // tigPin high
  delayMicroseconds(10);      // trigPin high for 10 micro seconds
  digitalWrite(trig, LOW);   // trigPin low

  duration = pulseIn(echo, HIGH);   //Read echo pin, time in microseconds
  distance = duration * 0.034 / 2;   //Calculating actual/real distance

  Serial.print("Distance = ");        //Output distance on arduino serial monitor
  Serial.println(distance);
  
  if(distance >= 5)
  {

    Blynk.tweet("My Arduino project is tweeting using @blynk_app and it’s awesome!\n #arduino #IoT #blynk");
     Blynk.email("mymail@gmail.com", "Safety Alert", "distance!");
    Blynk.notify("distance! ");

  
  }
  Blynk.virtualWrite(V0, distance);
  delay(1000);                        //Pause for 1 seconds and start measuring distance again
}

when i add these code in the above sketch, i am getting error while compiling it.


float h = dht.readHumidity();
float t = dht.readTemperature();

if (t > 50.0) {

Blynk.email(“mymail@gmail.com”, “high Temperature”, “High Temp!”);
Blynk.notify(“Alert : High Temp!”);

Thank you,

You need to sort-out post #16 with backticks otherwise it will be deleted.

Pete.

Please post the complete code with the backticks…

First thing I can say; in the second “alert” piece of the code with the distance, you’ll need to use a flag there like you did before, otherwise you spam the server every second if distance becomes > 5.

1 Like