espressif / esp-aws-expresslink-eval

Espressif AWS IoT ExpressLink Evaluation and Firmware Repository
Apache License 2.0
17 stars 17 forks source link

Arduino R4 - sample sketch fails very weirdly #23

Open TAMHAN opened 3 months ago

TAMHAN commented 3 months ago

Hello Folks, sorry to report back a very weird problem - I spent quite a bit of time getting this to run on the Arduno Uno R4 WiFi. Now I have it working, but am a lot perplexed.

Particularly, the normal code failed after receiving the EVT Code 2. The exact error, as decoded by addr2line, was this:

tamhan@TAMHAN18:~$ addr2line -e "/tmp/arduino/sketches/1A128C413601ED586E7C4A67A698B41B/arduinoAWSTest1.ino".elf -a -f 0000d364 0000966c 000041f6 00009694 0000425e 0000a0e0 0000a11e 0000448e 0000774e 00007744
0x0000d364
_free_r
??:?
0x0000966c
_ZN7arduino6StringD2Ev
/home/tamhan/.arduino15/packages/arduino/hardware/renesas_uno/1.1.0/cores/arduino/api/String.cpp:146
0x000041f6
_Z13process_eventv
/home/tamhan/Arduino/arduinoAWSTest1/arduinoAWSTest1.ino:59 (discriminator 1)
0x00009694
_ZN7arduino6String12changeBufferEj
/home/tamhan/.arduino15/packages/arduino/hardware/renesas_uno/1.1.0/cores/arduino/api/String.cpp:179
0x0000425e
loop
/home/tamhan/Arduino/arduinoAWSTest1/arduinoAWSTest1.ino:111
0x0000a0e0
_Z12arduino_mainv
/home/tamhan/.arduino15/packages/arduino/hardware/renesas_uno/1.1.0/cores/arduino/main.cpp:117 (discriminator 1)
0x0000a11e
$t
/home/tamhan/.arduino15/packages/arduino/hardware/renesas_uno/1.1.0/cores/arduino/main.cpp:139
0x0000448e
main
/home/tamhan/.arduino15/packages/arduino/hardware/renesas_uno/1.1.0/variants/UNOWIFIR4/tmp_gen_c_files/main.c:7
0x0000774e
Reset_Handler
/ssd/products-Eslov/FW/MainPrograms/hardware/arduino-git/renesas/extras/e2studioProjects/Santiago/Debug/../ra/fsp/src/bsp/cmsis/Device/RENESAS/Source/startup.c:67
0x00007744
Reset_Handler
/ssd/products-Eslov/FW/MainPrograms/hardware/arduino-git/renesas/extras/e2studioProjects/Santiago/Debug/../ra/fsp/src/bsp/cmsis/Device/RENESAS/Source/startup.c:62

Apparently, the receiving of the code failed. I instrumented it like this:

[
String response; //TODO TAMHAN TOUCH
event_t process_event()
{
    int val = 0;
    event_t event_number = EVENT_NONE;
    val = digitalRead(EVENT_PIN);
    if(val)
    {
        response = execute_command("AT+EVENT?", 3000);
        if (response.equals("OK"))
        {
            return EVENT_NONE;
        }
        else {
            char ok_string[3];
            int topic_index;
            int total_read;
            total_read = sscanf(response.c_str(), "%s %d %d %*s" , ok_string, &event_number, &topic_index);

        Serial.print("OAIRON : ");
        Serial.println(event_number);

            return event_number;
        }
    }
}

The change I did to the location of the declaration of response "fixed" the problem, but of course caused a memory leak. My question, thus, now is what to do.

TAMHAN commented 3 months ago

And I think I fixed it:

event_t process_event()
{
  String response; //TODO TAMHAN TOUCH

    int val = 0;
    event_t event_number = EVENT_NONE;
    val = digitalRead(EVENT_PIN);
    if(val)
    {
        response = execute_command("AT+EVENT?", 3000);
        if (response.equals("OK"))
        {
            return EVENT_NONE;
        }
        else {
            char ok_string[3];
            int topic_index;
            int total_read;
            char someResponse[300];
            strcpy(someResponse, response.c_str());
            total_read = sscanf(someResponse, "%s %d %d %*s" , ok_string, &event_number, &topic_index);                       
        Serial.print("OAIRON : ");
        Serial.println(event_number);

            return event_number;
        }
    }
}

Please DO NOT ask me what is happening. However, this line clearly caused the problem - it seems to fuxate the inner state of the C++ string somehow: total_read = sscanf(response.c_str(), "%s %d %d %*s" , ok_string, &event_number, &topic_index);

And yes, avoid C++ if you can.