Clean start and stop wifi ap + http server

doglike
Posts: 63
Joined: Fri Aug 18, 2017 4:21 pm

Clean start and stop wifi ap + http server

Postby doglike » Mon Oct 28, 2019 5:36 pm

Hi,
in some cases I want to shut down the wifi and http server.
So I wrote this simple piece of code and wanted to ask you guys, if I did it right (API-calls and sequencing)
or if there is a cleaner way to do it. (???)
Thanks in advance!

PS: It works quite well, but in some rare cases wifi ap starts up, but the http server does not responde.

Code: Select all

static bool Flg_WifiRunning = false;

void handleWifi(void)
{
	//Flg_MainEnabler is a global variable set by another function
	if (!Flg_MainEnabler && Flg_WifiRunning){
		server.stop();
		server.close();
		esp_wifi_stop();
		Flg_WifiRunning = false;
	}
	else{	 
		// if already stopped, wait for Flg_MainEnabler
		while (Flg_MainEnabler){
			if (!Flg_WifiRunning){
				esp_wifi_start();
				WiFi.softAP(_SSID, _PASSWORD, _CHANNEL, _INVISIBLE, _USER_NUM);
				server.begin();	
				Flg_WifiRunning = true;					
			}
			else{
				server.handleClient();												
			}
		}
	}
}

Who is online

Users browsing this forum: Baidu [Spider], Bing [Bot] and 69 guests