Create a timer for the notifications

Yes, Sorry was just copying and pasting from the posted code

1 Like

uh, yep i miss that.
But the connection problem? Do you have any solution?

What is the connection problem? It wont connect to BLYNK, or that you must keep Ethernet cable plugged it?

1 Like

from another thread with similar issue:

Blynk.begin() is a blocking routine… no connection no run.

Try Blynk.config() with prior WiFi connection and reconnection routines…

http://docs.blynk.cc/#blynk-firmware-configuration

1 Like

The problem is that i NEED an internet connection to let the circuit work. Even the sensors values on the display lcd doesn’t work without the connection. I would like a circuit that coul work fine even if the internet drops

This was taken from @Gunner’s code examples found HERE

change your void loop() to this

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);
      Blynk.connect();  // Try to reconnect to the server
    });  // END Timer Function
  }
}

and add this routine

BLYNK_CONNECTED() {
  Serial.println("Cconnected");
  ReCnctCount = 0;
}

and add these variables

int ReCnctFlag;  // Reconnection Flag
int ReCnctCount = 0;  // Reconnection counter
1 Like

Ok, let’s try

Nothing change…
is this ok?

LiquidCrystal lcd(2, 3, 4, 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[] = "b92ca63401f34dd98ef80930c0899e4f";
BlynkTimer timer;
#define W5100_CS  10
#define SDCARD_CS 4
int ReCnctFlag;  // Reconnection Flag
int ReCnctCount = 0;  // Reconnection counter

int livelloacqua = 0;
int umiditaterra = 0;
int notifica = false;
#define LEDV1 8
#define LEDV2 9
#define LEDR1 11
#define LEDR2 12

void setup() {
  // Debug console
  Serial.begin(9600);
  pinMode(SDCARD_CS, OUTPUT);
  digitalWrite(SDCARD_CS, HIGH); // Deselect the SD card
  Blynk.begin(auth);
  // You can also specify server:
  //Blynk.begin(auth, "blynk-cloud.com", 80);
  //Blynk.begin(auth, IPAddress(192,168,1,100), 8080);
  // For more options, see Boards_Ethernet/Arduino_Ethernet_Manual example

  lcd.begin(16, 2);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  lcd.clear();
  timer.setInterval(400L, programma);
}
BLYNK_CONNECTED() {
  Serial.println("Cconnected");
  ReCnctCount = 0;
}

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!");
    Blynk.tweet("Arduino ha sete!");
    notifica = true;
  }
  if ((livelloacqua >= 160) && (notifica == true)) {
    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(12, HIGH);
  }
  if (umiditaterra < 500) {
    lcd.setCursor(0, 1);
    lcd.print("Pianta OKAY         ");
    digitalWrite(8, HIGH);
    digitalWrite(9, HIGH);
    digitalWrite(11, LOW);
    digitalWrite(12, LOW);
  }
}


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);
      Blynk.connect();  // Try to reconnect to the server
    });  // END Timer Function
  }
}

Oops looks like I forgot something in setup(), change it to this

void setup() {
  // Debug console
  Serial.begin(9600);
  pinMode(SDCARD_CS, OUTPUT);
  digitalWrite(SDCARD_CS, HIGH); // Deselect the SD card
  Blynk.config(auth);
  Blynk.connect();

  lcd.begin(16, 2);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  lcd.clear();
  timer.setInterval(1000L, programma); // run programma every 1 second
}
1 Like

IT WORKS! ah ah, this is the project for my final school exam and you are literally saving my life! Thank you so much :slight_smile:

There is only another little thing to make it perfect. When i receive the notification of the low water level then, that’s it until i refill the water. Instead i would like to receive a notification every 20 minutes or so even if i didn’t refill the water and the level is still low

Where do you go to school?? Battle Royale? :rofl:

2 Likes

:joy:

have a look here:

as soon as you set the flag notification initiate a timer that sends you an notification every 20minutes.

1 Like

Since this is a school project I will leave this final part for you to implement, but basically just set up another timed routine that every 20 minutes checks the sensor and sends a notification if necessary. I wouldn’t increase the routine that controls the pump as it may cause the plant to flood.

