[CODE] remember when wifi connect failed + fix reconnect sleep time
This commit is contained in:
parent
f967676088
commit
c56085613d
|
@ -65,7 +65,9 @@ String serviceUri = "/write?db=test";
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* GLOBAL VARIABLES */
|
/* GLOBAL VARIABLES */
|
||||||
const unsigned int wifiConnectTimeout = 30; /* num of 500ms intervals */
|
const uint16_t wlanConnectCheckInterval = 250; /* milli seconds: poll wifi.state after wifi.begin() */
|
||||||
|
const uint16_t wlanConnectTimeout = 15000; /* milli seconds */
|
||||||
|
RTC_DATA_ATTR uint8_t wlanConnectFailCnt = 0;
|
||||||
|
|
||||||
Si7021 si7021 = Si7021();
|
Si7021 si7021 = Si7021();
|
||||||
float temp = 0.0;
|
float temp = 0.0;
|
||||||
|
@ -74,7 +76,7 @@ float humi = 0.0;
|
||||||
uint16_t battLevel = 0;
|
uint16_t battLevel = 0;
|
||||||
float battPerc = 0.0;
|
float battPerc = 0.0;
|
||||||
|
|
||||||
RTC_DATA_ATTR uint8_t sampleCnt = 0;
|
RTC_DATA_ATTR uint8_t sampleCnt = 0; // for average value
|
||||||
RTC_DATA_ATTR float tempAccu = 0.0; // for average value
|
RTC_DATA_ATTR float tempAccu = 0.0; // for average value
|
||||||
RTC_DATA_ATTR float humiAccu = 0.0; // for average value
|
RTC_DATA_ATTR float humiAccu = 0.0; // for average value
|
||||||
|
|
||||||
|
@ -102,11 +104,10 @@ bool wlanConnect(void) {
|
||||||
WiFi.mode(WIFI_STA);
|
WiFi.mode(WIFI_STA);
|
||||||
WiFi.begin(wlan_ssid, wlan_passwd);
|
WiFi.begin(wlan_ssid, wlan_passwd);
|
||||||
while (WiFi.status() != WL_CONNECTED) {
|
while (WiFi.status() != WL_CONNECTED) {
|
||||||
delay(500);
|
delay(wlanConnectCheckInterval);
|
||||||
INFO_PRINT(".");
|
INFO_PRINT(".");
|
||||||
|
|
||||||
cycles++;
|
cycles++;
|
||||||
if (cycles > wifiConnectTimeout) {
|
if (cycles > wlanConnectTimeout / wlanConnectCheckInterval) {
|
||||||
INFO_PRINTLN(" FAILED");
|
INFO_PRINTLN(" FAILED");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -154,10 +155,10 @@ void printWakeupReason() {
|
||||||
}
|
}
|
||||||
|
|
||||||
extern "C" void app_main() {
|
extern "C" void app_main() {
|
||||||
|
// ESP32 Setup
|
||||||
initArduino();
|
initArduino();
|
||||||
pinMode(4, OUTPUT);
|
pinMode(4, OUTPUT);
|
||||||
digitalWrite(4, HIGH);
|
digitalWrite(4, HIGH);
|
||||||
//do your own thing
|
|
||||||
|
|
||||||
#ifdef PRINT_INFO
|
#ifdef PRINT_INFO
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
|
@ -173,10 +174,6 @@ extern "C" void app_main() {
|
||||||
printWakeupReason();
|
printWakeupReason();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// switch WiFi off for the moment
|
|
||||||
DEBUG_PRINTLN("[DEBUG] Turning wifi off for now");
|
|
||||||
WiFi.mode(WIFI_OFF);
|
|
||||||
|
|
||||||
// setup the si7021 sensor
|
// setup the si7021 sensor
|
||||||
if (false == si7021.begin()) {
|
if (false == si7021.begin()) {
|
||||||
INFO_PRINTLN("[ERROR] SI7021 not set up properly");
|
INFO_PRINTLN("[ERROR] SI7021 not set up properly");
|
||||||
|
@ -200,16 +197,14 @@ extern "C" void app_main() {
|
||||||
|
|
||||||
#ifdef PRINT_INFO
|
#ifdef PRINT_INFO
|
||||||
Serial.printf("[INFO ] > Humidity: %5.2f Temperature: %5.2f", humi, temp);
|
Serial.printf("[INFO ] > Humidity: %5.2f Temperature: %5.2f", humi, temp);
|
||||||
Serial.printf(" Battery Bat: %5.1f /100 (raw: %4d)\n", battPerc, battLevel);
|
Serial.printf(" Battery Bat: %5.1f/100 (raw: %4d)\n", battPerc, battLevel);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* ---------- MAIN LOGIC START ---------- */
|
/* ---------- MAIN LOGIC START ---------- */
|
||||||
// accumulate and average $sampleAvgNum samples before sending data
|
// accumulate and average $sampleAvgNum samples before sending data
|
||||||
if (sampleCnt < sampleAvgNum) {
|
|
||||||
sampleCnt++;
|
sampleCnt++;
|
||||||
tempAccu += temp;
|
tempAccu += temp;
|
||||||
humiAccu += humi;
|
humiAccu += humi;
|
||||||
}
|
|
||||||
|
|
||||||
// calculate average and send data, if $sampleAvgNum are reached
|
// calculate average and send data, if $sampleAvgNum are reached
|
||||||
if (sampleCnt >= sampleAvgNum) {
|
if (sampleCnt >= sampleAvgNum) {
|
||||||
|
@ -217,9 +212,6 @@ extern "C" void app_main() {
|
||||||
INFO_PRINTLN("[INFO ] Enough samples collected -> send average to database");
|
INFO_PRINTLN("[INFO ] Enough samples collected -> send average to database");
|
||||||
temp = tempAccu / (float)sampleCnt;
|
temp = tempAccu / (float)sampleCnt;
|
||||||
humi = humiAccu / (float)sampleCnt;
|
humi = humiAccu / (float)sampleCnt;
|
||||||
sampleCnt = 0;
|
|
||||||
tempAccu = 0;
|
|
||||||
humiAccu = 0;
|
|
||||||
#ifdef PRINT_INFO
|
#ifdef PRINT_INFO
|
||||||
Serial.printf("[INFO ] < Humidity: %5.2f Temperature: %5.2f\n", humi, temp);
|
Serial.printf("[INFO ] < Humidity: %5.2f Temperature: %5.2f\n", humi, temp);
|
||||||
#endif
|
#endif
|
||||||
|
@ -253,6 +245,13 @@ extern "C" void app_main() {
|
||||||
if (line.indexOf("204") == -1) { // expect a HTTP 204 status code
|
if (line.indexOf("204") == -1) { // expect a HTTP 204 status code
|
||||||
INFO_PRINTLN("[ERROR]: invalid http status code");
|
INFO_PRINTLN("[ERROR]: invalid http status code");
|
||||||
INFO_PRINTLN(line);
|
INFO_PRINTLN(line);
|
||||||
|
} else { // successful database submission
|
||||||
|
// reset average after successful database submission
|
||||||
|
sampleCnt = 0;
|
||||||
|
tempAccu = 0;
|
||||||
|
humiAccu = 0;
|
||||||
|
|
||||||
|
wlanConnectFailCnt = 0;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
|
@ -270,11 +269,15 @@ extern "C" void app_main() {
|
||||||
} else { // ELSE: !wifiConnect
|
} else { // ELSE: !wifiConnect
|
||||||
|
|
||||||
/* WIFI CONNECT FAILED, WAIT MAX 5 MINUTES IN DEEP SLEEP */
|
/* WIFI CONNECT FAILED, WAIT MAX 5 MINUTES IN DEEP SLEEP */
|
||||||
INFO_PRINTLN("[INFO ] WiFi connect failed -> wait a bit and try again");
|
wlanConnectFailCnt++;
|
||||||
if (sendInterval_sec > (5 * 60)) {
|
#ifdef PRINT_INFO
|
||||||
|
Serial.printf("[INFO ] WiFi connect failed %d time(s) -> wait %03d s and try again\n",
|
||||||
|
wlanConnectFailCnt, sampleInterval_sec);
|
||||||
|
#endif
|
||||||
|
if (sampleInterval_sec > (5 * 60)) {
|
||||||
doDeepSleep(5 * 60);
|
doDeepSleep(5 * 60);
|
||||||
} else {
|
} else {
|
||||||
doDeepSleep(sendInterval_sec);
|
doDeepSleep(sampleInterval_sec);
|
||||||
}
|
}
|
||||||
} // ENDIF: wifiConnect
|
} // ENDIF: wifiConnect
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue