[SOLVED] Emails Notifications Repeating

Hello, I created a sketch to include email notifications if my temperature climbs above 60 degrees Fahrenheit. Once the temperature climbs above 60 I get my first notification. Then the notifications continue about 3 every minute and don’t stop even if the temperature has leveled off at lets say 74 degrees. Below I am including my sketch:

#define BLYNK_PRINT Serial // Comment this out to disable prints and save space

#include <ESP8266WiFi.h>

#include <BlynkSimpleEsp8266.h>

#include <DHT.h>

#include <SimpleTimer.h>

#define DHTPIN D6

#define DHTTYPE DHT11

DHT dht(DHTPIN, DHTTYPE);

SimpleTimer timer;

float humidity, temp_f; // Values read from sensor

char auth[] = "e54a63f72f1b487b8fa1908fc0d*****"; //insert here your token generated by Blynk

void setup()

{

Serial.begin(9600); // See the connection status in Serial Monitor

delay(10);

Blynk.begin(auth, "H*****2-2.4", "**********"); //insert here your SSID and password
timer.setInterval(1000, sendData);

}

void sendData()
{

   //Read the Temp and Humidity from DHT

float h = dht.readHumidity();

float t = dht.readTemperature(true);

//Write values to V04 and V05

Blynk.virtualWrite(4, h);

Blynk.virtualWrite(5, t);

if (t > 60){
Blynk.email("************@**************", "ALARM", "Temp is above 60F");

}

if (t < 40){
Blynk.email("************@**************", "ALARM", "Temp is below 40F");  


}

}

void loop()

{

Blynk.run();

timer.run();

}

Hello. Try to use eventor. It is designed for this kind of tasks. http://docs.blynk.cc/#widgets-other-eventor

1 Like

Eventor will help with this… or if you are iOS (and still waiting for Eventor)… then you will need to set a flag so that the Blynk,email() function is only called once and not looped like you have it now.

