State Syncing

A feature in the Blynk library firmware API that allows the hardware device and the Blynk server and apps to keep track of the same state.

State syncing ensures that the device and the server and the apps are always on the same page, even if there are network disruptions or other issues.

How State Syncing Works

State syncing in Blynk means that the device constantly sends updates to the server about its current state. For example, if you're controlling a light bulb with a hardware device, the device will send updates to the server about the current state of the light bulb (on or off). The server then sends these updates to the Blynk app, so you can see the current state of the light bulb in real-time.

If there's a network disruption or other issue that prevents the device from sending updates to the server, the server will automatically resend the last known state to the device when it comes back online. This ensures that the device is back online exactly in the state it was at the time of disruption.

How to Use State Syncing

To use state syncing in your Blynk project, you'll need to enable state syncing for the Datastreams that need this feature. You can do this in your Blynk Console by editing the datastream:

For hardware

If your hardware loses an Internet connection or resets, you can restore all the values from Datastreams in the Blynk app.


//here are handlers for the sync command

The Blynk.syncAll() command restores all Dastatstream's values based on the last saved values on the server. All analog and digital pin states will be restored. Every Virtual Pin will perform BLYNK_WRITE call.

If the datastream value is empty (doesn't have a default value,) and wasn't initialized, hardware will not get any response for those datastreams during sync.

Blynk.syncAll() returns data only for the datastreams marked as "Sync with latest server value" in your template setup.

Sync Hardware with App state

You can also request a single Virtual Pin value by calling Blynk.syncVirtual(V0) or you can request several datastreams with Blynk.syncVirtual(V0, V1, V2, ...). In case of the Blynk.syncVirtual() command you don't need to mark your datastream as "Sync with latest server value" in your template settings.

Storing a single value on the server

Storing multiple values on the server

For app

If you need to keep your hardware in sync with the Datastreams state even if the app is offline use Blynk.virtualWrite.

Imagine you have a LED Widget connected to the Virtual Pin V1 in the app, and a physical button attached to your hardware. When you press a physical button, you would expect to see the updated state of the LED Widget in the app. To achieve that you need to send Blynk.virtualWrite(V1, 255) when a physical button gets pressed.

Represent physical button state via LED widget with interrupts

Represent physical button state via LED widget with polling

Represent physical button state via Button widget with polling

Last updated