Insight needed; BLYNK_WRITE won't update variable

Hi All,

I’m busy with building a battery powered temperature sensor. Being battery powered i want to put it in sleepmode for most of the time, i only need the temperatures between 00.00 and 09.00.

There are a couple of things i need to do;

  1. Display the hourly measured temperature on every hour between 00.00 and 09.00. (Value Display)
  2. Display the average temperature measured between 00.00 and 09.00. (Value Display)
  3. Display this for every day the past 6 weeks (SuperChart)

For all these things i’m getting stuck on the fact that after sleepmode the esp does not remember variables. So i need to take them from the last variable written to the virtual pins when esp was awake the last time. I just cant seem to build this.

If i am to understand correctly from Can I read data stored in the server?
the steps would be

  1. Blynk.syncVirtual
  2. BLYNK_WRITE
  3. Use variable however i want
  4. Blynk.virtualWrite
  5. Close program
  6. Restart steps

Thus the code below should work shouldnt it?
Can someone help me out with what i’m doing wrong?
The variable in the app itself is updating but the hardware never seems to pull the updated variable back to re-use

#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);


//Time basic settings

int dayNumber;
int startingHour = 0; // set your starting hour here, not at int hour. This ensures accurate daily correction of time
int seconds = 0;
int minutes = 0;
int days = 0;

//Blynk and wifi

char auth[] = "0a7c1f0a0b4b424498c763e785ce77a6"; //stefan

//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();
}

void increment() {
  uptimeCounter++;

  //storing int in V0 pin on server
  Blynk.virtualWrite(V0, uptimeCounter);
}

unsigned long timeNow = 0;
unsigned long timeLast = 0;
int timeBlynkLast;
int blynkCalled;
int hours = startingHour;
int minutesCounter = 0;  
int timeEstimate = 0;
int timeTemp = 0;
int temperature = 24.0;
int TempInF = 77.00;
int TempInC = 24.00;
int Count = 0;
int getal = 0;



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

while(!Serial) { }
}




void blynkUpdate1 () {

  if ((timeNow - timeBlynkLast) > 7 && blynkCalled == 0) {
Blynk.virtualWrite(9, TempInC);

  Serial.println("Blynk has been updated");
    blynkCalled = 1;
 // Serial.println(strDay);
  }

}



void loop() {


  Blynk.run();
  blynkUpdate1 ();
  
//rowIndex+=1;

  //TEMPERATURE SETTINGS

  sensors.begin();
  sensors.requestTemperatures();  // Send the command to DS18B20 to get temperatures
  temperature = sensors.getTempCByIndex(0);
  TempInC = sensors.getTempCByIndex(0); // Get and save sensor value
  Serial.println("TempInC=");
  Serial.println(TempInC);


  timeNow = millis() / 1000; // the number of milliseconds that have passed since boot
  seconds = timeNow - timeLast;//the number of seconds that have passed since the last time 60 seconds was reached.
  if (seconds >= 60) {
    timeLast = timeNow;
    minutes = minutes + 1;
    minutesCounter = minutesCounter + 1;
  }
  //if one minute has passed, start counting milliseconds from zero again and add one minute to the clock.
  if (minutes >= 60) {
    minutes = 0;
    hours = hours + 1;
  }
  // if one hour has passed, start counting minutes from zero and add one hour to the clock
  if (hours == 24) {
    hours = 0;
    days = days + 1;
  }
  //if 24 hours have passed , add one day

  if (hours == (24 - startingHour) && correctedToday == 0) {
    delay(dailyErrorFast * 1000);
    seconds = seconds + dailyErrorBehind;
    correctedToday = 1;
  }
  //every time 24 hours have passed since the initial starting time and it has not been reset this day before, add milliseconds or delay the progran with some milliseconds.
  //Change these varialbes according to the error of your board.
  //The only way to find out how far off your boards internal clock is, is by uploading this sketch at exactly the same time as the real time, letting it run for a few days
  // and then determine how many seconds slow/fast your boards internal clock is on a daily average. (24 hours).
  if (hours == 24 - startingHour + 2)  {
    correctedToday = 0;
  }

  Serial.println("I'm awake.");


 
  if (Count == 10){
    increment();
         Serial.println("uptimeCounter =");
         Serial.println(uptimeCounter);

     Serial.println("Going into deep sleep for 20 seconds");
//  ESP.deepSleep((20e6*3)*60); // 20e6 is 20 microseconds
   ESP.deepSleep(20e6 ); // 20e6 is 20 microseconds
  Count = 0;
  }
  Count +=1;
}  // end void loop

please format you’re submitted code first using the back ticks or use the </> in the menu, before anyone is going to read it.

I have done so, thanks for letting me know. I’m a first timer here :wink:

I had a look and I think you’re looking for:

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

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]