forked from tidbyt/hdk
-
Notifications
You must be signed in to change notification settings - Fork 8
Wifi health check #47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+64
−5
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,6 +57,9 @@ static char s_image_url[MAX_URL_LEN + 1] = {0}; | |
static int s_reconnect_attempts = 0; | ||
static bool s_connection_given_up = false; | ||
|
||
// Counter for tracking consecutive WiFi disconnections | ||
static int s_wifi_disconnect_counter = 0; | ||
|
||
// Callback functions | ||
static void (*s_connect_callback)(void) = NULL; | ||
static void (*s_disconnect_callback)(void) = NULL; | ||
|
@@ -272,8 +275,9 @@ static bool has_saved_config = false; | |
ap_config.ap.max_connection = 4; | ||
ap_config.ap.authmode = WIFI_AUTH_OPEN; | ||
ap_config.ap.beacon_interval = 100; // Default beacon interval | ||
// failure_retry_cnt ?? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
ESP_LOGI(TAG, "Setting AP SSID: %s", DEFAULT_AP_SSID); | ||
ESP_LOGI(TAG, "Setting AP SSID: %s", DEFAULT_AP_SSID); | ||
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &ap_config)); | ||
|
||
// Start WiFi | ||
|
@@ -760,3 +764,44 @@ static void url_decode(char *str) { | |
} | ||
*dst = '\0'; // Null-terminate the decoded string | ||
} | ||
|
||
/** | ||
* @brief Check WiFi health and attempt reconnection if needed | ||
* | ||
* This function checks if WiFi is connected. If not, it attempts to reconnect | ||
* and increments a counter. If the counter reaches 10 consecutive failures, | ||
* the system will reboot. The counter is reset whenever WiFi is connected. | ||
*/ | ||
void wifi_health_check(void) { | ||
if (wifi_is_connected()) { | ||
// Reset counter when WiFi is connected | ||
if (s_wifi_disconnect_counter > 0) { | ||
// ESP_LOGI(TAG, "WiFi reconnected successfully, resetting disconnect counter"); | ||
s_wifi_disconnect_counter = 0; | ||
} | ||
return; | ||
} | ||
|
||
// WiFi is not connected, increment counter | ||
s_wifi_disconnect_counter++; | ||
ESP_LOGW(TAG, "WiFi Health check. Disconnect count: %d/15", s_wifi_disconnect_counter); | ||
|
||
// Try to reconnect | ||
if (strlen(s_wifi_ssid) > 0) { | ||
ESP_LOGI(TAG, "Attempting to reconnect to WiFi..."); | ||
esp_err_t err = esp_wifi_connect(); | ||
if (err != ESP_OK) { | ||
ESP_LOGW(TAG, "WiFi reconnect attempt failed: %s", esp_err_to_name(err)); | ||
} | ||
} else { | ||
ESP_LOGW(TAG, "No SSID configured, cannot reconnect"); | ||
} | ||
|
||
// If counter reaches threshold, reboot the system | ||
if (s_wifi_disconnect_counter >= 15) { | ||
ESP_LOGE(TAG, "WiFi disconnected for 15 consecutive checks. Rebooting system..."); | ||
// Wait a moment before rebooting | ||
vTaskDelay(pdMS_TO_TICKS(1000)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
esp_restart(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The magic number
600
representing the WiFi connection timeout should be a named constant for better readability.