Unable to access AP mode after 5 cycle

design4
Posts: 6
Joined: Tue May 18, 2021 2:49 am

Unable to access AP mode after 5 cycle

Postby design4 » Tue May 18, 2021 3:47 am

Hi genius,

Im using ESP8266 freertos sdk. esptool.py v2.4.0. Im using AP mode and HTTP server to access webpage. However, the webpage can only be access after 5 cycle. Seems like some socket is not properly closed and caused httprx_tx error. Below I describe my cycle.

1- Init AP mode & create webpage
2- Connect with PC
3- Submit Button
4- Deinit AP mode & delete webpage
5- Go to step 1


Below is my code

Code: Select all

//-------------------------------------------------------------------------------------
// Function :
// Description:
//-------------------------------------------------------------------------------------
uint8_t Wifi_Initialize_Process(void)
{
    uint8_t ret = INITIALIZE_PROCESS;

	if(Wifi.wIsChanged == WIFI_TO_AP)
	{
		Tcount_WifiAP = Wifi_AP_Interval;
		Wifi.wMode = WIFI_MODEAP;
		Wifi.wStatus = WIFI_APCONFIG;
		Wifi.wIsChanged = WIFI_NO_CHANGE;

		Initialise_wifi();
		Wifi_Start_Ap();

#ifdef WIFI_USE_WEBPAGE
		Wifi_Server_Create();
#endif
	}
	else
	{
		Wifi.wMode = WIFI_MODENONE;
		Wifi.wStatus = WIFI_READY;
		Wifi.wIsChanged = WIFI_NO_CHANGE;

		Initialise_wifi();
	}

	ret = BACKGROUND_PROCESS;

#ifdef _DEBUG_
	_DEBUG_("Wifi: INITIALIZE_PROCESS ok");
#endif
    return ret;
}
//-------------------------------------------------------------------------------------
// Function :
// Description:
//-------------------------------------------------------------------------------------
void Initialise_wifi(void)
{
	if(Wifi.wMode > WIFI_MODENONE)
	{
		esp_err_t err;

		tcpip_adapter_init();
		
		err = tcpip_adapter_dhcps_stop(TCPIP_ADAPTER_IF_AP);
		if(err != ESP_OK)
		{
#ifdef _DEBUG_
			_DEBUG_("Wifi: tcpip_adapter_dhcps_stop() : %s", esp_err_to_name(err));
#endif
		}
	        tcpip_adapter_ip_info_t ip_info;
	        IP4_ADDR(&ip_info.ip, Esp_Param.Ap_IP[0], Esp_Param.Ap_IP[1], Esp_Param.Ap_IP[2], Esp_Param.Ap_IP[3]);
	        IP4_ADDR(&ip_info.gw, Esp_Param.Ap_IP[0], Esp_Param.Ap_IP[1], Esp_Param.Ap_IP[2], Esp_Param.Ap_IP[3]);
	        IP4_ADDR(&ip_info.netmask, 255, 255, 255, 0);
#ifdef _DEBUG_
                _DEBUG_("Wifi: ip_info.ip = " IPSTR, IP2STR(&ip_info.ip));
#endif
	        err = tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_AP, &ip_info); //set static IP
		if(err != ESP_OK)
		{
#ifdef _DEBUG_
			_DEBUG_("Wifi: tcpip_adapter_set_ip_info() : %s", esp_err_to_name(err));
#endif
		}
		err = tcpip_adapter_dhcps_start(TCPIP_ADAPTER_IF_AP);
		if(err != ESP_OK)
		{
#ifdef _DEBUG_
			_DEBUG_("Wifi: tcpip_adapter_dhcps_start() : %s", esp_err_to_name(err));
#endif
		}

		err = esp_event_loop_create_default();
		if(err != ESP_OK)
		{
#ifdef _DEBUG_
			_DEBUG_("Wifi: esp_event_loop_create_default() : %s", esp_err_to_name(err));
#endif
		}

		wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
		err = esp_wifi_init(&cfg);
		if(err != ESP_OK)
		{
#ifdef _DEBUG_
			_DEBUG_("Wifi: esp_wifi_init() : %s", esp_err_to_name(err));
#endif
		}

		err = esp_wifi_set_storage(WIFI_STORAGE_RAM);
		if(err != ESP_OK)
		{
#ifdef _DEBUG_
			_DEBUG_("Wifi: esp_wifi_set_storage() : %s", esp_err_to_name(err));
#endif
		}

        err = esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL);
		if(err != ESP_OK)
		{
#ifdef _DEBUG_
			_DEBUG_("Wifi: esp_event_handler_register() : %s", esp_err_to_name(err));
#endif
		}
        err = esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL);
		if(err != ESP_OK)
		{
#ifdef _DEBUG_
			_DEBUG_("Wifi: esp_event_handler_register() : %s", esp_err_to_name(err));
#endif
		}
	}
}

