Page 1 of 1

MQTT and Multi-core Problems

Posted: Fri Nov 09, 2018 9:30 am
by John__
So, I thought it would be fun to have on task handle MQTT messages from other tasks so they are non-blocking (in case of wifi problems etc). However, although the queue messages are passed correctly and print fine, the ESP core running the (MQTT_Handle task crashes immediately the client.publish () line is called.

(Its been a week now - on and off - so any hints would be gratefully received)

N.B. I can call client.publish() from another task (without using the queue) and all is well.

Code: Select all

Exit Alarmstate: Guru Meditation Error: Core  0 panic'ed (Unhandled debug exception)
Debug exception reason: Stack canary watchpoint triggered (MQTT_Task) 
The debug trace is:

Code: Select all

Decoding stack results
0x4008cb01: xQueueGenericSend at /Users/ficeto/Desktop/ESP32/ESP32/esp-idf-public/components/freertos/queue.c line 827
0x401145ea: sys_mbox_post at /Users/ficeto/Desktop/ESP32/ESP32/esp-idf-public/components/lwip/port/esp32/freertos/sys_arch.c line 243
0x401028dc: tcpip_send_msg_wait_sem at /Users/ficeto/Desktop/ESP32/ESP32/esp-idf-public/components/lwip/lwip/src/api/tcpip.c line 380
0x40114b35: netconn_apimsg at /Users/ficeto/Desktop/ESP32/ESP32/esp-idf-public/components/lwip/lwip/src/api/api_lib.c line 109
0x401150fa: netconn_write_partly at /Users/ficeto/Desktop/ESP32/ESP32/esp-idf-public/components/lwip/lwip/src/api/api_lib.c line 868
0x40101870: lwip_send at /Users/ficeto/Desktop/ESP32/ESP32/esp-idf-public/components/lwip/lwip/src/api/sockets.c line 1248
0x40102055: lwip_send_r at /Users/ficeto/Desktop/ESP32/ESP32/esp-idf-public/components/lwip/lwip/src/api/sockets.c line 3401
0x400d419d: WiFiClient::write(unsigned char const*, unsigned int) at /home/john/Arduino/hardware/espressif/esp32/tools/sdk/include/lwip/lwip/sockets.h line 587
0x400d6fb5: PubSubClient::write(unsigned char, unsigned char*, unsigned short) at /home/john/Arduino/libraries/PubSubClient/src/PubSubClient.cpp line 460
0x400d73a1: PubSubClient::publish(char const*, unsigned char const*, unsigned int, bool) at /home/john/Arduino/libraries/PubSubClient/src/PubSubClient.cpp line 374
0x400d73bd: PubSubClient::publish(char const*, char const*) at /home/john/Arduino/libraries/PubSubClient/src/PubSubClient.cpp line 346
0x400d2439: MQTT_Handle(void*) at /home/john/Arduino/_My_Projects/BPS/BPS_vMT32.17.ino/MT.ino line 89
0x4008c1b1: vPortTaskWrapper at /Users/ficeto/Desktop/ESP32/ESP32/esp-idf-public/components/freertos/port.c line 141

Code: Select all


//In Setup

struct MQMessage {
  char topic[32];
  char message[32];
};

//Called from any task (Currently called from task on Core 1
void MQ_Publish(char *mytopic, char *mymsg) {
  struct MQMessage mqMessage;
  strcpy(mqMessage.topic , mytopic);
  strcpy(mqMessage.message , mymsg);
  Serial.println(mymsg);
  Serial.println("-----");
  Serial.print(mqMessage.topic);
  Serial.print(" - ");
  Serial.println(mqMessage.message);
  Serial.println("-----");
  Serial.println();
  xQueueSend(MQ_Queue, &mqMessage, 0);
}

void MQTT_Handle(void * parameter) {
  //Runs as a task on Core zero ir Core 1 but crashes either way
  struct MQMessage myMessage;
  for (;;) {
    //Receive topic and msg from Queue
    xQueueReceive(MQ_Queue, &myMessage, portMAX_DELAY);
    //Check connections
    if (wifi) {
      int count = 0;
      while (WiFi.status() != WL_CONNECTED) {
        DEBUGPRINT3("+");
        WiFi.begin(ssid, password);
        vTaskDelay(500);
        count++;

        if (count > 20) {
          ESP.restart();
        }
      }

      if (!client.connected()) {
        mqttconnect();
      }
      //Check the Message content
      Serial.println("_____________");
      Serial.print(myMessage.topic);
      Serial.print(" - ");
      Serial.println(myMessage.message);
      Serial.println("_____________");
      Serial.println();

      //client.publish(myMessage.topic, myMessage.message);
    }
  }
}

Re: MQTT and Multi-core Problems

Posted: Sat Nov 10, 2018 7:41 pm
by John__
This matter was solved by allocating sufficient stack size when starting the task. It seems that the task was using more stack than I expected or initially measured!