Arduino icon indicating copy to clipboard operation
Arduino copied to clipboard

Rebooting when joining WiFi

Open sticilface opened this issue 11 years ago • 48 comments

Using latest version 1.6.1-esp8266-1-1054-g3183c7c

I have a project that has been working fine (at least booting and joining wifi.) now it crashes upon trying to join wifi...

NORMAL ACCESS MODE ENABLED
Scanning for Networks

Joining Wifi Network....
 ets Jan  8 2013,rst cause:2, boot mode:(3,7)

load 0x4010f000, len 1464, room 16 
tail 8
chksum 0x7a
csum 0x7a
e���(�SQS�(RQ�)HT�)SHHHC���r

Not sure what to do next, to help? I should add that the same code compiled on a previous IDE works fine.

sticilface avatar Jun 15 '15 14:06 sticilface

Figured it out....

This used to work just fine...

int i = 0;
    while ((WiFi.status() != WL_CONNECTED ) && i < 40 ) {
    delay(500);
    i++;
    Serial.print(".");
    if (i == 39) Serial.print("Failed");
    }

but now it needs to be with the brackets around each part of the &&

int i = 0;
    while ((WiFi.status() != WL_CONNECTED ) && (i < 40 )) {
    delay(500);
    i++;
    Serial.print(".");
    if (i == 39) Serial.print("Failed");
    }

sticilface avatar Jun 15 '15 15:06 sticilface

OK that has not fixed it. Seems like some small code changes can cause this... then un cause it.

sticilface avatar Jun 15 '15 15:06 sticilface

Not really helping, but dispensing wisdom or so:

my two top rules: -- always use {}, even if not needed. -- always use (), even if not needed.

I don't do if (a==1) b=2; Always: if (a==1) { b=2; }

Just too difficult to remember: if (a & 2 <7) ... if (a * 2<7) ... What was the evaluation rule?

And as far as I see, I use this in wifi-client-mode in my code without any problem.

holgerlembke avatar Jun 15 '15 17:06 holgerlembke

Have similar issue with specific router (Mikrotik 951). But not with other (Allied Telesis AT-WA1104G). Got irregular resets by wdt. Sometimes the hardware "reset" solves the issue but not always

BUT! When I switch my program to "more verbose" mode (not recompile, just invoke CLI command for change "logging" flag) and I have no resets. There are a lot of "delay(...)" inside main loop so possibly no room for locking

PS: I use 1.6.5 git build from 2015-05-28

av1024 avatar Jun 17 '15 14:06 av1024

I suspect that there is something going on..... i just had several reboots... and then it worked all of a sudden. This is on an ESP12. the flash previous to this failed, then i changed it from 512K to 4M and it worked. so it is quite an intermittent problem...

�*h!�`�h
�����怘�~3f<��<���f<����`<?f<~?�3�rl��r��c�n�����p�|����x��ǒ��p�nn��;�n�����b�cl`$`�p�n�������l�����b�n��n�$����b��<~�n�����l`���#�n�rnr���;���lxrr�ےn�����`�����(�SQS�(RQ�)HT�)SHHHC���r
Welcome to Andrew's ESP Software

EMERGENCY ACCESS MODE ENABLED
Scanning for Networks

Joining Wifi Network....
 ets Jan  8 2013,rst cause:2, boot mode:(3,7)

load 0x4010f000, len 1464, room 16 
tail 8
chksum 0x7a
csum 0x7a
e���(�SQS�(RQ�)HT�)SHHHC���r
Welcome to Andrew's ESP Software

EMERGENCY ACCESS MODE ENABLED
Scanning for Networks

Joining Wifi Network....
 ets Jan  8 2013,rst cause:2, boot mode:(3,7)

load 0x4010f000, len 1464, room 16 
tail 8
chksum 0x7a
csum 0x7a
e���(�SQS�(RQ�)HT�)SHHHC���r
Welcome to Andrew's ESP Software

EMERGENCY ACCESS MODE ENABLED
Scanning for Networks

