-->
Page 1 of 3

To delay or not to delay....

PostPosted: Sat Nov 19, 2016 6:11 am
by eketjall
First look att this code:
Code: Select allvoid setup() {
  Serial.begin(74880);
  Serial.println("Booting\n");
 
  Serial.printf("Reset reason: %s\n",ESP.getResetReason().c_str());
  Serial.printf("Vcc:  %d\n", ESP.getVcc());

  Serial.printf("ESP8266 ID:  %08X\n", ESP.getChipId());
  Serial.printf("Flash ID:    %08X\n", ESP.getFlashChipId());
  Serial.printf("Flash size:  %u\n\n", ESP.getFlashChipRealSize());

  Serial.printf("\nConnecting");
  WiFi.mode(WIFI_STA);
  WiFi.config(wifi_ip, wifi_gateway, wifi_subnet);
  WiFi.begin(wifi_ssid, wifi_password, wifi_channel, wifi_bssid);
  while (WiFi.status() != WL_CONNECTED) {
    delay(100);
    Serial.printf(".");
  }
  Serial.printf("\n");
  Serial.printf("\nSSID:     %s\n", WiFi.SSID().c_str());
  Serial.printf("IP:       %s\n",WiFi.localIP().toString().c_str());
  Serial.printf("Mac-addr: %s\n", WiFi.BSSIDstr().c_str());
  Serial.printf("RSSI:    %d dBm\n", WiFi.RSSI());
}

This produces this output: (Notice the gibberish output from the first Serial.printf lines)
Code: Select all ets Jan  8 2013,rst cause:2, boot mode:(3,0)

load 0x4010f000, len 1384, room 16
tail 8
chksum 0x2d
csum 0x2d
v09f0c112
~ld
   ½½Ñ¥¹�)jR$Uk•Ñ�’•…ͽ¹é*…ѕɹ…±�šåÍÑ•µ)²��éšÂ‚ªRT5…’²²JE¥‚‚‚*Â2•RdÅ+k RŠ”‚‚Š¢¢‚*RdÅ+k ÒW-¯Y'HHL˜š8576


Connecting....

SSID:     heeke
IP:       192.168.0.51
Mac-addr: 10:C3:7B:44:5C:BA
RSSI:    -60 dBm


So I added a delay after Serial.begin, Everything else the same.
Code: Select allvoid setup() {
  Serial.begin(74880);
  delay(1000);
  Serial.println("Booting\n");
  ...

AndI get this output.
Code: Select all ets Jan  8 2013,rst cause:2, boot mode:(3,0)

load 0x4010f000, len 1384, room 16
tail 8
chksum 0x2d
csum 0x2d
v09f0c112
~ld
ÿBooting

Reset reason: External System
Vcc:  3797
ESP8266 ID:  000E08F9
Flash ID:    001440E0
Flash size:  1048576


Connecting....................................................................................


Only problem, the wifi never connects. And yes, this is repeatable over and over again with just adding or deleting that 1 second delay.
What is really happening here?
Why do I get gibberish output in the first case?
And why won't the wifi connect if I add a delay?

Re: To delay or not to delay....

PostPosted: Sat Nov 19, 2016 6:35 am
by Pablo2048
IMHO it's because how the SDK behave - try to add WiFi.disconnect() after delay(1000) - ESP WiFi stores credentials given into flash area and after next boot SDK automatically uses this info for starting WiFi part BEFORE setup() was called, so Your WiFi.mode(...) sometime strike somewhere into middle of connecting sequence and breaks things up. I also suggest to use WiFi.persistent(false) if You want to set WiFi credentials in every bootup (spare flash configuration area overwrite on every reset).

Re: To delay or not to delay....

PostPosted: Mon Nov 21, 2016 12:59 am
by eketjall
Pablo2048 wrote:IMHO it's because how the SDK behave - try to add WiFi.disconnect() after delay(1000) - ESP WiFi stores credentials given into flash area and after next boot SDK automatically uses this info for starting WiFi part BEFORE setup() was called, so Your WiFi.mode(...) sometime strike somewhere into middle of connecting sequence and breaks things up. I also suggest to use WiFi.persistent(false) if You want to set WiFi credentials in every bootup (spare flash configuration area overwrite on every reset).


So what is best practice when still wanting autoconnect?
In my case above a shorter delay, 10ms, solved both Serial.print and WiFi, but how should it be done if I don't want to rely on "luck"?

Is there a way to check if autoconnection is in progress and just manually connect if that fails?

Re: To delay or not to delay....

PostPosted: Mon Nov 21, 2016 1:30 am
by Pablo2048
Look into ESP8266WiFiGeneric.h, ESP8266WiFiType.h and ESP8266WiFiSTA.h - there is many ways how to do what You want and nobody can tell You what is the best practice (it depends on use case)...