BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Blynk e-mail: I want to send e-mail only ONCE

Dear Blynk Community Members, I’m new to blank and find it very interesting and trying many different things.
I want to make an Incubator and want to get an e-mail notification only ONCE if Temp < 30 or Temp > 40 or Humidifity < 40%.

I wrote (basically modify) the following code. With this code I’m getting multiple (every 20 seconds)

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <DHT.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "***"; //Enter the Auth code which was send by Blink

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "***";  //Enter your WIFI Name
char pass[] = "***";  //Enter your WIFI Password

#define DHTPIN 2          // Digital pin 4
#define LITEPIN1 D1          //Light 1
#define LITEPIN2 D2          //Light 2
//Register Light to virtual pin 1 (V1)
WidgetLED led1(V1);          //Virtual Pin V1 for light ON/OFF
//Register Light to virtual pin 2 (V2)
WidgetLED led2(V2);          //Virtual Pin V1 for Humidity


// 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);
SimpleTimer 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);  //V5 is for Humidity
  Blynk.virtualWrite(V6, t);  //V6 is for Temperature
  Serial.println(t);
  Serial.println(h);
if (t <= 37.5) // the temperature in your incubator should be maintained between 37 and 39 degrees Celcius.
  {
    digitalWrite(LITEPIN1,LOW);
    digitalWrite(LITEPIN2,LOW);
    led1.on();
    Serial.println("Lights are ON");    
    }
    else
    {
      digitalWrite(LITEPIN1,HIGH);
      digitalWrite(LITEPIN2,HIGH);
      led1.off();
      Serial.println("Lights are OFF");
      }
if (h <= 40) //The humidity levels should be between 50 and 55% and then increase to about 65% for the final three days of incubation. 
  {
    led2.on();
    Serial.println("Low Humidity");
    Blynk.email("my_email@hotmail.com", "Subject: Low Humidity", "Humidity level is low");
    }
    else
    {
      led2.off();
      Serial.println("Humidity Level is OK");
      }
if (t > 40) // You can write any condition to trigger e-mail sending
  {
    Serial.println("Temperature is higher than 40oC"); // This can be seen in the Serial Monitor
    Blynk.email("my_email@hotmail.com", "Subject: High Temp", "Temp is > 40");
     }
if (t < 30) // You can write any condition to trigger e-mail sending
  {
    Serial.println("Temperature is Low than 30oC"); // This can be seen in the Serial Monitor
    Blynk.email("my_email@hotmail.com", "Subject: Low Temp", "Temp is < 30");
  }
 delay(20000); 
}

void setup()
{
  Serial.begin(9600); // See the connection status in Serial Monitor
  Blynk.begin(auth, ssid, pass);
  // Send e-mail when your hardware gets connected to Blynk Server
  // Just put the recepient's "e-mail address", "Subject" and the "message body"
  Blynk.email("my_email@hotmail.com", "Subject: Blynk is Online", "hardware gets connected to Blynk Server");

  dht.begin();
  pinMode (LITEPIN1, OUTPUT);
  pinMode (LITEPIN2, OUTPUT);

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

void loop()
{
  Blynk.run(); // Initiates Blynk
  timer.run(); // Initiates SimpleTimer
}

The way to do this is to use a variable as a flag.
Flag is set to 0 at start-up
When criteria is met that requires an email then check the flag. If it’s 0 then send the email and set the flag to 1
Next time the test is done then the email won’t be sent because the flag is now ==1

The important bit is setting the flag back to 0 once the alert criteria is no longer met, otherwise you’ll never get any more alerts. You’ll do this in your else statement, which is met when the readings are (back) in range.

Pete.

Many thanks Pete.

Do you have any code example? I understood a bit the first part of introducing a flag and set it as “0”. But can’t understand how to make it back to “0” again once it meet the criteria and e-mail in out.

Something like this…

if (h <= 40)
{
  led2.on();
  Serial.println("Low Humidity");
  if (email_sent_falg ==0)
    {    
      Blynk.email("my_email@hotmail.com", "Subject: Low Humidity", "Humidity level is low");
      email_sent_falg ==1;
    }
 }
 else
 {
   led2.off();
   Serial.println("Humidity Level is OK");
   email_sent_falg ==0;
  }
}

Pete

Many thanks Pete.

what is the main problem?

Maybe the main problem is the thread is a year old… :joy::rofl: