1

My ESP32 is connected to an NRF24L01 chip where by it receives a stream of audio data. I then relay this information via WiFi over to an MQTT broker.

Here's a few parts from my arduino IDE sketch code from my first attempt (I'll show the full code at end of question):

void onReceiveRadio(void * param) {

  esp_task_wdt_add(nullptr);
  while(1)
  {    
    bool n = !whichBuff;
    if(!buffFull[n] && radio.available() )
    {   
      radio.read(&buffer[n][loadBufferN],buffSize);         // Read the payload from the radio
      loadBufferN++;
    }

    if(loadBufferN >= bucketTotal)
    {
      loadBufferN = 0;
      buffFull[n] = true;
      whichBuff = n;
    }
  }
}

void setup() {      
  Serial.begin(115200);   // Enable Arduino serial library

  radio.begin();

  radio.setChannel(1);                 // Set RF channel to 1
  radio.setAutoAck(0);                 // Disable ACKnowledgement packets
  radio.setDataRate(RF_SPEED);         // Set data rate as specified in user options
  radio.setCRCLength(RF24_CRC_8);
  radio.openWritingPipe(pipes[0]);          // Set up reading and writing pipes
  radio.openReadingPipe(1,pipes[1]);
  radio.startListening();                   // Exit sending mode

  setup_wifi();
  client.setServer(mqtt_server, 1883);
  reconnect();

  esp_task_wdt_init(2,false);
 xTaskCreatePinnedToCore(
   onReceiveRadio,                  /* pvTaskCode */
   "Workload1",            /* pcName */
   1000,                   /* usStackDepth */
   NULL,                   /* pvParameters */
   1,                      /* uxPriority */
   &TaskA,                 /* pxCreatedTask */
   0);                     /* xCoreID */

}
void loop() {

  if (!client.connected()) {
    reconnect();
  }
  if(buffFull[whichBuff])
  {
    byte payload[bucketTotal*buffSize];
    int w=0;
    for(int x=0;x<bucketTotal;x++)
    {
      for(int y=0;y<buffSize;y++)
      {
        payload[w++] = buffer[whichBuff][x][y];
      }
    }
    client.publish("/esp32/output",payload,bucketTotal*buffSize); // mqtt transmission

    buffFull[whichBuff] = false;
  }
}

When I run the full code this, I get the following watchdog errors:

E (6608) task_wdt: Task watchdog got triggered. The following tasks did not reset the watchdog in time:
E (6608) task_wdt:  - IDLE0 (CPU 0)
E (6608) task_wdt: Tasks currently running:
E (6608) task_wdt: CPU 0: Workload1
E (6608) task_wdt: CPU 1: loopTask
E (6608) task_wdt: Aborting.
abort() was called at PC 0x400db86f on core 0

I was able to get rid of this error by putting in a else { vTaskDelay(1); } just after the first if statement in onReceiveRadio which I'll show below. And although the code ran without errors, the problem is that the delay is undesirable since I need to receive the payload at a higher frequency. So I then tried to get rid of my original problem by feeding the watchdog, but this gave me back the same errors I originally had. Below is my entire code with the watchdog set up.

#include <RF24.h>
#include <SPI.h>
#include <WiFi.h>
#include <PubSubClient.h>
#include <esp_task_wdt.h>

// Replace the next variables with your SSID/Password combination
const char* ssid = "mynetwork";
const char* password = "mypass";
const char* mqtt_server = "192.168.0.29";

/***********************************************
//******* MANDATORY User Variables **************/

 // Maximum Range: Sample rate 16000, RF_SPEED RF24_250KBPS
 // Maximum Quality: Sample rate 44000, RF_SPEED RF24_2MBPS

#define SAMPLE_RATE 24000                     // The sample rate to use for transferring audio samples  Note: 44khz+ sample rate requires 8-bits per sample
#define RF_SPEED RF24_1MBPS                   // RF24_250KBPS will do 13-20khz+ sample rate, RF24_1MBPS up to 24-44khz+, RF24_2MBPS for higher. These are not limits, just a guide.

/***************** Optional/Advanced User Variables ******/

#define bucketTotal 3
#define buffSize 32                           // The size of the memory buffer to use. Not really configurable.


/*********  Automated pin selections, override by defining above  ************/

//********Radio Defines ****************************
// Radio pipe addresses for the 2 nodes to communicate.
const uint64_t pipes[14] = { 0xABCDABCD71LL, 0x544d52687CLL, 0x544d526832LL, 0x544d52683CLL,0x544d526846LL, 0x544d526850LL,0x544d52685ALL, 0x544d526820LL, 0x544d52686ELL, 0x544d52684BLL, 0x544d526841LL, 0x544d526855LL,0x544d52685FLL,0x544d526869LL};



/* BEGIN PROJECT */

volatile boolean buffFull[2] = {false,false}, whichBuff = false;
int loadBufferN=0, intCount = 0;
byte buffer[2][bucketTotal][buffSize+1];
TaskHandle_t TaskA;

RF24 radio(12,14);    // Set radio up using pins 7 (CE) 8 (CS)
WiFiClient espClient;
PubSubClient client(espClient);

void onTimer(void * param) {

  esp_task_wdt_add(nullptr);
  while(1)
  {    
    bool n = !whichBuff;
    if(!buffFull[n] && radio.available() )
    {   
      radio.read(&buffer[n][loadBufferN],buffSize);         // Read the payload from the radio
      loadBufferN++;
    }
    else
    {
      //vTaskDelay(1);
      delayMicroseconds(50); // Maybe delaying things help a bit?
      esp_task_wdt_reset();
    }
    if(loadBufferN >= bucketTotal)
    {
      loadBufferN = 0;
      buffFull[n] = true;
      whichBuff = n;
    }
  }
}

void setup_wifi() {
  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect("ESP8266Client")) {
      Serial.println("connected");
      // Subscribe
      //client.subscribe("esp32/output");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void setup() {      
  Serial.begin(115200);   // Enable Arduino serial library

  radio.begin();

  radio.setChannel(1);                 // Set RF channel to 1
  radio.setAutoAck(0);                 // Disable ACKnowledgement packets
  radio.setDataRate(RF_SPEED);         // Set data rate as specified in user options
  radio.setCRCLength(RF24_CRC_8);
  radio.openWritingPipe(pipes[0]);          // Set up reading and writing pipes
  radio.openReadingPipe(1,pipes[1]);
  radio.startListening();                   // Exit sending mode

  setup_wifi();
  client.setServer(mqtt_server, 1883);
  reconnect();

  esp_task_wdt_init(2,false);
 xTaskCreatePinnedToCore(
   onTimer,                  /* pvTaskCode */
   "Workload1",            /* pcName */
   1000,                   /* usStackDepth */
   NULL,                   /* pvParameters */
   1,                      /* uxPriority */
   &TaskA,                 /* pxCreatedTask */
   0);                     /* xCoreID */

}

void loop() {

  if (!client.connected()) {
    reconnect();
  }
  if(buffFull[whichBuff])
  {
    byte payload[bucketTotal*buffSize];
    int w=0;
    for(int x=0;x<bucketTotal;x++)
    {
      for(int y=0;y<buffSize;y++)
      {
        payload[w++] = buffer[whichBuff][x][y];
      }
    }
    client.publish("/esp32/output",payload,bucketTotal*buffSize); // mqtt transmission

    buffFull[whichBuff] = false;
  }
}

So my question is how can I go about relaying the stream of information from NRF24L01 over to the MQTT broker without these watchdog issues?

UPDATE

I don't know if this is really a long term/durable solution since I haven't fully tested it, but I got the errors to subside by changing

esp_task_wdt_init(2,false);

to

esp_task_wdt_init(999,false);

My onReceiveRadio is like this now:

void onReceiveRadio(void * param) {

  esp_task_wdt_add(nullptr);
  while(1)
  {    
    bool n = !whichBuff;
    if(!buffFull[n] && radio.available() )
    {   
      radio.read(&buffer[n][loadBufferN],buffSize);         // Read the payload from the radio
      loadBufferN++;
    }

    esp_task_wdt_reset();

    if(loadBufferN >= bucketTotal)
    {
      loadBufferN = 0;
      buffFull[n] = true;
      whichBuff = n;
    }
  }
}

So essentially, I told the watch dog to wait as long as 999 seconds to be fed.

  • 1
    In light of the update, what exactly is your question? If the update is actually the answer to your question, please edit the question to remove the update and post it in an answer instead. Thanks! – anonymous2 Mar 30 '19 at 12:37
  • your watchdog is getting triggered ..... why do you call it watchdog error? .... it is doing what it is supposed to do – jsotola Mar 31 '19 at 5:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.