@wolph42’s suggestion will work as well, and is probably a little “cleaner”, but will require a better understanding of the timer. As you will need to enable/disable the timer.

The BLYNK timer is the same as SimpleTimer, so if you need more information on how to use the times, and the different types, check out THIS.

If you get stuck, post your code with your attempt and we will see if we can get you going in the right direction.

1 Like

Nnnnope, the all thing was working offline but with this changes it doesn’t connect to internet anymore and i didn’t understand why

LiquidCrystal lcd(2, 3, 4, 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[] = "b92ca63401f34dd98ef80930c0899e4f";
BlynkTimer timer;
#define W5100_CS  10
#define SDCARD_CS 4

int livelloacqua = 0;
int umiditaterra = 0;
int notifica = false;
#define LEDV1 9
#define LEDV2 10
#define LEDR1 11
#define LEDR2 12
int ReCnctFlag;  // Reconnection Flag
int ReCnctCount = 0;  // Reconnection counter

void setup() {
  // Debug console
  Serial.begin(9600);
  pinMode(SDCARD_CS, OUTPUT);
  digitalWrite(SDCARD_CS, HIGH); // Deselect the SD card
  Blynk.config(auth);
  Blynk.connect();

  lcd.begin(16, 2);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  lcd.clear();
  timer.setInterval(400L, programma); // run programma every 1 second
}
BLYNK_CONNECTED() {
  Serial.println("Cconnected");
  ReCnctCount = 0;
}

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) && (notifica == true)) {
    lcd.setCursor(0, 0);
    lcd.print("Acqua OK            ");
    notifica=false;
  }

  umiditaterra = analogRead(A1);
  if (umiditaterra > 500) {
    lcd.setCursor(0, 1);
    lcd.print("Annaffiare          ");
    digitalWrite(9, LOW);
    digitalWrite(10, LOW);
    digitalWrite(11, HIGH);
    digitalWrite(12, HIGH);
  }
  if (umiditaterra < 500) {
    lcd.setCursor(0, 1);
    lcd.print("Pianta OKAY         ");
    digitalWrite(9, HIGH);
    digitalWrite(10, HIGH);
    digitalWrite(11, LOW);
    digitalWrite(12, LOW);
  }
}


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);
      Blynk.connect();  // Try to reconnect to the server
    });  // END Timer Function
  }
}

I don’t see any additional timer for notification in your posted code.

I do see something that is perhaps flawed, but difficult to wrap my head around, this part:

 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);
      Blynk.connect();  // Try to reconnect to the server
    });  // END Timer Function

have you worked through the logic of ReCnctFlag when its activated and deactivated, in my head this goes wrong, but I could be wrong of course.
EDIT, no in hind sight, it looks ok.

1 Like

concerning logic, I don’t get this one either. IMO this should be

if ((livelloacqua &gt;= 130)) {
   lcd.setCursor(0, 0);
   lcd.print("Acqua OK            ");
   notifica = false;
 }

note that there was also a typo in the code (but that was already corrected)

1 Like

Nope, i didn’t work at the timer because i found this problem in the sketch that is way more important. In this way i can use the circuit offline but it simply doesn’t conncet anymore. I tried to search online but i can’t find a solution. The problem are the reconnect strings obviusly…any idea?

so what did you change? and if you revert the change, does it connect again? Have you also rebooted you router?

1 Like

No , i didn’t change anything. I worked on this sketch all day yesterday and when i did the last modify that @Toro_Blanco said i only checked that the circuit worked offline and no with the ethernet plugged in. My mistake .

I just tried to change the blynk token (just in case) and reboot the route but nothing changed.
If i don’t put the last lines for the reconnection the circuit connect again, yes

Did you give it some time to reconnect? The function tries every 30 seconds, not instantly.

The code was taken from @Gunner’s example. Maybe he can lend some insight as to why it may not be working. I suspect he tested it quite a bit as well before he posted it, and I have used it successfully in one of my projects. Maybe it doesn’t work for Ethernet and is only for WiFi. I do not have the hardware necessary to test this.

1 Like