How to make Blynk.sync ALL after the power loss?

BLYNK_CONNECTED() {
if (isFirstConnect) {
Blynk.syncAll();
Blynk.notify(“TIMER STARTING!!!”);
rtc.begin();

isFirstConnect = false;
}
}

how to make blynk .syncALL with my hardware if the power is loss?my blynk application is not syncron and i have to push or slide the button to…make the hardware work again. please help me…thank you

Doing a syncAll is a chancy thing… it literally acts like you touched each and every button, slider and control widget in your project with the last known setting, ALL at the SAME time!! Thus it is best to sync only the essential vPins, and if doing more than a few at a time (5 or so), or if some sync actions trigger longer functions calls, then running a small timer delay between them.

As for doing such after a power or disconnection event… that depends on how your sketch is set up… did the device keep running (on battery?) and only loose connection to the router is the house power was lost or is the device itself also rebooting from power outage.

I have a example of keeping your (presumably battery powered) sketch running after disconnection and reconnection. Might be something in there that helps your use case.

If i use code below , whe the power or wifi hotspot is loss, the slider/button data value is syncron, but lamp timer RTC widget is not syncron, how to solve this problem?.thank you

how to make the button/slider data value ON/OFF timer RTC widget syncron with hardware…if the power or wifi hot spot is loss? thank you for your answer…

if my ESP8266 is power loss or hotspot loss…setting turn ON timer is reseting then relay OFF. how to solve this problem…? thank you

/**************************************************************
 * timeinput.ino Demonstrate interaction of Time library with
 * Blynk's TimeInput widget.
 * App project setup:
 * RTC widget (no pin required!!!)
 * V1 : Manual/Auto button
 * V2: On-off button
 
 * 
 * Time Input widget on V8 (All days)
 * Button selection for Time Input (All days) on V9
 * 
 * Time Input widget on V10 (Up to you)
 * Button selection for Time Input (Up to you) on V11
 * 
 **************************************************************/
#define BLYNK_DEBUG // Optional, this enables lots of prints

#define BLYNK_PRINT Serial
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <SimpleTimer.h>
#include <TimeLib.h>

#include <WidgetRTC.h>


#include <DHT.h>
#include <LiquidCrystal.h>
#define DHTPIN 3          // What digital pin we're connected to
#define DHTTYPE DHT11     // DHT 11


LiquidCrystal lcd(13, 8, 7, 6, 5, 4);
const int relay1 =  A0;
const int relay2 =  A1;
const int relay3 =  A5;
int humLowTrigger;
int tempHighTrigger;



char auth[] = "c2d143sssa2f9e";
char ssid[] = "kxxxxadi";
char pass[] = "polytron";
#include <WidgetLED.h>



#define TestLED 9                 // on board LED pin assignment
char Date[16];
char Time[16];

long startsecondswd;            // weekday start time in seconds
long stopsecondswd;             // weekday stop  time in seconds
long nowseconds;                // time now in seconds
bool isFirstConnect = true;

 
int manual=0;
int oldstatus;


int alldays;
//int uptoyou;


#include <SoftwareSerial.h>
SoftwareSerial EspSerial(10, 11); // RX, TX



#include  <TimerOne.h>          // Avaiable from http://www.arduino.cc/playground/Code/Timer1
volatile int i=0;               // Variable to use as a counter volatile as it is in an interrupt
volatile boolean zero_cross=0;  // Boolean to store a "switch" to tell us if we have crossed zero
int AC_pin = 12;                // Output to Opto Triac
int brightness = 128;           // Dimming level (0-128)  0 = on, 128 = 0ff
int freqStep = 75;    // This is the delay-per-brightness step in microseconds.
int val = 0;

const byte degreeSymbol = B11011111; //Simbol Degree

int autodimmer;



#define ESP8266_BAUD 9600
//#define ESP8266_BAUD 115200
SimpleTimer timer;

WidgetRTC rtc;

ESP8266 wifi(&EspSerial);
DHT dht(DHTPIN, DHTTYPE);
void updateHum(int param);
void updateTemp(int param);
//BlynkTimer timer;




