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) {
timerExpired = false;
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();
now = ntp.epoch();
Serial.println(ntp.formattedTime("\nIt is %d.%m.%Y %H:%M UTC"));
@ -254,44 +266,41 @@ void loop() {
client.stop();
} // endif timerExpired
if (!error) {
uint16_t brightness = analogRead(BRIGHTNESS_PIN);
Serial.print("brightness: ");
Serial.print(brightness);
pixels.clear();
if (brightness < 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);
Serial.print(", clamped: ");
Serial.print(clamped);
Serial.print(", power: ");
Serial.println(power);
// safeguard!
if (power > spiffs_config.brightness_max) {
power = spiffs_config.brightness_max;
}
//power = 10;
for (uint16_t i = 0; i < WS2812_LEN; ++i) {
switch(states[i]) {
case SpaceState::Closed:
pixels.setPixelColor(i, pixels.Color(power, 0, 0));
break;
case SpaceState::Open:
pixels.setPixelColor(i, pixels.Color(0, power, 0));
break;
case SpaceState::Invalid:
pixels.setPixelColor(i, pixels.Color(power, power/2, 0));
break;
case SpaceState::Outdated:
pixels.setPixelColor(i, pixels.Color(0, 0, power));
break;
default:
break;
}
// Read brightness from LDR (0..2500 mV) and map to power level (0..255)
uint16_t brightness = analogRead(BRIGHTNESS_PIN);
pixels.clear();
if (brightness < 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);
// safeguard!
if (power > spiffs_config.brightness_max) {
power = spiffs_config.brightness_max;
}
// Set the color of each LED according to the space's state
for (uint16_t i = 0; i < WS2812_LEN; ++i) {
switch(states[i]) {
case SpaceState::Closed:
pixels.setPixelColor(i, pixels.Color(power, 0, 0)); // closed -> red
break;
case SpaceState::Open:
pixels.setPixelColor(i, pixels.Color(0, power, 0)); // open -> green
break;
case SpaceState::Invalid:
pixels.setPixelColor(i, pixels.Color(power, power/2, 0)); // invalid response -> orange
break;
case SpaceState::Outdated:
pixels.setPixelColor(i, pixels.Color(0, 0, power)); // unreachable -> blue
break;
default:
break;
}
}
pixels.show();
Serial.println();
}
// Turn off the first LED every 10s in case of error
if (error && now % 10 == 0) {
pixels.setPixelColor(0, pixels.Color(0, 0, 0));
}
// Finally, update the LED chain
pixels.show();
delay(10);
}