Skip to content

WiFi IP config issues, need WiFi::reset() #1045

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
giorgiogallina opened this issue Mar 27, 2025 · 2 comments
Open

WiFi IP config issues, need WiFi::reset() #1045

giorgiogallina opened this issue Mar 27, 2025 · 2 comments

Comments

@giorgiogallina
Copy link

giorgiogallina commented Mar 27, 2025

Dear developers,

I am having troubles in switching between WiFi's STA and AP mode on my Arduino GIGA R1 board.
In my scenario, I do the following:

  1. I try to connect to some network (STA)
  2. If connection fails, I open AP (with random IP address) to ask credentials from user
  3. Once credentials are retrieved (or on timeout, or on error), repeat from point 1.

So far I found that:

  • WiFiUDP::endPacket() often fails with error -3003 (NSAPI_ERROR_PARAMETER).
  • For some unknown reasons, if I call IPAddress nullip = IPAddress(); WiFi.config(nullip, nullip, nullip, nullip); before WiFi.end() before STA connection, the latter is incredibly more likely to succeed.
  • However, if I do so, WiFiUDP:endPacket() always fails as above.
  • I found that if I do WiFi = WiFiClass(WiFiInterface::get_default_instance()) after WiFi.end() every time I switch between STA and AP, I have less troubles.

Before AP creation, a random IP address is set as follows:

_wifi_ap_ip = IPAddress((char)random(11, 172), (char)random(0, 255), (char)random(0, 255), 0x01);
WiFi.config(_wifi_ap_ip, _wifi_ap_ip, _wifi_ap_ip, IPAddress(255, 255, 255, 0));

I used to test my code on a protected network and I experienced fewer problems. Then I had to switch to an open network and a mess arose.

I guess a function like a total reset of the WiFi interface with sockets and everything, so to come back to a status as if the Arduino board was just switched on and no connection was attempted, would be advantageous in switching between STA and AP mode.

Thank you

@giorgiogallina giorgiogallina changed the title WiFi IP issues, need WiFi::reset() WiFi IP config issues, need WiFi::reset() Mar 31, 2025
@giorgiogallina
Copy link
Author

giorgiogallina commented Mar 31, 2025

So far, I have found that I need to call WiFi.disconnect() before WiFi.end(), since the latter does not automatically perform a disconnection. Since I used to invoke WiFi.config(nullip, ...) after explicit disconnection, probably it was not the config that enhanced subsequent probability of successful connection, but rather the explicit disconnection.
[Edit: this is true, but DHCP would not be restored]

In my code, I have also substituted the command

WiFi = WiFiClass(WiFiInterface::get_default_instance());

with the following

NetworkInterface* nif = WiFi.getNetwork();
if (nif != nullptr)
{ nif->set_default_parameters();
}

These modifications seem to improve my program, yet the WiFiUDP::endPacket() still fails with code -3003.
I'll keep debugging and update this issue with more details, if I discover anything interesting.

Meanwhile, I report that in WiFiClass::end(), the command _softAP = nullptr is invoked even if _sofAP was not stopped ("disconnected"), which may cause issues like some of the ones I was experiencing

@giorgiogallina
Copy link
Author

On the side, I am wondering if in file ArduinoCore-mbed/libraries/WiFi/src/WiFi.cpp in function WiFiClass::beginAP(...), lines 131-134, a softAPInterface->stop() should be performed before returning.

Moreover, on line 136, the condition result == NSAPI_ERROR_OK seems redundant, isn't it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant