Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By snowdenator
#31747 Hi. First post, and it's already help needed (maybe not a good sign!).

I am trying to list the first 5 found SSIDs using the ESP8266 Arduino, and it is failing on strncpy() operations with an exception. The code in question is below:
Code: Select all  if (NETWORKS_FOUND != 0) {
    for (int i = 0; i < NETWORKS_FOUND; i++) {
      if (i > 5) {
        break; //We only want 5 on the OLED
      }

      CURRENT_SSID_CHAR_FULL = WiFi.SSID(i); //Copy to variable

      Serial.println(CURRENT_SSID_CHAR_FULL); //Prints SSID fine
     
      os_strncpy(CURRENT_SSID_CHAR, CURRENT_SSID_CHAR_FULL, 15); //This is where the code fails with an exception, trying to truncate the string to 15 chars
     
      Serial.print("SSID #"); Serial.print(i); Serial.print(": "); Serial.println(CURRENT_SSID_CHAR);
      //sendStrXY(CURRENT_SSID_CHAR, i, 0);
      delay(10);
    }
  }


With CURRENT_SSID_CHAR_FULL being const char* and CURRENT_SSID_CHAR being char* which is supposed to be the truncated code. The code is on GitHub https://github.com/snowdenator/ESP8266_Stuff/commit/971242dce4a3705770b367927df142504fe461d2

I have tried things such as os_strncpy() to no avail.

The exception that is produced:
Code: Select allWiFi network scan complete
No. of networks found: 5
SKY1AB19

Exception (29):
epc1=0x4000c081 epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000000 depc=0x00000000

ctx: cont
sp: 3ffea5f0 end: 3ffea7c0 offset: 01a0

>>>stack>>>
3ffea790:  00000000 00000000 00000000 3ffea7ec 
3ffea7a0:  3fffdc20 00000000 3ffea7e4 40201cca 
3ffea7b0:  00000000 00000000 3ffe97a0 40100378 
<<<stack<<<

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

wdt reset


Any help would be appreciated.
User avatar
By schufti
#31763 the last line of the "error message" tell us that it is due to a watchdog timer reset.

I didn't have a look at your complete source but somehow you allready spent too much time in the part before the strncpy(). Try to put a yield() just before it ( in the for loop enumerating the ssid's) as the delay(10) seems to be too late ...
User avatar
By snowdenator
#31770
schufti wrote:the last line of the "error message" tell us that it is due to a watchdog timer reset.

I didn't have a look at your complete source but somehow you allready spent too much time in the part before the strncpy(). Try to put a yield() just before it ( in the for loop enumerating the ssid's) as the delay(10) seems to be too late ...


After doing some testing with your suggestions, it appears that even putting a ridiculous amount of yield()s everywhere doesn't help. It seems to be something related to the strncpy() itself which blocks for too long. Commenting out strncpy() makes the code work consistently but it needs to somehow truncate the char array still.
User avatar
By eduperez
#31804 I am afraid there is a bug (or two) in you code:

You have declared CURRENT_SSID_CHAR as a pointer, but you have not initialized it, or allocated any memory for the data you want to store there. Thus, your call to os_strncpy is writing data somewhere within your device's memory where you are not expected to write any data.

Now, remember that os_strncpy does not zero-terminate the destination string when it has to truncate the input. Thus, when the SSID name is longer than 15 characters, CURRENT_SSID_CHAR becomes an unterminated string, and the call to Serial.println will probably send some unwanted characters to the serial.

Hope this helps!