A good solution to Auto reset Wifi or ethernet connection when disconnected

Hi,

This week end I found a good solution to Reset the connection when the arduino card is deconnected ( any other card can use it). doesn’t mater the way to be connected to cayenne. you wil have to adapt my solution to your device.

to detect easily if the wifi connection or ethernet connection if lost, you just have to make a virtual pin “V0” switching ON / OFF every minutes. then in cayenne. you create 1 sensor connected to this virtual pin “V0”. you creat an actuator “V1”.

you create 2 triggers :
1- trigger N°1 put V1 at 1 if V0 = 1
2- trigger N°2 put V1 at 0 if V0 = 0.

In the program of arduino,
you just have to control if the V1 =0 after 40 seconds.

if the connection is OK → V1 state will be actualised following V0 state.

So if V1!=0 it means the connection is lost, so you just have to connect the reset wire of the Wifi or Ethernet card to the PIN 12 ( for exemple).

I use an arduino MEGA + ESP01 wifi card.

I give you the code :

#include <CayenneESP8266Shield.h>

char token[] = "xxxxxxxxx"; // This is the Auth Token

// Your network name and password.
char ssid[] = "xxxxxxxxx";
char password[] = "xxxxxxxxxxx";


// Set ESP8266 Serial object
#define EspSerial Serial

ESP8266 wifi(EspSerial);

unsigned long previousMillis = 0;        // will store last time LED was updated

const long interval = 60000;           // interval at which to blink the V0 (milliseconds)
unsigned long previousMillis2 = 0;        // will store last time V0 was updated
// constants won't change:
const long interval2 = 40000;           // interval at which to test communication
#define VIRTUAL_PIN0 V0
int VIRTUAL_PIN0_STATE = 0;
#define VIRTUAL_PIN1 V1
int VIRTUAL_PIN1_STATE = 0;
void setup()
{
// RESET the connection at the beginin to be sure the ESP01 will start easily
  pinMode(14, OUTPUT);      // sets the digital pin as output connected to the reset pin of communication card
  swhwReset();

  delay(10);
  // Set ESP8266 baud rate

  EspSerial.begin(115200);
  delay(10);

  Cayenne.begin(token, wifi, ssid, password);
}

void loop()
{
  unsigned long currentMillis = millis();
  unsigned long currentMillis2 = millis();
  Cayenne.run();

 // this is to blink the virtual PIN V0 every minute
  if (currentMillis - previousMillis >= interval)
  {
    // save the last time you blinked V0
    previousMillis = currentMillis;
    if (VIRTUAL_PIN0_STATE == LOW) 
    {
       Cayenne.virtualWrite(V5, millis() / 1000);
       VIRTUAL_PIN0_STATE = HIGH;
     }
    else 
    {
      VIRTUAL_PIN0_STATE = LOW;
    }
    Cayenne.virtualWrite(VIRTUAL_PIN0, VIRTUAL_PIN0_STATE);
 }

 // ctrl the state of VIRTUAL_PIN2 if it has followed the state of VIRTUAL_PIN1 in less than 20s
 if (currentMillis2 - previousMillis >= interval2)
 {
    //If they are different-> it's disconnected ->> RESET
    if (VIRTUAL_PIN0_STATE == VIRTUAL_PIN1_STATE) 
    {
    } 
    else
    {
      swhwReset();
      // as the connection have been reseted the values must be set at 0
      previousMillis2 = 0;
      currentMillis2 = 0;
      previousMillis = 0;
      currentMillis = 0;
    }
  }
}

CAYENNE_OUT(VIRTUAL_PIN0)
{
  Cayenne.virtualWrite(VIRTUAL_PIN0, VIRTUAL_PIN0_STATE);
}

CAYENNE_IN(VIRTUAL_PIN1)
{
  VIRTUAL_PIN1_STATE = getValue.asInt();
}

// function call to reset the communication card ( wifi or ethernet)
void swhwReset()
{
  digitalWrite(14, LOW); // sets the RESET LOW
  delay( 2000);
  digitalWrite(14, HIGH); // sets the RESETD HIGH
}
4 Likes

Nice work around!

Post a picture or discuss the wiring please. Did you use the mega for power to esp-01 or use the esp-01 as the interface to WiFi only?!