Sync Hardware --> Blynk Project

Hi All.

Let’s say my Blynk project is not currently running on my phone but the hardware (ESP8266, Arduino, etc) is running and it’s changing states based on physical buttons, sensors, etc.

Now I start the Blynk app on my phone and run the project. Is there a callback() I can put in the sketch that will be run on app start-up so that the hardware can push the current status to all the app’s widgets?

Said another way, whenever the app is started I want to sync Hardware --> Blynk Project. Blynk.syncAll() and similar functions seem to work the opposite way. They can be run when the sketch first starts and initiate BLYNK_WRITE events for syncing Blynk Project --> Hardware

I know what I want could be done by periodically syncing Hardware --> Blynk Project using a timer. But that seems inefficient. Would be cleaner to have a callback that only runs when the Blynk Project starts or restarts on the phone.

Thanks.

Greg

Hello. This is already done. When you open app latest pin states in widgets updated accordingly. Is behaviour is by default.

OK, but it doesn’t seem to pick up changes that the hardware sketch makes using Blynk.setProperty() calls while the Blynk app is off-line.

iOS/Android? What property do you change? Are you sure setProperty is triggered?

I’ll look through code again and try a few things. If I still can’t get it to work, I’ll post code. Thanks for the replies.

OK, here’s my code. It uses either the Blynk Button or the Physical Button to swap which one of the ESP8266 LEDs (Red or Blue) is lit. When ever either button is pushed, the lit LED is turned off and the other one is turned on.

When the LEDs are swapped, the sketch code changes the color and label of the Blynk Button using Blynk.setProperty() to match which LED on the ESP8266 is lit.

As long as the Blynk Project is running, everything stays in sync perfectly. But, if I stop the Blynk Project, use the Physical Button to swap the LEDs, and then restart the Blynk Project then the Blynk Button does not update to the current LED color. Things only go back in sync again when I press either Blynk or Physical Button.

Running Blynk on an Android phone and I’ve attached a screen shot of my Button setup.

Thanks again for your help.

Greg

/**************************************************************
 * Blynk is a platform with iOS and Android apps to control
 * Arduino, Raspberry Pi and the likes over the Internet.
 * You can easily build graphic interfaces for all your
 * projects by simply dragging and dropping widgets.
 *
 *   Downloads, docs, tutorials: http://www.blynk.cc
 *   Blynk community:            http://community.blynk.cc
 *   Social networks:            http://www.fb.com/blynkapp
 *                               http://twitter.com/blynk_app
 *
 * Blynk library is licensed under MIT license
 * This example code is in public domain.
 *
 **************************************************************
 * This example runs directly on ESP8266 chip.
 *
 * You need to install this for ESP8266 development:
 *   https://github.com/esp8266/Arduino
 *
 * Please be sure to select the right ESP8266 module
 * in the Tools -> Board menu!
 *
 * Change WiFi ssid, pass, and Blynk auth token to run :)
 *
 **************************************************************/
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

#define BLYNK_PRINT Serial
#define BLYNK_DEBUG
#define BLYNK_GREEN     "#23C48E"
#define BLYNK_BLUE      "#04C0F8"
#define BLYNK_YELLOW    "#ED9D00"
#define BLYNK_RED       "#D3435C"
#define BLYNK_DARK_BLUE "#5F7CD8"
#define redLED 0
#define blueLed 2
#define ledOFF 1
#define ledON 0
#define PHYSICAL_BUTTON 14
#define BOUNCE_TIME 200

enum ledSelect {red, blue};

void checkPhysicalButton(void);
void initLEDs(void);
void setLeds(void);
void swapLeds(void);
void labelButton(ledSelect);

uint8_t btnState = HIGH;
uint32_t lastButtonPush = 0;
char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
char ssid[] = "yyyyyyyyyyy";
char pass[] = "zzzzzzzzzzz";
ledSelect litLED;

void setup()
{
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
  initLEDs();
  pinMode(PHYSICAL_BUTTON, INPUT_PULLUP);
}

void loop() {
  Blynk.run();
  checkPhysicalButton();
}

void initLEDs() {
  pinMode(redLED, OUTPUT);
  pinMode(blueLed, OUTPUT);
  litLED = blue;
  setLeds();
}

void swapLeds() {
  switch (litLED) {
    case red:
      litLED = blue;
      setLeds();
      break;

    case blue:
      litLED = red;
      setLeds();
      break;

    default:
      break;
  }
}

void setLeds() {
  switch (litLED) {
    case red:
      digitalWrite(redLED, ledON);
      digitalWrite(blueLed, ledOFF);
      labelButton(red);
      break;

    case blue:
      digitalWrite(redLED, ledOFF);
      digitalWrite(blueLed, ledON);
      labelButton(blue);
      break;

    default:
      break;
  }
}

void checkPhysicalButton()
{
  if (digitalRead(PHYSICAL_BUTTON) == LOW) {
    // btnState is used to avoid sequential toggles BOUNCE_TIME check provides debounce
    if ((btnState != LOW) && (millis()-lastButtonPush > BOUNCE_TIME)) {
      // Swap states of LEDs
      swapLeds();
      lastButtonPush = millis();
    }
    btnState = LOW;
  } else {
    btnState = HIGH;
  }
}

BLYNK_WRITE(V0) {
  int value = param.asInt();
  if (value) {
    swapLeds();
  }
}

BLYNK_CONNECTED() {
  labelButton(litLED);
}

BLYNK_READ(V0) {
  labelButton(litLED);
}

void labelButton(ledSelect color) {
  switch (color) {  
    case red:
      Blynk.setProperty(V0, "label", "Red");
      Blynk.setProperty(V0, "color", BLYNK_RED);
      break;

    case blue:
      Blynk.setProperty(V0, "label", "Blue");
      Blynk.setProperty(V0, "color", BLYNK_BLUE);
      break;

    default:
      break;
  }  
}

That’s correct. SetProperty works only for Active project. This a feature indeed :slight_smile:. So you can close app. And properties will be changed, but project should be active.