espressif / arduino-esp32

Arduino core for the ESP32
GNU Lesser General Public License v2.1
13.58k stars 7.4k forks source link

BLE + WiFi: Task watchdog got triggered. - IDLE (CPU 0) #2360

Closed LynxyssCZ closed 5 years ago

LynxyssCZ commented 5 years ago

Hardware:

Board: M5Stack Core Installation version: 1.5.0 IDE name: Arduino IDE, Platform.io Flash Frequency: 80Mhz PSRAM enabled: no Upload Speed: 115200 Computer OS: Windows 10, Arch Linux

Description:

I'm trying to build a BLE sensor reader that processes temperatures and provides HTTP API for reading data. If BLE and WiFi are running together, it operates properly for 10-20minutes and then both BLE scanning and WiFi stop working and following is repeatedly printed to output. loop method is still executing properly.

Removing either BLE or WiFi eliminates the problem.

Task watchdog got triggered. The following tasks did not reset the watchdog in time:
 - IDLE (CPU 0)
Tasks currently running:
CPU 0: wifi
CPU 1: IDLE
IP address:
192.168.0.150
UP 52.4m Free 31864

Sketch:

Minimal sketch that exhibits the same behavior

#include <Arduino.h>

#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>
#include <WiFi.h>
#include "WebServer.h"

int scanTime = 30; //In seconds
static WebServer webServer(80);

class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
    void onResult(BLEAdvertisedDevice advertisedDevice) {
      Serial.printf("Device Found\n");
    }
};

void vTaskBleReader(void *args) {
  BLEScan* pBLEScan = BLEDevice::getScan(); //create new scan
  pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks, true);

  while(true) {
    pBLEScan->start(2);
    vTaskDelay(3000 / portTICK_PERIOD_MS);
  }
}

void vTaskWebServer(void *args) {
    webServer.on("/", []() {
        String s = "<h1>It Works</h1>";
        webServer.send(200, "text/html", s);
  });
    webServer.begin();

    while (true) {
        if(WiFi.isConnected()) webServer.handleClient();
        vTaskDelay(150 / portTICK_PERIOD_MS);
    }
}

void setup() {
  Serial.begin(115200);
  BLEDevice::init("");
  WiFi.begin("ssid", "key");
  xTaskCreate(vTaskBleReader, "BLE_READER", 4096, NULL, 1, NULL);
  xTaskCreate(vTaskWebServer, "WEB_SERVER", 4096, NULL, 1, NULL);
}

void loop() {
  if (WiFi.isConnected()) {
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());
    } else {
    WiFi.reconnect();
  }

    Serial.printf("UP %.1fm ", esp_timer_get_time() / 60000000.0);
    Serial.printf("Free %d\n", xPortGetFreeHeapSize());
    Serial.print(uxTaskGetNumberOfTasks());
    Serial.println();

  delay(2000);
}

Debug Messages:

