HI,
I’m trying to save some energy by by sending sensor data once in a 5-10 minutes and using Light Sleep between them.
Maybe I’ll switch to the Deep Sleep later but I’d like to understand this strange behavior with the Light Sleep.
The idea for the loop is:
- Check if Blynk is connected
- If Yes - run it and go to the Light Sleep for 2 minutes (quite a short period only is for faster debugging)
- print the reason
- print increased Bootcount
- if No - Blynk.connect()
sending data to the Blynk server and mqtt is implemented inside the BLYNK_CONNECTED()
I believe that esp_sleep_enable_timer_wakeup() could be kept in the setup() but moved to the loop() just to try.
a dot is printed in the beacon() just to check the loop is completed.
print_wakeup_reason() - I took it from the Deep sleep example, not sure if it is relevant for the Light sleep. Strange that it prints “Wakeup caused by touchpad” instead of “Wakeup caused by timer”
The project starts working normally. But after some iterations it fails in reconnecting to Blynk.
It writes “Certificate OK” and that’s it.
I’ve tried to restart my local Blynk server but it does not help.
If I restart my TTGO ESP32 it connects properly and works fine for several iteration.
So I believe taht the issue is on the client side, not on the Blynk sever side.
Do you have any ideas what causes “Certificate OK” without connection and how to handle this?
By the way, I’ve seen such issues ealier without “sleep” (disabled/enabled Internet access for this ESP32).
It looks like Blynk poorly survives multiple re-connections but I have no idea how to debug it (or to properly handle and send the MCU to restart).
Log (complete, with multiple successful sleep cycles):
Starting v2.02
[61] Connecting to ASR
[1151] Connected to WiFi
[1151] IP: 192.168.6.59
[1151]
___ __ __
/ _ )/ /_ _____ / /__
/ _ / / // / _ / '/
///_, /////_
/__/ v0.6.1 on ESP32[1224] Connecting to ha2.r-as.ru:9443
[2663] Certificate OK
[3264] Ready (ping: 600ms).
[3656] Time sync: OK
[3793] 0 38.849396
11:30:25 25°С 39%
Brightness Slider VPIN_Bright value is: 2
[3861] Voltage: 3160.50
MQTT connecting …connected
[4121] Subscribe: 1
[5123] Go to sleep in 1 sec…
[126123] Bootcount: 1
Wakeup caused by touchpad
.[126125] Heartbeat timeout
[126128] Connecting to ha2.r-as.ru:9443
[128358] Certificate OK
[128859] Ready (ping: 500ms).
[129250] Time sync: OK
Brightness Slider VPIN_Bright value is: 2
[129388] 1 38.681549
11:32:30 25°С 39%
[129454] Voltage: 4002.25
.[129523] Go to sleep in 1 sec…
[250524] Bootcount: 2
Wakeup caused by touchpad
.[250526] Heartbeat timeout
[250528] Connecting to ha2.r-as.ru:9443
[252790] Certificate OK
[253288] Ready (ping: 497ms).
[253680] Time sync: OK
[253819] 1 38.612885
11:34:33 25°С 39%
[253886] Voltage: 4004.00
Brightness Slider VPIN_Bright value is: 2
.[253966] Go to sleep in 1 sec…
[374966] Bootcount: 3
Wakeup caused by touchpad
.[374968] Heartbeat timeout
[374970] Connecting to ha2.r-as.ru:9443
[377369] Certificate OK
[377901] Ready (ping: 531ms).
[378291] Time sync: OK
Brightness Slider VPIN_Bright value is: 2
[378426] 0 38.849396
11:36:37 25°С 39%
[378494] Voltage: 4004.00
.[378564] Go to sleep in 1 sec…
[499564] Bootcount: 4
Wakeup caused by touchpad
.[499566] Heartbeat timeout
[499568] Connecting to ha2.r-as.ru:9443
[501160] Certificate OK
[501557] Ready (ping: 396ms).
[501947] Time sync: OK
Brightness Slider VPIN_Bright value is: 2
[502085] 0 38.948578
11:38:39 25°С 39%
[502153] Voltage: 4004.00
.[502223] Go to sleep in 1 sec…
[623223] Bootcount: 5
Wakeup caused by touchpad
.[623225] Heartbeat timeout
[623227] Connecting to ha2.r-as.ru:9443
[624698] Certificate OK
[624899] Ready (ping: 199ms).
[625288] Time sync: OK
Brightness Slider VPIN_Bright value is: 2
[625424] 0 38.757843
11:40:41 25°С 39%
[625493] Voltage: 4004.00
. [625562] Go to sleep in 1 sec…
[746563] Bootcount: 6
Wakeup caused by touchpad
.[746565] Heartbeat timeout
[746567] Connecting to ha2.r-as.ru:9443
[748001] Certificate OK
[748399] Ready (ping: 396ms).
[748791] Time sync: OK
Brightness Slider VPIN_Bright value is: 2
[748926] 0 38.788361
11:42:43 24°С 39%
[748994] Voltage: 4005.75
.[749064] Go to sleep in 1 sec…
[870064] Bootcount: 7
Wakeup caused by touchpad
.[870066] Heartbeat timeout
[870069] Connecting to ha2.r-as.ru:9443
[871620] Certificate OK
[871822] Ready (ping: 200ms).
[872210] Time sync: OK
Brightness Slider VPIN_Bright value is: 2
[872347] 0 38.895172
11:44:45 24°С 39%
[872416] Voltage: 4004.00
.[872486] Go to sleep in 1 sec…
[993486] Bootcount: 8
Wakeup caused by touchpad
.[993488] Heartbeat timeout
[993490] Connecting to ha2.r-as.ru:9443
[994956] Certificate OK
[995159] Ready (ping: 201ms).
[995548] Time sync: OK
Brightness Slider VPIN_Bright value is: 2
[995684] 0 38.658661
11:46:47 24°С 39%
[995752] Voltage: 3998.75
.[995822] Go to sleep in 1 sec…
[1116822] Bootcount: 9
Wakeup caused by touchpad
.[1116824] Heartbeat timeout
[1116826] Connecting to ha2.r-as.ru:9443
[1118280] Certificate OK
[1118486] Ready (ping: 204ms).
[1118875] Time sync: OK
Brightness Slider VPIN_Bright value is: 2
[1119012] 0 38.788361
11:48:49 24°С 39%
[1119081] Voltage: 4004.00
.[1119151] Go to sleep in 1 sec…
[1240151] Bootcount: 10
Wakeup caused by touchpad
.[1240153] Heartbeat timeout
[1240155] Connecting to ha2.r-as.ru:9443
[1241632] Certificate OK
[1241843] Ready (ping: 209ms).
[1242234] Time sync: OK
Brightness Slider VPIN_Bright value is: 2
[1242371] 0 38.658661
11:50:51 24°С 39%
[1242439] Voltage: 4004.00
. [1242509] Go to sleep in 1 sec…
[1363509] Bootcount: 11
Wakeup caused by touchpad
.[1363512] Heartbeat timeout
[1363513] Connecting to ha2.r-as.ru:9443
[1365402] Certificate OK
[1365635] Ready (ping: 231ms).
[1366025] Time sync: OK
Brightness Slider VPIN_Bright value is: 2
[1366162] 0 38.773102
11:52:54 24°С 39%
[1366231] Voltage: 4000.50
.[1366300] Go to sleep in 1 sec…
[1487300] Bootcount: 12
Wakeup caused by touchpad
.[1487302] Heartbeat timeout
[1487304] Connecting to ha2.r-as.ru:9443
[1489071] Certificate OK
[1489574] Ready (ping: 501ms).
[1489964] Time sync: OK
Brightness Slider VPIN_Bright value is: 0
[1490101] 0 38.704437
11:54:56 24°С 39%
[1490169] Voltage: 4004.00
.[1490239] Go to sleep in 1 sec…
[1611239] Bootcount: 13
Wakeup caused by touchpad
.[1611242] Heartbeat timeout
[1611243] Connecting to ha2.r-as.ru:9443
[1612657] Certificate OK
[1613466] Ready (ping: 807ms).
[1613855] Time sync: OK
Brightness Slider VPIN_Bright value is: 0
[1613991] 0 38.589996
11:56:59 24°С 39%
[1614060] Voltage: 4035.50
.[1614130] Go to sleep in 1 sec…
[1735130] Bootcount: 14
Wakeup caused by touchpad
.[1735132] Heartbeat timeout
[1735134] Connecting to ha2.r-as.ru:9443
[1736551] Certificate OK
[1737359] Ready (ping: 806ms).
[1737747] Time sync: OK
Brightness Slider VPIN_Bright value is: 0
[1737884] 0 38.551849
11:59:01 25°С 39%
[1737953] Voltage: 4004.00
.[1738023] Go to sleep in 1 sec…
[1859023] Bootcount: 15
Wakeup caused by touchpad
Something goes wrong:
.[1859025] Heartbeat timeout
[1859027] Connecting to ha2.r-as.ru:9443
[E][ssl_client.cpp:87] start_ssl_client(): Connect to Server failed!
[E][WiFiClientSecure.cpp:132] connect(): start_ssl_client: -1
.
[1864027] Connecting to ha2.r-as.ru:9443
[E][ssl_client.cpp:87] start_ssl_client(): Connect to Server failed!
[E][WiFiClientSecure.cpp:132] connect(): start_ssl_client: -1
…[1869028] Connecting to ha2.r-as.ru:9443
[1870577] Certificate OK
…[1875580] Connecting to ha2.r-as.ru:9443
[1877025] Certificate OK
…[1882028] Connecting to ha2.r-as.ru:9443
[1883684] Certificate OK
…[1888687] Connecting to ha2.r-as.ru:9443
[1890250] Certificate OK
… [1895253] Connecting to ha2.r-as.ru:9443
[1896894] Certificate OK
…[1901897] Connecting to ha2.r-as.ru:9443
[1903350] Certificate OK
…[1908353] Connecting to ha2.r-as.ru:9443
[1909893] Certificate OK
…[1914896] Connecting to ha2.r-as.ru:9443
[1916348] Certificate OK
…[1921351] Connecting to ha2.r-as.ru:9443
[1922817] Certificate OK
… [1927820] Connecting to ha2.r-as.ru:9443
[1929358] Certificate OK
I restart my Blynk server:
…[1934361] Connecting to ha2.r-as.ru:9443
[E][ssl_client.cpp:87] start_ssl_client(): Connect to Server failed!
[E][WiFiClientSecure.cpp:132] connect(): start_ssl_client: -1
…[1939362] Connecting to ha2.r-as.ru:9443
[1940855] Certificate OK
…[1945858] Connecting to ha2.r-as.ru:9443
[1947270] Certificate OK
…[1952273] Connecting to ha2.r-as.ru:9443
[1953729] Certificate OK
…
[1958732] Connecting to ha2.r-as.ru:9443
[1960299] Certificate OK
…[1965302] Connecting to ha2.r-as.ru:9443
[1966836] Certificate OK
…[1971839] Connecting to ha2.r-as.ru:9443
[1973301] Certificate OK
…[1978304] Connecting to ha2.r-as.ru:9443
[1979845] Certificate OK
…[1984848] Connecting to ha2.r-as.ru:9443
[1986598] Certificate OK
… [1991601] Connecting to ha2.r-as.ru:9443
[1993051] Certificate OK
…[1998054] Connecting to ha2.r-as.ru:9443
[1999607] Certificate OK
.
Any ideas, please?
Well, some code:
#define uS_TO_S_FACTOR 1000000 /* Conversion factor for micro seconds to seconds */
#define TIME_TO_SLEEP 120 /* Time ESP32 will go to sleep (in seconds) */
RTC_DATA_ATTR int bootCount = 0;
void newGraphValue() {
char str[20];
int status;
float temp_c = sht20.temperature();
float hum = sht20.humidity();
terminal.printf("%3i ", bootCount);
terminal.flush();
display.clear(); // clear the display
display.drawRect(0,0,128,64);
if (temp_c < 100) {
Blynk.virtualWrite(VPIN_Temp1, temp_c);
Blynk.virtualWrite(VPIN_Hum1, hum);
/* publish the message */
snprintf (str, 20, "%lf", temp_c);
status = client.publish(TEMP_TOPIC, str);
snprintf (str, 20, "%lf", hum);
status = client.publish(HUM_TOPIC, str);
BLYNK_LOG3(status, " ", str);
snprintf(str, 20, "%02i:%02i:%02i ", hour(), minute(), second());
display.drawString(10, 47, str);
Serial.print(str);
snprintf(str, 20, "%2.0f°С %2.0f%%", temp_c, hum);
display.drawString(10, 0, str);
Serial.println(str);
}
float adcr = adc1_get_raw(ADC1_CHANNEL_0)*K_adc0;
Blynk.virtualWrite(VPIN_Voltage, adcr);
BLYNK_LOG2("Voltage: ", adcr);
snprintf(str, 20, "%6.3fV", adcr/1000);
status = client.publish(VOLT_TOPIC, str);
snprintf(str, 20, "%4.0fmV", adcr);
display.drawString(60, 32, str);
display.display(); // display whatever is in the buffer
digitalWrite(ledPin, LOW);
delay(1);
digitalWrite(ledPin, HIGH);
} // newGraphValue
BLYNK_CONNECTED() {
rtc.begin();
Blynk.syncVirtual(VPIN_Bright);
newGraphValue();
} // Connect
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
Serial.println(START_VER);
display.init(); // initialise the OLED
display.clear();
// Blynk.begin(auth, ssid, pass);
Blynk.begin(auth, ssid, pass, "ha2.r-as.ru", 9443);
terminal.clear();
terminal.println(START_VER);
terminal.flush();
// timer.setInterval(5000L, newGraphValue);
if (!client.connected()) {
mqttconnect();
}
// http://www.bizkit.ru/2019/05/11/13827/
adc1_config_width(ADC_WIDTH_BIT_12);
adc1_config_channel_atten(ADC1_CHANNEL_0,ADC_ATTEN_DB_11);
delay(1000);
} //setup
void loop()
{
timer.run();
if (Blynk.connected()) { // checks to see if connected to server
Blynk.run(); // runs only if connected to server
client.loop(); // MQTT
BLYNK_LOG1(" Go to sleep in 1 sec...");
delay(1000);
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
esp_light_sleep_start();
bootCount++;
BLYNK_LOG2(" Bootcount: ", bootCount); print_wakeup_reason();
}
else {
Blynk.connect();
}
beacon();
}