Just remember that all of your code is happening thousands of times a second… so your lineif (t > 60){ will basically be true thousands of times WHILE the temp > 60… not good!

You need to do sometyhing like this:

if (t >= 60 && notified == 0){ // check to see if flag is also set
  notified = 1; 
  Blynk.email("************@**************", "ALARM", "Temp is above 60F");
} else if(t < 60){
  notified = 0;
}

You will still run in to a small problem with this code. Imagine if you temp is bouncing between 59 and 60… like 59.9-60.1 etc… your code will constantly be saying the above IF() statement is true then false.

You might be better to set a timeout of say 5 minutes instead of an ELSE() statement.

So like this:

if (t >= 60 && notified == 0){ // check to see if flag is also set
  notified = 1; 
  Blynk.email("************@**************", "ALARM", "Temp is above 60F");
  timer.setTimeout(18000, resetNotified); // 5 min between emails 
} 

void resetNotified(){
  notified = 0;
}
2 Likes

Thank you. I updated the code with your suggestion. When verifying the updated code I get:

“notified was not declared in this scope”

Below i’ve repasted the lastest code. thanks again:

#define BLYNK_PRINT Serial // Comment this out to disable prints and save space

#include <ESP8266WiFi.h>

#include <BlynkSimpleEsp8266.h>

#include <DHT.h>

#include <SimpleTimer.h>

#define DHTPIN D6

#define DHTTYPE DHT11

DHT dht(DHTPIN, DHTTYPE);

SimpleTimer timer;

float humidity, temp_f; // Values read from sensor

char auth[] = “e54a63f72f1b487b8fa1908fc******”; //insert here your token generated by Blynk

void setup()

{

Serial.begin(9600); // See the connection status in Serial Monitor

delay(10);

Blynk.begin(auth, “*******************”, “*****************”); //insert here your SSID and password

timer.setInterval(1000, sendData);

}

void sendData()

{

//Read the Temp and Humidity from DHT

float h = dht.readHumidity();

float t = dht.readTemperature(true);

//Write values to V04 and V05

Blynk.virtualWrite(4, h);

Blynk.virtualWrite(5, t);

if (t >= 60 && notified == 0){
notified = 1;
Blynk.email("***@", “ALARM”, “Temp is above 60F”);
timer.setTimeout(18000, resetNotified); // 5 min between emails

void resetNotified(){
notified = 0;

}

}

void loop()

{

Blynk.run();

timer.run();

}

@flhtraveler you need to declare the notified variable.

So after:

float humidity, temp_f; // Values read from sensor

add

unsigned int notified = 0;

That worked after adding:

unsigned int notified = 0;

I also get:

“resetNotified was not declared”

How to declare it? Thank you

@flhtraveler you need to learn how to post formatted code on the forum. It has been covered many times.

Looking at your illegible code it looks like you have braces in the wrong place.

Move the } before void loop() to just before void resetNotified() and you should be good to go.

If this fails post a formatted sketch.

That worked in removing the declaration error. Now that I re-connected the temperature sensor I continue to get 3 emails per minute notifying me that the temperature is above 60 F. Here is the code:

#define BLYNK_PRINT Serial // Comment this out to disable prints and save space

#include <ESP8266WiFi.h>

#include <BlynkSimpleEsp8266.h>

#include <DHT.h>

#include <SimpleTimer.h>

#define DHTPIN D6

#define DHTTYPE DHT11

DHT dht(DHTPIN, DHTTYPE);

SimpleTimer timer;

float humidity, temp_f; // Values read from sensor

unsigned int notified = 0;

char auth[] = “e54a63f72f1b487b8fa19*********”; //insert here your token generated by Blynk

void setup()

{

Serial.begin(9600); // See the connection status in Serial Monitor

delay(10);

Blynk.begin(auth, “", "****”); //insert here your SSID and password

timer.setInterval(1000, sendData);

}

void sendData()

{

//Read the Temp and Humidity from DHT

float h = dht.readHumidity();

float t = dht.readTemperature(true);

//Write values to V04 and V05

Blynk.virtualWrite(4, h);

Blynk.virtualWrite(5, t);

if (t >= 60 && notified == 0){
notified = 1;
Blynk.email("***@", “ALARM”, “Temp is above 60F”);
timer.setTimeout(18000, resetNotified); // 5 min between emails

}

}

void resetNotified(){
notified = 0;

}

void loop()

{

Blynk.run();

timer.run();

}

I think it’s your timeout that needs a timerId, see http://playground.arduino.cc/Code/SimpleTimer#F_setTimeout

When you have it working post the formatted sketch here and don’t post any more unformatted versions on the forum.

Hi Dimitry,

Thank you for your reply. After several other replies from the community members and running hours of research I have not been able to find a solution to our problem. Basically I want an email to be sent after the temperature climbs above 60 degrees fahrenheit. The email is sent out but we get 3 emails per minute none stop. Can you please take a look at our code and let me know what else that I need to do to get Blynks email notification up & running smoothly. Here is the piece of code we plugged in:

if (t >= 60 && notified == 0){
notified = 1;
Blynk.email("************@************", “ALARM”, “Temp is above 60F”);
timer.setTimeout(18000, resetNotified); // 5 min between emails

@flhtraveler 18000 is 18s not 5 minutes!

Change 18000 to 300000L

I just changed from 18000 to 300000L however still getting 3 email per minute non stop. Is there anything else I can do?

@flhtraveler study this thread In case you see SMTHNG strange in Blynk

1 minute divide 18s is 3 and a bit, hence 3 emails per minute. Maximum is 4 per minutes as Blynk have a 15s minimum frequency between messages.

300000L is definitely your fix.

Post your code from the information you learnt by studying the details in the link I provided.

Yes you are right. The issue now is that my temperature is fixed at 66.2 however I get a notification every 5 minutes. I just want to get a notification one time only when the temperature climbs above 60 degrees. How to make this happen? This is the code I currently have:

if (t >= 60 && notified == 0){
notified = 1;
Blynk.email("andres.segrera@flhnorthamerica.com", “ALARM”, “Temp is above 60F”);
timer.setTimeout(300000L, resetNotified); // 5 min between emails

Of course.

Before we go any further did you study the link and how did your code suddenly change from 18s to 5minute intervals for emails?

This is the code you need, nothing else…

@Costas you also see the problem about dancing between 5.9 and 6.1 (Hysteresis should solve it) and you tried to make a better code but… you see the result :sweat_smile:

Here you go:

if (t >= 60 && notified == 0){ // check to see if flag is also set
  notified = 1; 
  Blynk.email("************@**************", "ALARM", "Temp is above 60F");
} else if(t < 58){ // set lower than 60... to stop the 59.9-60.0 bouncing. 
  notified = 0;
}
1 Like

The code is yours, I just corrected the 5 minutes from 18000 to 300000L :slight_smile:

No, it’s not my code, i’m just passing through reading everything in the forum :joy:

Edit: it’s truth, you also din’t made any code, it was Jamin…

@darkmoon I said the code belongs to @jamin, not you, see his first post in this thread.