如何关闭搭载ESP8266的D1 WiFi开发板创建的热点?
Hey there! I get it—you've set up a SoftAP on your ESP8266 D1 WiFi board using the ESP8266WiFi library, but trying to shut it down with serverWF.close() or serverWF.stop() isn't working. Let's clear up why that's happening and fix it right away.
The Problem with Your Current Approach
First off, serverWF is a WiFiServer instance—those close() and stop() methods only shut down the TCP server you've started, not the SoftAP hotspot itself. The hotspot will keep running in the background even after the server is stopped, which is why you're still seeing it active.
The Correct Way to Shut Down the SoftAP
To fully turn off the SoftAP, you need to use the WiFi.softAPdisconnect() function. Here's the catch: by default, this function only disconnects connected clients but leaves the hotspot enabled. To completely disable the hotspot, pass true as the parameter.
Step-by-Step Solution with Code
Here's how to properly shut down both the server and the hotspot:
// First, shut down the WiFi server to clean up resources serverWF.stop(); // Now, completely disable the SoftAP hotspot WiFi.softAPdisconnect(true); // Optional: Verify the hotspot is closed (check connected stations) Serial.print("SoftAP status: Closed. Connected stations remaining: "); Serial.println(WiFi.softAPgetStationNum());
Notes for Re-Enabling Later
If you want to start the hotspot again later, just re-run your original setup code:
WiFi.softAP(ssidHS, passwordHS); serverWF.begin();
That should do the trick—your hotspot will disappear from available networks once you run the shutdown code.
内容的提问来源于stack exchange,提问作者Guy Luz