Joining Wifi Network.rl��r��c�n�����p�|����x��ǒ��p�nn��;�n�����b�cl`$`�p�n�������l�����b�n��n�$����b��>~�n��Ì�l`���#�n�rnr���;���lxrr�ےn����`�����(�SQS�(RQ�)HT�)SHHHC���r
Welcome to Andrew's ESP Software

EMERGENCY ACCESS MODE ENABLED
Scanning for Networks

Joining Wifi Network....
 ets Jan  8 2013,rst cause:2, boot mode:(3,7)

load 0x4010f000, len 1464, room 16 
tail 8
chksum 0x7a
csum 0x7a
e���(�SQS�(RQ�)HT�)SHHHC���r
Welcome to Andrew's ESP Software

EMERGENCY ACCESS MODE ENABLED
Scanning for Networks
rl��r��c�n�����p�|����x��ǒ��p�nn��;�n�����b�$rrp�n�������l�����b�n��n�$����b��<~�n�����l`���#�n�rnr���;���lxrr�ےn����`�����(�SQS�(RQ�)HT�)SHHHC���r
Welcome to Andrew's ESP Software

EMERGENCY ACCESS MODE ENABLED
Scanning for Networks

Joining Wifi Network.........
Connected to fyffest
IP address: 192.168.1.162
Initiating MQTT Connection: Connecting: Success
MQTT msg SENT: esp/speed, Message: 192.168.1.162
MQTT msg SENT: speed/IP, Message: 192.168.1.162
MQTT msg SENT: speed/Version, Message: WS2812
MQTT msg SENT: speed/Status, Message: Device Ready
Current wifi mode is : 1
MQTT Message Recieved: speed/IP => 192.168.1.162
MQTT Message Recieved: speed/Version => WS2812
MQTT Message Recieved: speed/Status => Device Ready

sticilface avatar Jun 17 '15 14:06 sticilface

I am also having an issue similar to you. My code loops through open wifi-networks attempting to connect to each one. Every few loops it crashes on a WiFi.begin(ssid) call. I have the most recent commit, and I've tried many different delays, using/not using WiFi.disconnect(), using a fake password [WiFi.begin(ssid, pass)], stripping down the code and rewriting line by line, trying different Serial bauds, trying different memory sizes, trying the esp-01 and esp-12... It runs fine until I start trying to connect to a variety of APs.

This is what I add that causes the issue: Names[i] holds all the open networks found and is set-up properly.

 for (int i=0; i<netcount; i++) {
      ESP.wdtFeed();
      Serial.println(WiFi.localIP());
      WiFi.begin(names[i]); 
      delay(5000);
      Serial.println(names[i]);
      Serial.println(WiFi.localIP());
      WiFi.disconnect();
      delay(3000);
    }

I also tried this code without feeding the watchdog and with removing the delays:

 for (int i=0; i<netcount; i++) {
      Serial.println(WiFi.localIP());
      WiFi.begin(names[i]); 
      Serial.println(names[i]);
      Serial.println(WiFi.localIP());
      WiFi.disconnect();
    }

Still resets. Output:

CHELSEA
0.0.0.0
0.0.0.0
HP852D2E
0.0.0.0
0.0.0.0
Pubert's-guest
0.0.0.0
0.0.0.0

 ets Jan  8 2013,rst cause:4, boot mode:(3,7)

wdt reset
load 0x4010f000, len 1464, room 16 
tail 8
chksum 0x7a
csum 0x7a
erp“à;ÏÈ„ÄÛñ9’œØÒSetup done

I'm not sure if its my code or if it is an SDK issue.

Commander13 avatar Jun 17 '15 21:06 Commander13

Just fishing in the dark, but could you try a

void *p = malloc(5000); if (p) { free(p); } else { Serial.println("Heap full"); }

somewhere in your loop? The getfreeheap() function might report misleading values...

holgerlembke avatar Jun 17 '15 21:06 holgerlembke