BLYNK_WRITE(V5) {
  updateHum(param.asInt());
}
BLYNK_WRITE(V6) {
  updateTemp(param.asInt());
}
void Readdata()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  
  
  

  if (isnan(h) || isnan(t)) {
    lcd.print("ERROR");
    return;
  }
    lcd.setCursor(0, 0); //
    lcd.print("TMP:"); 
    lcd.print((int)t);
    lcd.write(degreeSymbol); // Tulis simbol derajat
    lcd.print("C");
    //menampilkan nilai suhu pada LCD
    lcd.setCursor(9, 0);
    lcd.print("HUM:"); 
    lcd.print((int)h);
    lcd.println("%");

    lcd.setCursor(0, 1);  
    lcd.print("JAM:");                
   
    if(hour() < 10)
    {
    lcd.print("0");
    }
    lcd.print(hour(), DEC); //Print hour
    lcd.print(':');
    if(minute() < 10)
    {
    lcd.print("0");
    }
    lcd.print(minute(), DEC); //Print min


  

    
  Blynk.virtualWrite(V15, h);
  Blynk.virtualWrite(V16, t);
  Blynk.virtualWrite(V25, humLowTrigger);
  Blynk.virtualWrite(V24, tempHighTrigger);

  
  if(h < humLowTrigger) {
      digitalWrite(relay1, LOW); 
      Blynk.virtualWrite(V26, 255);
  } else {
      digitalWrite(relay1, HIGH);
      Blynk.virtualWrite(V26, 0);
  }    
  if(t < tempHighTrigger) {
      digitalWrite(relay2, HIGH); 
      Blynk.virtualWrite(V27, 0);
  } else {
      digitalWrite(relay2, LOW);
      Blynk.virtualWrite(V27, 255);
  }
  
}
void updateHum(int param) {
humLowTrigger = param;

}
void updateTemp(int param){
tempHighTrigger = param;
}



void zero_cross_detect() {    
  zero_cross = true;               
  i=0;
  digitalWrite(AC_pin, LOW);       // turn off TRIAC (and AC)
}                                 

// Turn on the TRIAC at the appropriate time
void dim_check() {                   
  if(zero_cross == true) {              
    if(i>=brightness) {                     
      digitalWrite(AC_pin, HIGH); // turn on light       
      i=0;  // reset time step counter                         
      zero_cross = false; //reset zero cross detection
    } 
    else {
      i++; // increment time step counter                     
    }                                
  }                                  
}           





void setup()
{
  lcd.begin(16, 2);
  dht.begin();


  

  
  pinMode(AC_pin, OUTPUT);                          // Set the Triac pin as output
  attachInterrupt(0, zero_cross_detect, RISING);    // Attach an Interupt to Pin 2 (interupt 0) 
  Timer1.initialize(freqStep);                      // Initialize TimerOne library 
  Timer1.attachInterrupt(dim_check, freqStep);   


  
  Serial.begin(9600);

  EspSerial.begin(ESP8266_BAUD);
  delay(10);  // stabil
  
  Blynk.begin(auth, wifi, ssid, pass);
  Blynk.syncAll();

  timer.setInterval(10000, Readdata);  // stabil


  pinMode(relay1, OUTPUT);
  digitalWrite(relay1, HIGH);
  pinMode(relay2, OUTPUT);
  digitalWrite(relay2, HIGH);
  pinMode(relay3, OUTPUT);
  digitalWrite(relay3, HIGH);

  
  int humLowTrigger = 65;
  int TempHighTrigger = 20;


  pinMode(TestLED, OUTPUT);
  digitalWrite(TestLED, HIGH); // set LED OFF
  int mytimeout = millis() / 1000;
  while (Blynk.connect() == false) { // try to connect to server for 10 seconds
  if((millis() / 1000) > mytimeout + 8){ // try local server if not connected within 9 seconds
  break;
    }
  }
  rtc.begin();
  
  timer.setInterval(30000L, activetoday);  // check every 10 SECONDS if schedule should run today 
  timer.setInterval(20000L, reconnectBlynk);  // check every 30s if still connected to server 


}



BLYNK_CONNECTED() {
   Blynk.syncAll();
    if (isFirstConnect) {
  //  Blynk.syncVirtual(V8); // sync timeinput widget 
    //Blynk.syncVirtual(V9); // sync timeinput widget 
  
    Blynk.notify("TIMER STARTING!!!!");
    rtc.begin();
  
    isFirstConnect = false;
 
  }
}


void activetoday(){        // check if schedule should run today
  if(year() != 1970){

   
   if (alldays==1) { 
    Blynk.syncVirtual(V8); // sync timeinput widget 
 //   Blynk.syncVirtual(V9); // sync timeinput widget  
 
   }
  }
}

