Yes Sir! You have all the rights to suggest and make modifications to my sketch
therefore, I did all the changes you suggest. Here is the full code of the project:
Thanks again!
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <TimeLib.h>
#include <WidgetRTC.h>
#include <HTTPClient.h>
#include <WifiFHD.h>
BlynkTimer timer;
WidgetRTC rtc;
char auth[] = "xxxxxxxxxxxxxxx";
#define Bucket_Size 0.3 // rain bucket size (0.3mm) - cat ai masurat tu la un click
volatile unsigned long contactTime; // Timer to manage any contact bounce in interrupt routine
unsigned long intervalrain1h=3600000; // 1h timer for reset rain1h counter.
unsigned long previousMillisrain1h=0; // 1h timer for reset rain1h counter.
int releu = 13; // pin releu senzor ploaie.
const int butonmanual = 12; //buton manual ON/OFF la pin 12
//variabile
int tipCount1h, tipCount24h, manualrequest, resetrain;
float rain1h, rain24h;
unsigned long counter24h;
unsigned long counter48h;
unsigned long milisec24h = 86400000;
unsigned long milisec48h = 172800000;
unsigned long difcontor24h;
unsigned long diftimer24h;
unsigned long time24h;
unsigned long difcontor48h;
unsigned long diftimer48h;
unsigned long time48h;
unsigned long contor24h;
unsigned long contor48h;
int precip24h, precip48h;
int buttonpressed;
int iora, iminut, isecunda, izi, iluna, ian;
int senzor, starereleu;
int yy, cyear, cmonth, cday, chour, cminute, cseconds, httpCode;
char memwstemp[4];
char memwsws[4];
char memwswd[4];
int memwshum;
float memwspressure;
char memwstempchar[5];
char memwswschar[5];
String memwswdchar;
char rain1hchar[5];
char rain24hchar[5];
String wswd;
char buf[5];
float fwstemp, fwsws;
String wstemp;
String wshum;
String wspressure;
String wsws;
String valsenzor;
// Digital clock display of the time // functia de clock.
void clockDisplay()
{
// You can call hour(), minute(), ... at any time
// Please see Time library examples for details
String ora = String(hour());
String minut = String(minute());
String secunda = String(second());
String zi = String(day());
String luna = String(month());
String an = String(year());
iora = ora.toInt();
iminut = minut.toInt();
isecunda = secunda.toInt();
izi = zi.toInt();
iluna = luna.toInt();
ian = an.toInt();
}
BLYNK_CONNECTED() {
// Synchronize time on connection
rtc.begin();
}
void IRAM_ATTR get_rain() { // functia care incrementeaza variabilele tipcount1h si tipcount24h atunci cand face click parghia
if((millis() - contactTime) > 30 ) { // debounce of sensor signal
tipCount1h += 1;
tipCount24h += 1;
contactTime = millis();
}
}
void setup() {
Serial.begin(9600);
pinMode(33, INPUT_PULLUP); // senzor ploaie la D33 (intrerupt pin).
attachInterrupt(digitalPinToInterrupt(33), get_rain, FALLING);
pinMode(releu, OUTPUT); // releu senzor ploaie
pinMode(butonmanual, OUTPUT);
digitalWrite(butonmanual, HIGH);
setSyncInterval(10 * 60); // set rtc Sync interval in seconds (10 minutes)
// Display digital clock every 10 seconds
timer.setInterval(10000L, clockDisplay);
timer.setInterval(60000L, get_ws_data);
timer.setInterval(1000L, MyFunction);
// conectarea la wifi ....
WiFi.mode(WIFI_STA);
WiFi.setSleep(false);
WiFi.enableSTA(true);
WiFi.setAutoConnect(true);
WiFi.setAutoReconnect(true);
Blynk.begin(auth, ssid, pass, "myserver", 8088); // !!!!! change that for your Blynk server in cloud!!
}
BLYNK_WRITE(V4) {
if (param.asInt() == 1) {
digitalWrite(releu, LOW);
buttonpressed = 3;
} else {
digitalWrite(releu, HIGH);
buttonpressed = 2;
}
}
BLYNK_WRITE(V6) {
if (param.asInt() == 1) {
resetrain = 1;
} else { // If button state is 0 (LOW) then...
resetrain = 0;
}
}
void MyFunction()
{
rain1h = tipCount1h * Bucket_Size; //calculate 1h rain.
rain24h = tipCount24h * Bucket_Size; // calculate 24h rain.
if ((unsigned long)(millis() - previousMillisrain1h) >= intervalrain1h) { // reset 1h rain
previousMillisrain1h = millis();
tipCount1h = 0;
}
if ((iora == 23 && iminut == 59)) // reset rain at the end of the day
{
tipCount1h = 0;
tipCount24h = 0;
}
// conditions for rain volume high, not to start irrigation
if ((rain24h >=3.3) && (rain24h < 7)) {
precip24h = 1;
}
if (rain24h >=7) {
precip48h = 1;
}
if (precip24h == 1)
{
counter24h = counter24h + 1;
senzor = 1;
}
if (precip48h == 1)
{
counter48h = counter48h + 1;
senzor = 1;
}
if ((counter24h >= 2) && (counter24h <10))
{
contor24h = millis();
}
if (counter24h > 1)
{
difcontor24h = millis() - contor24h;
diftimer24h = milisec24h - difcontor24h;
time24h = diftimer24h / 60000;
valsenzor = "ON";
}
if (time24h < 0) {
counter24h = 0;
precip24h = 0;
senzor = 0;
difcontor24h = 0;
diftimer24h = 0;
time24h = 0;
}
if ((counter48h >= 2) && (counter48h <10))
{
contor48h = millis();
}
if (counter48h > 1)
{
difcontor48h = millis() - contor48h;
diftimer48h = milisec48h - difcontor48h;
time48h = diftimer48h / 60000;
valsenzor = "ON";
}
if (time48h < 0) {
counter48h = 0;
precip48h = 0;
senzor = 0;
difcontor48h = 0;
diftimer48h = 0;
time48h = 0;
}
if ((counter48h != 0) || (counter24h !=0)) valsenzor = "OFF";
if ((buttonpressed == 3) || (buttonpressed == 0))
{
if (senzor == 1) digitalWrite(releu, HIGH);
if (senzor == 0) digitalWrite(releu, LOW);
}
if (buttonpressed == 2) digitalWrite(releu, HIGH);
dtostrf(rain1h, 4, 1, rain1hchar);
dtostrf(rain24h, 4, 1, rain24hchar);
if (resetrain == 1) // reset rain from manual reset rain button
{
tipCount1h = 0;
tipCount24h = 0;
counter24h = 0;
precip24h = 0;
senzor = 0;
difcontor24h = 0;
diftimer24h = 0;
time24h = 0;
counter48h = 0;
precip48h = 0;
senzor = 0;
difcontor48h = 0;
diftimer48h = 0;
time48h = 0;
}
Blynk.virtualWrite(V0, rain1hchar);
Blynk.virtualWrite(V1, rain24hchar);
Blynk.virtualWrite(V2, time24h);
Blynk.virtualWrite(V3, time48h);
Blynk.virtualWrite(V10, memwstempchar);
Blynk.virtualWrite(V11, memwswschar);
Blynk.virtualWrite(V22, wswd);
Blynk.virtualWrite(V8, wshum);
Blynk.virtualWrite(V9, wspressure);
Blynk.virtualWrite(V13, valsenzor);
starereleu = digitalRead(releu);
if (starereleu == HIGH) Blynk.virtualWrite(V4, LOW); // send back to Blynk relay state
if (starereleu == LOW) Blynk.virtualWrite(V4, HIGH); // send back to Blynk relay state
if (starereleu == HIGH) Blynk.virtualWrite(V5, LOW); // send back to Blynk relay state
if (starereleu == LOW) Blynk.virtualWrite(V5, HIGH); // send back to Blynk relay state
}
void get_ws_data()
{
HTTPClient http;
http.begin("http://home.adrianc.ro/weatherstation/readtemp.php"); //Specify the URL
httpCode = http.GET();
wstemp = http.getString();
Serial.println(wstemp);
http.end();
fwstemp = wstemp.toFloat() ;
dtostrf(fwstemp, 4, 1, memwstempchar);
http.begin("http://home.adrianc.ro/weatherstation/readhum.php"); //Specify the URL
httpCode = http.GET();
wshum = http.getString();
Serial.println(wshum);
http.end();
http.begin("http://home.adrianc.ro/weatherstation/readpressure.php"); //Specify the URL
httpCode = http.GET();
wspressure = http.getString();
Serial.println(wspressure);
http.end();
http.begin("http://home.adrianc.ro/weatherstation/readws.php"); //Specify the URL
httpCode = http.GET();
wsws = http.getString();
Serial.println(wsws);
http.end();
fwsws = wsws.toFloat() ;
dtostrf(fwsws, 4, 1, memwswschar);
http.begin("http://home.adrianc.ro/weatherstation/readwd.php"); //Specify the URL
httpCode = http.GET();
wswd = http.getString();
Serial.println(wswd);
http.end();
wswd.trim();
}
void loop()
{
Blynk.run();
timer.run();
}