Aquarium monitoring system

Hardware: NodeMCU ESP8266-12E, pH sensor, digital waterproof temperature sensor
Issue: The triggered mail is continuously sent on email id. Is there any way which will send the email only once the change is encountered in the sensor value.

Improvisation of code is appreciable :smiley:



#define BLYNK_PRINT Serial


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <SimpleTimer.h>
SimpleTimer timer;

#define ONE_WIRE_BUS 2  //D4 of nodemcu
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

float temp;

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

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

int V5variable;
int phValue;

void setup()
{
  // Debug console
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
  
  sensors.begin();
  timer.setInterval(10000L, sendTemps);

  Blynk.email("xxxxxxxxx", "Aquarium Alert", "My Blynk project is online.");  //sends email to this id

  attachInterrupt(digitalPinToInterrupt(2), emailPush , CHANGE);   // temp. sensor (digital)
  attachInterrupt(digitalPinToInterrupt(17), emailPush , CHANGE);    //pH sensor (analog)
  
  Blynk.syncVirtual(V5);
  Blynk.virtualWrite(V5, int(V5variable));

}

void emailPush()
{

  if (V5variable > 400) // You can write any condition to trigger e-mail sending
  {
    Serial.println("Turbidity alert"); // This can be seen in the Serial Monitor
    Blynk.email("xxxxxx", "Subject: turbidity", "Turbidity imbalance");
  }

  if (phValue <4 || phValue > 8) // You can write any condition to trigger e-mail sending
  {
    Serial.println("pH Alert"); // This can be seen in the Serial Monitor
    Blynk.email("xxxxxxxx", "Subject: pH Alert", "pH imbalance");
  }

  if (temp < 23 ) // You can write any condition to trigger e-mail sending
  {
    Serial.println("Temperature Alert"); // This can be seen in the Serial Monitor
    Blynk.email("xxxxxxxx", "Subject: Temperature Alert", "Temperature imbalance");
  }

}

//BLYNK_READ(V5){
//  int v_val = analogRead(A0);
//  float v_volt = v_val*(5/1023);
// int v_tur = (int)(100 - (v_volt/5)*100);
//  Blynk.virtualWrite(V5, v_val);
//}

BLYNK_READ(V5) {
  int avg = analogRead(A0);
  float pHVol = (avg * 5.0) / 1024;
  phValue = 3.5 * pHVol + 0.56 ;
  Blynk.virtualWrite(V5, phValue);
  Serial.println(avg);
}

BLYNK_WRITE(V5)
{
  V5variable = param.asInt();
}

void sendTemps()
{
  sensors.requestTemperatures();                  // Polls the sensors.
  temp = sensors.getTempCByIndex(0);          // Stores temperature.
  Serial.println(temp);
  Blynk.virtualWrite(10, temp);         // Send temperature to Blynk app virtual pin 10.
}

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


Create a variable that is cleared after the condition is reset, something like:

int phAlertEmail = 0;

....

if (phValue <4 || phValue > 8) // You can write any condition to trigger e-mail sending
  {
phAlertEmail++;
If (phAlertEmail == 1 ){
    Serial.println("pH Alert"); // This can be seen in the Serial Monitor
    Blynk.email("xxxxxxxx", "Subject: pH Alert", "pH imbalance");
}
  }else phAlertEmail=0;
1 Like

Bump

@LissMaker have you bumped this because you are experiencing a similar issue and are looking for a solution?
If so, the question has been answered. A flag is needed to indicate if an email has already been sent for this warning. If the warning state still exists, but an email has already been sent (the flag has been set) then do nothing.
Once the alert state is cleared (pH reading is within tolerance again) then clear the flag - and maybe send a “pH is back in range” email).

If you’re struggling with this concept then I suggest that you create a new topic and provide all the necessary information, along with your code (correctly formatted of course) and a full explanation of what you are trying to achieve and what issues you are experiencing.

Pete.