Hi Guys,
This is my first post so please allow some patience for the newbie
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