Strange issue by restoring virtual pin value from Blynk server

Hi Guys,

This is my first post so please allow some patience for the newbie :wink:

I’m trying to run pretty simple tempperature measurement project and I’m really struggling with reading the proper value from Blynk virtual pin after Wemos D1 restart. I’d expect the following steps to be taken:
1 Start the MCU and initiate Blynk connection
2 Synchronize V1 pin
3 Assign V1 pin value to MinTemp variable
4 If MinTemp is equal to -127 (i.e. temp sensor was disconnected) then assign dummy value of 99 to MinTemp variable to allow proper min() calculation
5 Blynk connection completed
6 Set Blynk timer for periodic temperature measurement and upload
7 Check MinTemp value before temp measurement
8 Check MinTemp value before temp measurement

Unfortunately I’m facing strange behaviour: once ESP8266 was started, the MinTemp variable value read from Blynk server in BLYNK_WRITE(V1) routine is correct but then two lines down, once the code entered ‘if’ loop, the value changes to -127, see the serial monitor output below:

2 Pin V1 synchronized
5 Connected to Blynk
6 Timer for Temperature update set
3 Min Temp value read from Blynk server: 24	  *<--- correct value read from Blynk server*
4 Min Temp value within if loop: -127		  *<--- incorrect value in the 'if' loop*
7 Min Temp value before reading the sensor: 99    *<--- value changed properly in the 'if' loop*
8 Min Temp value after reading the sensor: 24˚C   *<--- correct value read from temp sensor*

I can also see that the steps 1-8 are executed in different order than expected but I don’t know if it’s causing any issues with the MinTemp variable value or not.

See below the sketch - I’ve simplified the original one to easily demonstrate the structure. There’s also Serial.print() command added with the corresponding step number for each above step:

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <OneWire.h>
#include <DallasTemperature.h>

OneWire oneWire(D1);
DallasTemperature TempSensor(&oneWire);
BlynkTimer TempTimer;

int curTemp;
int minTemp;

void setup() {
  Serial.begin(115200);
  Serial.println("\n1 System started.");
  pinMode(D1, INPUT);  
  TempSensor.begin();
  Blynk.begin(blynkAuth, wifi_ssid, wifi_pass);
  Serial.println(F("5 Connected to Blynk"));		
  TempTimer.setInterval(5000, TempUpdate);
  Serial.println(F("6 Timer for Temperature update set"));		
}

BLYNK_CONNECTED() {
  Blynk.syncVirtual(V1);
  Serial.println("2 Pin V1 synchronized");
}

BLYNK_WRITE(V1) {
  minTemp = param.asInt();
  Serial.println("3 Min Temp value read from Blynk server: " + String(minTemp));	
  if (minTemp = -127) {
	Serial.println("4 Min Temp value within if loop: " + String(minTemp));	
	minTemp = 99;
  }
}

void TempUpdate() {
  Serial.println("7 Min Temp value before reading the sensor: " + String(minTemp));
  TempSensor.requestTemperatures();
  curTemp = TempSensor.getTempCByIndex(0);
  minTemp = min(curTemp, minTemp);
  Blynk.virtualWrite(V1, minTemp);
  Serial.println("8 Min Temp value after reading the sensor: " + String(minTemp)+"˚C     ");
}

void loop() {
  Blynk.run();
  TempTimer.run();
}

Any help or tips how to identify/fix the issue would be appreciated.

Thanks
Maciej

Hey there,
Are you using blynk IOT or legacy ?

It would be really useful if you copied and pasted your serial monitor output (also between triple backticks).

Pete.

Peter,

Thanks for quick reply. The serial monitor output is in the post, it wasn’t properly formatted though - I fixed it already.

Thanks
Maciej

John,

Thanks for you reply. I tried both: initially I’ve built the app based on the legacy Blynk hosted on Raspberry Pi but as I couldn’t fix the issue there I created new template on Blynk IOT and simplified the sketch. In both cases the behaviour is exactly the same :frowning:

Maciej

Have you configured the datastream correctly ?

I hope so but I may be missing something, have a look pls:

BTW, note please that the data being originally read from Blynk server in V1 pin is correct (see serial monitor output in step no 3) but only then something changes is to -127 in th e’if loop (see serial monitor output in step 4)

The thing that jumps out is this…

Which should be …

if (minTemp == -127) {

Try changing this and see if it helps.

Pete.

If you would like to use this function in your sketch, you have to enable " sync with latest server value every time device connects to the cloud " option.

Sure it does, now it works like a charm!
I’ve spent so many hours digging in it just by one missed ‘=’ :slight_smile:

Thank you Peter very much for the hint. Thanks John also for your support and the tip re recent value.

Maciej

2 Likes

I think that’s just if you use the syncAll() command rather than syncVirtual(vPin)

Pete.

1 Like