knolleary / pubsubclient

A client library for the Arduino Ethernet Shield that provides support for MQTT.
http://pubsubclient.knolleary.net/
MIT License
3.79k stars 1.46k forks source link

FreeRTOS task and PubSubClient crash #941

Closed maxdd closed 2 years ago

maxdd commented 2 years ago

Hello, i'm setting up a separate task for my MQTT setup

void Class::_run(void* pTaskInstance){
    Class* aMbr = (Class*)pTaskInstance;
    while(1){
        aMbr->run();
        vTaskDelay(500 / portTICK_PERIOD_MS);
    }
    vTaskDelete(NULL);
}

void Class::init(){
    xTaskCreate(
        this->_run,   // Function that should be called
        "aMbr",       // Name of the task (for debugging)
        4096,         // Stack size (bytes)
        NULL,         // Parameter to pass
        2,            // Task priority
        NULL          // Task handle
    );
}

void Class::run()
{ 
    while (!client.connected()){
        Serial.printf("[I][ESP32]: MQTT connecting to %s as %s\n", serverHostname, NAME);
        /* client ID */
        /* connect now */
        if (client.connect(NAME, TOKEN, TOKEN, LWT_TOPIC, 0, 1, LWT_MESSAGE)) {
            Serial.println("[I][ESP32]: MQTT connected");
            /* subscribe topic */
            client.subscribe(ACTIONS_TOPIC);
        } else {
            Serial.print("[I][ESP32]: failed, status code = ");
            Serial.println(client.state());
            Serial.println("[I][ESP32]: try again in 5 seconds");
            /* Wait 5 seconds before retrying */
            vTaskDelay(5000/ portTICK_PERIOD_MS);
        }
    }
    client.loop();
}

in WiFi.onEvent(..., WiFiEvent_t::SYSTEM_EVENT_STA_GOT_IP) callback i call the init() of the class The loop has ArduinoOTA only

The crash i'm receiving is the following

Guru Meditation Error: Core  1 panic'ed (LoadProhibited). Exception was unhandled.
Core 1 register dump:
PC      : 0x400d2b97  PS      : 0x00060c30  A0      : 0x800d110f  A1      : 0x3ffd32d0
A2      : 0x00000054  A3      : 0x3f402b4a  A4      : 0x3ffc22e4  A5      : 0x3ffbf970  
A6      : 0x00000003  A7      : 0x00060023  A8      : 0x800d2b97  A9      : 0x3ffd32b0
A10     : 0x00000003  A11     : 0x00000001  A12     : 0x00000001  A13     : 0x0000ff00
A14     : 0x00ff0000  A15     : 0xff000000  SAR     : 0x00000000  EXCCAUSE: 0x0000001c
EXCVADDR: 0x0000005c  LBEG    : 0x400014fd  LEND    : 0x4000150d  LCOUNT  : 0xffffffff

ELF file SHA256: 0000000000000000

Backtrace: 0x400d2b97:0x3ffd32d0 0x400d110c:0x3ffd32f0 0x400d11be:0x3ffd3320 0x40089a76:0x3ffd3340

xtensa-esp32-elf-addr2line -pfiaC -e \.pio\build\esp32dev\firmware.elf 0x400d2b97:0x3ffd32d0
0x400d2b97: PubSubClient::connected() at .pio\libdeps\esp32dev\PubSubClient\src/PubSubClient.cpp:741

it seem like i'm failing to understand why client.connected(); cannot stay as a member of the object/instance but only as a static Apparently if i change it to this

void Class::init(){
    Serial.println(client.connected());
    xTaskCreate(
        this->_run,   // Function that should be called
        "aMbr",       // Name of the task (for debugging)
        4096,         // Stack size (bytes)
        NULL,         // Parameter to pass
        2,            // Task priority
        NULL          // Task handle
    );
}

the println() is reporting 0 without crashing