Read Slider Values withtout loop()

Hello my friends,

iam a little confused and desprate.
Sorry for my bad english and my small knowledge about programming.

I have a wemos d1 mini pro. That connects to the wifi and sending Sensor Data to a SuperChart in the Blynk app. After that it fall sleeping. That works fine.

The whole script just runs in setup and in custom functions. I added an Slider on pin V20. This Value should be read on startup and is needed vor the calculation.

After some research i found this little code block:

int SensorHoehe;
BLYNK_WRITE(V20)
{
  SensorHoehe = param.asInt();
  Serial.print("V20 Slider value is: ");
  Serial.println(SensorHoehe);
}

and in my custom function a added

Blynk.run();

But in the serial monitor is no Text and the Value does not get read from Blynk. Whats wrong?
i dont want to post the code, its absolutly garbage at the moment …

That function typically only runs when the slider is changed. If your slider is moved when the device is asleep it will be missed. What you wan to do is, when the device wakes up, and is connected to BLYNK, use a Blynk.syncVirtual(V20); to trigger the associated BLYNK_WRITE(V20) function.

You need to call Blynk.syncVirtual(V20) to get the 'BLYNK_WRITE(V20)callback function to 'fire'. This needs to be done after you have established a connection to Blynk, and probably needs to be followed by aBlynk.run()` command.

Pete.

1 Like

Hey Pete,

i think you made my day. slowly i get there where i want to be. So after adding the syncVirtual and Blynk.run code. i get the serial output correctly with the value of the slider. Great! So i put the code before my calculation function. But the Sync runs after my calculation function and after sending the “wrong” data to Blynk. Like you say i need to establish a connection to Blynk (before i calculate and update blynk). how can i do this? (sorry if its a stupid question, iam an absolut beginner)

How are you initialising your Blynk connection, is it with Blynk.begin, or Blynk.config and Blynk.connect?

Pete.

You could try restructuring your code. Add the BLYNK_CONNECTED function to call the Blynk.syncVirtual(20) command, and call your calculation function at the end of the BLYNK_WRITE(V20)

BLYNK_CONNECTED() {
Blynk.syncVirtual(V20);
}

BLYNK_WRITE(V20)
{
  SensorHoehe = param.asInt();
  Serial.print("V20 Slider value is: ");
  Serial.println(SensorHoehe);
  calculationFunction(); //except use your functions name
}

at the beginning of the setup segment i use: Blynk.begin(auth, ssid, pass);

So that’s what establishes your Blynk connection, and your code wont progress beyond that point until you have a connection to your WiFi and the Blynk server. If you are calculating the wrong values then the problem is with your code structure. @Toro_Blanco’s recommendation is a good one.

However, using Blynk.begin is a bad move for projects that use deep sleep, especially if your device is battery powered.
If you device can’t connect to either WiFi or the Blynk server - for whatever reason - then the code execution will stop at Blynk.begin and your device will never go back to sleep; flattening the battery in the process.

The structure used in this code is much better:

Pete.

thank you for your answers.
i think restructuring the code is a good idea. Iam doing that right know. Iam not really familiar of this kind of programming. i know only a bit scripting.
I keep u updated the next days!
good night! :slight_smile: