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

To get in sync

Hello everyone !

I will try to be as precise as possible. Sorry for my bad english if i am not able to put in the scene properly -----

I have a relay that is controlled both over physical button as well as Blynk app. Which works well. But the issue i am facing is =

Scene 1
When the device boots for the 1st time it just sync with the previous state stored in the server.(Good) And lets say i have turned on the relay from the app.

Scene 2
But later when the connection drops(internet) i will no longer be able to control it over the app. So i use the physical button to say turn the relay off

Scene 3
Now when the connection is up again what’s happening is it will sync from the app side and will turn the relay back on (which i dont want), as i had that previously turned on from the app.

What i am trying to achieve is, after the connection drop the App must sync to the hardware. Not vice versa (hardware syncing to App)

But at the boot process let the hardware sync from the app(server).
Is there a way out ??

So are you saying that you want…

First connection (on device boot) get relay state from Blynk server

Subsequent connections, push relay state to Blynk server?

If so, then you’ll need a flag to track if this is the first connection, or do the first synchronisation from void setup (which only rinds once on boot-up) and all subsequent synchronisations from BLYNK_CONNECTED.

Pete.

int ReCnctFlag;       // Reconnection Flag
int ReCnctCount = 0;  // Reconnection counter
bool isFirstConnect = true;


BLYNK_CONNECTED() {
  if (isFirstConnect) {
     Blynk.syncAll();
     Serial.print("In Sync from my code");
     isFirstConnect = false;
  }
  Serial.println("Connected");
    ReCnctCount = 0;
}

I am having this in my setup…
This is working as it should.

should i again BlynksyncAll(); under BLYNK_CONNECTED ??

Is there any snippet code that you can provide ??

Posting snippets of your code doesn’t really help, because so much depends on what else you’re doing.

I would recommend just to sync the pins that you need to, not use Blynk.syncAll.

You haven’t answered my question about how you want this to work, but if you do want it to work the way that I described then after the first connection and synchronisation you need to be doing Blynk.virtualWrites to push your relay values to the Blynk server.

Pete.

Yes i am looking to achieve the same as you described.

this is already done in this function

void lightOn1() {
digitalWrite(RelayPin1, LOW);
LampState1 = 1;
Blynk.virtualWrite(VPIN1, HIGH); 
}

But when the connection is dropped this wont send any data to Blynk server. :cold_sweat:

I just need to get the blynk server to get in sync with relay state after connection drop.

Posting little snippets of code don’t help us to help you.

Pete.

Here is the code

#include <ESP8266WiFi.h>          
#include <BlynkSimpleEsp8266.h>

#define VPIN1 V1

char server[] = "";
char port[] = "8080";
char auth[] = "" ;
char ssid[] = "";
char pass[] = "";

int ReCnctFlag;
int ReCnctCount = 0;
bool isFirstConnect = true;

void lightOn1();
void lightOff1();

boolean LampState1 = 0;
boolean SwitchReset1 = true;

const int TacSwitch1 = D2; 
const int RelayPin1 = D3;

BlynkTimer timer;

void setup() {
  Serial.begin(115200);
  WiFi.begin (ssid, pass);
    Blynk.config(auth, server, 8080);
  bool result = Blynk.connect();

    pinMode(RelayPin1, OUTPUT);
      pinMode(TacSwitch1, INPUT_PULLUP);
       digitalWrite(RelayPin1, HIGH);
    timer.setInterval(100, ButtonCheck1);
}

BLYNK_CONNECTED() {
  if (isFirstConnect) {
     Blynk.syncAll();
     isFirstConnect = false;
  }
  Serial.println("Connected");
  ReCnctCount = 0;
}

void loop() {
    timer.run();
  if (Blynk.connected()) {  
    Blynk.run();
  } else if (ReCnctFlag == 0) {  
    ReCnctFlag = 1;   
    Serial.println("Starting reconnection timer in 10 seconds...");
    timer.setTimeout(10000L, []() {  
      ReCnctFlag = 0;  
      ReCnctCount++;  
      Serial.print("Attempting reconnection #");
      Serial.println(ReCnctCount);
      Blynk.connect();  
    }); 
  }
}

void ButtonCheck1() {
  boolean SwitchState1 = (digitalRead(TacSwitch1));
  if (!SwitchState1 && SwitchReset1 == true) {
if (LampState1) {
  lightOff1();
} else {
  lightOn1();
}
SwitchReset1 = false;
delay(50);
  }
  else if (SwitchState1) {
SwitchReset1 = true;
  }
}

void ToggleRelay1() {
  LampState1 = !LampState1;
  if (LampState1) {
   lightOn1();
  }
  else lightOff1();
}

void lightOn1() {
digitalWrite(RelayPin1, LOW);
LampState1 = 1;
Blynk.virtualWrite(VPIN1, HIGH); 
}

void lightOff1() {
digitalWrite(RelayPin1, HIGH);
LampState1 = 0;
Blynk.virtualWrite(VPIN1, LOW); 
}

BLYNK_WRITE(VPIN1) {
  int SwitchStatus1 = param.asInt();
if (SwitchStatus1 == 2){
ToggleRelay1();
  }
  else if (SwitchStatus1){
lightOn1();
  }
  else lightOff1();
}

cant find the way out :thinking:

The if statement in your BLYNK_CONNECTED callback needs to have an else clause tagged on the end.

This will contain code that will be executed when Blynk connects and isFirstConnect is not true.
The code you place in this else clause will push the current relay values out to the Blynk server using Blynk.virtualWrite command(s).

Pete.

1 Like

Yessssssss !!! Got it woking. Blynk.virtualWrite is very powerful !!
And as i was doing this i was wondering like

What it the internet is down and i am out of home, and i want the device to be On, then even if i turn on the from the app and as soon as the internet is up the input from the app will be ignored as i have Blynk.virtualWrite running.

Is there any way to balance out this ?

So, you’ve now just moved the goalposts and want different functionality!
I guess the simplest way would be to have a switch widget on the app that allows you to specify which is the master - the app or the device.
If its in device mode then it will work as it currently does, if it is in app mode then it will work as it did before.

However, I think that this is a messy solution, that will lead to misunderstanding of what the expected behaviour in various scenarios.
As you can expect the internet and home power to be working as expected the vast majority of the time, I’d work on the principal that you currently have, and know that if the device shows offline in the app then you need to check it later and activate the switch when the device is back online.
You could add a notification to the BLYNK_CONNECTED routine to let you know when a re-connection has taken place.

Pete.

I would consider it as improvisation.

Yes felt the same… that is why i asked you as you are an experienced person and may have more knowledge on this.

But thank you for helping me…