Skip to content
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

wifi.Status returns 6 while busy connecting to AP, not 0. #4091

Open
errolt opened this issue Jan 4, 2018 · 8 comments
Open

wifi.Status returns 6 while busy connecting to AP, not 0. #4091

errolt opened this issue Jan 4, 2018 · 8 comments
Assignees

Comments

@errolt
Copy link

errolt commented Jan 4, 2018

Basic Infos

Hardware

Hardware: NodeMCU 0.9
Core Version: 2.4.0

Description

WiFi.Status() returns 6 while attempting to connect to AP. According to documentation, 6 means that the module is not set up for Station mode. Should it not return 0 while connecting to AP?

Settings in IDE

Module: NodeMCU 0.9
Flash Size: 4MB
CPU Frequency: 80Mhz
Flash Mode: ?
Flash Frequency: ?
Upload Using: SERIAL
Reset Method: nodemcu

Sketch

#include <ESP8266WiFi.h>
void setup() {
// put your setup code here, to run once:
WiFi.begin("xyz", "xyz"); // connect to the network
Serial.begin(115200);
Serial.println("Reset Reason: " + String(ESP.getResetReason()));
Serial.print("Auto Connect:");
Serial.println(WiFi.getAutoConnect()?"True":"False");
Serial.println("Core Version:" + String(ESP.getCoreVersion()));
Serial.println("SDK Version:" + String(ESP.getSdkVersion()));

while(WiFi.status() != WL_CONNECTED)
{
Serial.println("WiFi.Status():" + String(WiFi.status()));
delay(100);
}
Serial.println("WiFi.Status():" + String(WiFi.status()));
Serial.println("WiFi connected");

}

void loop() {
// put your main code here, to run repeatedly:
ESP.deepSleep(1000000 * 10, WAKE_RF_DEFAULT);
}

Debug Messages

Auto Connect:True
Core Version:2_4_0
SDK Version:2.1.0(deb1901)
WiFi.Status():6
wifi evt: 2
WiFi.Status():6
WiFi.Status():6
WiFi.Status():6
WiFi.Status():6
WiFi.Status():6
WiFi.Status():6
WiFi.Status():6
wifi evt: 0
WiFi.Status():6
wifi evt: 3
WiFi.Status():3
WiFi connected
state: 5 -> 0 (0)
rm 0
del if0
usl
wifi evt: 1
STA disconnect: 8
enter deep sleep

@kapyaar
Copy link

kapyaar commented Jan 8, 2018

I have had a similar issue. In my case it was mostly in the second or third time (as in program, run, turn off, and then when turned back on) that I would see this issue. After many digs, Something that seems to improve the situation was as follows.

Wifi.disconnect();
delay(1);
WiFi.begin("xyz", "xyz"); // connect to the network

Give it a shot and see if this works.

@errolt
Copy link
Author

errolt commented Jan 10, 2018

Hi kapyaar,

This is for a battery powered device, so it must connect as fast as possible, I can't afford to kill the connection that is in progress, wait 1ms, then restart the connection process.

Thank you,
Errol

@kapyaar
Copy link

kapyaar commented Jan 10, 2018

True, and I am not even sure if this will fix your issue, but if not having wifi.disconnect() will keep the module waiting for many more milliseconds and there is no other obvious fix, this might be worth a try. :)

@electron1979
Copy link

electron1979 commented Sep 15, 2018

Didn't work for me. Thanks!
Try #119 (comment)

@neu-rah
Copy link

neu-rah commented Mar 26, 2019

I'm facing a similar issue, wifi state seems to get updated only when there is activity, servers can sit behind a lost connection (reporting state 3) while the connection was long gone. My assembly is primarly a server so it sits there waiting.

Periodic pings to the gateway made onStationModeDisconnected fire again when a connection is lost... the first attempt with a ping utility revealed inadequate has it was a blocking thing, so I came up with this short tick. One non-blocking ping request and a tolerance check.

extern "C" {
  #include <ping.h>
}

void tick_back(void *opt, void *resp) {
  constexpr int ticks_tolerance=3;//how many fails in a row will we tolerate (or sort off)
  static volatile int ticks_ok=0;
  ping_resp* ping_resp = reinterpret_cast<struct ping_resp*>(resp);
  if (ticks_ok>0&&ping_resp->ping_err==-1) ticks_ok--;
  else if (ticks_ok<ticks_tolerance) ticks_ok++;
  wifiConnected&=ticks_ok>0;//update my own state
}

//network tick, not using delay stuff and sending only one packet
void nw_tick(IPAddress dest) {
  static ping_option tick_options;
  memset(&tick_options, 0, sizeof(struct ping_option));
  tick_options.count = 1;
  tick_options.coarse_time = 0;
  tick_options.ip = dest;
  tick_options.sent_function = NULL;
  tick_options.recv_function = reinterpret_cast<ping_recv_function>(&tick_back);
  ping_start(&tick_options);
}

...

nw_tick(WiFi.gatewayIP());//network tick, not realy a ping, keep things alive

it might shed some light on what the problem is

@niteshjindalxb
Copy link

Use this:

while (WiFi.waitForConnectResult() != WL_CONNECTED)

instead of

while (WiFi.status() != WL_CONNECTED)

@GitArunsh
Copy link

GitArunsh commented Mar 7, 2020

trick by niteshjindalxb worked for me.
use => (WiFi.waitForConnectResult() != WL_CONNECTED)

@d-a-v d-a-v self-assigned this Mar 7, 2020
@Westcott1
Copy link

Thanks, that worked for me as well!
An old ESP8266 would no longer connect, but an ESP32 did on the same code

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

8 participants