hello every one!!
first of all i want to tell you that i am newbie in coding, and all my work is
most copy and paste from here and there.
i have make a controller for my aquarium that runs almost a year
it is a nano arduino with esp8266-01 and control a water refill system from
an optic IR sensor (auto and manual),also watching a battery voltage for
for power backup,control an on off lights relay , a fan relay,and a temperature
monitor.BUT i dont want to stop here ,in the future i want to add a Ph meter
from Atlas ,and some other staff.
My question is that: can i somehow get an email alert without running the blynk app
all the time. because when running continuously it draws my cellphone battery to
quickly.
/*Comment this out to disable prints and save space */
//#define BLYNK_PRINT Serial
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <TimeLib.h> // need for server time
#include <WidgetRTC.h> // need for server time
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 7 ////////////////////////////////// Data wire is plugged into pin 7 on the Arduino
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
char auth[] = "..............................";
char ssid[] = "............";
char pass[] = ".......................";
#include <SoftwareSerial.h>
SoftwareSerial EspSerial(10,11); // RX, TX (exodos arduino)
#define ESP8266_BAUD 9600
ESP8266 wifi(&EspSerial);
BlynkTimer timer_4 ; ////////////////////////////////////// DECLARE A TIMER AS " timer_4 "
WidgetRTC rtc; // for clock
//////////////////////////////////////////////////////////////////////// PINS
const int RelayPin3 =3; // AC-3
const int RelayPin4 =4; // AC-4
const int analogPin =0; // optic sensor
const int analogPinV =1; // voltmeter
const int PumpPin =13; // AC-1
const int LightsPin =2; // AC-2
const int FanPin =5; // DC1
const int VaccumPumpPin =6; // DC2
/////////////////////////////////////////////////////////////////////// VARIABLES BLYNK
int LightsOn =11;
int LightsOff =21;
int V;
int Slide ;
int Counter =0;
String LastReffil ;
String LastContact ;
float AlphaTemp;
float Corecting=0;
int SetChangeTemp = 25;
const uint16_t flash_time = 1000;
///////////////////////////////////////////////////////////////////// OTHER VARIABLES FOL LOOP SECTION
unsigned long MaxRunTime =10*1000L; // ( 1 minute = 60*1000L ) max pump run
unsigned long MinOffTime =60*60*1000L; // ( 1 hour = 60*60*1000L ) debounce
unsigned long FanOff = 5*60*1000L; // ON(reverse relay)
unsigned long FanOn = 10*60*1000L; // OFF (reverse relay)
int WaterState;
boolean PumpRunning=false;
int LastWaterState =HIGH;
unsigned long PreviousPumpMillis =0;
int FanState = LOW;
unsigned long PreviousFanMillis =0;
////////////////////////////////////////////////////////////////////// synchronize blynk with server
bool isFirstConnect = true;
BLYNK_CONNECTED() {
if (isFirstConnect) {
Blynk.syncAll();
isFirstConnect = false;
}
rtc.begin();
}
//////////////////////////////////////////////////////////////////////// check connection
void checkBlynk(){
bool isconnected = Blynk.connected();
if (isconnected == false){
Blynk.virtualWrite(V13,LightsOff);
digitalWrite(LightsPin,LOW);
}
}
//////////////////////////////////////////////////////////////////////// relay_action FUNCTION
void Relay() {
unsigned long x = millis();
while(millis() - x < (flash_time*Slide)){
digitalWrite(PumpPin, HIGH);
yield();
}
}
/////////////////////////////////////////////////////////////////////// CLOCK FUNCTION
void Clock()
{
String currentTime = String(hour()) + ":" + minute() + ":" + second();
// String currentDate = String(day()) + " " + month() + " " + year();
Blynk.virtualWrite(V7, currentTime);
// Blynk.virtualWrite(V10, currentDate);
}
/////////////////////////////////////////////////////////////////////// VOLTMETER
void VoltMeter()
{
float Vout=0.0;
float Vin =0.0;
float R1 =102000.0; //true (106000.0)
float R2 =65000.0 ; //true (47000.0)
int Value =0;
Value= analogRead(analogPinV); // read analog pin 1
Vout=(Value*5)/1024.0;
Vin =Vout/(R2/(R1+R2));
if(Vin<0.09){Vin=0.0;}
Blynk.virtualWrite(V17,Vin);
}
/////////////////////////////////////////////////////////////////////// Lights FUNCTION
void Lights()
{
Blynk.virtualWrite(V12,LightsOn);
Blynk.virtualWrite(V13,LightsOff);
if (hour()>=LightsOn && hour()<LightsOff){
digitalWrite(LightsPin,HIGH);}
else{digitalWrite(LightsPin,LOW);}
}
///////////////////////////////////////////////////////////////// temp fuction
void TempFuction(){
sensors.requestTemperatures();
//Serial.println(sensors.getTempCByIndex(0));
AlphaTemp=(sensors.getTempCByIndex(0));
AlphaTemp=AlphaTemp+Corecting;
Blynk.virtualWrite(V21, AlphaTemp); // Temperature for gauge
Blynk.virtualWrite(V22,AlphaTemp); // Temperature for graph // or V13,sensors.getTempCByIndex(0));
AlphaTemp=AlphaTemp-Corecting;
}
//////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////// functions in time ////////////////////////////////////////////////////////////
void TimeFunctions() // functions in timers
{
Blynk.virtualWrite(V9,V);
Blynk.virtualWrite(V6,Counter); if(Counter>20){Counter=0; }
Clock();
Lights();
VoltMeter();
TempFuction();
Blynk.virtualWrite(V8,LastReffil); // sendSensor();
Blynk.virtualWrite(V11,LastContact); //last time optic sensor hit water
Blynk.virtualWrite(V20,(MaxRunTime/1000));
Blynk.virtualWrite(V0,(FanOn/60000));
Blynk.virtualWrite(V10,(FanOff/60000));
Blynk.virtualWrite(V23, SetChangeTemp);
Blynk.virtualWrite(V24, Corecting);
}
////////////////////////////////////////////////////////////////// TARGET TEMP SETPOINT
BLYNK_WRITE(V26) // set
{
SetChangeTemp = SetChangeTemp + param.asInt();
}
////////////////////////////////////////////////////////////////// TEMP CORECTION SETPOINT
BLYNK_WRITE(V27) // set
{
Corecting = Corecting + param.asFloat();
}
BLYNK_WRITE(V19) // set
{
MaxRunTime = MaxRunTime + param.asInt();
}
BLYNK_WRITE(V14) // set
{
LightsOn = LightsOn + param.asInt();
}
BLYNK_WRITE(V15) // set
{
LightsOff = LightsOff + param.asInt();
}
BLYNK_WRITE(V16) // set
{
FanOn = FanOn + param.asInt();
}
BLYNK_WRITE(V18) // set
{
FanOff = FanOff + param.asInt();
}
//////////////////////////////////////////////////////////////////////// RELAY BUTTON REFFIL ON TIME FROM SLIDE
BLYNK_WRITE(V1) //RelayPin4
{
int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
// process received value
if(pinValue==0){digitalWrite(PumpPin,LOW);}
if(pinValue==1){
Relay();
digitalWrite(PumpPin,LOW);
Blynk.virtualWrite(V1, 0); // "Blynk.virtualWrite(V1, 0);"(update app button 0=off/255=on)
Blynk.virtualWrite(V3, 0);
Counter=Counter+1;
LastReffil=String(hour()) + ":" + minute() ;
}
}
/////////////////////////////////////////////////////////////////////// SLIDE
BLYNK_WRITE(V2)
{
int A = param.asInt();
Slide=A;
}
/////////////////////////////////////////////////////////////////////// RELAY BUTTON REFFIL PUSH
BLYNK_WRITE(V3) // relaypin4
{
int pinValue = param.asInt(); // assigning incoming value from pin V3 to a variable
// process received value
if(pinValue==0){digitalWrite(RelayPin4,LOW);}
if(pinValue==1){ digitalWrite(RelayPin4,HIGH);}
}
/////////////////////////////////////////////////////////////////////// RELAY BUTTON FREE
BLYNK_WRITE(V4) // RelayPin3
{
int pinValue = param.asInt(); // assigning incoming value from pin V3 to a variable
if(pinValue==0){digitalWrite(RelayPin3,LOW);}
if(pinValue==1){ digitalWrite(RelayPin3,HIGH);}
}
////////// /////////////////////////////////////////////////////////// CounterZero
BLYNK_WRITE(V5) // CounterZero
{
int pinValue = param.asInt(); // assigning incoming value from pin V5 to a variable
if(pinValue==1){ Counter=0;}
}
/////////////////////////////////////////////////////////////////////// VaccumPump
BLYNK_WRITE(V25)
{
int pinValue = param.asInt(); // assigning incoming value from pin V25 to a variable
if(pinValue==0){digitalWrite( VaccumPumpPin,LOW);}
if(pinValue==1){ digitalWrite( VaccumPumpPin,HIGH);}
}
///////////////////////////////////////////////////////////////////// SETUP //////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
delay(300000UL);
sensors.begin();
pinMode(analogPinV,INPUT);
pinMode(analogPin,INPUT);
pinMode(PumpPin,OUTPUT);
pinMode(LightsPin,OUTPUT);
pinMode(FanPin,OUTPUT);
pinMode(RelayPin4,OUTPUT);
pinMode(RelayPin3,OUTPUT);
pinMode(VaccumPumpPin,OUTPUT);
//Serial.begin(9600);
EspSerial.begin(ESP8266_BAUD);
delay(10);
Blynk.begin(auth, wifi, ssid, pass);
timer_4.setInterval(4000L,TimeFunctions); // start timer
}
/////////////////////////////////////////////////////////////////// LOOP ////////////////////////////////////////////////////////////////////////
void loop() {
unsigned long CurrentMillis = millis();
V = analogRead(0);
V= map(V,0,1023,0,255);
if (V<90)
{
WaterState =HIGH; // close pump
LastContact=String(hour()) + ":" + minute() ;
}
else{
if(V>=90)
{
WaterState =LOW; // open pump
}
}
if (WaterState != LastWaterState){ PreviousPumpMillis = CurrentMillis;}
if(PumpRunning==true){
if ((WaterState== HIGH ) || ((CurrentMillis-PreviousPumpMillis)>=MaxRunTime))
{digitalWrite(PumpPin,LOW);
PumpRunning=false;
PreviousPumpMillis=CurrentMillis;
}}
else { if (( WaterState== LOW ) &&(CurrentMillis-PreviousPumpMillis >= MinOffTime))
{digitalWrite(PumpPin,HIGH);
PumpRunning=true;
PreviousPumpMillis=CurrentMillis;
Counter=Counter+1;
LastReffil=String(hour()) + ":" + minute() ;
}}
LastWaterState = WaterState;
// Serial.print(V); // delete(//) for check for false optic sensor
// Serial.println();
//////////////////////////////////////////////////////////////////////// FAN
if((FanState == HIGH)&&(CurrentMillis-PreviousFanMillis>=FanOn))
{
FanState = LOW;
PreviousFanMillis = CurrentMillis;
digitalWrite(FanPin,FanState);
}
else if((FanState == LOW)&&(CurrentMillis-PreviousFanMillis>=FanOff))
{
FanState = HIGH;
PreviousFanMillis = CurrentMillis;
digitalWrite(FanPin,FanState);
}
/////////////////////////////////////////////////////////////////////////// BLYNK /////////////////////////////////////////////////////////////////
Blynk.run();
timer_4.run(); // Initiates SimpleTimer
} // close loop