This is the final sketch that works. I tried to make the adjustment to check the water level every 20 minutes (i set to 1 second to test it), I made a new “void checknotifica” with his timer… is it ok?
The sketch:
LedControl lc = LedControl(22, 24, 23, 1); // Pins: DIN,CLK,CS, # of Display connected
#include <LiquidCrystal.h> // Includi libreria Display LCD
LiquidCrystal lcd(2, 3, 30, 5, 6, 7); // Dichiara Pin Display LCD
#define BLYNK_PRINT Serial
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "a21d67306eb74d609d85d130b284e443";
BlynkTimer timer;
#define W5100_CS 10
#define SDCARD_CS 4
int livelloacqua = 0;
int umiditaterra = 0;
int notifica = false;
#define LEDV1 8
#define LEDV2 9
#define LEDR1 11
#define LEDR2 32
int ReCnctFlag; // Reconnection Flag
int ReCnctCount = 0; // Reconnection counter
unsigned long delayTime = 200; // Delay between Frames
byte mac[] = {
0xC8, 0x60, 0x00, 0x49, 0xA2, 0xEE
};
// Put values in arrays
byte felice[] =
{
B00111100,
B01000010,
B10100101,
B10000001,
B10100101,
B10011001,
B01000010,
B00111100
};
byte triste[] =
{
B00111100,
B01000010,
B10100101,
B10000001,
B10011001,
B10100101,
B01000010,
B00111100
};
void setup() {
// Debug console
Serial.begin(9600);
pinMode(SDCARD_CS, OUTPUT);
digitalWrite(SDCARD_CS, HIGH); // Deselect the SD card
Ethernet.begin(mac);//add this
Blynk.config(auth, "blynk-cloud.com", 8080);
Blynk.connect();
lcd.begin(16, 2);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(11, OUTPUT);
pinMode(32, OUTPUT);
lcd.clear();
lc.shutdown(0, false); // Wake up displays
lc.shutdown(1, false);
lc.setIntensity(0, 5); // Set intensity levels
lc.setIntensity(1, 5);
lc.clearDisplay(0); // Clear Displays
lc.clearDisplay(1);
timer.setInterval(400L, programma); // run programma every 400 millisecond
timer.setInterval(1000L, checknotifica); // run programma every second
}
BLYNK_CONNECTED() {
Serial.println("Cconnected");
ReCnctCount = 0;
}
// Take values in Arrays and Display them
void sfelice()
{
for (int i = 0; i < 8; i++)
{
lc.setRow(0, i, felice[i]);
}
}
void striste()
{
for (int i = 0; i < 8; i++)
{
lc.setRow(0, i, triste[i]);
}
}
void checknotifica () {
livelloacqua = analogRead(A0);
if (livelloacqua < 160) {
lcd.setCursor(0, 0);
lcd.print("Riempire acqua ");
Blynk.notify("L'acqua è in esaurimento! Devi riempirla!");
}
if (livelloacqua >= 130) {
lcd.setCursor(0, 0);
lcd.print("Acqua OK ");
}
}
void programma() {
livelloacqua = analogRead(A0);
Blynk.virtualWrite(V1, livelloacqua);
umiditaterra = analogRead(A1);
Blynk.virtualWrite(V2, umiditaterra);
lcd.setCursor(0, 0);
livelloacqua = analogRead(A0);
if ((livelloacqua < 160 && !notifica)) {
lcd.setCursor(0, 0);
lcd.print("Riempire acqua ");
Blynk.notify("L'acqua è in esaurimento! Devi riempirla!");
notifica = true;
}
if (livelloacqua >= 130) {
lcd.setCursor(0, 0);
lcd.print("Acqua OK ");
notifica = false;
}
umiditaterra = analogRead(A1);
if (umiditaterra > 500) {
lcd.setCursor(0, 1);
lcd.print("Annaffiare ");
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(11, HIGH);
digitalWrite(32, HIGH);
striste();
delay(100);
}
if (umiditaterra < 500) {
lcd.setCursor(0, 1);
lcd.print("Pianta OKAY ");
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
digitalWrite(11, LOW);
digitalWrite(32, LOW);
sfelice();
delay(100);
}
}
void loop() {
timer.run();
if (Blynk.connected()) { // If connected run as normal
Blynk.run();
} else if (ReCnctFlag == 0) { // If NOT connected and not already trying to reconnect, set timer to try to reconnect in 30 seconds
ReCnctFlag = 1; // Set reconnection Flag
Serial.println("Starting reconnection timer in 30 seconds...");
timer.setTimeout(30000L, []() { // Lambda Reconnection Timer Function
ReCnctFlag = 0; // Reset reconnection Flag
ReCnctCount++; // Increment reconnection Counter
Serial.print("Attempting reconnection #");
Serial.println(ReCnctCount);
Ethernet.begin(mac);
Blynk.connect(); // Try to reconnect to the server
}); // END Timer Function
}
}