Initial state sync from device to app

My challenge is achieving efficient synchronization of a button’s state from my device to the app when the app starts.

Example:

  • In the Blynk app, there’s a button widget linked to a virtual pin on the ESP32.

Problem:

  • The primary concern arises when I launch the Blynk app. I need it to initiate a one-time read of the button’s current state from the ESP32 (and possibly along with other relevant values), as the state could have changed due to code running on the ESP.
  • From my understanding, Blynk doesn’t seem to offer a built-in event that automatically triggers a value read from the ESP32 upon app initiation.
  • My goal is to avoid unnecessary regular data syncing from the ESP32 to the Blynk server. I prefer syncing only upon state changes or specific events.
  • While Blynk provides a BLYNK_CONNECTED() event for when the ESP32 connects to the server, I’m looking for a solution that works when the Blynk app starts or reopens.

I could implement a manual sync by adding a button in the app for this purpose, but that would require user intervention each time.

Does anyone have insights or solutions for automatically triggering an ESP32 function when the Blynk app starts?

There is no longer an equivalent feature to the old AppConnected event that was available in Blynk Legacy. Presumably this is because it’s more complex because there is now the web dashboard as well as the mobile app, and because the assumption is that devices will send data to the server on a regular basis anyway.

The only reason not to keep sending data to the server is if you’re using a data-limited connection such as GPRS, but in reality devices need to keep doing handshake/ping data exchanges anyway to keep the device online and the data overhead if sensing data isn’t really that significant.

Pete.

It’s quite unfortunate that Blynk no longer has a feature equivalent to the old AppConnected event from Blynk Legacy. This feature seems essential, especially in scenarios where users need to immediately see the current status of a device, like for example the status of a relay (or 20 relays), upon opening the app. A delay in updating this status, could lead to user confusion, prompting them to press the button, potentially triggering unintended actions.

This situation necessitates frequent data syncing, at least every second, to ensure the interface displays the correct values when the app is opened. However, this approach seems counterintuitive and could impose unnecessary load on both the MCU and the cloud infrastructure, which is not ideal, especially in cases where data usage or bandwidth is a concern.

Perhaps this ought to be proposed as a feature request to Blynk, considering they also bear the burden of processing unnecessary data transmissions. While I’m not overly concerned about the web dashboard, I do believe that an option to syncAll should be feasible for both the mobile app and the web dashboard.

That statement doesn’t make much sense. The Blynk.syncAll or Blynk.syncVirtual(VPin) commands are requests send by the device to the server to ask the server to send the current state of all or some of the virtual datastreams.
This has nothing to do with the process that you’re wanting to achieve, which appears to be having a method of signalling to your device that either the app or web console is open so your device(s) should start sending data at a higher frequency.

Have you looked at the use of data invalidation to ensure that users aren’t confused by seeing old data?

You’re more than welcome to create an “Ideas” post, or contribute your use-case scenario to one an existing topic like this one…

Pete.

Sorry for using syncAll, I did not want to reference the command, but the principle that all the data should be send to the server.

I’ll have a look, but BLYNK_APP_CONNECTED was so much cleaner. What could be simpler that this example?

#define BLYNK_TEMPLATE_ID "YourTemplateID"
#define BLYNK_DEVICE_NAME "YourDeviceName"
#define BLYNK_AUTH_TOKEN "YourAuthToken"

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

// Your WiFi credentials
char ssid[] = "YourNetworkName";
char pass[] = "YourPassword";

// Virtual Pin
#define VIRTUAL_PIN V1

BlynkTimer timer;

void updateBlynkValues() {
    // Read values from your ESP32
    int sensorValue = analogRead(25); // Example reading

    // Update Blynk virtual pin
    Blynk.virtualWrite(VIRTUAL_PIN, sensorValue);
}

BLYNK_APP_CONNECTED() {
    // This function is called when the Blynk app connects to the device
    updateBlynkValues();
}

void setup() {
    WiFi.begin(ssid, pass);
    Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
    timer.setInterval(10000L, updateBlynkValues); // Update every 10 seconds
}

void loop() {
    Blynk.run();
    timer.run();
}
1 Like

@Cactixxx Please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Copy and paste these if you can’t find the correct symbol on your keyboard.

Pete.

Your Legacy code sends the correct data to the Bkynk server when the app is opened, but then it continues to update every 10 seconds, so quickly goes out of date while the app remains open and in use.
If the user is needing to know the up-to-the second status of these 20 relays whilst the app is in use then surly you would need to change the a BlynkTimer so that it runs at a higher frequency during the time when the app remains open?

Pete.

Thanks Pete, I know what the code does. Is was just a sample on how I would like to use the event that is not available anymore. I’ll continue to use Flutter for app development.

@Cactixxx

When your hardware connects to the server you do:

Blynk.sync() on the hardware side, so you get the latest state from the user/app (this is in case you don’t block the UI when the hardware is offline).

When the user opens the app there are 2 possible cases:

  1. Hardware is already connected and it has the latest state due to the Blynk.sync(); When user logins and changes anything all commands are send to the hardware in realtime. So the hardware is in sync with the user as long as it is connected.
  2. Hardware is not connected. Here we have 2 subcases:
    a) User logins and changes the state while the hardware offline. In that case, hardware will get the latest state with Blynk.sync() when it connects back;
    b) User logins and doesn’t change anything while the hardware is offline. In that case, hardware will get the latest state with Blynk.sync() when it connects back;

So it seems like there is no usage for app_connected event. Am I wrong?

1 Like