Insight needed; BLYNK_WRITE won't update variable

I’ve read that topic before posting and it doesn’t solve my problem. I think i already have implemented that part correctly in my code as below:

//BlynkTimer timer;
int uptimeCounter;

// This function will run every time Blynk connection is established
BLYNK_CONNECTED() {
  //get data stored in virtual pin V0 from server
  Blynk.syncVirtual(V0);
}

// restoring counter from server
BLYNK_WRITE(V0)
{
  //restoring int value
  uptimeCounter = param.asInt();
}

The problem is that that’s not working…

V0 needs to have a value before you can sync the value… what is on V0 in the App? Where does this initial value come from?

The first tries there was nothing there because the only thing i want is to store a variable to be retrieved later on. Because it didn’t work i tried putting a value display there and the value’s became visible on the app thus giving me feedback that at least the sending of the variable to the app worked.

The hardware will shut down and at startup i want it to read the variable which it stored last so it can continue from there on.

I read on this page that Can I read data stored in the server? that johanan was trying something similar.

I get the feeling that it A) does not retrieve the variable correctly with Blynk sync and _write or B) the variable is a local variable which does never reach the rest of the program like in the loop section.

I’m now trying to pinpoint the above statements but both seem invalid because if i run the code below i still don’t get any feedback in the serial monitor. This time i put a slider on V15 if only to try to get a variable to display on my hardware (in this case the serial monitor)

#include <Adafruit_SSD1306.h>
#include <OneWire.h>
#include <DallasTemperature.h>
 
// Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS 14
 
// Setup a oneWire instance to communicate with any OneWire devices 
// (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
 
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);




unsigned long lastlasttime;
unsigned long Nownow;

//BLYNK:

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
WidgetTerminal terminal(V1);
WidgetLCD lcd(V3);


// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "0a7c1f0a0b4b424498c763e785ce77a6"; //stefan
// This function will be called every time Slider Widget
// in Blynk app writes values to the Virtual Pin 1
BLYNK_CONNECTED() {
    Blynk.syncAll();
}

 int pinValue;
BLYNK_WRITE(V15)
{
  pinValue = param.asInt(); // assigning incoming value from pin V15 to a variable
  // You can also use:
  // String i = param.asStr();
  // double d = param.asDouble();
  Serial.print("V15 Slider value is: ");
  Serial.println(pinValue);
}


void setup() {
  Serial.begin(115200);
  Serial.setTimeout(2000);
  Blynk.begin(auth, "Tuypgroup", "Kleurboek1");
 // Serial.println("test");
// Wait for serial to initialize.
while(!Serial) { }

}

void loop()
{

  Blynk.run();
 //Serial.print("V15 Slider value is: ");
  //Serial.println(pinValue);
  }

The resulting serial monitor is (even tough the slider is adjusted to 764 in the app):

rl��r��c�n�����p�|����x��ǒ��p�nn��;�n����e�b�$errrl��r��#�n�����p�|����x��ǒ��p�nn��;�n����e�b�$rrp�n�������l�����b�n��n�$��b��<~�n�����l`e���#�n�rnr���;����{r�ےn��b��`eĞ�l�����8��r��n�b�[41] Connecting to Tuypgroup
[542] Connected to WiFi
[542] IP: 192.168.178.179
[542] Blynk v0.3.10 on Arduino
V15 Slider value is: 0
V15 Slider value is: 0
V15 Slider value is: 0
V15 Slider value is: 0
V15 Slider value is: 0
V15 Slider value is: 0
V15 Slider value is: 0
V15 Slider value is: 0
V15 Slider value is: 0
V15 Slider value is: 0
V15 Slider value is: 0
V15 Slider value is: 0
V15 Slider value is: 0
V15 Slider value is: 0
V15 Slider value is: 0
V15 Slider value is: 0
V15 Slider value is: 0
V15 Slider value is: 0
V15 Slider value is: 0
V15 Slider value is: 0
V15 Slider value is: 0
V15 Slider value is: 0
V15 Slider value is: 0
[5002] Connecting to blynk-cloud.com:8442
V15 Slider value is: 0
V15 Slider value is: 0
V15 Slider value is: 0
V15 Slider value is: 0
V15 Slider value is: 0
V15 Slider value is: 0
V15 Slider value is: 0
V15 Slider value is: 0
V15 Slider value is: 0
V15 Slider value is: 0
V15 Slider value is: 0
V15 Slider value is: 0
V15 Slider value is: 0
V15 Slider value is: 0
V15 Slider value is: 0
V15 Slider value is: 0
V15 Slider value is: 0
V15 Slider value is: 0
V15 Slider value is: 0
V15 Slider value is: 0
V15 Slider value is: 0
V15 Slider value is: 0
V15 Slider value is: 0
V15 Slider value is: 0
V15 Slider value is: 0
V15 Slider value is: 0
[5288] Ready (ping: 43ms).
V15 Slider value is: 0
V15 Slider value is: 0
V15 Slider value is: 0
�rl��r��c�n�����p�|����x��ǒ��p�nn��;�n����e�b�$rrp�n�������l�����b�n��n�$��b��>~�n�����l`e���#�n�rnr���;��?��{r�ےn��b��`eĞ�l�����x��r��n�b�[41] Connecting to Tuypgroup
[542] Connected to WiFi
[542] IP: 192.168.178.179
[542] Blynk v0.3.10 on Arduino

