Create a timer for the notifications

You CANNOT use GPIO 10 when using the Ethernet Shield.

It looks like 11, 12, 13, 50, 51, 52 and 53 may have restrictions as well.

I would re-wire your hardware and avoid using these pins.

Yeah, i changed the pin but simply i didn’t post another time the sketch.
Even with the 12, 12, 10 and 4 free it doesn’t connect.
This is the actual sketch, i also added a Led Matrix 8x8 to make a smile

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

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

I would be going the other direction if I were you. That is, take stuff out until it works, then start adding stuff back in. You need to determine what is causing the connection issue first.

Try starting with a very basic sketch that will connect and slowly add the other stuff in, checking each time you add something.

Ok so you don’t think that the problem is the lines for the reconnection? if i delete that part of the sketch he circuit doesn’t work offline but perfectly with the ethernet

OK, then that may be the issue.

@Gunner, do you have any insight on this as the re-connection portion was taken from your examples. Does it only work for WiFi, or are there additional lines of cede needed for Ethernet?

Post the code you use without the re-connection stuff.

Tested and, just with ethernet, everything’s good:

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
unsigned long delayTime = 200; // Delay between Frames

// 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
  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(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);
}

//  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 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(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() {
  Blynk.run();
  timer.run();
}

In setup(), try changing

Blynk.begin(auth);

to

Blynk.config(auth);
Blynk.connect();

I am curious to see if this still works. To be honest, I am not certain on these topics and it may require a bit of trial and error to get it going

1 Like

Nope, adding that line nothing works, offline or online

Ok after more digging. I think I found something. You need to add the mac address.

Add this near you variable declarations, but use the mac address for your Ethernet Shield

byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};

And change/add this in the setup()

Ethernet.begin(mac);//add this
Blynk.config(auth, "blynk-cloud.com", 8080);
Blynk.connect();
1 Like

Ok so, i upload the sketch with the reconnection-thing adding this 3 lines and the mac address. Now the circuit go online just for a few seconds, send the notification and then disconnect and re-connect again all by itself with the ethernet connected. BUT all the circuit, without the ethernet, doesn’t work :frowning:

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

Does it trigger the re-connection stuff? That is, do the serial prints show up for the re-connection on the monitor?

Also, what does the monitor show when it disconnects? Screenshot?

What about if you leave the re-connection stuff out of loop(). That is just have timer.run and blynk.run in the loop().

1 Like

Where should i put that lines? In the setup?

What if to do the reconnection i use an interrupt instead of the if and the flag?

Don’t think that is how it works. The IF checks if it is connected then takes appropriate action.

1 Like

Everything the same as the last code you posted, except…

void loop() 
{
timer.run();
Blynk.run();
}

With the Ethernet cable plugged in.

I know it wont work with the Ethernet cable unplugged, just want to see if it stays connected

1 Like

Yes, exactly like you said. Unplugged nothing but now the connection doesn’t drop like before and online everything fine

Try this, and let me know what the serial prints show.

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
  }
}
1 Like

Nope, nothing. I bought an ESP8266-01 model (https://www.amazon.it/SunFounder-ESP8266-Wireless-Transceiver-Mega2560/dp/B00U293Y5M) this guy and now i wan to try with this type of connection instead of the ethernet and with the first @Gunner code to see if this can be the solution. How can i do? I’m trying to blink a led with an arduino UNO but i’m sure that i’m making some mistakes. Any help?

with this it doesn’t work offline…

EDIT: just my mistake, IT WORKS !!! I have to wait like 2 minutes before the “offline-start” but it works perfectly

How are you testing the offline functionality?
If you’re powering-off your router then it won’t work, no matter what code you’re using.

Pete.