Skip to content

WIFI_AP_STA can cause AP too slow and not working if it can't connect to STA #1624

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

Closed
hallard opened this issue Feb 11, 2016 · 9 comments
Closed

Comments

@hallard
Copy link
Contributor

hallard commented Feb 11, 2016

Following this case #1615 I'm opening this dedicated one to avoid mixing issue.

To isolate and see the issue, I've done a sketch that switch Wifi autoConnect parameter back and forth (I'm using NodeMCU board with flash button, pushing button toggle autoConnect on and off). But as changing autoConnect in loop does nothing (SDK says) so on each change we need to reboot the board, that what's the sketch do.
What is clear is that if autoConnect is on and and Wifi STA SSID/PSK does not connect (wrong SSID/PSK or main router AP down), the problem occurs in AP mode (may be take too much time trying to auto connect to wrong router)
And, as soon as autoConnect is disabled, AP is working fine, so I suspect autoConnect trying to connect cause issue in AP code.

But there is a 2nd point, all I've just written is true only if you call ESP.eraseConfig() at the beginning of setup(). If you don't do that, AP is slow and broken whatever autoConnect is on or off. This means that SDK is may be storing something else somewhere used in conjunction with autoConnect/SSID/PSK.

Note also that ESP.eraseConfig() preserve autoConnect flag ;-)

Now the real problem is that when I'm at home, my NodeMCU Wifi connect to my main router and AP is up for 5 minutes for smartconfig in case we want to do so. Now I want to take my nodeMCU to another place for a customer demo. Of course It can't connect to main Wifi because I'm not at home, but the issue if that I see AP but once connected it's too slow, I miss about 50% of ping packet and thus I can't do anything, my web page are not coming and I can't config the NodeMCU to connect to customer Wifi.

Here is the sketch used and step to reproduce issue

  • burn it to a fresh ESP8266 (or use esptool.py with erase_flash option before)
  • once launched use serial monitor
  • after starting loop displayed, press flash button, this will disable autoConnect and reboot
  • connect your computer to new AP ESP-TESTAP
  • from your computer ping 192.168.4.1 => works fine
  • now press flash button again , this will enable autoConnect and reboot
  • connect your computer to new AP ESP-TESTAP
  • from your computer ping 192.168.4.1 => does not work ping slow and missed packet
#include <ESP8266WiFi.h>
#include <WiFiClient.h> 
#include <ESP8266WebServer.h>

#define SERIAL_DEBUG  Serial

bool autoConnect;
int buttonState;            
int lastButtonState = LOW;  
long lastDebounceTime = 0;  
long debounceDelay = 50;    // the debounce time

// NodeMCU led is GPIO16 and switch (Flash) on GPIO 0
uint8_t handle_key_led( uint8_t level)
{
  uint8_t temp;
  digitalWrite(16, 1);
  pinMode(16, OUTPUT);
  digitalWrite(16, !level);

  // Flash is on GPIO0
  pinMode(0, INPUT);
  temp = digitalRead(0);
  pinMode(0, OUTPUT); 
  return !temp;
}

// ==============
void setup() {
  int ret;
  // without delay sometime you got infinite reset loop 
  // (SDK config corrupted ???) which only a erase_flash can recover
  delay(500);
  ESP.eraseConfig();
  delay(500);
  // Set WiFi to station mode and disconnect from an AP if it was previously connected
  WiFi.mode(WIFI_AP_STA);
  SERIAL_DEBUG.begin(115200);
  SERIAL_DEBUG.println("\r\n========== SDK Saved parameters Start"); 
  WiFi.printDiag(SERIAL_DEBUG);
  SERIAL_DEBUG.println("========== SDK Saved parameters End"); 
  SERIAL_DEBUG.println("Connecting...");

  // Get current autoconnect state from SDK
  autoConnect = WiFi.getAutoConnect();
  WiFi.begin("DummySSID", "whateverkeyjustdontconnect");

  uint8_t timeout = 10; // 10 * 500 ms = 5 sec time out
  while ( ((ret = WiFi.status()) != WL_CONNECTED) && timeout-- ) {
    SERIAL_DEBUG.print(".");
    delay(500);
  }

  // connected ? disable AP, client mode only
  if ((ret = WiFi.status()) == WL_CONNECTED) {
    SERIAL_DEBUG.println("connected!");
    SERIAL_DEBUG.print("STA IP address "); 
    SERIAL_DEBUG.println(WiFi.localIP());
  // not connected ? start AP
  } else {
    //WiFi.mode(WIFI_AP);
    SERIAL_DEBUG.println("Configuring access point...");
    WiFi.softAP("ESP-TESTAP","allowmeistooshort");
    WiFi.softAPIP();
    SERIAL_DEBUG.print("AP IP address "); 
    SERIAL_DEBUG.println(WiFi.softAPIP());
  }

  SERIAL_DEBUG.println("Starting loop");
}

