Hi,
I’m working on Wemos D1 temperature sensor project. I’ve removed any unnecessary code to clearly illustrate the issue. There are few steps supposed to be executed:
- Wake up, connect to wifi and local Blynk server
- Restore last min temperature value from Blynk server
- Measure current temperature and update min temperature value (if needed)
- Write to Blynk server current temperature and new min temperature
- Go to deep sleep
See below the simplified sketch:
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include "WifiBlynkPass.h" // wifi & local Blynk server parameter (e.g. wifi_ssid, my_ip, blynkServer)
#include <DHT.h>
DHT dht(D1, DHT11);
unsigned long prevMillis;
float curTemp = 0;
float minTemp = 99;
void setup() {
Serial.begin(115200);
pinMode(D1, INPUT);
pinMode(D0, WAKEUP_PULLUP);
dht.begin();
WiFi.mode(WIFI_STA);
WiFi.config(my_ip, gateway, mask);
Serial.println("\n01 System started, connecting WiFi ");
WiFi.begin(wifi_ssid,wifi_pass);
Blynk.config(blynkAuth,blynkServer,blynkPort);
prevMillis = millis();
while (WiFi.status() != WL_CONNECTED && millis() - prevMillis < 7000) {
delay(200);
}
Blynk.connect();
if (Blynk.connected()) {
Serial.println("02 Wi-Fi connected, Blynk connected");
TempUpdate();
Blynk.run();
}
Serial.println("06 Going to sleep...");
system_deep_sleep(10 * 1000 * 1000);
delay(100);
}
BLYNK_CONNECTED() {
Blynk.syncVirtual(V1);
Serial.println("03 Blynk Connection Handler kicked off");
}
BLYNK_WRITE(V1) {
minTemp = param.asFloat();
Serial.println("04 minTemp from Blynk server: " + String(minTemp));
}
void TempUpdate() {
delay(500); // Added for testing only
curTemp = dht.readTemperature();
minTemp = min(curTemp, minTemp);
Serial.println("05 CurTemp from Sensor: " + String(curTemp) + "˚C, Calculated MinTemp: " + String(minTemp) + "˚C");
Blynk.virtualWrite(V0, curTemp);
Blynk.virtualWrite(V1, minTemp);
}
void loop() {
}
I’ve added some serial output lines, and I was expecting them to be printed according to the numbering in the sketch, i.e.:
01 System started, connecting WiFi
02 Blynk Connection Handler kicked off
03 minTemp read from Blynk server: 23.00˚C
04 Current Temp from sensor: 23.10˚C, Calculated Min Temp: 23.00˚C
05 Going to sleep...
Unfortunately, it looks like the BLYNK_WRITE(V1) function is triggered late, i.e., only after the current temperature was read from the sensor and the min temperature was calculated. See the actual serial output below:
01 System started, connecting WiFi
02 Blynk Connection Handler kicked off
04 Current Temp from sensor: 23.10˚C, Calculated Min Temp: 23.10˚C
03 minTemp read from Blynk server: 23.00˚C
05 Going to sleep...
In result, the minTemp is not calculated correctly, additionally the calculated minTemp is then overwritten by the minTemp value that was received from Blynk server.
I don’t know what’s causing late triggering of BLYNK_WRITE(V1) function. I understand it should be triggered immediately after Blynk was connected. I thought there may be some delays in communication with Blynk server, so I’ve even added little delay before curTemp reading but it didn’t help - the minTemp value is always received from the Blynk server after curTemp was read.
Could you please help fixing the issue, i.e. making sure the last minTemp was restored from Blynk server immediately after Wemos was started and Blynk connected.
Thanks
Maciej