This may or may not be important information: serial monitor never prints the result of this piece of code

BLYNK_WRITE(V15)
{
  pinValue = param.asInt(); // assigning incoming value from pin V15 to a variable
  // You can also use:
  // String i = param.asStr();
  // double d = param.asDouble();
  Serial.print("V15 Slider value is: ");
  Serial.println(pinValue);
}

I’ve checked by turning all other serial.prints off. maybe the value doesn’t get written in the first place because the function is never called?

Blynk sync works, but there needs to be something in the server (previous value) in order to “sync” it :wink:

That is entirely dependent on how you write your code… declare the variable in pre-setup and it becomes global.

E.g pinvalue doesn’t seem to be declared anywhere?.. that should cause a compiling error.

You have nothing in your code supplying a value, and unless V15 is attached to a widget… and one that actually supples a value, either manually (you) or automatically (Eventor, Timer or something), then as stated… nothing will happen as there is no initial value.

In the first example the variable is empty at the start. Then;
• The code starts running
• It tries to retrieve the variable from the server but can’t because it’s empty
• It goes on with the code and it assigns it a number
• It goes on to rewrite itself a couple of times
• It is written to the virtual pin
• The program stops and the esp goes to sleep

Now it restarts and the following should happen;
• The code starts running
• It tries to retrieve the variable from the server and this time it can because it has been written in the last iteration
• It goes on with the code and it assigns it a number
• It goes on to rewrite itself a couple of times
• It is written to the virtual pin
• The program stops and the esp goes to sleep

Repeat, repeat, etc.

In the second code the variable has been written from the start because of a slider value of whatever i put the slider in the app to, in this case 734. The 734 never seems to make it to the hardware…

If you read the documention about virtual pins you’ll see that BLYNK_WRITE(V15) is only triggered when the value of the slider connected to V15 changes.

I think you need a Blynk.syncVirtual(v15) in there to trigger the BLYNK_WRITE to return a value…

http://docs.blynk.cc/#blynk-firmware-virtual-pins-control-blynksyncvirtualvpin

Pete.

Yes, but even when i have the app open and slide to and fro it still does not update. The

BLYNK_CONNECTED() {
  //get data stored in virtual pin V0 from server
  Blynk.syncVirtual(V0);
}

In the first sketch and;

 BLYNK_CONNECTED() {
     Blynk.syncAll();
 }

In the second sketch

Should take care of this doesn’t it? or do i need to call this on another point in the code?

It would have Ben useful if you’d made that clear right from the start.

I’m lost about why you’re using V0 in one sketch and V15 in the other. It makes it very difficult to follow the thread of what you’ve been doing with the two different code versions and the app.

It would also help if you tidied up the code a bit, with your global variable declarations at the top, followed by void setup, then either your finctions or void loop. It would make it much easier for others to follow, and may fix some of your issues.

Pete.

Forgive me if I’m a bit chaotic, it’s just because I’m spending pretty much time on this by trying out the most frivolous things because if even with clean freshly downloaded code of other peoples projects it just does not work for me.

I did say that already, i’m just using different virtuals in the hope that something as simple as that could solve it. I’m also trying to figure this out and trying out all of your comments as i go along. The first V0 was tied to an value display and the V15 tied to slider. As I’m troubleshooting from multiple sources and codes I tend to keep the Virtuals names as they are.

Beneath the tidied code and the result in the serial monitor

#    define BLYNK_PRINT Serial
    #include <ESP8266WiFi.h>
    #include <BlynkSimpleEsp8266.h>

    //Global variable declarations
    int pinValue = 20; //doesnt matter, slider should sync from the slider in the app set to v15 at the moment set to 412
    char auth[] = "0a7c1f0a0b4b424498c763e785ce77a6"; //stefan

    //Void setup
    void setup() {
      Serial.begin(115200);
      Serial.setTimeout(2000);
      Blynk.begin(auth, "Tuypgroup", "Kleurboek1");
      }

    //Functions
    BLYNK_CONNECTED() {
        Blynk.syncVirtual(V15); //If i understand correctly the hardware should at this moment be told that V15 is set to 412
    }
    BLYNK_WRITE(V15)
    {
      pinValue = param.asInt(); //And at this moment V15 is parsed asInt into the variable "pinValue"
      Serial.print("V15 Slider value is: ");
      Serial.println(pinValue); //and thus should print 412
    }


    //Void Loop
    void loop(){
     Blynk.run();
     Serial.print("V15 Slider value is: ");
     Serial.println(pinValue); // still should print 412
      }

