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?
Brick
  • 184
  • 4
  • 13

2 Answers2

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();
Juraj
  • 18,264
  • 4
  • 31
  • 49
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
DWB
  • 141
  • 4