feat(esp32): improve wifi connection error handling

This commit is contained in:
s3lph 2024-10-20 17:40:55 +02:00
parent 00d37795ad
commit 02fa13d116
Signed by: s3lph
GPG key ID: 0AA29A52FB33CFB5

View file

@ -190,6 +190,18 @@ void loop() {
if (timerExpired) { if (timerExpired) {
timerExpired = false; timerExpired = false;
Serial.println("Timer expired!"); Serial.println("Timer expired!");
if (WiFi.status() != WL_CONNECTED) {
Serial.println("WiFi disconnected, reconnecting");
WiFi.reconnect();
for (uint8_t i = 0; i < 100 && WiFi.status() != WL_CONNECTED; ++i) {
delay(500);
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println("Reconnected!");
} else {
Serial.println("Reconnecting failed.");
}
}
ntp.update(); ntp.update();
now = ntp.epoch(); now = ntp.epoch();
Serial.println(ntp.formattedTime("\nIt is %d.%m.%Y %H:%M UTC")); Serial.println(ntp.formattedTime("\nIt is %d.%m.%Y %H:%M UTC"));
@ -254,44 +266,41 @@ void loop() {
client.stop(); client.stop();
} // endif timerExpired } // endif timerExpired
if (!error) { // Read brightness from LDR (0..2500 mV) and map to power level (0..255)
uint16_t brightness = analogRead(BRIGHTNESS_PIN); uint16_t brightness = analogRead(BRIGHTNESS_PIN);
Serial.print("brightness: ");
Serial.print(brightness);
pixels.clear(); pixels.clear();
if (brightness < spiffs_config.brightness_thresh_lower) { if (brightness < spiffs_config.brightness_thresh_lower) {
uint16_t clamped = min(max(brightness, spiffs_config.brightness_thresh_upper), spiffs_config.brightness_thresh_lower); uint16_t clamped = min(max(brightness, spiffs_config.brightness_thresh_upper), spiffs_config.brightness_thresh_lower);
uint8_t power = spiffs_config.brightness_min + (spiffs_config.brightness_max - spiffs_config.brightness_min) * pow(1.0f * (spiffs_config.brightness_thresh_lower - clamped) / (spiffs_config.brightness_thresh_lower - spiffs_config.brightness_thresh_upper), 2); uint8_t power = spiffs_config.brightness_min + (spiffs_config.brightness_max - spiffs_config.brightness_min) * pow(1.0f * (spiffs_config.brightness_thresh_lower - clamped) / (spiffs_config.brightness_thresh_lower - spiffs_config.brightness_thresh_upper), 2);
Serial.print(", clamped: ");
Serial.print(clamped);
Serial.print(", power: ");
Serial.println(power);
// safeguard! // safeguard!
if (power > spiffs_config.brightness_max) { if (power > spiffs_config.brightness_max) {
power = spiffs_config.brightness_max; power = spiffs_config.brightness_max;
} }
//power = 10; // Set the color of each LED according to the space's state
for (uint16_t i = 0; i < WS2812_LEN; ++i) { for (uint16_t i = 0; i < WS2812_LEN; ++i) {
switch(states[i]) { switch(states[i]) {
case SpaceState::Closed: case SpaceState::Closed:
pixels.setPixelColor(i, pixels.Color(power, 0, 0)); pixels.setPixelColor(i, pixels.Color(power, 0, 0)); // closed -> red
break; break;
case SpaceState::Open: case SpaceState::Open:
pixels.setPixelColor(i, pixels.Color(0, power, 0)); pixels.setPixelColor(i, pixels.Color(0, power, 0)); // open -> green
break; break;
case SpaceState::Invalid: case SpaceState::Invalid:
pixels.setPixelColor(i, pixels.Color(power, power/2, 0)); pixels.setPixelColor(i, pixels.Color(power, power/2, 0)); // invalid response -> orange
break; break;
case SpaceState::Outdated: case SpaceState::Outdated:
pixels.setPixelColor(i, pixels.Color(0, 0, power)); pixels.setPixelColor(i, pixels.Color(0, 0, power)); // unreachable -> blue
break; break;
default: default:
break; break;
} }
} }
} }
pixels.show(); // Turn off the first LED every 10s in case of error
Serial.println(); if (error && now % 10 == 0) {
pixels.setPixelColor(0, pixels.Color(0, 0, 0));
} }
// Finally, update the LED chain
pixels.show();
delay(10); delay(10);
} }