I am facing an issue. I am setting two WiFi hotspts and trying to switch between them if data connection is not present in
any one of the WiFi hotspot. If data connection is not present the ESP32 device will try to switch to the other available
WiFi hotspot and try to reconnect to internet there.
One SSID set as DLINKTZ and the other one set as DLINK1234
The logic for switching of WiFi hotspot is as follows:
Inside the aws_iot_task in the example code in the while loop mentioned below we are trying the switch WiFi operation:
Code: Select all
while((NETWORK_ATTEMPTING_RECONNECT == rc || NETWORK_RECONNECTED == rc || SUCCESS == rc)) {
//Max time the yield function will wait for read messages
rc = aws_iot_mqtt_yield(&client, 100);
if(NETWORK_ATTEMPTING_RECONNECT == rc) {
// If the client is attempting to reconnect we will skip the rest of the loop.
/********************MY SWITCH WiFi LOGIC**************************/
if(one_time_task_timer == false)
{
one_time_task_timer = true;
xTaskCreate(no_internet_timer, "no_internet_timer", 2048, NULL, 1, NULL );
}
if(attempt_net_flag == true)
{
printf("Attempting to switch WiFi\r\n\r\n");
one_time_task_timer = false;
attempt_net_flag = false;
switching_wifi();
}
continue;
}
one_time_task_timer = false;
available_net = true; //got net
attempt_net_flag = false;
switch_wifi1 = true;
switch_wifi2 = false;
/*******************MY SWITCH WiFi LOGIC*****************************/
ESP_LOGI(TAG, "Stack remaining for task '%s' is %d bytes", pcTaskGetTaskName(NULL), uxTaskGetStackHighWaterMark(NULL));
vTaskDelay(1000 / portTICK_RATE_MS);
sprintf(cPayload, "%s : %d ", "{\"Hello\":\"Welcome\"(QOS0)}", i++);
paramsQOS0.payloadLen = strlen(cPayload);
rc = aws_iot_mqtt_publish(&client, TOPIC, TOPIC_LEN, ¶msQOS0);
sprintf(cPayload, "%s : %d ", "{\"Hello\":\"Welcome\"(QOS0)}", i++);
paramsQOS1.payloadLen = strlen(cPayload);
rc = aws_iot_mqtt_publish(&client, TOPIC, TOPIC_LEN, ¶msQOS1);
if (rc == MQTT_REQUEST_TIMEOUT_ERROR) {
ESP_LOGW(TAG, "QOS1 publish ack not received.");
rc = SUCCESS;
}
}
ESP_LOGE(TAG, "An error occurred in the main loop.");
abort();
When I am running the code with the above logic my ESP32 device is showing an error after 7 to 8 successful switch between WiFi
hotspot when data connection is not present (but WiFi hotspot is present). The error message is as follows:
Code: Select all
/*******************ERROR MESSAGE AS SHOWN IN TERA TERM*****************************/
E (388179) subpub: An error occurred in the main loop.
abort() was called at PC 0x400d2fd7 on core 1
Backtrace: 0x4008e650:0x3ffc7ee0 0x4008e827:0x3ffc7f00 0x400d2fd7:0x3ffc7f20
Rebooting...
/********************ERROR MESSAGE AS SHOWN IN TERA TERM*************************/
Code: Select all
void switching_wifi(void) {
//switching from wifi-1 to wifi-2
if (switch_wifi1 == true) //hotspot1
{
switch_wifi1 = false;
switch_wifi2 = true;
strcpy(ssid_buf,"DLINK1234");
strcpy(pass_buf,"123DLINK");
}
else if (switch_wifi2 == true) //hotspot1
{
switch_wifi2 = false;
switch_wifi1 = true;
strcpy(ssid_buf,"DLINKTZ");
strcpy(pass_buf,"DLINK1234");
}
else
{
}
esp_wifi_stop(); //Function to stop the WiFi
esp_wifi_deinit();
initialise_wifi();
}