NodeMCU reboots while parsing a string using Strtok()

0

I am trying to write a code which parses a string, received from an inverter, delimited by commas "," and i am using strtok(). whenever the controller reaches the end of parsing routine, it raises an exception and reboots the ESP8266.

This is what is get when i run the serial monitor:


    .........Connected
    IP address: 192.168.0.103

    ESP8266_2512899799

    Connecting to broker mqtt.thingspeak.com

    MQTT Broker Connected

    Sending Command: ^P005GSX


    ^D1062521,498,2521,498,0756,0667,015,483,000,000,000,000,061,036,000,000,0000,0000,0000,0000,1,0,0,1,0,0,1,0βΈ®

    I am in updateNewData

    2521,498,2521,498,0756,0667,015,483,000,000,000,000,061,036,000,000,0000,0000,0000,0000,1,0,0,1,0,0,1,0

    V_grid (V): 2521
    0F_grid (Hz): 498
    1V_out (V): 2521
    2F_out (Hz): 498
    3KVA_out: 756
    4KW_out: 667
    5load_percentage: 15
    6V_bat (V): 483
    7V_bat_SCC (V): 0
    8V_bat_SCC2 (V): 0
    9bat_dis_cur (A): 0
    10bat_cha_cur (A): 0
    11bat_cap: 61
    12HS_temp: 36
    13MPPT1_temp: 0
    14MPPT2_temp: 0
    15PV1_watts: 0
    16PV2_watts: 0
    17V_PV1: 0
    18V_PV2: 0
    19s_val_conf_state: 1
    20MPPT1_status: 0
    21MPPT2_status: 0
    22load_status: 1
    23bat_pow_dir: 0
    24DCAC_pow_dir: 0
    25line_pow_dir: 1
    26power_direction: 0
    27
    Exception (28):
    epc1=0x40209912 epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000000 depc=0x00000000

    >>>stack>>>

    ctx: cont
    sp: 3ffffcf0 end: 3fffffc0 offset: 01a0
    3ffffe90:  00000000 00000001 3ffe8304 40203788  
    3ffffea0:  3fffff56 3ffee60a 3ffee990 0000001b  
    3ffffeb0:  3fffff56 0000001b 3ffee990 0000001b  
    3ffffec0:  00000000 3ffee60a 3ffee990 40209a74  
    3ffffed0:  3fffff56 3ffee60a 3ffee990 4020745c  
    3ffffee0:  3fffff56 3ffee60a 3ffee990 40201103  
    3ffffef0:  31323532 38393400 32353200 39340031  
    3fffff00:  37300038 30003635 00373636 00353130  
    3fffff10:  00333834 00303030 00303030 00303030  
    3fffff20:  00303030 00313630 00363330 00303030  
    3fffff30:  00303030 30303030 30303000 30300030  
    3fffff40:  30003030 00303030 00300031 00310030  
    3fffff50:  00300030 00300031 4010054c 40203a25  
    3fffff60:  0000001c 00000036 3ffee788 40203ab0  
    3fffff70:  3fffdad0 00000000 3ffee990 3ffeeab0  
    3fffff80:  3fffdad0 00000000 3ffee7a0 4020115e  
    3fffff90:  3fffdad0 00000000 3ffee82c 402013fe  
    3fffffa0:  3fffdad0 00000000 3ffeea80 402043d0  
    3fffffb0:  feefeffe feefeffe 3ffe8544 40100bcd  
    <<<stack<<<

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

    load 0x4010f000, len 1384, room 16 
    tail 8
    chksum 0x2d
    csum 0x2d
    v8b899c12
    ~ld
    Connecting to Freedom
    .....Connected
    IP address: 192.168.0.103
    ESP8266_6698254931
    Connecting to broker mqtt.thingspeak.com
    MQTT Broker Connected

I have run the ESP exception decoder and it get the following stack trace:


    Exception 28: LoadProhibited: A load referenced a page mapped with an attribute that does not permit loads
    PC: 0x40209912: _strtol_r at /home/earle/src/esp-quick-toolchain/repo/newlib/newlib/libc/stdlib/strtol.c line 152
    EXCVADDR: 0x00000000

    Decoding stack results
    0x40203788: HardwareSerial::write(unsigned char const*, unsigned int) at C:\Users\usman hassan\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.2\cores\esp8266/HardwareSerial.h line 158
    0x40209a74: strtol at /home/earle/src/esp-quick-toolchain/repo/newlib/newlib/libc/stdlib/strtol.c line 224
    0x4020745c: atoi at /home/earle/src/esp-quick-toolchain/repo/newlib/newlib/libc/stdlib/atoi.c line 71
    0x40201103: parsedata() at C:\Users\usman hassan\Documents\Arduino\Infini Solar V  KW Monitor\Step_by_Step_connection/Step_by_Step_connection.ino line 137
    0x4010054c: millis() at C:\Users\usman hassan\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.2\cores\esp8266\core_esp8266_wiring.cpp line 186
    0x40203a25: Print::write(char const*) at C:\Users\usman hassan\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.2\cores\esp8266/Print.h line 60
    0x40203ab0: Print::println() at C:\Users\usman hassan\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.2\cores\esp8266\Print.cpp line 178
    0x4020115e: updateNewData() at C:\Users\usman hassan\Documents\Arduino\Infini Solar V  KW Monitor\Step_by_Step_connection/Step_by_Step_connection.ino line 149
    0x402013fe: loop() at C:\Users\usman hassan\Documents\Arduino\Infini Solar V  KW Monitor\Step_by_Step_connection/Step_by_Step_connection.ino line 254
    0x402043d0: loop_wrapper() at C:\Users\usman hassan\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.2\cores\esp8266\core_esp8266_main.cpp line 125

i know this much that its probably being caused by program trying to print the last NULL value, but i dont know how to fix this. I developed the code using an arduino Uno and the same code worked just fine. i have checked the program without this parsing function and the code just works fine on ESP8266 which tells me that the problem lies in this function.

This is my function that is causing the problem:


    char receivedChars[] = "^D1062399,502,2399,502,1703,1673,034,482,000,000,000,000,060,038,000,000,0000,0000,0000,0000,0,0,0,1,0,0,1,0je";

    unsigned int para_var[29];

    void parsedata()
    {
      char* valPosition; 
      char temp1[103];
      int i = 0; 

      strncpy(temp1, &receivedChars[5],103);
      Serial.println(temp1);
      valPosition = strtok(temp1,",");
      para_var[i] = atoi(valPosition); 

      while (valPosition != NULL)
       {
          Serial.print(parameters[i]); 
          Serial.print(": "); 
          Serial.println(para_var[i]);
          Serial.print(i++); 
          valPosition = strtok(NULL, ",");
          para_var[i] = atoi(valPosition); 
       }
       Serial.println("I am in parseData");
    }

I am not a programmer, i just learn from resources on the web and try and build things and i am out of my depth with this code. i would appreciate all the help i can get.

Thanks

PS: i am using the following libraries

ESP8266WiFi.h
PubSubClient.h
SoftwareSerial.h
exception
arduino
esp8266
strtok
nodemcu
asked on Stack Overflow Sep 23, 2019 by thehrao

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0