Thanks for the suggestion. I modified like so:

     for (int i=0; i<netcount; i++) {
      ESP.wdtFeed();
      Serial.println(WiFi.localIP());
      void *p = malloc(5000);
      if (p) {
        free(p);
      } else {
        Serial.println("Heap full");
      }
      WiFi.begin(names[i], names[i]); 
       void *g = malloc(5000);
      if (g) {
        free(g);
      } else {
       Serial.println("Heap full");
      }
      delay(5000);
      Serial.println(names[i]);
      Serial.println(ESP.getFreeHeap());
      Serial.println(WiFi.localIP());
      WiFi.disconnect();
       void *pnus = malloc(5000);
      if (pnus) {
        free(pnus);
      } else {
       Serial.println("Heap full");
      }
      delay(3000);
    }

But it still rebooted without giving me a Heap full...

scan start
scan done
59 networks found
Open nets: 15
0.0.0.0
KindFlamingo-guest
28712
192.168.34.12
0.0.0.0

 ets Jan  8 2013,rst cause:4, boot mode:(3,7)

wdt reset
load 0x4010f000, len 1464, room 16 
tail 8
chksum 0x7a
csum 0x7a
erp›à;OÈ„ÄÙq9’œØb¬ÿSetup done

Commander13 avatar Jun 17 '15 22:06 Commander13

This is what the Debugger gives me:

[W]sec 1073686212 error
scandone

0.0.0.0
scandone
reconnect
Fatal exception (28):
epc1=0x4000debe, epc2=0x00000000, epc3=0x00000000, excvaddr=0x00000000, depc=0x00000000
scandone
del if0
usl
sul 0 0

This error is killing me.

Commander13 avatar Jun 18 '15 00:06 Commander13

I try code below called every minute in main loop:

// === TEST ===
  uint16_t a0, a1, a2;
  void *_tmp;
  a0 = ESP.getFreeHeap();
  Serial.print("Heap-test : "); Serial.print(a0); Serial.print("/"); 
  _tmp = malloc(5000);
  if (!_tmp) {
    Serial.print("FULL/");
  } else {
    a1 = ESP.getFreeHeap();
    free(_tmp);
    Serial.print(a1); Serial.print("/"); 
  }
  a2 = ESP.getFreeHeap();
  Serial.println(a2);
  // ====

and got Heap-test : 22896/17880/22896 prints (+/- 16 bytes). So it is possibly not heap issue. But I still have no resets here. Emulating "bad wifi" via setting wrong password for SSIDs also has no effect

av1024 avatar Jun 18 '15 07:06 av1024

Fatal exception (28): epc1=0x4000debe, epc2=0x00000000, epc3=0x00000000, excvaddr=0x00000000, depc=0x00000000 scandone del if0 usl sul 0 0

memcmp function was passed a zero for the first argument and generated an exception.

igrr avatar Jun 18 '15 07:06 igrr

Problem with getFreeHeap() is, that it gives the total sum of all free heap blocks. So if you have 20 free blocks of 150 bytes in size, you get 3000 bytes free heap. But malloc(200) will fail because no block that size is free.

Basically the function is useless in its current state. And that's why I work on a heap walk utility. Current state: fail. :-/

holgerlembke avatar Jun 18 '15 08:06 holgerlembke

As an update about heap: I have "No heap available, failed to malloc 0" message every ~14-15 sec after setDebugOutput(true). There is no 15-sec events in my code (1 or 60sec through delay(10) in loop only)

The same error was in ESP8255WiFiMulti while malloc(<empty-string>) called so I wrote own enumeration class.

UPD: not reproduced on clean sketch... Will try debug PubsubCliend by Imroy... UPD2: Yes. I have got source of my malloc(0) message. Look at mqtt.cpp:readPacket() called for example by PubSubClient::loop().

av1024 avatar Jun 18 '15 09:06 av1024

I think the error in @Commander13 's case is one of names[i] being a null pointer.

Regarding the "No heap available, failed to malloc 0" messages, Espressif's malloc implementation erroneously prints this message when you try to allocate zero bytes. I will add a check to our malloc wrapper so that these messages are not generated.