Serial monitor:

rl��r��c�n�����p�|����x��ǒ��p�nn��;�n����e�b�$	rrrl��r��c�n�����p�|����x��ǒ��p�nn��;�n����e�b�$rrp�n�������l�����b�n��n�$��b��<~�n��Ì�l`e���#�n�rnr���;����{r�ےn��b��`eĞ�l�����x��r��n�b�[43] Connecting to Tuypgroup
[5043] Connected to WiFi
[5044] IP: 192.168.178.179
[5044] Blynk v0.3.10 on Arduino
[5044] Connecting to blynk-cloud.com:8442
V15 Slider value is: 20
V15 Slider value is: 20
V15 Slider value is: 20
V15 Slider value is: 20
V15 Slider value is: 20
V15 Slider value is: 20
V15 Slider value is: 20
V15 Slider value is: 20
V15 Slider value is: 20
V15 Slider value is: 20
V15 Slider value is: 20
V15 Slider value is: 20
V15 Slider value is: 20
V15 Slider value is: 20
V15 Slider value is: 20
V15 S�rl��r��c�n�����p�|����x��ǒ��p�nn��;�n����e�b�cl`e$`e�p�n�������l�����b�n��n�$��b��<~�n�����l`e���#�n�rnr���;����{r�ےn��b��`eĞ�l�����x��r��n�b�[42] Connecting to Tuypgroup
[1543] Connected to WiFi
[1543] IP: 192.168.178.179
[1543] Blynk v0.3.10 on Arduino
V15 Slider value is: 20
V15 Slider value is: 20
V15 Slider value is: 20
V15 Slider value is: 20
V15 Slider value is: 20
V15 Slider value is: 20
V15 Slider value is: 20
V15 Slider value is: 20
V15 Slider value is: 20

I’m pretty much just trying to save a variable to use it later on in the loop. At this moment I’m just grasping at straws as how to do that and trying everything. The ideal situation would just be to write some variables to the server without a widget or whatever connected to it and retrieve it later on to update it, and store it away again.

After 8 of these iterations i could calculate a median and send that to a display in the app as “the average temperature over night”

Code looks fine to me, what’s you Library and App version? Phone and Board?

Saying that your slider is set to a specific value, an that moving the slider doesn’t produce an output are very different things - at least in my book.

I do t think you’re using the BLYNK_CONNECTED function correctly.
http://docs.blynk.cc/#blynk-firmware-connection-management-blynkconnected

This doesn’t explain why the BLYNK_WRITE (V15) function isn’t working, but with that Serial.print statement in your void loop you could be missing serial monitor output that results from moving the slider widget.

This may sound like a silly question, but have you checked that the slider on V15 in the app is connected to the same project as the Auth code that you’re using in your code? I’ve caused myself problems by connecting a widget to a different project from the one I’m working on in my code!

Pete.

Library:
Tried maybe five, did most on 0.3.3, tried 0.4.1, 0.4.2, 0.5.1
Now running on 0.5.2 just to be one step ahead of your next comment (“you should update the library” :wink: ) but to no avail

App:
Version 2.18.0(1)

Phone:
Iphone SE

Board:
ESP-12-F

Not a silly question at all, I’m pretty sure that it is something simple as this that will eventually prove to be the cause this;
Yes I checked before hand and just to be sure I checked again.

The explanation at the end of this link

BLYNK_CONNECTED()
This function is called every time Blynk gets connected to the server. It’s convenient to call sync functions here.
BLYNK_CONNECTED() {
// Your code here
}

Is pretty limited, the only thing I learn from this that at the moment of the establishment of a connection between my esp and the blink app the code between the brackets runs. If at that moment the app syncs the virtual pint I’m happy whit that because it shouldn’t sync anymore after that anyway. (until i need to store it that is)

Important note (I think); The loop won’t run on 0.5.2, it just never gets to it, the same tidy code as above gives the below serial on 0.5.2

rl��r��c�n�����p�|����x��ǒ��p�nn��;�n����e�b�$rrp�n�������l�����b�n��n�$��b��>~�n�����l`e���#�n�rnr���;��?��{r�ےn��b��`eĞ�l�����x��r��n�b�[43] Connecting to Tuypgroup
[544] Connected to WiFi
[544] IP: 192.168.178.179
[544] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.5.2 on Arduino

