feat(esp32): improve wifi connection error handling
This commit is contained in:
parent
00d37795ad
commit
02fa13d116
1 changed files with 45 additions and 36 deletions
|
@ -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: ");
|
pixels.clear();
|
||||||
Serial.print(brightness);
|
if (brightness < spiffs_config.brightness_thresh_lower) {
|
||||||
pixels.clear();
|
uint16_t clamped = min(max(brightness, spiffs_config.brightness_thresh_upper), spiffs_config.brightness_thresh_lower);
|
||||||
if (brightness < 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);
|
||||||
uint16_t clamped = min(max(brightness, spiffs_config.brightness_thresh_upper), spiffs_config.brightness_thresh_lower);
|
// safeguard!
|
||||||
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);
|
if (power > spiffs_config.brightness_max) {
|
||||||
Serial.print(", clamped: ");
|
power = spiffs_config.brightness_max;
|
||||||
Serial.print(clamped);
|
}
|
||||||
Serial.print(", power: ");
|
// Set the color of each LED according to the space's state
|
||||||
Serial.println(power);
|
for (uint16_t i = 0; i < WS2812_LEN; ++i) {
|
||||||
// safeguard!
|
switch(states[i]) {
|
||||||
if (power > spiffs_config.brightness_max) {
|
case SpaceState::Closed:
|
||||||
power = spiffs_config.brightness_max;
|
pixels.setPixelColor(i, pixels.Color(power, 0, 0)); // closed -> red
|
||||||
}
|
break;
|
||||||
//power = 10;
|
case SpaceState::Open:
|
||||||
for (uint16_t i = 0; i < WS2812_LEN; ++i) {
|
pixels.setPixelColor(i, pixels.Color(0, power, 0)); // open -> green
|
||||||
switch(states[i]) {
|
break;
|
||||||
case SpaceState::Closed:
|
case SpaceState::Invalid:
|
||||||
pixels.setPixelColor(i, pixels.Color(power, 0, 0));
|
pixels.setPixelColor(i, pixels.Color(power, power/2, 0)); // invalid response -> orange
|
||||||
break;
|
break;
|
||||||
case SpaceState::Open:
|
case SpaceState::Outdated:
|
||||||
pixels.setPixelColor(i, pixels.Color(0, power, 0));
|
pixels.setPixelColor(i, pixels.Color(0, 0, power)); // unreachable -> blue
|
||||||
break;
|
break;
|
||||||
case SpaceState::Invalid:
|
default:
|
||||||
pixels.setPixelColor(i, pixels.Color(power, power/2, 0));
|
break;
|
||||||
break;
|
|
||||||
case SpaceState::Outdated:
|
|
||||||
pixels.setPixelColor(i, pixels.Color(0, 0, power));
|
|
||||||
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);
|
delay(10);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue