Arduino + ESP wifi shield connection manager scipt with connect and disconnect log

Hi guys, I’ve just developed a sketch tool for a project and thought that it could perhaps be useful for others who what to keep track of when they lose wifi connection. I am using an Arduino + ESP-01 wifi shield as my connection to Blynk and as noted by a number of people, this setup isn’t the most suited to Blynk and it potentially leads to more connection/disconnection issues than would be the case for other setups e.g. standalone ESP connect.

What I wanted to do was develop a sketch that would keep my hardware online, even after a disconnect, and also keep a record of when it got disconnected and when it was able to reconnect.

In order to assist with connection management I have used a WDT and have modified the BlynkSimpleShieldEsp8266.h library as described by @yaamr in their post (How to make ESP8266 Shield work full automatically without any effect by router's interruptions) (thanks yaamr for providing info on your project).

In order to log the disconnection times I have set it up to write the disconnect time to the EEPROM member prior to the Arduino reset so that this is saved and accessible whent the hardware gets back on line. The connection and disconnect times and dates are displayed on the serial monitor and on the blynk app terminal.

Here is the code below. Note that this is just the bare bones of the sketch which manages the connection and keeps a log. This is intended to be blended with what ever project you are working on and what to keep online.

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial

#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266_hack.h>   //  This is the hacked Esp8266 library with the WDT functions added to reset the arduino when the ESP cannot connect to wifi.
#include <avr/wdt.h> // Watchdog timer to reset if script gets stuck.

// Real Time Clock library
#include <WidgetRTC.h>
#include <TimeLib.h>

#include <EEPROM.h>  // Library to save numbers to EEPROM member which will be stored when Arduino is reset.

// Blynk project authentication code
char auth[] = "xxxx";

// WiFi credentials.
char ssid[] = "xxxx";
char pass[] = "xxxx";

// Hardware Serial on Mega, Leonardo, Micro...
#define EspSerial Serial1

// Your ESP8266 baud rate:
#define ESP8266_BAUD 115200

ESP8266 wifi(&EspSerial);

BlynkTimer timer;

WidgetRTC rtc;

WidgetTerminal terminal(V1);

char Buffer[12];
String timebuffer;
String datebuffer;
long Record_time; 

//from EEPROM
const int eeAddress = 0;
long long_fromEEPROM=1532379132;
time_t time_t_fromEEPROM;
tmElements_t tmElements_t_fromEEPROM;

void setup()
{
  // Debug console
  Serial.begin(9600);

  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);

  Blynk.begin(auth, wifi, ssid, pass);
  
  //Real time clock begin.
  rtc.begin();

  // Run Blynk.run a few times to allow rtc to sync correctly.
  Blynk.run();
  Blynk.run();
  Blynk.run();
  Blynk.run();
    
  timer.setInterval(1000L, Uptime);
  timer.setInterval(5001L, CheckConnection);
  timer.setInterval(5002L, recordtimedate);
  timer.setInterval(5003L, MY_PROJECT);  //  this is where you can add functions for you project

  setSyncInterval(10 * 60); // Sync interval for RTC in seconds (interval is in seconds so this is 10 minutes)

  //terminal.clear();    

  // Log time of last disconnect
  //Get time from EEPROM
  EEPROM.get(eeAddress, long_fromEEPROM);
  time_t_fromEEPROM = time_t(long_fromEEPROM);
  breakTime(time_t_fromEEPROM, tmElements_t_fromEEPROM);

  // Format time and date
  sprintf(Buffer, "%02d:%02d:%02d", tmElements_t_fromEEPROM.Hour, tmElements_t_fromEEPROM.Minute, tmElements_t_fromEEPROM.Second);  
  timebuffer = Buffer;
  sprintf(Buffer, "%02d/%02d/%04d", tmElements_t_fromEEPROM.Day, tmElements_t_fromEEPROM.Month, tmElements_t_fromEEPROM.Year+1970); 
  datebuffer = Buffer;
  
  // Display time from EEPROM on terminal on Blynk app and serial
  displaytimedate("Last disconnect time: ");
  
  // Log time of new connection 
  recordtimedate();

  // Disply current time on terminal on Blynk app and serial
  displaytimedate("Connected at: ");
}

BLYNK_CONNECTED() {
  //Do this when Blynk is connected.
}

void loop()
{
  Blynk.run();
  timer.run(); // Initiates BlynkTimer
}

//// Uptime counter
void Uptime()
{
  Blynk.virtualWrite(V0, millis() / 1000);
  //Serial.println(millis() / 1000);
}

void  CheckConnection()
{
  if (Blynk.connected()){
    // Blynk is connected.
  }
  else {
    // Blynk is disconnected.
    LogDisconnected();  //Log time of disconnect.
    wdt_enable(WDTO_1S);
  }
}

void  LogDisconnected()
{
  // Save record_time to EEPROM.
  EEPROM.put(eeAddress, Record_time);
  
  // Print to serial monitor
  Serial.print("Disconnected at: ");
  Serial.print(timebuffer);
  Serial.print(" ");
  Serial.print(datebuffer);
  Serial.println();
  Serial.flush();
}

// Digital clock display of the time
void recordtimedate()
{
  sprintf(Buffer, "%02d:%02d:%02d", hour(), minute(), second());  
  timebuffer = Buffer;
  sprintf(Buffer, "%02d/%02d/%04d", day(), month(), year()); 
  datebuffer = Buffer;
    
  Record_time=now();  // save current time as a number to save to EEPROM if disconnected.
}

void displaytimedate(String Title)
{
  // Print to terminal on Blynk app
  terminal.println(F("------------"));
  terminal.print(Title);
  terminal.print(timebuffer);
  terminal.print(" ");
  terminal.println(datebuffer);
  terminal.flush();

  // Print to serial monitor 
  Serial.println(F("------------"));
  Serial.print(Title);
  Serial.print(timebuffer);
  Serial.print(" ");
  Serial.print(datebuffer);
  Serial.println();
}

//// Add your project function here...
void MY_PROJECT()
{
    // Add something.
}
1 Like