ESP -IDF v3.1.3 SWITCHING WiFi ISSUE IN AWS TASK

john_peter
Posts: 7
Joined: Wed May 15, 2019 7:36 am

ESP -IDF v3.1.3 SWITCHING WiFi ISSUE IN AWS TASK

Postby john_peter » Thu Jun 27, 2019 8:10 am

When I am using AWS_IOT(esp-idf-v3.1.3\examples\protocols\aws_iot\subscribe_publish) example code of ESP-IDF version-3.1.3
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, &paramsQOS0);

        sprintf(cPayload, "%s : %d ", "{\"Hello\":\"Welcome\"(QOS0)}", i++);
        paramsQOS1.payloadLen = strlen(cPayload);
        rc = aws_iot_mqtt_publish(&client, TOPIC, TOPIC_LEN, &paramsQOS1);
        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*************************/
The function switching_wifi() that I wrote is as follows:

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();
}
I will be much obliged if I get any response regarding this issue. THANK YOU.

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: ESP -IDF v3.1.3 SWITCHING WiFi ISSUE IN AWS TASK

Postby ESP_Angus » Fri Jun 28, 2019 12:08 am

Hi John,

I took the liberty of adding "code" blocks to your original post so the code was easier to read.

A lot of the example code is simplified for the purposes of demonstrating the APIs, but it's not necessarily robust enough for a full project - particularly in terms of error handling if something goes wrong. A full project will need to add error handling code that's appropriate for whatever the project needs to do.

As you can see in the first piece of code, the "while" loop will exit if the 'rc' variable doesn't hold one of 3 expected values:

Code: Select all

while((NETWORK_ATTEMPTING_RECONNECT == rc || NETWORK_RECONNECTED == rc || SUCCESS == rc))
And outside the while loop, the error is printed and then the code abort()s.

We can also see in this code that 'rc' is set at the bottom of the loop when the code tries to publish to mqtt:

Code: Select all

rc = aws_iot_mqtt_publish(&client, TOPIC, TOPIC_LEN, &paramsQOS0);
I would recommend adding some logging or doing some JTAG debugging so you can see what the value of "rc" is at this point.

My best guess is: disconnecting and reconnecting the WiFi causes the MQTT connection to fail with an error that isn't NETWORK_ATTEMPTING_RECONNECT or NETWORK_RECONNECTED. This can easily happen if the TCP connection is interrupted by the Wi-Fi layer disconnecting and trying to reconnect.

You need to handle this error in your code (maybe by ignoring it and waiting for a reconnect, maybe by waiting for an event from the Wifi subsystem that says the ESP32 has reconnected and only resuming the MQTT loop after this event is received.)

john_peter
Posts: 7
Joined: Wed May 15, 2019 7:36 am

Re: ESP -IDF v3.1.3 SWITCHING WiFi ISSUE IN AWS TASK

Postby john_peter » Fri Jun 28, 2019 6:11 am

Thank You very much ESP_Angus for your valuable suggestion.

saikrishna
Posts: 2
Joined: Tue Jan 19, 2021 9:17 am

Re: ESP -IDF v3.1.3 SWITCHING WiFi ISSUE IN AWS TASK

Postby saikrishna » Sat Jan 23, 2021 5:13 am

The same problem im also facing but here im getting rc value = -12 means NETWORK_SSL_READ_ERROR
and im pasting here my code.Please help me to resolve this issue.
Tq
while((NETWORK_ATTEMPTING_RECONNECT == rc || NETWORK_RECONNECTED == rc || SUCCESS == rc)) {


printf("task running in while loop\n");

//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.
continue;
}

// 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 from ESP32 (QOS0)", i++);
// paramsQOS0.payloadLen = strlen(cPayload);
// rc = aws_iot_mqtt_publish(&client, TOPIC, TOPIC_LEN, &paramsQOS0);

// sprintf(cPayload, "%s : %d ", "hello from ESP32 (QOS1)", i++);
printf("blocked here\n");
//while(flag == 0);

xSemaphoreTake(binarysemaphore,portMAX_DELAY);

printf("unblocked code to publish data\n");
send_data(cPayload);
printf("payload string is %s\n",cPayload);
printf("length of the publish string is %d\n",strlen(cPayload));
paramsQOS1.payloadLen = strlen(cPayload);
rc = aws_iot_mqtt_publish(&client, TOPIC, TOPIC_LEN, &paramsQOS1);


printf("publish done checking rc value\n");

printf("rc=======>%d", rc);
if (rc == MQTT_REQUEST_TIMEOUT_ERROR) {
ESP_LOGW(TAG, "QOS1 publish ack not received.");
rc = SUCCESS;
}
}

Who is online

Users browsing this forum: No registered users and 121 guests