Well… This is a automatic hydroponic system, which checks the water level using ultrasound sensor and uses valves and pumps during watering process. Here´s part of the code, whole code is too long here… To this I´d like to include WiFi-Manager cause I´d like to make couple more of these… For my friends without profiting. But still it´s too hard to use hardcoded method here. Maybe I try with other computer… this is so strange anyway.
/**************************************************************
Samsara Exit Hydroponics V1.0
13.03.2019
Hydroponics System for NodeMCU 1.0
* ************************************************************
Blynk app Tab-1:
V0: On-Off Button (Water Pump ON/OFF)
V1: Value Display
V2: On-off button (Lights Manual)
V3: Terminal // Label will be the clock + wifi signal
V4: On-Off Button (Main watering ON/OFF)
V5: On-Off Button (Water Leak Sensor ON/OFF)
V7: Virtual LED (lights)
V8: Segmented switch (1-Veg, 2-Bloom1, 3-Bloom2, 4-Plain Water, 5-Drain and End)
V10: Timer - Time Input widget on V10
V11: On-Off button (Lights Timer)
Blynk app Tab-2 (settings):
V6: Numeric Input Display (Water Capacity (Litres)
V9: Numeric Input Display (Drain Time (minutes)
V12: Numeric Input Display (Water Level Full (distance from the sensor cm)
V13: Numeric Input Display (Water Level Low (distance from the sensor cm)
V14: Numeric Input Display (Vegetative State - Fertilizer A (Grow) Ml/100L)
V15: Numeric Input Display (Vegetative State - Fertilizer B (Bloom) Ml/100L)
V16: Numeric Input Display (Vegetative State - Fertilizer C (Micro) Ml/100L)
V17: Numeric Input Display (Bloom 1 State - Fertilizer A (Grow) Ml/100L)
V18: Numeric Input Display (Bloom 1 State - Fertilizer B (Bloom) Ml/100L)
V19: Numeric Input Display (Bloom 1 State - Fertilizer C (Micro) Ml/100L)
V20: Numeric Input Display (Bloom 2 State - Fertilizer A (Grow) Ml/100L)
V21: Numeric Input Display (Bloom 2 State - Fertilizer B (Bloom) Ml/100L)
V22: Numeric Input Display (Bloom 2 State - Fertilizer C (Micro) Ml/100L)
V23: Numeric Input Display (Water Pump OFF time (minutes)
V24: Numeric Input Display (Water Pump ON time (minutes)
**************************************************************/
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <TimeLib.h>
#include <WidgetRTC.h>
#include <SPI.h>
SimpleTimer timer;
WidgetRTC rtc;
WidgetTerminal terminal(V3);
#define server "blynk.cloud-com" // or "blynk.cloud-com" for Blynk's cloud server
char Date[16];
char Time[16];
char auth[] = "***********";
char ssid[] = "*********";
char pass[] = "**********";
long startsecondswd; // weekday start time in seconds
long stopsecondswd; // weekday stop time in seconds
long nowseconds; // time now in seconds
bool isFirstConnect = true;
// Set water sensor analog pins
float water_sensor_a = A0;
float sensorValue = 0;
float capacityAdjustValue = 0;
float WaterFullOutputValue = 0;
float WaterLowOutputValue = 0;
float WaterLevelMeterAdjustValue = 0;
unsigned long WaterPumpUserONPeriod = 0; //Set Water Pump Open Time
unsigned long WaterPumpUserOFFPeriod = 0; //Set Water Pump Closed Time
//int WaterPumpState = LOW;
unsigned long DrainValvePeriod = 0; //Set Drain Valve Open Time
unsigned long PumpAVPeriod = 0; //Fertilizer Pump 1 Fertilizer A (GROW) ON Vegetative State period
unsigned long PumpBVPeriod = 0; //Fertilizer Pump 2 Fertilizer B (BLOOM) ON Vegetative State period
unsigned long PumpCVPeriod = 0; //Fertilizer Pump 3 Fertilizer C (MICRO) ON Vegetative State period
unsigned long PumpABPeriod = 0; //Fertilizer Pump 1 Fertilizer A (GROW) ON BLOOM 1 State period
unsigned long PumpBBPeriod = 0; //Fertilizer Pump 2 Fertilizer B (BLOOM) ON BLOOM 1 State period
unsigned long PumpCBPeriod = 0; //Fertilizer Pump 3 Fertilizer C (MICRO) ON BLOOM 1 State period
unsigned long PumpABFPeriod = 0; //Fertilizer Pump 1 Fertilizer A (GROW) ON BLOOM 2 State period
unsigned long PumpBBFPeriod = 0; //Fertilizer Pump 2 Fertilizer B (BLOOM) ON BLOOM 2 State period
unsigned long PumpCBFPeriod = 0; //Fertilizer Pump 3 Fertilizer C (MICRO) ON BLOOM 2 State period
unsigned long DrainValveStartMillis;
unsigned long PumpABStartMillis;
unsigned long PumpBBStartMillis;
unsigned long PumpCBStartMillis;
unsigned long PumpABFStartMillis;
unsigned long PumpBBFStartMillis;
unsigned long PumpCBFStartMillis;
unsigned long PumpAVStartMillis;
unsigned long PumpBVStartMillis;
unsigned long PumpCVStartMillis;
unsigned long WaterPumpStartMillis;
unsigned long WaterPumpCurrentMillis = 0;
unsigned long DrainValveCurrentMillis = 0;
unsigned long PumpABCurrentMillis = 0;
unsigned long PumpBBCurrentMillis = 0;
unsigned long PumpCBCurrentMillis = 0;
unsigned long PumpAVCurrentMillis = 0;
unsigned long PumpBVCurrentMillis = 0;
unsigned long PumpCVCurrentMillis = 0;
unsigned long PumpABFCurrentMillis = 0;
unsigned long PumpBBFCurrentMillis = 0;
unsigned long PumpCBFCurrentMillis = 0;
#define DHTPIN3 = 0; //D3
#define DHTPIN4 = 2; //D4
#define DHTPIN5 = 14; //D5
#define DHTPIN6 = 12; //D6
#define DHTPIN7 = 13; //D7
#define DHTPIN8 = 15; //D8
#define DHTPIN0 = 16; //D0
enum VEGWaterStates {IsVEGWaterNeeded, VEGStep1, VEGStep2, VEGStep3, VEGStep4, VEGWaterNotNeeded, VEGWaterNeeded};
VEGWaterStates VEGState = IsVEGWaterNeeded;
enum BLOOMWaterStates {IsBLOOMWaterNeeded, BLOOMStep1, BLOOMStep2, BLOOMStep3, BLOOMStep4, BLOOMWaterNotNeeded, BLOOMWaterNeeded};
BLOOMWaterStates BLOOMState = IsBLOOMWaterNeeded;
enum BLOOMFWaterStates {IsBLOOMFWaterNeeded, BLOOMFStep1, BLOOMFStep2, BLOOMFStep3, BLOOMFStep4, BLOOMFWaterNotNeeded, BLOOMFWaterNeeded};
BLOOMFWaterStates BLOOMFState = IsBLOOMFWaterNeeded;
enum PLAINWaterStates {IsPLAINWaterNeeded, PLAINStep1, PLAINStep2, PLAINWaterNotNeeded, PLAINWaterNeeded};
PLAINWaterStates PLAINState = IsPLAINWaterNeeded;
enum DRAINWaterStates {IsDRAINWaterNeeded, DRAINStep1, DRAINStep2, DRAINWaterNotNeeded, DRAINWaterNeeded};
DRAINWaterStates DRAINState = IsDRAINWaterNeeded;
enum WateringStates {WateringON, WateringOFF};
WateringStates WateringState = WateringOFF;
enum LowWaterNotifyStates {LowWaterNotifyDone, LowWaterNotifyUnDone};
LowWaterNotifyStates LowWaterNotifyState = LowWaterNotifyUnDone;
enum LeakStates {WaterLeakNO, WaterLeakYES};
LeakStates LeakState = WaterLeakNO;
enum WaterPumpStates {WaterPumpON, WaterPumpOFF};
WaterPumpStates WaterPumpState = WaterPumpON;
enum WaterPumpUserStates {WaterPumpUserON, WaterPumpUserOFF};
WaterPumpUserStates WaterPumpUserState = WaterPumpUserON;
enum MainWateringStates {MainWateringON, MainWateringOFF};
MainWateringStates MainWateringState = MainWateringON;
enum LeakSensorStates {LeakSensorON, LeakSensorOFF};
LeakSensorStates LeakSensorState = LeakSensorON;
byte MainValveState = LOW;
byte DrainValveState = LOW;
byte PumpAState = LOW;
byte PumpBState = LOW;
byte PumpCState = LOW;
int RELAY1 = D3; //Main Valve
int RELAY2 = D4; //Lights ON/OFF
const byte RELAY6 = D8; //Drain Valve
const byte RELAY3 = D5; //Fertilizer Pump 1 GROW
const byte RELAY4 = D6; //Fertilizer Pump 2 BLOOM
const byte RELAY5 = D7; //Fertilizer Pump 3 MICRO
const byte RELAY7 = D0; //Water Pump
String displaycurrenttimepluswifi;
int wifisignal;
int manual = 0;
int oldstatus;
int uptoyou;
//-------------------
//ultrasound sensor:
unsigned long echo = 0;
int ultraSoundSignal = 5; // Ultrasound trig signal pin D1
int ultraSoundSignalIN = 4; // Ultrasound echo signal pin D2
unsigned long ultrasoundValue = 0;
void setup()
{
pinMode(RELAY1, OUTPUT);
pinMode(RELAY2, OUTPUT);
pinMode(RELAY3, OUTPUT);
pinMode(RELAY4, OUTPUT);
pinMode(RELAY5, OUTPUT);
pinMode(RELAY6, OUTPUT);
pinMode(RELAY7, OUTPUT);
digitalWrite(RELAY1, LOW); // set Main Valve OFF
digitalWrite(RELAY2, LOW); // set LIGHTS OFF
digitalWrite(RELAY3, LOW); // set Fertilizer Pump 1 GROW OFF
digitalWrite(RELAY4, LOW); // set Fertilizer Pump 2 BLOOM OFF
digitalWrite(RELAY5, LOW); // set Fertilizer Pump 3 MICRO OFF
digitalWrite(RELAY6, LOW); // set Drain Valve OFF
digitalWrite(RELAY7, LOW); // set Water Pump OFF
pinMode(ultraSoundSignal, OUTPUT);
pinMode(ultraSoundSignalIN, INPUT);
Serial.begin(115200);
Serial.println("\Starting");
Blynk.begin(auth, ssid, pass, IPAddress(139, 59, 206, 133), 80);
int mytimeout = millis() / 1000;
WaterPumpStartMillis = millis(); //initial start time of Water Pump
while (Blynk.connect() == false) { // try to connect to server for 10 seconds
if ((millis() / 1000) > mytimeout + 8) { // try local server if not connected within 9 seconds
break;
}
}
rtc.begin();
timer.setInterval(10000L, activetoday); // check every 10 SECONDS if schedule should run today
timer.setInterval(30000L, reconnectBlynk); // check every 30s if still connected to server
timer.setInterval(5000L, clockvalue); // check value for time
timer.setInterval(5000L, sendWifi); // Wi-Fi singal
timer.setInterval(1000L, ultrasoundsensor_send); //ultrasoundsensorvalue
timer.setInterval(2000L, MoistureSensor); // Moisture Sensor Value
}
unsigned long ping()
{
pinMode(ultraSoundSignal, OUTPUT); // Switch signalpin to output
digitalWrite(ultraSoundSignal, LOW); // Send low pulse
delayMicroseconds(2); // Wait for 2 microseconds
digitalWrite(ultraSoundSignal, HIGH); // Send high pulse
delayMicroseconds(15); // Wait for 15 microseconds
digitalWrite(ultraSoundSignal, LOW); // Holdoff
pinMode(ultraSoundSignalIN, INPUT); // Switch signalpin to input
digitalWrite(ultraSoundSignalIN, HIGH); // Turn on pullup resistor
// please note that pulseIn has a 1sec timeout, which may
// not be desirable. Depending on your sensor specs, you
// can likely bound the time like this — marcmerlin
// echo = pulseIn(ultraSoundSignal, HIGH, 38000)
echo = pulseIn(ultraSoundSignalIN, HIGH); //Listen for echo
Serial.println(echo);
ultrasoundValue = (echo / 58.138); // * .39; //convert to CM then to inches
BLYNK_WRITE(V6) //Water Capacity Value Input
{
int capacityValueIn = param.asInt(); //Get value as integer
capacityAdjustValue = capacityValueIn * 100.00 / 10000.00; //Calculate User Water Capacity Adjusting Value
}
BLYNK_WRITE(V9) //Drain Time Value Calculations
{
int DrainTimeValueIn = param.asInt(); //Get value as integer
DrainValvePeriod = DrainTimeValueIn * 60000; //Calculate User Drain Time Value
}
BLYNK_WRITE(V24) //Water Pump ON Time Value Calculations
{
int WaterPumpONTimeValueIn = param.asInt(); //Get value as integer
WaterPumpUserONPeriod = WaterPumpONTimeValueIn * 60000; //Calculate User Water Pump ON Time Value
}
BLYNK_WRITE(V23) //Water Pump OFF Time Value Calculations
{
int WaterPumpOFFTimeValueIn = param.asInt(); //Get value as integer
WaterPumpUserOFFPeriod = WaterPumpOFFTimeValueIn * 60000; //Calculate User Water Pump OFF Time Value
}
BLYNK_WRITE(V12) //Water Level Full Distance Value Calculations
{
int WaterFullValueIn = param.asInt(); //Get value as integer
float WaterFullAdjustValue = WaterFullValueIn * 100.00 / 10000.00; //Calculate User Water Full Adjusting Value
WaterFullOutputValue = (WaterFullAdjustValue * 2860.00) * 2; //Calculate User Water Full Value (2860.00 = 50cm)
}
BLYNK_WRITE(V13) //Water Level Low Distance & Water Level Meter Adjust Values Calculations
{
int WaterLowValueIn = param.asInt(); //Get value as integer
float WaterLowAdjustValue = WaterLowValueIn * 100.00 / 10000.00; //Calculate User Water Low Adjusting Value
WaterLowOutputValue = (WaterLowAdjustValue * 2860.00) * 2; //Calculate User Water Low Value (2860.00 = 50cm)
WaterLevelMeterAdjustValue = (WaterLowOutputValue - WaterFullOutputValue) / 20; // Calculate Water Level Meter Adjust Value. 5% of the User Water Level Scale
}
BLYNK_WRITE(V14) //Vegetative State Fertilizer A (Grow) Value Calculations
{
int VegFertAValueIn = param.asInt(); //Get value as integer
float VegFertAAdjustValue = VegFertAValueIn * 100.00 / 10000.00; //Calculate User Vegetative State Fertilizer A (Grow) Adjusting Value
PumpAVPeriod = (VegFertAAdjustValue * 62000) * capacityAdjustValue; //Calculate User Vegetative State Fertilizer A (Grow) Output Value (62000 = 100Ml)
}
BLYNK_WRITE(V15) //Vegetative State Fertilizer B (Bloom) Value Calculations
{
int VegFertBValueIn = param.asInt(); //Get value as integer
float VegFertBAdjustValue = VegFertBValueIn * 100.00 / 10000.00; //Calculate User Vegetative State Fertilizer B (Bloom) Adjusting Value
PumpBVPeriod = (VegFertBAdjustValue * 62000) * capacityAdjustValue; //Calculate User Vegetative State Fertilizer B (Bloom) Output Value (62000 = 100Ml)
}
BLYNK_WRITE(V16) //Vegetative State Fertilizer C (Micro) Value Calculations
{
int VegFertCValueIn = param.asInt(); //Get value as integer
float VegFertCAdjustValue = VegFertCValueIn * 100.00 / 10000.00; //Calculate User Vegetative State Fertilizer C (Micro) Adjusting Value
PumpCVPeriod = (VegFertCAdjustValue * 62000) * capacityAdjustValue; //Calculate User Vegetative State Fertilizer C (Micro) Output Value (62000 = 100Ml)
}
//Bloom 1 State Fertilizer Calculations
BLYNK_WRITE(V17) //Bloom 1 State Fertilizer A (Grow) Value Calculations
{
int Bloom1FertAValueIn = param.asInt(); //Get value as integer
float Bloom1FertAAdjustValue = Bloom1FertAValueIn * 100.00 / 10000.00; //Calculate User Bloom 1 State Fertilizer A (Grow) Adjusting Value
PumpABPeriod = (Bloom1FertAAdjustValue * 62000) * capacityAdjustValue; //Calculate User Bloom 1 State Fertilizer A (Grow) Output Value (62000 = 100Ml)
}
BLYNK_WRITE(V18) //Bloom 1 State Fertilizer B (Bloom) Value Calculations
{
int Bloom1FertBValueIn = param.asInt(); //Get value as integer
float Bloom1FertBAdjustValue = Bloom1FertBValueIn * 100.00 / 10000.00; //Calculate User Bloom 1 State Fertilizer B (Bloom) Adjusting Value
PumpBBPeriod = (Bloom1FertBAdjustValue * 62000) * capacityAdjustValue; //Calculate User Bloom 1 State Fertilizer B (Bloom) Output Value (62000 = 100Ml)
}
BLYNK_WRITE(V19) //Bloom 1 State Fertilizer C (Micro) Value Calculations
{
int Bloom1FertCValueIn = param.asInt(); //Get value as integer
float Bloom1FertCAdjustValue = Bloom1FertCValueIn * 100.00 / 10000.00; //Calculate User Bloom 1 State Fertilizer C (Micro) Adjusting Value
PumpCBPeriod = (Bloom1FertCAdjustValue * 62000) * capacityAdjustValue; //Calculate User Bloom 1 State Fertilizer C (Micro) Output Value (62000 = 100Ml)
}
//Bloom 2 State Fertilizer Calculations
BLYNK_WRITE(V20) //Bloom 2 State Fertilizer A (Grow) Value Calculations
{
int Bloom2FertAValueIn = param.asInt(); //Get value as integer
float Bloom2FertAAdjustValue = Bloom2FertAValueIn * 100.00 / 10000.00; //Calculate User Bloom 2 State Fertilizer A (Grow) Adjusting Value
PumpABFPeriod = (Bloom2FertAAdjustValue * 62000) * capacityAdjustValue; //Calculate User Bloom 2 State Fertilizer A (Grow) Output Value (62000 = 100Ml)
}
BLYNK_WRITE(V21) //Bloom 2 State Fertilizer B (Bloom) Value Calculations
{
int Bloom2FertBValueIn = param.asInt(); //Get value as integer
float Bloom2FertBAdjustValue = Bloom2FertBValueIn * 100.00 / 10000.00; //Calculate User Bloom 2 State Fertilizer B (Bloom) Adjusting Value
PumpBBFPeriod = (Bloom2FertBAdjustValue * 62000) * capacityAdjustValue; //Calculate User Bloom 2 State Fertilizer B (Bloom) Output Value (62000 = 100Ml)
}
BLYNK_WRITE(V22) //Bloom 2 State Fertilizer C (Micro) Value Calculations
{
int Bloom2FertCValueIn = param.asInt(); //Get value as integer
float Bloom2FertCAdjustValue = Bloom2FertCValueIn * 100.00 / 10000.00; //Calculate User Bloom 2 State Fertilizer C (Micro) Adjusting Value
PumpCBFPeriod = (Bloom2FertCAdjustValue * 62000) * capacityAdjustValue; //Calculate User Bloom 2 State Fertilizer C (Micro) Output Value (62000 = 100Ml)
}
void sendUptime()
{
// 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.
Blynk.virtualWrite(V5, millis() / 1000);
Blynk.virtualWrite(V6, millis() / 1000);
}
BLYNK_CONNECTED() {
if (isFirstConnect) {
Blynk.syncAll();
Blynk.notify("TIMER STARTING!!!!");
isFirstConnect = false;
}
}
BLYNK_WRITE (V8) {
switch (param.asInt())
{
case 1: // Vegetative Mix
if (echo >= WaterLowOutputValue && echo < 9000)
{
VEGMachine();
}
else
{
if (WateringState == WateringON)
{
VEGMachine();
}
}
break;
case 2: // Bloom Mix
if (echo >= WaterLowOutputValue && echo < 9000)
{
BLOOMMachine();
}
else
{
if (WateringState == WateringON)
{
BLOOMMachine();
}
}
break;
case 3: // Bloom Final Mix
if (echo >= WaterLowOutputValue && echo < 9000)
{
BLOOMFMachine();
}
else
{
if (WateringState == WateringON)
{
BLOOMFMachine();
}
}
break;
case 4: // Plain Water Mix
if (echo >= WaterLowOutputValue && echo < 9000)
{
PLAINMachine();
}
else
{
if (WateringState == WateringON)
{
PLAINMachine();
}
}
break;
case 5: // Drain All And Stop Growing
DRAINMachine();
break;
default:
// if nothing else matches, do the default
// default is optional
//watermixVEG();
break;
}
}
void ultrasoundsensor_send()
{
int x = 0;
x = ping();
//Serial.println(x);
// delay(500); //delay 1/2 seconds.
}
void sendWifi() {
wifisignal = map(WiFi.RSSI(), -105, -40, 0, 100);
}
void clockvalue() // Digital clock display of the time
{
int gmthour = hour();
if (gmthour == 24) {
gmthour = 0;
}
String displayhour = String(gmthour, DEC);
int hourdigits = displayhour.length();
if (hourdigits == 1) {
displayhour = "0" + displayhour;
}
String displayminute = String(minute(), DEC);
int minutedigits = displayminute.length();
if (minutedigits == 1) {
displayminute = "0" + displayminute;
}
displaycurrenttimepluswifi = " Clock: " + displayhour + ":" + displayminute + " Signal: " + wifisignal + " %";
Blynk.setProperty(V3, "label", displaycurrenttimepluswifi);
}
void activetoday() { // check if schedule should run today
if (year() != 1970) {
if (uptoyou == 1) {
Blynk.syncVirtual(V10); // sync timeinput widget
}
}
}
void checklastbuttonpressed () {
if (uptoyou == 1) {
oldstatus = 5;
}
if (uptoyou == 0) {
oldstatus = 6;
}
}
void restorelastbuttonpressed () {
if (oldstatus == 5) {
uptoyou = 1;
Blynk.virtualWrite(V11, 1);
}
if (oldstatus == 6) {
uptoyou = 0;
Blynk.virtualWrite(V11, 0);
}
}
void resetTerminal()
{
terminal.println();
terminal.println();
terminal.println();
terminal.println("TIMER has been selected");
terminal.println("Wait for update (10 seconds as maximum)");
terminal.println();
//terminal.println();
//terminal.println();
terminal.flush();
}
void resetManual()
{
Blynk.virtualWrite(V1, 0); //Turn OFF Manual Mode Widget
Blynk.virtualWrite(V2, 0); //Turn OFF Button Widget Device
Blynk.virtualWrite(V7, 0); //Turn OFF virtual LED V7
digitalWrite(RELAY2, LOW); // set LIGHTS OFF
}
BLYNK_WRITE(V2) // ON-OFF Manual
{
if (param.asInt() == 1) { //
terminal.println();
terminal.println();
terminal.println();
terminal.println("Manual MODE is ON");
terminal.println("Press ON/OFF button if required");
terminal.println("Lights Are ON");
terminal.println();
// terminal.println();
terminal.flush();
if (manual == 0) {
checklastbuttonpressed ();
manual = 1;
uptoyou = 0;
Blynk.virtualWrite(V11, 0);
Blynk.virtualWrite(V7, 1023); //Turn ON virtual LED V7
digitalWrite(RELAY2, HIGH); // set LIGHTS ON
Blynk.virtualWrite(V2, 1); //Turn ON Button Widget
} else { //está en modo manual
uptoyou = 0;
Blynk.virtualWrite(V11, 0);
Blynk.virtualWrite(V7, 1023); //Turn ON virtual LED V7
digitalWrite(RELAY2, HIGH); // set LIGHTS ON
Blynk.virtualWrite(V2, 1); //Turn ON Button Widget
}
} else {
terminal.println();
terminal.println();
terminal.println();
terminal.println("Manual MODE is ON");
terminal.println("Press ON/OFF button if required");
terminal.println("Light Are OFF");
terminal.println();
//terminal.println();
terminal.flush();
if (manual == 0) { //modo automático
checklastbuttonpressed ();
manual = 1;
uptoyou = 0;
Blynk.virtualWrite(V11, 0);
Blynk.virtualWrite(V7, 0); //Turn OFF virtual LED V7
digitalWrite(RELAY2, LOW); // set LIGHTS OFF
Blynk.virtualWrite(V2, 0); //Turn OFF Button Widget
} else {
uptoyou = 0;
Blynk.virtualWrite(V11, 0);
Blynk.virtualWrite(V7, 0); //Turn OFF virtual LED V7
digitalWrite(RELAY2, LOW); // set LIGHTS OFF
Blynk.virtualWrite(V2, 0); //Turn OFF Button Widget
}
}
}
BLYNK_WRITE(V0) // ON-OFF Water Pump
{
if (param.asInt() == 1) {
terminal.println();
terminal.println();
terminal.println();
terminal.println();
terminal.println();
terminal.println("Water Pump Manual ON");
terminal.println();
// terminal.println();
terminal.flush();
Serial.println("Water Pump Manual ON");
LeakState = WaterLeakNO;
WaterPumpState = WaterPumpON;
} else {
terminal.println();
terminal.println();
terminal.println();
terminal.println();
terminal.println();
terminal.println("Water Pump Manual OFF");
terminal.println();
//terminal.println();
terminal.flush();
Serial.println("Water Pump Manual OFF");
WaterPumpState = WaterPumpOFF;
}
}
BLYNK_WRITE(V4) // ON-OFF Main Watering
{
if (param.asInt() == 1) { // boton encendido
terminal.println();
terminal.println();
terminal.println();
terminal.println();
terminal.println();
terminal.println("Main Watering ON");
terminal.println();
// terminal.println();
terminal.flush();
Serial.println("Main Watering ON");
MainWateringState = MainWateringON;
} else {
terminal.println();
terminal.println();
terminal.println();
terminal.println();
terminal.println();
terminal.println("Main Watering OFF");
terminal.println();
//terminal.println();
terminal.flush();
digitalWrite(RELAY1, LOW); //change the state of the Main Valve to Closed
// Serial.println("Main Valve Closed");
MainValveState = LOW;
digitalWrite(RELAY6, LOW); //change the state of the Drain Valve to Closed
DrainValveState = LOW;
digitalWrite(RELAY3, LOW); //change the state of the Fertilizer Pump 1 (GROW) to Closed
PumpAState = LOW;
digitalWrite(RELAY4, LOW); //change the state of the Fertilizer Pump 2 (BLOOM) to Closed
PumpBState = LOW;
digitalWrite(RELAY5, LOW); //change the state of the Fertilizer Pump 3 (MICRO) to Closed
PumpCState = LOW;
Serial.println("All Fertilizer Pumps + Drain & Main Valves Closed");
WateringState = WateringOFF;
BLOOMState = IsBLOOMWaterNeeded;
BLOOMFState = IsBLOOMFWaterNeeded;
VEGState = IsVEGWaterNeeded;
PLAINState = IsPLAINWaterNeeded;
DRAINState = IsDRAINWaterNeeded;
Serial.println("Main Watering OFF");
MainWateringState = MainWateringOFF;
}
}
BLYNK_WRITE(V5) // ON-OFF WaterLeak Sensor
{
if (param.asInt() == 1) { // boton encendido
terminal.println();
terminal.println();
terminal.println();
terminal.println();
terminal.println();
terminal.println("Leak Sensor Is ON");
terminal.println();
// terminal.println();
terminal.flush();
LeakSensorState = LeakSensorON;
} else {
terminal.println();
terminal.println();
terminal.println();
terminal.println();
terminal.println();
terminal.println("Leak Sensor Is OFF");
terminal.println();
//terminal.println();
terminal.flush();
LeakSensorState = LeakSensorOFF;
}
}
BLYNK_WRITE(V11) // Up to you selected
{
if (param.asInt() == 1) {
timer.setTimeout(50, resetTerminal);
timer.setTimeout(50, resetManual);
timer.setTimeout(50, checklastbuttonpressed);
uptoyou = 1;
} else {
uptoyou = 0;
}
}
BLYNK_WRITE(V10)//Up to you
{
if (uptoyou == 1) {
sprintf(Date, "%02d/%02d/%04d", day(), month(), year());
sprintf(Time, "%02d:%02d:%02d", hour(), minute(), second());
TimeInputParam t(param);
terminal.print("TIMER Checked schedule at: ");
terminal.println(Time);
terminal.flush();
int dayadjustment = -1;
if (weekday() == 1) {
dayadjustment = 6; // needed for Sunday, Time library is day 1 and Blynk is day 7
}
if (t.isWeekdaySelected(weekday() + dayadjustment)) { //Time library starts week on Sunday, Blynk on Monday
terminal.println("TIMER ACTIVE today");
terminal.flush();
if (t.hasStartTime()) // Process start time
{
terminal.println(String("Start: ") + t.getStartHour() + ":" + t.getStartMinute());
terminal.flush();
}
if (t.hasStopTime()) // Process stop time
{
terminal.println(String("Stop : ") + t.getStopHour() + ":" + t.getStopMinute());
terminal.flush();
}
// Display timezone details, for information purposes only
terminal.println(String("Time zone: ") + t.getTZ()); // Timezone is already added to start/stop time
terminal.println("At least ONE day MUST be selected");
// terminal.println(String("Time zone offset: ") + t.getTZ_Offset()); // Get timezone offset (in seconds)
terminal.flush();
for (int i = 1; i <= 7; i++) { // Process weekdays (1. Mon, 2. Tue, 3. Wed, ...)
if (t.isWeekdaySelected(i)) {
terminal.println(String("Day ") + i + " is selected");
terminal.flush();
}
}
nowseconds = ((hour() * 3600) + (minute() * 60) + second());
startsecondswd = (t.getStartHour() * 3600) + (t.getStartMinute() * 60);
//Serial.println(startsecondswd); // used for debugging
if (nowseconds >= startsecondswd) {
terminal.print("TIMER STARTED at");
terminal.println(String(" ") + t.getStartHour() + ":" + t.getStartMinute());
terminal.flush();
if (nowseconds <= startsecondswd + 90) { // 90s on 60s timer ensures 1 trigger command is sent
Blynk.virtualWrite(V7, 1023); //Turn ON virtual LED V7
digitalWrite(RELAY2, HIGH); // set LIGHTS ON
Blynk.virtualWrite(V2, 0); //Turn OFF Button Widget
// code here to switch the relay ON
}
}
else {
terminal.println("Lights Are OFF");
terminal.flush();
}
stopsecondswd = (t.getStopHour() * 3600) + (t.getStopMinute() * 60);
//Serial.println(stopsecondswd); // used for debugging
if (nowseconds >= stopsecondswd) {
Blynk.virtualWrite(V7, 0); //Turn OFF virtual LED V7
digitalWrite(RELAY2, LOW); // set LIGHTS OFF
Blynk.virtualWrite(V2, 0);
terminal.print("TIMER STOPPED at");
terminal.println(String(" ") + t.getStopHour() + ":" + t.getStopMinute());
terminal.flush();
if (nowseconds <= stopsecondswd + 90) { // 90s on 60s timer ensures 1 trigger command is sent
Blynk.virtualWrite(V7, 0); //Turn OFF virtual LED V7
digitalWrite(RELAY2, LOW); // set LIGHTS OFF
Blynk.virtualWrite(V2, 0);
// code here to switch the relay OFF
}
}
else {
if (nowseconds >= startsecondswd) {
Blynk.virtualWrite(V7, 0); //Turn OFF virtual LED V7
digitalWrite(RELAY2, HIGH); // set LIGHTS ON
Blynk.virtualWrite(V2, 0);
terminal.println("Lights Are ON");
terminal.flush();
}
}
}
else {
terminal.println("TIMER INACTIVE today");
terminal.flush();
// nothing to do today, check again in 30 SECONDS time
}
terminal.println();
}
}
void reconnectBlynk() {
if (!Blynk.connected()) {
if (Blynk.connect()) {
BLYNK_LOG("Reconnected");
} else {
BLYNK_LOG("Not reconnected");
}
}
}
void WaterPump() //function to ON/OFF Water Pump
{
if ((WaterPumpUserState == WaterPumpUserON) && (WaterPumpCurrentMillis - WaterPumpStartMillis >= WaterPumpUserONPeriod)) //test whether the period has elapsed
{
WaterPumpUserState = WaterPumpUserOFF;
digitalWrite(RELAY7, LOW); //if so, change the state of the Water Pump to OFF
WaterPumpStartMillis = WaterPumpCurrentMillis; //IMPORTANT to save the start time of the current RELAY state.
Serial.println("Water Pump OFF");
terminal.println();
terminal.println();
terminal.println();
terminal.println();
terminal.println();
terminal.println("Water Pump Turned OFF");
terminal.println();
// terminal.println();
terminal.flush();
}
else if ((WaterPumpUserState == WaterPumpUserOFF) && (WaterPumpCurrentMillis - WaterPumpStartMillis >= WaterPumpUserOFFPeriod)) //test whether the period has elapsed
{
WaterPumpUserState = WaterPumpUserON;
digitalWrite(RELAY7, HIGH); //if so, change the state of the Water Pump to ON
WaterPumpStartMillis = WaterPumpCurrentMillis; //IMPORTANT to save the start time of the current RELAY state.
Serial.println("Water Pump ON");
terminal.println();
terminal.println();
terminal.println();
terminal.println();
terminal.println();
terminal.println("Water Pump Turned ON");
terminal.println();
// terminal.println();
terminal.flush();
}
}
void MoistureSensor()
{
sensorValue = analogRead(water_sensor_a);
Serial.println(sensorValue);
if (sensorValue >= 50 && sensorValue < 900)
}
}
if (sensorValue >= 900 && sensorValue < 1300)
{
if (LeakSensorState == LeakSensorON) {
// if (LeakStateNotifyState == LeakStateNotifyUnDone) {
if (LeakState == WaterLeakYES)
{
terminal.print("No Water Leak Found Anymore.");
Blynk.notify("No Water Leak Found Anymore");
Serial.print("No Water Leak Found Anymore.");
Blynk.syncVirtual(V0);
// LeakStateNotifyState = LeakStateNotifyDone;
LeakState = WaterLeakNO;
}
}
}
}
void loop()
{
if (Blynk.connected()) {
Blynk.run();
}
timer.run();
if (WaterPumpState == WaterPumpON) {
if (WateringState == WateringOFF) {
WaterPumpCurrentMillis = millis(); //get the current time of Water Pump
WaterPump();
}
if (WateringState == WateringON) {
Blynk.syncVirtual(V8);
}
}
}
```cpp