Regarding heap fragmentation, it is quite hard to achieve such an extreme case in practice without contrived scenarios. I did have the hooks on the allocator functions, pvPortMalloc/vPortFree, installed when I had just started developing this core. I wanted to check if dynamic allocation is useable at all. The hooks just logged the malloc/free calls to console, so I could analyze what was happening. Have to say, with most sketches from the standard Arduino example set which I was able to compile, fragmentation was a non-issue in the long run. While real apps are more complex than these examples, i still think a bit of though and planning goes a long way eliminating issues caused by heap fragmentation.

That is not to deter @holgerlembke from the task of writing a heap walking utility, I just want to point out that it is highly unlikely that this is the root cause of the problem reported here :)

igrr avatar Jun 18 '15 09:06 igrr

I'm not entirely sure how to proceed with debugging. For me this only happens on an ESP-12 module, not any of the ESP-01 or a NODE-MCU board.

Second, my script never gets to the loop. in fact it never gets to the line past joining the wifi, where it prints success.. before it reboots..

sticilface avatar Jun 18 '15 18:06 sticilface

@sticilface, Just add Serial.setDebugOutput(true) after Serial.begin(...)

Yet another test with resets. The same debug output few times:

beacon timeout
rm match
pm close 7 0 0/383183288

 ets Jan  8 2013,rst cause:4, boot mode:(3,0)

wdt reset
load 0x40100000, len 29904, room 16 
tail 0
chksum 0x4f
load 0x3ffe8000, len 1580, room 8 
tail 4
chksum 0x53
load 0x3ffe8630, len 4228, room 4 
tail 0
chksum 0x12
csum 0x12
rl

av1024 avatar Jun 18 '15 18:06 av1024

ah... getting more info than when i tried that before.. here is what i get

Joining Wifi Networkf -240, scandone
Fatal exception (28):
epc1=0x4020a6e6, epc2=0x00000000, epc3=0x00000000, excvaddr=0x00000000, depc=0x00000000

 ets Jan  8 2013,rst cause:2, boot mode:(3,7)

load 0x4010f000, len 1464, room 16 
tail 8
chksum 0x7a
csum 0x7a
e���(�SQS�(RQ�)HT�)SHHHC���r

NORMAL ACCESS MODE ENABLED
Scanning for Networks
scandone
f 0, scandone

Joining Wifi Networkf -240, scandone
Fatal exception (28):
epc1=0x4020a6e6, epc2=0x00000000, epc3=0x00000000, excvaddr=0x00000000, depc=0x00000000

 ets Jan  8 2013,rst cause:2, boot mode:(3,7)

load 0x4010f000, len 1464, room 16 
tail 8
chksum 0x7a
csum 0x7a
e���(�SQS�(RQ�)HT�)SHHHC���r

NORMAL ACCESS MODE ENABLED
Scanning for Networks
scandone
f 0, scandone

sticilface avatar Jun 18 '15 18:06 sticilface

What about minimal sketch? If no error on minimal sketch - try disable parts of your code and check

void setup() {
  Serial.begin(115200);
  Serial.setDebugOutput(true);
  WiFi.begin("hardcoded-ssid", "hardcoded-pass");
}
loop () {
  ;
}

UPD: Hey! Did you use non-printable characters in password? AFAIR, some special symbols was reset my ESP until I change password. It may be '&' or '$'...

av1024 avatar Jun 18 '15 18:06 av1024

You might also try setting wifi.mode() before calling wifi.begin.

The SDK seems to hold on to your previous wifi.mode setting, and will try to connect to an AP, for instance, even if you don't call wifi.begin, based on your previous setting. Makes it difficult to debug, because it appears random.

TimeTravelingOwls avatar Jun 18 '15 18:06 TimeTravelingOwls

As per the suggestion of @igrr I checked to ensure that names[i] was not null:

 for (int i=0; i<netcount; i++) {
      ESP.wdtFeed();
      Serial.println(WiFi.localIP());
      if (names[i] != NULL) {
          WiFi.begin(names[i]); 
          delay(5000);
          Serial.println(names[i]);
          Serial.println(WiFi.localIP());
          WiFi.disconnect();
      }
      delay(3000);
    }

Unfortunately it still reboots...

Commander13 avatar Jun 18 '15 20:06 Commander13