Reconnection function with ENC28J60 board

Hello,
in my project I use an arduino mega+ENC28J60 with local server.

According the Blynk Example Browser, in setup I use the function:

Blynk.begin(auth, server_ip, 8080, arduino_ip, dns_ip, gateway_ip, subnet_mask, arduino_mac);

Sometimes (once a week, once a month, it isn’t constant) i found my arduino offline. I think the problem occour when there is no internet connection or a problem in the lan. Yesterday (about for 1 hour) I had no internet connection.
When the internet connection resumed, my hardware was unable to connect again (if I manually reset the board everythink works again). Arduino didn’t also respond to the ping command…
So I assume that the board wasn’t connect to the lan and consequently to the local server.

I’d like to use the reconnection function like this (but this is for wifi module) C++ Blynk - Code Examples for Basic Tasks (Work in Progress)
Basically I think that I can solve this problem if I can perform a reconnection to the LAN and to the server…

Anyone can help me?

I spent a few months using a an Ethernet shield with an Uno, and it was a real pain. It would lock up randomly in the same way you described and the only thing that would resolve the issue was pressing the reset button on the shield.
My temporary solution was to connect one of the digital pins to the reset pin on the Uno and pull it low to reset the board when there was no Ethernet connection.
I was using ethernetclient.connected() on a timer to check if the shield had locked-up.

The long term solution was to move to an ESP8288 with Wi-Fi, and throw the Uno and Ethernet shield in the junk box.

Pete.

Thanks for your reply.
So do you think that isn’t possible to create a “reconnection function”?

I’m thinking at a function like this:

void setup() {
  lan.begin();                        // I don't know the real function
  Blynk.config(auth, server, port);
  Blynk.connect();
}

void loop() {
  
  if (Blynk.connected()) {      Blynk.run();  } 
  else if (ReCnctFlag == 0) { 
    ReCnctFlag = 1;  
    timer.setTimeout(30000L, []() {  
           ReCnctFlag = 0;  
           lan.begin();            // I don't know the real function
           Blynk.connect();  
    });  
  }
}

Something similar didn’t work for me, a complete reboot was the only answer.

Pete.

So… you suggest to connect my arduino mega to the lan with an esp8266 module?
After that I will use this sketch for the reconnection:

In this way I will resolve the problem of disconnection? totally?

No, that’s slightly better than using an Ethernet adopter, but isn’t my preferred route. A NodeMCU or ESP32 in place of the Arduino would be far better, provided it gives you the connectivity you need. A discussion of Arduinos versus NodeMCU’s is here:

This doesn’t cover ESP32s, but that’s an option of you need more pins or more analogue inputs.

After that I will use this sketch for the reconnection:
The sketch you linked is for a stand-a;one ESP8266, not an ESP-01 used as a Wi-Fi adopter. There are some examples on the forum for the Arduino/ESP-01.

Yes, of course, provided your router or internet connection never goes down, the Blynk server never goes down, you don’t have any power supply brownouts, and all of the libraries that you use are 100% reliable and don’t have any bugs or memory leaks.

Pete.

In my project I use about 24 digital pins… (4xt+h sensors, 12x relay for window shutters, 2x PZEM004, 8x relay to manage the cooling and heating)…

so I think the only way is using Arduino Mega + esp8266 module, do you think that with this solution I will again problems of disconnections? there aren’t solution? isn’t possible program a daily reconnection (I know it isn’t a smart solution but could resolve the problem)?
long story short I need a sistem reliable… but i think that these devices aren’t designed to be reliable…

Honestly I prefer using cabled solutions, I don’t love having many WIFI items alwys ON… At the design phase I thought that cabled solution was the most reliable…

From what you’ve said, I’d guess that the devices your controlling aren’t all in the same physical location within your house.
My solution in that situation is to have multiple remote devices that all talk to a central server which in turn talks to Blynk.
I currently have 19 (I think) different devices scattered around my holiday home here in Spain, that all connect to Wi-Fi and talk to the central server using MQTT messaging. The server is a Raspberry Pi which uses Node-Red to handle the logic of how the devices talk to each other, and a interfaces with Blynk, Amazon Alexa, IKEA Tradfri etc.
The system has more resilience the because failure of one single device will have a limited impact on things (not that I tend to suffer failures), and it allows control via multiple devices (a Nextion touch screen for heating/cooling, Amazon Alexa, 433MHz remote controls, Blynk etc). If the internet connection fails then the core system keeps running and can still be used effectively.

