0

I'm programming an ESP8266 via the Arduino IDE with Arduino libraries. I explicitly set the WiFi mode to WIFI_AP, but then, as described below, I'm still able to use it as both AP and station. That leaves me unclear what's meant by the three modes available (WIFI_AP, WIFI_STA, and WIFI_AP_STA) since it seems that I've set to WIFI_AP explicitly but am getting behavior consistent with WIFI_AP_STA.

I have read various documents online about these modes. I think I'm clear on the conceptual difference between them. What concerns / confuses me is that the actual behavior does not appear to match the specified behavior.

More detail:

Near the beginning of my setup function, I have this code

WiFi.mode(WIFI_AP);
WiFi.softAP(ssid, key);

server.begin();

where ssid and key are strings defined with appropriate values, and server is a global variable of type WiFiServer that has been initialized outside of setup. When run, I see the network with the given name on my phone and can connect to it with the given password. I can then browse to the IP address assigned by the ESP8266 and set the webpage sent by the server that's been defined in code. So far so good.

At some point later, inside my loop, a condition triggers the ESP8266 to connect to my router. Specifically, I have this sequence of code that runs once that trigger occurs:

WiFi.disconnect();
WiFi.begin(router_ssid, router_key);
auto status = WiFi.status();
while ( (status != WL_CONNECTED) && (status != WL_CONNECT_FAILED) ) {
    delay(500);
    status = WiFi.status();
}

As before, router_ssid and router_key are variables declared elsewhere with the proper values for my router. I can verify from subsequent print statements, that the connection succeeds and get the IP address as assigned by my router. It is clear that the new IP is assigned by the router because it is on the subnet used by my router, which is different than the subnet the ESP8266 is defining in AP mode.

The ESP8266 is configured to serve a small webpage, as noted above. I can now reach that page using my phone, which is connected directly to the ESP8266 via the IP address assigned initially by the ESP8266 in AP mode, and I can also reach the same page from my laptop (connect to my router) using the IP address assigned by my router. It would seem that I'm effectively in "AP+STA" mode now, despite having explicitly set to "AP" mode at the beginning.

So what's happening here? Did I automatically switch to "AP+STA" mode when I called WiFi.begin? If yes, what's the point (if any) of calling WiFi.mode? If no, then why am I able to operate as if in "AP+STA" mode given the explicit setting of "AP" mode?

0

WiFi.begin() is a function of the ESP8266WiFiSTAClass. It changes the WiFi mode for you to STA or AP+STA based on the current mode.

begin functions are in ESP8266WiFiSTA

enableSTA function is in class ESP8266WiFiGeneric

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.