void checklastbuttonpressed (){
   
    if(alldays==1)
    { oldstatus=1; }
    if(alldays==0)
    { oldstatus=0; }
 
  
}


void restorelastbuttonpressed (){
   
    if(oldstatus==1){
      alldays=1; 
      Blynk.virtualWrite(V9, 1);}
    
    if(oldstatus==0){ 
    
      alldays=0;
      
    
      Blynk.virtualWrite(V9, 0);
     
      }
 }


BLYNK_WRITE(V3)//      slider brillo
{
 int brillo = param.asInt(); 
 if (autodimmer == 0) // If auto dimmer is OFF then set the dimmer according slider value
 brightness=brillo;

    
}




BLYNK_WRITE(V1)  // Manual/Auto selection
{
  if (param.asInt()==1) {
    manual=1;
    
  

    checklastbuttonpressed ();
  
    alldays=0;
    //uptoyou=0;
   
    Blynk.virtualWrite(V9, 0);
   
    
  } else {
    restorelastbuttonpressed ();
    manual=0;
   
    
 }
}

BLYNK_WRITE(V7)  // Manual/Auto selection
{
  if (param.asInt()==1) {
    digitalWrite(relay3, LOW);
   
  } else {
     digitalWrite(relay3, HIGH);
  
    
 }
}

void resetManual()
{
Blynk.virtualWrite(V1, 0);   //Turn OFF Manual Mode Widget
Blynk.virtualWrite(V2, 0);   //Turn OFF Button Widget Device
digitalWrite(TestLED, HIGH); // set LED OFF
lcd.setCursor(10, 1);
lcd.print("L-OFF");
}


BLYNK_WRITE(V2)  // ON-OFF Manual 
{
    if (param.asInt()==1) {  // boton encendido  
    
   
    
        if (manual==0){  //está en modo automático     
         checklastbuttonpressed ();
         manual=1;
     
         alldays=0;
        
         Blynk.virtualWrite(V1, 1);
        
         Blynk.virtualWrite(V9, 0);
        
         digitalWrite(TestLED, LOW); // set LED ON 
         Blynk.virtualWrite(V2, 1);   //Turn ON Button Widget
         lcd.setCursor(10, 1);
         lcd.print("L--ON");
         
         } else {             //está en modo manual 
       
         alldays=0;
         
         Blynk.virtualWrite(V1, 1);
        
         Blynk.virtualWrite(V9, 0);
        
         digitalWrite(TestLED, LOW); // set LED ON 
         Blynk.virtualWrite(V2, 1);   //Turn ON Button Widget
         lcd.setCursor(10, 1);
         lcd.print("L--ON");
         }                           
         }else {
       

         if (manual==0){      //modo automático
         checklastbuttonpressed ();
         manual=1;
       
         alldays=0;
     
         Blynk.virtualWrite(V1, 1);
 
         Blynk.virtualWrite(V9, 0);
       
         digitalWrite(TestLED, HIGH); // set LED OFF
         Blynk.virtualWrite(V2, 0);   //Turn OFF Button Widget
          lcd.setCursor(10, 1);
          lcd.print("L-OFF");
         } else {  
         
         alldays=0;
       
         Blynk.virtualWrite(V1, 1);
         Blynk.virtualWrite(V9, 0);
        
         digitalWrite(TestLED, HIGH); // set LED OFF
         Blynk.virtualWrite(V2, 0);   //Turn OFF Button Widget
         lcd.setCursor(10, 1);
         lcd.print("L-OFF");
         }  
         }
}


BLYNK_WRITE(V9)  // All days selected
{
  if (param.asInt()==1) {

    timer.setTimeout(50, resetManual);
    timer.setTimeout(50, checklastbuttonpressed);
    alldays=1;
 
    
  //  Blynk.virtualWrite(V11, 0);
  } else {
    alldays=0;
  }
}