// ==============
void loop() {
  uint8_t btn = handle_key_led(autoConnect);

  // Debouncing
  if (btn != lastButtonState) {
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    // if the button state has changed:
    if (btn != buttonState) {
      buttonState = btn;

      // Only on button pressed, not released
      if (buttonState == HIGH) {
        autoConnect = !autoConnect;
        WiFi.setAutoConnect(autoConnect);
        SERIAL_DEBUG.print("Autoconnect will be now ");
        SERIAL_DEBUG.println(autoConnect);
        SERIAL_DEBUG.println("Restarting...");
        delay(500);
        // Changing Auto Connect in loop does nothing
        // it will be taken into account on reboot only 
        // see SDK wifi_station_set_auto_connect 
        ESP.reset();
      }
    }
  }

  lastButtonState = btn;
}

Hoping some SDK's guru will be able to help me to understand this strange behavior

Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

@miky2k
Copy link

miky2k commented Feb 11, 2016

I notice the anomaly randomly

@Links2004
Copy link
Collaborator

two short notes:
ESP.eraseConfig() take only a real effect when you do a force a reboot after it ESP.reset();
or the SDK may store back some data from ram later, but not all this can lead to a undefined state (may the reason for your infinite reset loop).

for the AP problem have you tried to use a other WiFi channel for the AP?
may the scanning for your wifi (fast channel switching) is the reason for your package lost, have read something in the SDK long ago.
have add it too a issues to this time need to search it.

@Links2004
Copy link
Collaborator

here it is (the second half)
#119 (comment)

@hallard
Copy link
Contributor Author

hallard commented Feb 11, 2016

@Links2004
whooo, what's an amazing post!!!
So I think you're right, since it can't connect to router, it tries on every channel and this cause AP issues due to channel changes, makes sense.

May be algorithm could be changed on SDK side, IE as SSID/PSK are stored in flash, and may be channel also, perhaps SDK could :

  • Try to connect to saved channel
  • if can't, try other all channels in case router as changed
  • if can't, ok we mainly have a problem (router off or ESP moved away), so let autoConnect activated but only on saved channel this will let AP mode working fine and also allow reconnect if router is back (on saved channel only)

@Links2004
Copy link
Collaborator

you can do the scan your self then your have full control over it.
basicly:

  • WiFi.disconnect(); /// SDK not longer try to connect any where
  • WiFi.scanNetworks(true); /// to trigger scan when you think it will not block system
  • if you see your router connect to it with WiFi.begin

its more or less the way my WiFi Multi code work, it scans for wifi on every run call when its not connected.
https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266WiFi/src/ESP8266WiFiMulti.cpp

@hallard
Copy link
Contributor Author

hallard commented Feb 13, 2016

Markus,
Nice idea, I like it ;-)

@dreamer2
Copy link

i have same problem with AP instability when STA tries to connect to nonexistent network
(incorrect password not lead to the problem)

sdk based webserver https://github.com/pvvx/esp8266web/ have "reconnect" delay and seems this parameter solves this problem

st0ff3r added a commit to nabovarme/MeterLogger that referenced this issue Apr 28, 2017
otherwise we have trouble connecting to ap when it tries to reconnect to non existing network
esp8266/Arduino#1624
@devyte
Copy link
Collaborator

devyte commented Oct 18, 2017

Channel hopping is known, behavior is expected and due to STA and SoftAP sharing a single radio.
Closing.

@devyte devyte closed this as completed Oct 18, 2017
@me21
Copy link

me21 commented Oct 24, 2017

Does WiFi.scanNetworks also cause channel hopping and associated clients disconnect?

dpgeorge pushed a commit to micropython/micropython that referenced this issue May 4, 2021
ksekimoto pushed a commit to ksekimoto/micropython that referenced this issue Jul 16, 2021
killw2017 pushed a commit to killw2017/micropython that referenced this issue May 7, 2024
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

6 participants