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

Thanks Pete,

I just sorted the #16.

The only challenge now is, sending an email when temperature is reached to 50.

No, that’s why I said…

Put these declarations at the top of your code, not scattered throughout it in random places.

Remove this delay.

I dont think tweets are supported anymore.

This code…

is missing a closing curly bracket }

Pete.

Hi Pete,

Here is my Sketch and I am getting the following error

Not used: C:\Users\PC\Documents\Arduino\libraries\blynk-library-master
exit status 1
stray ‘\342’ in program

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

#define  trig  D2
#define  echo  D1


long duration;
int distance;

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

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


#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;
if (t > 50.0) {

Blynk.email(“mymail@gmail.com”, “high Temperature”, “High Temp!”);
Blynk.notify(“Alert : High Temp!”);
}
    
  }
  // 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", "fire detected!");
    Blynk.notify("Alert : Fire Detected");
    flag=1;
  }
  else if (isButtonPressed==0)
  {
    flag=0;
  }
}


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(10000L, 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.email("mymail@gmail.com", "distance", "distance reached!");
    Blynk.notify("distance reached ! ");

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

Sorry if i had missed out anything

The compiler will point you to the location of the problems, but it’s most likely these quotation marks…

You’ll see that they are different to the " quotation marks that you have used in the rest of your sketch.

Pete.

Hi Pete,

Thanks I just noticed that,
I’ll correct it and recompile
Will update you.

Regards
KM

Hi Pete,

I am still getting error
“a function-definition is not allowed here before ‘{’ token”

I included int flag=0; but still getting the same error,

below is my latest sketch


#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[] = "AUTH";

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


#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;

if (t > 50.0) {

    Blynk.email("mymail@gmail.com", "Temp Alert", "Check Temperature!");
    Blynk.notify("Alert : Check Temperature");
  }
  // 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", "fire!");
    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);
    dht.begin();
    timer.setInterval(10000L, 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 reached!");
    Blynk.notify("distance ! ");

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

Your problem is a missing closing curly bracket in the sendDHTSensor() function.
If you laid-out your code better, with proper indents, and stopped putting your opening curly brackets at the end of a line, then you’d be able to spot these issues easier.

This is the fixed function…

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;
  }

  if (t > 50.0)
  {
    Blynk.email("mymail@gmail.com", "Temp Alert", "Check Temperature!");
    Blynk.notify("Alert : Check Temperature");
  }
  
  // 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);
}

Pete.

Hi Pete,

Thanks for the sketch, i’ll try in short while.

Regards
KM

Hi Pete,

My apologies on the delay.
your code fixed everything, things are working fine now, Thanks a lot.

Regards,
KM

1 Like