[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
|
||||
|
||||
/* 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();
|
||||
float temp = 0.0;
|
||||
|
@ -74,7 +76,7 @@ float humi = 0.0;
|
|||
uint16_t battLevel = 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 humiAccu = 0.0; // for average value
|
||||
|
||||
|
@ -102,11 +104,10 @@ bool wlanConnect(void) {
|
|||
WiFi.mode(WIFI_STA);
|
||||
WiFi.begin(wlan_ssid, wlan_passwd);
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(500);
|
||||
delay(wlanConnectCheckInterval);
|
||||
INFO_PRINT(".");
|
||||
|
||||
cycles++;
|
||||
if (cycles > wifiConnectTimeout) {
|
||||
if (cycles > wlanConnectTimeout / wlanConnectCheckInterval) {
|
||||
INFO_PRINTLN(" FAILED");
|
||||
return false;
|
||||
}
|
||||
|
@ -154,10 +155,10 @@ void printWakeupReason() {
|
|||
}
|
||||
|
||||
extern "C" void app_main() {
|
||||
// ESP32 Setup
|
||||
initArduino();
|
||||
pinMode(4, OUTPUT);
|
||||
digitalWrite(4, HIGH);
|
||||
//do your own thing
|
||||
|
||||
#ifdef PRINT_INFO
|
||||
Serial.begin(115200);
|
||||
|
@ -173,10 +174,6 @@ extern "C" void app_main() {
|
|||
printWakeupReason();
|
||||
#endif
|
||||
|
||||
// switch WiFi off for the moment
|
||||
DEBUG_PRINTLN("[DEBUG] Turning wifi off for now");
|
||||
WiFi.mode(WIFI_OFF);
|
||||
|
||||
// setup the si7021 sensor
|
||||
if (false == si7021.begin()) {
|
||||
INFO_PRINTLN("[ERROR] SI7021 not set up properly");
|
||||
|
@ -200,26 +197,21 @@ extern "C" void app_main() {
|
|||
|
||||
#ifdef PRINT_INFO
|
||||
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
|
||||
|
||||
/* ---------- MAIN LOGIC START ---------- */
|
||||
// accumulate and average $sampleAvgNum samples before sending data
|
||||
if (sampleCnt < sampleAvgNum) {
|
||||
sampleCnt++;
|
||||
tempAccu += temp;
|
||||
humiAccu += humi;
|
||||
}
|
||||
sampleCnt++;
|
||||
tempAccu += temp;
|
||||
humiAccu += humi;
|
||||
|
||||
// calculate average and send data, if $sampleAvgNum are reached
|
||||
if (sampleCnt >= sampleAvgNum) {
|
||||
|
||||
INFO_PRINTLN("[INFO ] Enough samples collected -> send average to database");
|
||||
temp = tempAccu / (float)sampleCnt;
|
||||
humi = humiAccu / (float)sampleCnt;
|
||||
sampleCnt = 0;
|
||||
tempAccu = 0;
|
||||
humiAccu = 0;
|
||||
temp = tempAccu / (float)sampleCnt;
|
||||
humi = humiAccu / (float)sampleCnt;
|
||||
#ifdef PRINT_INFO
|
||||
Serial.printf("[INFO ] < Humidity: %5.2f Temperature: %5.2f\n", humi, temp);
|
||||
#endif
|
||||
|
@ -253,6 +245,13 @@ extern "C" void app_main() {
|
|||
if (line.indexOf("204") == -1) { // expect a HTTP 204 status code
|
||||
INFO_PRINTLN("[ERROR]: invalid http status code");
|
||||
INFO_PRINTLN(line);
|
||||
} else { // successful database submission
|
||||
// reset average after successful database submission
|
||||
sampleCnt = 0;
|
||||
tempAccu = 0;
|
||||
humiAccu = 0;
|
||||
|
||||
wlanConnectFailCnt = 0;
|
||||
}
|
||||
break;
|
||||
} else {
|
||||
|
@ -270,11 +269,15 @@ extern "C" void app_main() {
|
|||
} else { // ELSE: !wifiConnect
|
||||
|
||||
/* WIFI CONNECT FAILED, WAIT MAX 5 MINUTES IN DEEP SLEEP */
|
||||
INFO_PRINTLN("[INFO ] WiFi connect failed -> wait a bit and try again");
|
||||
if (sendInterval_sec > (5 * 60)) {
|
||||
wlanConnectFailCnt++;
|
||||
#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);
|
||||
} else {
|
||||
doDeepSleep(sendInterval_sec);
|
||||
doDeepSleep(sampleInterval_sec);
|
||||
}
|
||||
} // ENDIF: wifiConnect
|
||||
|
||||
|
|
Loading…
Reference in a new issue