//-------------------------------------------------------------------------------------
// Function :
// Description:
//-------------------------------------------------------------------------------------
void DeInitialize_Wifi(void)
{
	if(Wifi.wMode > WIFI_MODENONE)
	{
		esp_err_t err;

#ifdef WIFI_USE_WEBPAGE
        Wifi_Server_Delete();
#endif

        err = esp_event_handler_unregister(ESP_EVENT_ANY_BASE, ESP_EVENT_ANY_ID, &event_handler);
    	if(err != ESP_OK)
    	{
#ifdef _DEBUG_
    		_DEBUG_("Wifi: esp_event_handler_unregister() : %s", esp_err_to_name(err));
#endif
    	}

		if(Wifi.wConnection == STA_CONNECTED)
		{
			err = esp_wifi_disconnect();
			if(err != ESP_OK)
			{
#ifdef _DEBUG_
				_DEBUG_("Wifi: esp_wifi_disconnect() : %s", esp_err_to_name(err));
#endif
			}
		}

		err = esp_wifi_stop();
		if(err != ESP_OK)
		{
#ifdef _DEBUG_
			_DEBUG_("Wifi: esp_wifi_stop() : %s", esp_err_to_name(err));
#endif
		}

		err = esp_wifi_deinit();
		if(err != ESP_OK)
		{
#ifdef _DEBUG_
			_DEBUG_("Wifi: esp_wifi_deinit() : %s", esp_err_to_name(err));
#endif
		}

#ifdef _DEBUG_
		_DEBUG_("Wifi: esp_wifi_stop() & esp_wifi_deinit()");
#endif

		err = esp_event_loop_delete_default();
		if(err != ESP_OK)
		{
#ifdef _DEBUG_
			_DEBUG_("Wifi: esp_event_loop_delete_default() : %s", esp_err_to_name(err));
#endif
		}
#ifdef _DEBUG_
		_DEBUG_("Wifi: esp_event_handler_unregister() & esp_event_loop_delete_default()");
#endif
	}
}

//-------------------------------------------------------------------------------------
// Function :
// Description:
//-------------------------------------------------------------------------------------
void Wifi_Start_Ap()
{
	esp_err_t err;
    wifi_config_t wifi_config = {0};

    memset(&wifi_config, 0 , sizeof(wifi_config));

    wifi_config.ap.max_connection = 1;
    wifi_config.ap.authmode = WIFI_AUTH_WPA2_PSK;
    sprintf((char*)wifi_config.ap.ssid, Esp_Param.AP_Ssid);
    sprintf((char*)wifi_config.ap.password, Esp_Param.AP_Pass);

    err = esp_wifi_set_mode(WIFI_MODE_AP);
	if(err != ESP_OK)
	{
#ifdef _DEBUG_
		_DEBUG_("Wifi: esp_wifi_set_mode() : %s", esp_err_to_name(err));
#endif
	}
    err = esp_wifi_set_config(ESP_IF_WIFI_AP, &wifi_config);
	if(err != ESP_OK)
	{
#ifdef _DEBUG_
		_DEBUG_("Wifi: esp_wifi_set_config() : %s", esp_err_to_name(err));
#endif
	}
    err = esp_wifi_start();
	if(err != ESP_OK)
	{
#ifdef _DEBUG_
		_DEBUG_("Wifi: esp_wifi_start() : %s", esp_err_to_name(err));
#endif
	}
}