[549] Connecting to blynk-cloud.com:80
[639] �rl��r��c�n�����p�|����x��ǒ��p�nn��;�n����e�b�$�rrp�n�������l�����b�n��n�$��b��<~�n��Ì�l`���#�n�rnr���;����{r�ےn��b��`eĞ�l�����x��r��n�b�[43] Connecting to Tuypgroup
[544] Connected to WiFi
[544] IP: 192.168.178.179
[544] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.5.2 on Arduino

[549] Connecting to blynk-cloud.com:80
[711] �rl��r��c�n�����p�|����x��ǒ��p�nn��;�n����e�b�$�rrp�n�������l�����b�n��n�$��b��>~�n�����l`e���#�n�rnr���;��?��{r�ےn��b��`eĞ�l�����x��r��n�b�[43] Connecting to Tuypgroup
[543] Connected to WiFi
[543] IP: 192.168.178.179
[543] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.5.2 on Arduino

[549] Connecting to blynk-cloud.com:80
[698] �rl��r��c�n�����p�|����x��ǒ��p�nn��;�n����e�b�$rrp�n�������l�����b�n��n�$��b��>~�n�����l`e���#�n�rnr���;����{r�ےn��b��`eĞ�l�����p��r��n�b�[43] Connecting to Tuypgroup
[543] Connected to WiFi
[543] IP: 192.168.178.179
[543]

Try removing this from your loop().

Add this under you auth code

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Tuypgroup";
char pass[] = "Kleurboek1";

and change this

to this:

Blynk.begin(auth, ssid, pass);

If it manages to connect to BLYNK, you should see the value printed to serial monitor. Then move the slide and see if it prints the new valve.

Not sure if it will solve the issue, but may be worth a try.

So you’re saying I’m not connecting right? I did managed to send values to the blynk app tough… so I know I did made a connection at some point… (this is a new project by the way, I’m having a working project | a beer brewery | on another arduino and it works just fine. I just never had to send data back to the hardware before…)

I’ve tried it before when I came across this link ([SOLVED] Blynk app says "device is offline" - #3 by sanjaysy), alas, did not work… tried it again tough just to try to pinpoint to the source of the problem with you all on this tread. Resulting serial:

rl��r��c�n�����p�|����x��ǒ��p�nn��;�n����e�b�cl`e$`e�rl��r��c�n�����p�|����x��ǒ��p�nn��;�n����e�b�cl`e$`e�p�n�������l�����b�n��n�$��b��>~�n�����l`e���#�n�rnr���;����{r�ےn��b��`eĞ�l�����x��r��n�b�[43] Connecting to Tuypgroup
[3544] Connected to WiFi
[3544] IP: 192.168.178.179
[3544] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.5.2 on Arduino

[3550] Connecting to blynk-cloud.com:80
[3645]�rl��r��c�n�����p�|����x��ǒ��p�nn��;�n�����b�$	rrp�n�������l�����b�n��n�$��b��<~�n��Ì�l`e���#�n�rnr���;����sr�ےn��b��`eĞ�l�����x��r��n�b�[43] Connecting to Tuypgroup
[1543] Connected to WiFi
[1544] IP: 192.168.178.179
[1544] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.5.2 on Arduino

[1550] Connecting to blynk-cloud.com:80
[1634]�rl��r��c�n�����p�|����p��ǒ��p�nn��;�n����e�b�$errp�n�������l�����b�n��n�$��b��>~�n�����l`e���#�n�rnr���;����{r�ےn��b��`eĞ�l�����8��r��n�b�[43] Connecting to Tuypgroup
[1543] Connected to WiFi
[1543] IP: 192.168.178.179
[1543] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.5.2 on Arduino

[1549] Connecting to blynk-cloud.com:80
[1641]�rl��r��c�n�����p�|����x��ǒ��p�nn��;�n����e�b�$	rrp�n�������l�����b�n��n�$��b��>~�n�����l`e���#�n�rnr���;����{r�ےn��b��`eĞ�l�����x��r��n�b�[43] Connecting to Tuypgroup
[544] Connected to WiFi
[544] IP: 192.168.178.179
[544] 
    ___  __          __

I did move the slider, the app said “device is offline”

No, I’m not saying that. Just throwing some things out there to try. May or May not help.

I loaded your above code onto an esp I have sitting here at work. You code works as it should when I test it. So this point to an issue on your end. What it is, I am not certain.

P.S. I didn’t do the edits I suggested either, so u can disregard my previous attempt to help.

P.P.S. Having this in the loop() may cause disconnection issues. As you have this same line in the BLYNK_WRITE() function, it should print the valve to the serial monitor if it changes.

   Serial.print("V15 Slider value is: ");
     Serial.println(pinValue); // still should print 412

Just an update, I got a new WeMos D1 Mini WIFI with an esp-12-f integrated onto it and the code worked without problem. I still have to get it to work on a standalone esp. Now I know for sure that it is as Toro_blanco said an hardware problem and I can troubleshoot directed at that.