0

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
    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 '18 at 2:16
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();
  • 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 '18 at 16:17
  • mode and AP settings are remembered too and initiated parallel to sketch – Juraj Aug 2 '18 at 4:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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