//-------------------------------------------------------------------------------------
// Function :
// Description:
//-------------------------------------------------------------------------------------
void Wifi_Server_Create(void)
{
	if(server == 0)
	{
	    httpd_config_t config = HTTPD_DEFAULT_CONFIG();
	    config.stack_size = Http_SERVER_Config_Stack_Size;
	    config.lru_purge_enable = true;

#ifdef _DEBUG_
	    _DEBUG_("HttpServer: Starting server");
#endif

		if(httpd_start(&server, &config) != ESP_OK)
		{
#ifdef _DEBUG_
			_DEBUG_("HttpServer: COULD NOT START SERVER");
#endif
		}

		if(uri_handler_inited == 0)
		{
			uri_handler_inited = 1;

		    httpd_uri_t first_end_point_config = {
		        .uri = "/",
		        .method = HTTP_GET,
		        .handler = on_url_hit};
		    httpd_register_uri_handler(server,&first_end_point_config);

		    httpd_uri_t setwifi_end_point_config = {
		        .uri = "/api/setwifi",
		        .method = HTTP_POST,
		        .handler = on_setwifi_set};
		    httpd_register_uri_handler(server,&setwifi_end_point_config);
		}
	}
}

//-------------------------------------------------------------------------------------
// Function :
// Description:
//-------------------------------------------------------------------------------------
void Wifi_Server_Delete(void)
{
	if(server != 0)
	{
#ifdef _DEBUG_
	    _DEBUG_("HttpServer: Delete server");
#endif

		esp_err_t err;

		if(uri_handler_inited == 1)
		{
			uri_handler_inited = 0;

			httpd_unregister_uri_handler(server, "/", HTTP_GET);
			httpd_unregister_uri_handler(server, "/api/setwifi", HTTP_POST);
		}

		err = httpd_stop(server);
		if(err != ESP_OK)
		{
#ifdef _DEBUG_
			_DEBUG_("Wifi: httpd_stop() : %s", esp_err_to_name(err));
#endif
		}

		server = 0;
	}
}
Below is httpd error printed in ESP log
(11:37:50.392) W (197233) httpd_txrx: httpd_sock_err: error in send : 0<CR><LF>

Below is the full debug log
(11:37:25.758) I (170692) Main: Exit config<CR><LF>
(11:37:25.758) I (170694) Main: Input[1] = 0 (615)<CR><LF>
(11:37:25.758) Wifi: Wifi.wIsChanged = WIFI_TO_NONE<CR><LF>
(11:37:25.758) HttpServer: Delete server<CR><LF>
(11:37:25.840) Wifi: WIFI_EVENT_AP_DISCONNECTED<CR><LF>
(11:37:25.840) Wifi: esp_wifi_stop() & esp_wifi_deinit()<CR><LF>
(11:37:25.840) Wifi: esp_event_handler_unregister() & esp_event_loop_delete_default()<CR><LF>
(11:37:25.840) Wifi: INITIALIZE_PROCESS ok<CR><LF>
(11:37:29.895) I (174843) Main: Enter config<CR><LF>
(11:37:29.895) Wifi: Wifi.wIsChanged = WIFI_TO_AP<CR><LF>
(11:37:29.895) Wifi: ip_info.ip = 13.18.0.10<CR><LF>
(11:37:31.879) set channel in sleep mode, fail and exit<CR><LF>
(11:37:31.879) HttpServer: Starting server<CR><LF>
(11:37:31.879) Wifi: INITIALIZE_PROCESS ok<CR><LF>
(11:37:37.506) Wifi: WIFI_EVENT_AP_CONNECTED<CR><LF>
(11:37:39.402) Wifi: Free Stack: 384<CR><LF>
(11:37:42.136) Wifi: Free Stack: 256<CR><LF>
(11:37:50.392) W (197233) httpd_txrx: httpd_sock_err: error in send : 0<CR><LF>
(11:37:50.392) I (197241) HttpServer: iLen = 898<CR><LF>
(11:37:50.392) I (197253) HttpServer: iLen = 785<CR><LF>
(11:37:52.453) I (199315) HttpServer: watermark : 1024<CR><LF>
(11:40:00.847) I (327663) Main: Exit config<CR><LF>


Thanks in advanced

ESP_Sprite
Posts: 9711
Joined: Thu Nov 26, 2015 4:08 am

Re: Unable to access AP mode after 5 cycle

Postby ESP_Sprite » Tue May 18, 2021 5:56 am

Note that this is an ESP32 forum. You're more likely to get answers if you post on ESP8266-related forums.

design4
Posts: 6
Joined: Tue May 18, 2021 2:49 am

Re: Unable to access AP mode after 5 cycle

Postby design4 » Tue May 18, 2021 7:44 am

Hi,

Yes, but the things is same. Its also happened on my ESP32. Need help here

Thanks in advanced

Who is online

Users browsing this forum: No registered users and 119 guests