IP address:
192.168.0.150
UP 4.8m Free 34540
18
[D][BLEScan.cpp:196] start(): >> start(duration=2)
[D][BLEUtils.cpp:1091] dumpGapEvent(): Received a GAP event: ESP_GAP_BLE_SCAN_PARAM_SET_COMPLETE_EVT
[D][BLEUtils.cpp:1229] dumpGapEvent(): [st[D][BLEUtils.cpp:1229] dumpGapEvent(): [status: 5]
[D][BLEUtils.cpp:1091] dumpGapEvent(): Received a GAP event: ESP_GAP_BLE_SCAN_START_COMPLETE_EVT
[D][BLEUtils.cpp:1305] dumpGapEvent(): [status: 0]
[D][BLEUtils.cpp:1091] dumpGapEvent(): Received a GAP event: ESP_GAP_BLE_SCAN_RESULT_EVT
[D][BLEUtils.cpp:1265] dumpGapEvent(): search_evt: ESP_GAP_SEARCH_INQ_RES_EVT, bda: 60:c8:77:ad:3b:68, dev_type: ESP_BT_DEVICE_TYPE_BLE, ble_addr_type: BLE_ADDR_TYPE_RANDOM, ble_evt_type: ESP_BLE_EVT_CONN_ADV, rssi: -84, ble_adv: ??, flag: 26 ([LE General Discoverable Mode] [Simultaneous LE and BR/EDR to Same Device Capable (Controller)] [Simultaneous LE and BR/EDR to Same Device Capable (Host)] ), num_resps: 1, adv_data_len: 14, scan_rsp_len: 0
[D][BLEAdvertisedDevice.cpp:422] setRSSI(): - setRSSI(): rssi: -84
[D][BLEAdvertisedDevice.cpp:251] parseAdvertisement(): Type: 0x01 (ESP_BLE_AD_TYPE_FLAG), length: 1, data: 1a
[D][BLEAdvertisedDevice.cpp:251] parseAdvertisement(): Type: 0xff (ESP_BLE_AD_MANUFACTURER_SPECIFIC_TYPE), length: 9, data: 4c00100503189074b9
[D][BLEAdvertisedDevice.cpp:399] setManufacturerData(): - manufacturer data: 4c00100503189074b9
Device Found
[D][BLEUtils.cpp:1091] dumpGapEvent(): Received a GAP event: ESP_GAP_BLE_SCAN_RESULT_EVT
[D][BLEUtils.cpp:1265] dumpGapEvent(): search_evt: ESP_GAP_SEARCH_INQ_RES_EVT, bda: 60:c8:77:ad:3b:68, dev_type: ESP_BT_DEVICE_TYPE_BLE, ble_addr_type: BLE_ADDR_TYPE_RANDOM, ble_evt_type: ESP_BLE_EVT_CONN_ADV, rssi: -83, ble_adv: ??, flag: 26 ([LE General Discoverable Mode] [Simultaneous LE and BR/EDR to Same Device Capable (Controller)] [Simultaneous LE and BR/EDR to Same Device Capable (Host)] ), num_resps: 1, adv_data_len: 14, scan_rsp_len: 0
[D][BLEAdvertisedDevice.cpp:422] setRSSI(): - setRSSI(): rssi: -83
[D][BLEAdvertisedDevice.cpp:251] parseAdvertisement(): Type: 0x01 (ESP_BLE_AD_TYPE_FLAG), length: 1, data: 1a
[D][BLEAdvertisedDevice.cpp:251] parseAdvertisement(): Type: 0xff (ESP_BLE_AD_MANUFACTURER_SPECIFIC_TYPE), length: 9, data: 4c00100503189074b9
[D][BLEAdvertisedDevice.cpp:399] setManufacturerData(): - manufacturer data: 4c00100503189074b9
Device Found
IP address:
192.168.0.150
UP 4.8m Free 34604
18
IP address:
192.168.0.150
UP 4.9m Free 34604
18
Task watchdog got triggered. The following tasks did not reset the watchdog in time:
 - IDLE (CPU 0)
Tasks currently running:
CPU 0: wifi
CPU 1: IDLE
IP address:
192.168.0.150
UP 4.9m Free 34604
18
beats0126 commented 5 years ago

I saw some comments said BLE and WiFi work together will using high heap. Currently esp32-arduino API still no fully support on both work together. Maybe u can try with do BLE scan first, after done your scan then turn to WiFi mode. This one i tried few month ago, it is worked but during switch mode to WiFi will abit slow on SSID connection.

LynxyssCZ commented 5 years ago

It is not a heap issue as I still have over 30k left. Your workaround is not really realistically acceptable, as I have to perform ble scans multiple times in order to get all data off the sensors and would take hours if I wanted to maintain reasonable api uptime. I solved heap issues some time ago and they usually end up with the esp reseting, in this case it continues to run, but is completely useless.

chegewara commented 5 years ago

Hi, maybe try to add small delay between some of those lines:

 BLEDevice::init("");
  WiFi.begin("ssid", "key");
  xTaskCreate(vTaskBleReader, "BLE_READER", 4096, NULL, 1, NULL);
  xTaskCreate(vTaskWebServer, "WEB_SERVER", 4096, NULL, 1, NULL);

Im just guessing now, since i didnt have any issue using ble + wifi, but its possible that the way you are initializing ble and wifi is causing radio init stuck.

LynxyssCZ commented 5 years ago

@chegewara will try, although as I said in the issue both BLE and WiFi operate normally for 10-20 minutes before watchdog error appears.

chegewara commented 5 years ago

Sorry @LynxyssCZ , my bad. I thought its ms here UP 52.4m Free 31864, but its minutes, so just ignore my last post.

In such situation maybe try with different time interval and time window in BLE, or if its not a problem try latest commit from https://github.com/nkolban/esp32-snippets.

RottenVanSuti commented 5 years ago

I have been searching long time ago about using BLE and WiFi at the same time in arduino and it has some issues because of task priority for Wifi and ble. Try writing this after your while cycles in your two tasks: TIMERG1.wdt_wprotect = TIMG_WDT_WKEY_VALUE; TIMERG1.wdt_feed = 1; TIMERG1.wdt_wprotect = 0; It will feed the watchdog. If for some reason you run the tasks in core 0, just change TIMERG1 to TIMERG0. Hope it helps

LynxyssCZ commented 5 years ago

@RottenVanSuti Okay, that did change the behavior. Execution now stops completely without any further logs.

[D][BLEUtils.cpp:1091] dumpGapEvent(): Received a GAP event: ESP_GAP_BLE_SCAN_RESULT_EVT
[D][BLEUtils.cpp:1270] dumpGapEvent(): search_evt: ESP_GAP_SEARCH_INQ_CMPL_EVT
IP address:
192.168.0.150
UP 12.0m Free 33216
18
IP address:
192.168.0.150
UP 12.1m Free 33216
18
[D][BLEScan.cpp:196] start(): >> start(duration=2)
[D][BLEUtils.cpp:1091] dumpGapEvent(): Received a GAP event: ESP_GAP_BLE_SCAN_PARAM_SET_COMPLETE_EVT
[D][BLEUtils.cpp:1229] dumpGapEvent(): [st[D][BLEUtils.cpp:1229] dumpGapEvent(): [status: 5]
[D][BLEUtils.cpp:1091] dumpGapEvent(): Received a GAP event: ESP_GAP_BLE_SCAN_START_COMPLETE_EVT
[D][BLEUtils.cpp:1305] dumpGapEvent(): [status: 0]
[D][BLEUtils.cpp:1091] dumpGapEvent(): Received a GAP event: ESP_GAP_BLE_SCAN_RESULT_EVT
[D][BLEUtils.cpp:1265] dumpGapEvent(): search_evt: ESP_GAP_SEARCH_INQ_RES_EVT, bda: f8:77:b8:5f:49:95, dev_type: ESP_BT_DEVICE_TYPE_DUMO, ble_addr_type: BLE_ADDR_TYPE_PUBLIC, ble_evt_type: ESP_BLE_EVT_CONN_ADV, rssi: -85, ble_adv: ??, flag: 26 ([LE General Discoverable Mode] [Simultaneous LE and
BR/EDR to Same Device Capable (Controller)] [Simultaneous LE and BR/EDR to Same Device Capable (Host)] ), num_resps: 1, adv_data_len: 31, scan_rsp_len: 0
[D][BLEAdvertisedDevice.cpp:422] setRSSI(): - setRSSI(): rssi: -85
[D][BLEAdvertisedDevice.cpp:251] parseAdvertisement(): Type: 0x01 (ESP_BLE_AD_TYPE_FLAG), length: 1, data: 1a
[D][BLEAdvertisedDevice.cpp:251] parseAdvertisement(): Type: 0xff (ESP_BLE_AD_MANUFACTURER_SPECIFIC_TYPE), length: 26, data: 7500420401014ff877b85f4995fa77b8e5499407000000000000
[D][BLEAdvertisedDevice.cpp:399] setManufacturerData(): - manufacturer data: 7500420401014ff877b85f4995fa77b8e5499407000000000000
Device Found
[D][BLEUtils.cpp:1091] dumpGapEvent(): Received a GAP event: ESP_GAP_BLE_SCAN_RESULT_EVT
[D][BLEUtils.cpp:1265] dumpGapEvent(): search_evt: ESP_GAP_SEARCH_INQ_RES_EVT, bda: 7b:74:46:d0:17:ec, dev_type: ESP_BT_DEVICE_TYPE_BLE, ble_addr_type: BLE_ADDR_TYPE_RANDOM, ble_evt_type: ESP_BLE_EVT_CONN_ADV, rssi: -86, ble_adv: ??, flag: 26 ([LE General Discoverable Mode] [Simultaneous LE and BR/EDR to Same Device Capable (Controller)] [Simultaneous LE and BR/EDR to Same Device Capable (Host)] ), num_resps: 1, adv_data_len: 30, scan_rsp_len: 0
[D][BLEAdvertisedDevice.cpp:422] setRSSI(): - setRSSI(): rssi: -86
[D][BLEAdvertisedDevice.cpp:251] parseAdvertisement(): Type: 0x01 (ESP_BLE_AD_TYPE_FLAG), length: 1, data: 1a
[D][BLEAdvertisedDevice.cpp:251] parseAdvertisement(): Type: 0xff (ESP_BLE_AD_MANUFACTURER_SPECIFIC_TYPE), length: 25, data: 4c000c0e00700bf84e9dca8f3ee1116f50b210050b1c09b7c2
[D][BLEAdvertisedDevice.cpp:399] setManufacturerData(): - manufacturer data: 4c000c0e00700bf84e9dca8f3ee1116f50b210050b1c09b7c2
Device Found
[D][BLEUtils.cpp:1091] dumpGapEvent(): Received a GAP event: ESP_GAP_BLE_SCAN_RESULT_EVT
[D][BLEUtils.cpp:1265] dumpGapEvent(): search_evt: ESP_GAP_SEARCH_INQ_RES_EVT, bda: 62:28:34:52:b0:18, dev_type: ESP_BT_DEVICE_TYPE_BLE, ble_addr_type: BLE_ADDR_TYPE_RANDOM, ble_evt_type: ESP_BLE_EVT_CONN_ADV, rssi: -88, ble_adv: ??, flag: 26 ([LE General Discoverable Mode] [Simultaneous LE and BR/EDR to Same Device Capable (Controller)] [Simultaneous LE and BR/EDR to Same Device Capable (Host)] ), num_resps: 1, adv_data_len: 14, scan_rsp_len: 0
[D][BLEAdvertisedDevice.cpp:422] setRSSI(): - setRSSI(): rssi: -88
[D][BLEAdvertisedDevice.cpp:251] parseAdvertisement(): Type: 0x01 (ESP_BLE_AD_TYPE_FLAG), length: 1, data: 1a
[D][BLEAdvertisedDevice.cpp:251] parseAdvertisement(): Type: 0xff (ESP_BLE_AD_MANUFACTURER_SPECIFIC_TYPE), length: 9, data: 4c001005011cc54699
[D][BLEAdvertisedDevice.cpp:399] setManufacturerData(): - manufacturer data: 4c001005011cc54699
Device Found
[D][BLEUtils.cpp:1091] dumpGapEvent(): Received a GAP event: ESP_GAP_BLE_SCAN_RESULT_EVT
[D][BLEUtils.cpp:1265] dumpGapEvent(): search_evt: ESP_GAP_SEARCH_INQ_RES_EVT, bda: 7b:74:46:d0:17:ec, dev_type: ESP_BT_DEVICE_TYPE_BLE, ble_addr_type: BLE_ADDR_TYPE_RANDOM, ble_evt_type: ESP_BLE_EVT_CONN_ADV, rssi: -83, ble_adv: ??, flag: 26 ([LE General Discoverable Mode] [Simultaneous LE and BR/EDR to Same Device Capable (Controller)] [Simultaneous LE and BR/EDR to Same Device Capable (Host)] ), num_resps: 1, adv_data_len: 30, scan_rsp_len: 0
[D][BLEAdvertisedDevice.cpp:422] setRSSI(): - setRSSI(): rssi: -83
[D][BLEAdvertisedDevice.cpp:251] parseAdvertisement(): Type: 0x01 (ESP_BLE_AD_TYPE_FLAG), length: 1, data: 1a
[D][BLEAdvertisedDevice.cpp:251] parseAdvertisement(): Type: 0xff (ESP_BLE_AD_MANUFACTURER_SPECIFIC_TYPE), length: 25, data: 4c000c0e00700bf84e9dca8f3ee1116f50b210050b1c09b7c2
[D][BLEAdvertisedDevice.cpp:399] setManufacturerData(): - manufacturer data: 4c000c0e00700bf84e9dca8f3ee1116f50b210050b1c09b7c2
Device Found
[D][BLEUtils.cpp:1091] dumpGapEvent(): Received a GAP event: ESP_GAP_BLE_SCAN_RESULT_EVT
[D][BLEUtils.cpp:1265] dumpGapEvent(): search_evt: ESP_GAP_SEARCH_INQ_RES_EVT, bda: 7b:74:46:d0:17:ec, dev_type: ESP_BT_DEVICE_TYPE_BLE, ble_addr_type: BLE_ADDR_TYPE_RANDOM, ble_evt_type: ESP_BLE_EVT_CONN_ADV, rssi: -84, ble_adv: ??, flag: 26 ([LE General Discoverable Mode] [Simultaneous LE and BR/EDR to Same Device Capable (Controller)] [Simultaneous LE and BR/EDR to Same Device Capable (Host)] ), num_resps: 1, adv_data_len: 30, scan_rsp_len: 0
[D][BLEAdvertisedDevice.cpp:422] setRSSI(): - setRSSI(): rssi: -84
[D][BLEAdvertisedDevice.cpp:251]

This is the last line in the debug.

RottenVanSuti commented 5 years ago

ok, the task in cpu 0 is the one that did not reset the watchdog, try replacing TIMER1 with TIMER0. @LynxyssCZ

BaaridunNasr commented 5 years ago

Try writing this after your while cycles in your two tasks

Hi, do you mean, at the end of the while loop(inside the loop) or just after the loop?

BaaridunNasr commented 5 years ago

@RottenVanSuti Also, I'm not able to include driver/include/driver/timer.h to get those timers...

RottenVanSuti commented 5 years ago

after your "while(true)" loops of your tasks, example: "while(true){TIMERG1.wdt_wprotect = TIMG_WDT_WKEY_VALUE; TIMERG1.wdt_feed = 1; TIMERG1.wdt_wprotect = 0; ...rest of code} You need to include:

include "soc/timer_group_struct.h"

include "soc/timer_group_reg.h"

no timer.h @baaridunnasr

BaaridunNasr commented 5 years ago

@RottenVanSuti thank you for the clarification.

However I've resolved the issue on my end as a low memory situation. The error I received was exactly the same as described in this issue, so it wasn't apparent.

Although I'm not entirely sure why low memory caused cpu starvation...

stale[bot] commented 5 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] commented 5 years ago

This stale issue has been automatically closed. Thank you for your contributions.