Code: Select all
/* Delay & Recover if WiFi doesn't get allocated an IP address */
// uint32_t timeOutCntr = 24 * 6 * 60 * 24; // 24 hours = 10 seconds * 6 * 60 * 24
uint32_t timeOutCntr = 12; // 2 minutes = 10 seconds * 12
while (pdTRUE) {
EventBits_t handlerEventBits;
const TickType_t xTicksToWait = 10000 / portTICK_PERIOD_MS;
handlerEventBits = xEventGroupWaitBits(
wifi_event_group, // The event group being tested.
HANDLER_EVENT_STA_GOT_IP | HANDLER_EVENT_STA_CONNECTED, // The bits within the event group to wait for.
pdFALSE, // BITS should NOT be cleared before returning.
pdTRUE, // Wait for ALL bits, holds it when no IP.
xTicksToWait // Wait a maximum of 10 s for either bit to be set.
);
// Connected and has IP Address OK!
if( ( handlerEventBits & HANDLER_EVENT_STA_GOT_IP ) != 0 )
{
// xEventGroupWaitBits() returned because HANDLER_EVENT_STA_GOT_IP was set.
ESP_LOGI(TAG, "IP Address allocated successfully. Starting Cloud Task shortly.");
break;
}
// Trying to Connect but Failing - Let's reset after a timeout!
else if( ( handlerEventBits & HANDLER_EVENT_STA_CONNECTED ) != 0 )
{
// xEventGroupWaitBits() returned because HANDLER_EVENT_STA_CONNECTED was set.
ESP_LOGI(TAG, "WiFi Connection Started, but no IP Address was available! Restarting after %d more checks.", timeOutCntr);
if (timeOutCntr-- <= 1) {
esp_restart();
}
}
}