1

The ESP8266 is supposed to remember the WiFi credentials and reconnect to the network (if it is available) after each reboot. That was the topic of this question: Does the ESP8266 somehow remember wifi access data?

I was observing that behavior and something has changed, I think when I updated via Board Manager in the Arduino IDE. Now the credentials are lost each time that the device reboots. I've tried going back to earlier versions, but I have not been able to roll back to a version of the board definition that restores the old behavior.

Here's a sample sketch that I boiled down for testing:

#include <ESP8266WiFi.h>

char ssid[] = "ssid";
char pss[]  = "pass";

void setup() {
  Serial.begin(115200);

  Serial.println("Started");

  pinMode(0, OUTPUT);
  pinMode(2, OUTPUT);

  if (WiFi.status() != WL_CONNECTED) {
    Serial.println("Not connected");
    WiFi.begin(ssid, pss);
    digitalWrite(0, LOW);
    digitalWrite(2, HIGH);
  } else {
    Serial.println("Connected");
    digitalWrite(0, HIGH);
    digitalWrite(2, LOW);
  }
}

void loop() {
  Serial.println("Loop");
  delay(500);
}

The first time through, of course, it hits the first case and prints "Not connected." On reboot, however, I was expecting it to hit the second case and print "Connected".

I'm programming via the Arduino IDE. The board is the Adafruit Feather HUZZAH ESP8266. I've tried lwIP v1.4 and lwIP v2 in version 2.4.1 of the board. I started rolling back versions from there got the same run-time results.

So, questions:

  1. Is this a known bug of the ESP8266 modules in recent versions?
  2. Is there some aspect of the WiFi configuration that's preserved on the board when I upload a new sketch? If so, how do I roll back?
1
  • 1
    remove the if statement from the setup() block .... put serial.println(WiFi.status()) into the loop() block .... reduce the delay to 100mS ..... that should show you if it connects and how long it takes to connect
    – jsotola
    Aug 1, 2018 at 2:16

2 Answers 2

2

The esp8266 SDK libraries starts the STA connection, but it is handled by other thread then the Arduino sketch. You can wait for this connection in setup with waitForConnectResult. It returns status().

  if (WiFi.waitForConnectResult() != WL_CONNECTED) {
    Serial.println("Not connected");
    WiFi.begin(ssid, pss);
    digitalWrite(0, LOW);
    digitalWrite(2, HIGH);
  } else {
    Serial.println("Connected");
    digitalWrite(0, HIGH);
    digitalWrite(2, LOW);
  }

to connect with remembered STA mode, ssid and password, but with static IP address

  WiFi.setAutoConnect(false);
  IPAddress ip(192, 168, 1, 8);
  IPAddress gw(192, 168, 1, 1);
  IPAddress sn(255, 255, 255, 0);
  WiFi.config(ip, gw, sn, gw);
  WiFi.begin(); // uses remembered ssid and password
  WiFi.waitForConnectResult();
2
  • This fixed the issue in the sample sketch that I created for the question. For anyone coming along later, to make it work in the real sketch, I had to use this and also coordinate it with other aspects of the WiFi set-up, especially the selection of the mode. My original was changing the mode from AP to AP+STA over the course of the sketch, and I had to eliminate that part so that it is in AP+STA from the start. Strange that this same sketch worked before and then suddenly stopped, but I'm happy to having working again now.
    – Brick
    Aug 1, 2018 at 16:17
  • mode and AP settings are remembered too and initiated parallel to sketch
    – Juraj
    Aug 2, 2018 at 4:53
2

First, let's answer the OP's questions directly:

  1. Is this a known bug?

    Ans: There are and will continue to be '[issues][1],' but as @Juraj's answer points out it was not an issue, but a sketch that wasn't waiting for the quickest method to get wifi + IP ready on an esp8266, the SDK connect.

  2. Is there ... WiFi configuration that's preserved on the board when I upload a new sketch? How to roll back?

    Ans: There is no 'rolling back,' only erase & write new data to flash to recover. At the time of sketch upload, 'wifi settings' can only be erased. The user would choose in the Arduino IDE menu > Tools > Erase Flash: Sketch + Wifi Settings. Those 'wifi settings' are typically written to flash by calling

WiFi.begin(ssid,passphrase);  // or,
WiFi.begin(ssid,passphrase,channel,bssid,connect);  // about 2x quicker

The final arg can be false; we don't even need to connect to wifi to save the settings, eg.

begin("ssid", "passphrase", 1, {0xA4,0xB1,0xE9,0xBC,0x6A,0x28}, false);
ESP.restart();

Then wait for the SDK firmware to connect.

And now let's conclude with the 'newer' information, due to changes in the Arduino esp8266 core 3.0, circa mid-2020.

Beginning in core 3.0, the SDK connect feature was disabled by default. Since then, sketches wanting to take advantage of the quick SDK connect (about 220 ms, or twice as fast as any WiFi.begin), the following line must be included early in the setup() function

enableWiFiAtBootTime();                // prevents shutdown of sdk connect

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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