0

I am encountering a strange behavior. I am using ESP8266 arduino SPIFFS to store configuration settings. Here is the relevant part of mycode;

void loop()
{
      handleUartRxOk();
}

void handleUartRxOk() {
    String cmd;

    char charBuff[3200];
    char char_print[50];
    static bool terminatorReceived = false;

    char incomingChar = 0;   // for incoming serial data

    if (Serial.available()) {
        incomingChar = Serial.read();
        saveChar(incomingChar);

        if (incomingChar == '\r') {
            terminatorReceived = true;
        }

        if (terminatorReceived) {
            buffer[buffer_index - 1] = '\0';
            cmd = String(buffer);
            if (cmd == "XXX") {
                ConfigSettings.ssid = "SSID_XX";
                ConfigSettings.password = "PASSWORD_XX";
                saveConfig();
            } 

            buffer_index = 0;
            terminatorReceived = false;
        }
    }
}

In the above code, the UART will receive a command "XXX\r", then run saveConfig() which will save the config parameters ssid and parameters into SPIFSS. This code works perfectly fine until I add more code which is totally unrelated.

This is how the new code looks like.

void handleUartRxOk() {
    String cmd;

    char charBuff[3200];
    char char_print[50];
    static bool terminatorReceived = false;

    char incomingChar = 0;   // for incoming serial data

    if (Serial.available()) {
        incomingChar = Serial.read();
        saveChar(incomingChar);

        if (incomingChar == '\r') {
            terminatorReceived = true;
        }

        if (terminatorReceived) {
            buffer[buffer_index - 1] = '\0';
            cmd = String(buffer);
            if (cmd == "XXX") {
                ConfigSettings.ssid = "SSID_XX";
                ConfigSettings.password = "PASSWORD_XX";
                saveConfig();
            } 

            //Why does adding this else statement cause saveConfig() to crash when run?
            else {
                    strcat(charBuff, cmd.c_str());
                }   

            buffer_index = 0;
            terminatorReceived = false;
        }
    }
}

After adding the additional else clause, sending "XXX\r" to the UARt and causing saveConfig() will cause an exception error. This is puzzling since the new code did not even get to run.

The exception error is as below;

Exception (3):

epc1=0x401002f0 epc2=0x00000000 epc3=0x00000000 excvaddr=0x400072f6 depc=0x00000
000

ctx: sys 

sp: 3fff06b0 end: 3fffffb0 offset: 01a0
guagay_wk
  • 26,337
  • 54
  • 186
  • 295

1 Answers1

2

Declare char charBuff[3200]; as global and not inside a function. Arduino ESP8266 has a default stack size of around 4k. Your array size is risking an overflow. Try it and see if it works. I have encountered similar problem before. It worked for me.

The default stack size can be raised within memory limits by modifying cont.h under cores/esp8266/

EDIT: Information about the modification of default stack size was provided by Daniel Minion in the comments.

guagay_wk
  • 26,337
  • 54
  • 186
  • 295
  • 2
    The stack size isn't a _maximum_ of 4K, it's the default size. You can ramp it up as high as you want (Within memory limits of course) by modifying cont.h under cores/esp8266/ – Dawn Minion Sep 30 '16 at 18:44