BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Real Time Clock without internet connection

I am using the RTC and time widget for triggering on and off a relay. My problem is that I want the RTC to work even if the internet connection drops, and when there is connection again to resync time with the servers. This is my schematic:

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <TimeLib.h>
#include <WidgetRTC.h>
 
// Esp8266 pins.
#define ESP8266_GPIO2    2 // Blue LED.
#define ESP8266_GPIO4    4 // Relay control.
#define ESP8266_GPIO5    5 // Optocoupler input.
#define LED_PIN          ESP8266_GPIO2

char auth[] = "...";
const char ssid[] = "...";
const char password[] = "...";

// Flag for sync on re-connection.
bool isFirstConnect = true; 
volatile int relayState = LOW;    // Blynk app pushbutton status.
volatile int inputState = LOW;    // Input pin state.

BlynkTimer timer;
WidgetRTC rtc;

void setup() {
  pinMode( ESP8266_GPIO4, OUTPUT );       // Relay control pin.
  pinMode( ESP8266_GPIO5, INPUT_PULLUP ); // Input pin.
  pinMode( LED_PIN, OUTPUT );             // ESP8266 module blue LED.
  digitalWrite( LED_PIN, LOW );           // Turn on LED.
  Blynk.begin( auth, ssid, password );    // Initiate Blynk conection.
  digitalWrite( LED_PIN, HIGH );          // Turn off LED.
  setSyncInterval(10 * 60); // Sync interval in seconds (10 minutes)
  timer.setInterval(10000L, clockDisplay);
}

BLYNK_CONNECTED() {
  rtc.begin();
  if (isFirstConnect) {
    Blynk.syncAll();
    isFirstConnect = false;
  }
}

// Blynk app relay command.
BLYNK_WRITE( V0 ) {
  if ( param.asInt() != relayState ) {
    relayState = !relayState;  // Toggle state.
    digitalWrite( ESP8266_GPIO4, relayState ); // Relay control pin.
  }
}
 
// Debounce input pin.
int DebouncePin( void ) {
  // Read input pin.
  if ( digitalRead( ESP8266_GPIO5 ) == HIGH ) {
    // Debounce input.
    delay( 25 );
    if ( digitalRead( ESP8266_GPIO5 ) == HIGH )
      return HIGH;
  }
  return LOW;
}

void clockDisplay()
{
  String currentTime = String(hour()) + ":" + minute() + ":" + second();
  String currentDate = String(day()) + " " + month() + " " + year();
  Blynk.virtualWrite(V1, currentTime);
  Blynk.virtualWrite(V2, currentDate);
}
 
void loop() {
  Blynk.run();
  timer.run();
}

I wanted to know if anyone has done this before and could help me with his schematic. Thanks!

Then use a real RTC Module in your circuit and don’t use the RTC widget which will require a Server to sync with

What I want to do is achievable with a little programming to check if connection is dropped and start a local time variable on ESP8266’s memory and switch back to RTCs widget time when the connection is re-established… I thought I’d ask here first in case anyone has done this before. Using a RTC module is not a solution for me.

Probably better to run a local server.

Pete.

1 Like

I leave my final code in case someone needs it. The Blynk app program needs two Value Display elements (this elements would be necessary for the offline operation, they are assigned to virtual pins 10 and 11), one timer (on virtual pin 1) this would turn on/off the relay if there’s internet connection. And an RTC element. Notice that this two elements (timer and value display) should have the same on/off time set up so that there’s no inconsistency (I could have just used the timer and read the values from it to avoid setting up 2 times the on/off moment, but I used this other approach for testing purposes)

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <TimeLib.h>
#include <WidgetRTC.h>
 
// Esp8266 pins.
#define ESP8266_GPIO2    2 // Blue LED.
#define ESP8266_GPIO4    4 // Relay control.
#define LED_PIN          ESP8266_GPIO2
#define RELAY_PIN        V0
#define INICIO           V10
#define FIN              V11


char auth[] = "...";
const char ssid[] = "...";
const char password[] = "...";

// Flag for sync on re-connection.
bool isFirstConnect = true;
volatile int relayState = LOW;    // Blynk app pushbutton status.

BlynkTimer timer;
WidgetRTC rtc;

// para detectar horarios de inicio y fin de relay
long startTimeInSecs;
long endTimeInSecs;
//start time
long shs;
long smins;
long ssecs;
//end time
long ehs;
long emins;
long esecs;

void setup() {
  //Serial.begin(9600);
  pinMode( ESP8266_GPIO4, OUTPUT );       // Relay control pin.
  pinMode( LED_PIN, OUTPUT );             // ESP8266 module blue LED.
  digitalWrite( LED_PIN, LOW );           // Turn off LED.
  Blynk.begin( auth, ssid, password );    // Initiate Blynk conection.
  digitalWrite( LED_PIN, HIGH );          // Turn on LED.
  setSyncInterval(720 * 60); // Sync interval in seconds (1 dia)
  timer.setInterval(60000L, clockDisplay); // Cada 60 segundos actualizar
}
 
// This function runs every time Blynk connection is established.
BLYNK_CONNECTED() {
  if ( isFirstConnect ) {
    Blynk.syncAll();
    rtc.begin();
    isFirstConnect = false;
  }
}
 
// Blynk app relay command.
BLYNK_WRITE( RELAY_PIN ) {
  if ( param.asInt() != relayState ) {
    relayState = !relayState;                  // Toggle state.
    digitalWrite( ESP8266_GPIO4, relayState ); // Relay control pin.
  }
}

// Para leer hora de inicio
BLYNK_WRITE(INICIO) {
  startTimeInSecs = param[0].asLong();
  shs = startTimeInSecs/3600;
  smins = (startTimeInSecs/60) - (60*shs);
  ssecs = (startTimeInSecs) - ((60*smins) + (3600*shs));
  //Serial.println("Inicio: "+String(shs)+":"+String(smins)+":"+String(ssecs));
}

// Para leer hora de fin
BLYNK_WRITE(FIN) {
  endTimeInSecs = param[0].asLong();
  ehs = endTimeInSecs/3600;
  emins = (endTimeInSecs/60) - (60*ehs);
  esecs = (endTimeInSecs) - ((60*emins) + (3600*ehs));
  //Serial.println("Fin: "+String(ehs)+":"+String(emins)+":"+String(esecs));
}

void clockDisplay()
{
  String currentTime = String(hour()) + ":" + minute() + ":" + second();
  String currentDate = String(day()) + " " + month() + " " + year();
  Blynk.virtualWrite(V1, currentTime);
  Blynk.virtualWrite(V2, currentDate);
  //Serial.println("HORA ACTUAL: "+currentTime);
}
 
// Main program loop.
void loop() {
  Blynk.run();
  timer.run();
  boolean isconnected = Blynk.connected();
  if (!isconnected) {
    if ((hour() == shs) and (minute() == smins) and (second() == ssecs)) {
      digitalWrite( ESP8266_GPIO4, HIGH );
      digitalWrite( LED_PIN, HIGH );          // Turn off LED.
    }
    if ((hour() == ehs) and (minute() == emins) and (second() == esecs)) {
      digitalWrite( ESP8266_GPIO4, LOW );
      digitalWrite( LED_PIN, LOW );          // Turn off LED.
    }
  }
}
1 Like