BLYNK_WRITE(V8)//All days
{  
   if (alldays==1) {   

       
    
    TimeInputParam t(param);
    int dayadjustment = -1;  
    if(weekday() == 1){
      dayadjustment =  6; // needed for Sunday, Time library is day 1 and Blynk is day 7
     }
    if(t.isWeekdaySelected(weekday() + dayadjustment)){ //Time library starts week on Sunday, Blynk on Monday   
    for (int i = 1; i <= 7; i++) {  // Process weekdays (1. Mon, 2. Tue, 3. Wed, ...)
    if (t.isWeekdaySelected(i)) {
        }
      } 
   // nowseconds = ((hour() * 3600) + (minute() * 60) + second());

    nowseconds = long(hour() * 3600L) + long(minute() * 60L) + long (second());
    
    
    startsecondswd = (t.getStartHour() * 3600) + (t.getStartMinute() * 60);
    stopsecondswd = (t.getStopHour() * 3600) + (t.getStopMinute() * 60);
    //Serial.println(startsecondswd);  // used for debugging
    if(startsecondswd ==  stopsecondswd) {  
        digitalWrite(TestLED, HIGH); // set LED OFF
        Blynk.virtualWrite(V2, 0);
        lcd.setCursor(10, 1);
        lcd.print("L-OFF");
        
    }
    if(startsecondswd < stopsecondswd){    // 90s on 60s timer ensures 1 trigger command is sent
         if ((nowseconds >= startsecondswd) && (nowseconds < stopsecondswd) ){
          digitalWrite(TestLED, LOW); // set LED On
          Blynk.virtualWrite(V2, 1);
          lcd.setCursor(10, 1);
          lcd.print("L--ON");
         }
          else if (nowseconds >= stopsecondswd){
          digitalWrite(TestLED, HIGH); // set LED On
          Blynk.virtualWrite(V2, 0);
          lcd.setCursor(10, 1);
          lcd.print("L-OFF");
          
          }
          else  {
          digitalWrite(TestLED, HIGH); // set LED OFF
          Blynk.virtualWrite(V2, 0);
          lcd.setCursor(10, 1);
          lcd.print("L-OFF");
          }
    }


    if(startsecondswd > stopsecondswd){    // 90s on 60s timer ensures 1 trigger command is sent
         if ((nowseconds >= startsecondswd) && (nowseconds <= 82800)){
          digitalWrite(TestLED, LOW); // set LED On
          Blynk.virtualWrite(V2, 1);
          lcd.setCursor(10, 1);
          lcd.print("L--ON");
         }
          else if (nowseconds < stopsecondswd){
          digitalWrite(TestLED, LOW); // set LED On
          Blynk.virtualWrite(V2, 1);
          lcd.setCursor(10, 1);
          lcd.print("L--ON");
          }
          else if (nowseconds >= stopsecondswd && nowseconds < startsecondswd ){
          digitalWrite(TestLED, HIGH); // set LED OFF
          Blynk.virtualWrite(V2, 0);
          lcd.setCursor(10, 1);
          lcd.print("L-OFF");
          
          
          
        
     
         
       }          
    }
  }
  else{
   
    // nothing to do today, check again in 30 SECONDS time    
  }
  //terminal.println();
 }
}
  

void reconnectBlynk() {
  if (!Blynk.connected()) {
    if(Blynk.connect()) {
     BLYNK_LOG("Reconnected");
    } else {
      BLYNK_LOG("Not reconnected");
    }
  }
}

void loop()
{
  if (Blynk.connected()) {
    Blynk.run();
  }
  timer.run();
  // yield();
}```

You already have this topic asking for the same thing… please do not post additional questions on other’s topics.

Your answer is to learn how to use Blynk.syncAll() or Blynk.syncVirtual()

http://docs.blynk.cc/#blynk-firmware-blynktimer-blynksyncall

And editing your posted code is done with backticks, not commas or apostrophes…

Blynk%20-%20FTFC

the lamp ON OFF manual, set temperatur, slider use blynk syncALL is OK…but the lamp Timer RTC…is reseting

I already said the using syncAll can be iffy. Try manually syncing only the needed pins.

Beside you probably have other issues that might be contributing to your problem… hard to see what you are doing with some of your code.

For instance… why still using SimpleTimer? BlynkTimer is built into the library.

This might also be a bad idea… this command is meant for the ESP (which you are just using as a wifi interface) not so much for the Arduino (which you are running your sketch on)

simple timer…use for dimmer lamp…

BlynkTimer is same thing, but built in…

Remove #include <SimpleTimer.h>

And change SimpleTimer timer; to BlynkTimer timer;

And you are running the syncAll twice each reboot

Same with rtc.begin();

That’s all I have time for now… you will simply need to work your way through the rest, but I recommend syncing only the relevant vPins so you have better control until you have better understanding.

thank you mister…my trouble is because interlocking button auto/manual lamp timer…

That mean you solved it?

1 Like

yes…