For me, this works much better than having wires running back to a central Arduino that acts as a hub. It’s much easier to expand, and troubleshoot, and its much more portable. I have devices that operate via solar power that are situated in locations that would be very difficult to hard wire. Doing a quick count I interface with 21 relays, 3 blinds (via 422MHz), two temp/Humidity sensors, a weather station, a solar charge controller, an RFID reader, 3 PIR detectors (via 433MHz) and the Nextion touch screen.

My Raspberry Pi is a single point of failure, as is my router, and I mitigate this by having spares that are pre-configured and can quickly be swapped for troubleshooting or replacement, not that I’ve ever had cause to use them.
The ESP8266/NodeMCU/ESP32 devices can all be updated remotely via OTA, so I can sit in my armchair and fiddle with the code then upload a new sketch (which is when I tend to create problems for myself!). An Arduino connected via Ethernet or Wi-Fi can’t be updated in this way.

So, I’ve tried the Arduino Ethernet approach, and had a brief excursion into the Arduino/ESP-01 approach, but for me the standalone wireless approach is far far better.

Pete.

A wifi solution based on the ESP8266 or ESP32 is much more stable than anything with Arduino’s and ethernet shields in my experience. It’s much more flexible in terms of placement. Also, when a single component like a central fails, e.g. your Arduino, everything will fail. Combining several wifi modules in a project is much more resilient.

As Pete Said, using a ESP as shield on an Arduino is not the way to go, it sucks really bad. I’d go for an ESP32 any day. It has a dual core 400mhz (!) CPU which will do anything you want. Also using dirty delays (with my other account here I made an example for that somewhere here). You could run Blynk on one core and do other stuff on the other totally independent and it’s so much faster than a stupid Atmel chip.

Other options for extending your pins is using one ESP and add something like an MCP23008. This is a very simple I2C extender. In theory you could daisy chain a LOT of them and get way more than 24 pins available.

Anyway, there are lots of options out there which, in my opinion are way better than the good 'ol Arduino.

I understand your reasons, but I have the problem that the system is already installed and is working.

At my home everything works and honestly I’d like to terminate the project…

The only problem is this disconnection…

May I try to replace the ENC28J60 with the ESP8266 (very quick modify) and see what is the reliability…

I could accept the very ugly solution to automatically reset everything each night… Sorry but I want to finish the project in the easiest and quick way…

Hi mate, why the second account?

Pete.

Because I cannot login anymore using my Twitter account (which I used). Twitter and Blynk blame each other, but meanwhile I cannot login, which is highly annoying, so hence, another account.

Understandable. But for future reference maybe :wink:

ENC is not the most stable piece of ethernet (I know, I tried). ESP could be a solution to act as a shield for connecting. It’ll be more stable I think, providing the connections and grounding are good. Serial connections can be really stable or a pain the ass.

I solved with the code below:

void eth_reset() { 
  Serial.println("Resetto scheda Rete"); 
  pinMode(ETH_RS_PIN, OUTPUT);
  digitalWrite(ETH_RS_PIN, LOW);
  delay(100);
  digitalWrite(ETH_RS_PIN, HIGH);
  pinMode(ETH_RS_PIN, INPUT); 
  Ethernet.begin(arduino_mac, arduino_ip, dns_ip, gateway_ip, subnet_mask);
}
void setup()
{
  eth_reset();                               
  Blynk.config(auth, server_ip, 8080);         
  Serial.println("Connessione a Blynk");
  Blynk.connect();   
}

void loop(){						 
timer.run();
wdt_reset();	
  if (Blynk.connected()) {  Blynk.run();  } 								
  else if (ReCnctFlag == 0) { 												
    ReCnctFlag = 1;   		
    Serial.println("Disconnesso, Provo a connettere tra 30s");                          
    timer.setTimeout(30000L, []() {ReCnctFlag = 0;				
                                   if (!Blynk.connected()) {eth_reset();		
                                                            Blynk.config(auth, server_ip, 8080); 			
                                                            Serial.println("Provo a riconnettere");   
                                                            Blynk.connect();}});   							
                              }				
}