Skip to content

ESP32 is crashing when WiFi is connecting / BLE is initializing #6541

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
1 task done
umerm64 opened this issue Apr 5, 2022 · 10 comments
Closed
1 task done

ESP32 is crashing when WiFi is connecting / BLE is initializing #6541

umerm64 opened this issue Apr 5, 2022 · 10 comments
Labels
Area: BT&Wifi BT & Wifi related issues Resolution: Expired More info wasn't provided

Comments

@umerm64
Copy link

umerm64 commented Apr 5, 2022

Board

ESP32 DEVKIT C

Device Description

I am using simple ESP32 module and there is no external device attached to it.

Hardware Configuration

I don't have any external device connected to it.
I am using the WLED project from here: https://github.com/Aircoookie/WLED
Only the GPIO pins used by this project are used.

Version

v2.0.2

IDE Name

Platform IO

Operating System

Windows 10

Flash frequency

40MHz

PSRAM enabled

no

Upload speed

115200

Description

I am using the WLED project here: https://github.com/Aircoookie/WLED
I am trying to add BLE support to this project.
For that, I am using partitions based on the huge_app.csv file
Currently, I am facing an exception at run time when using BLE as well as WiFi in this.
Basically when I am initializing BLE first then the exception occurs when connecting to the WiFi network.
Similarly when I am connecting to WiFi network then an exception occurs at the BLE initializing.
I am attaching the trace for both in this ticket.

I have also uploaded the code from the usermod.cpp file where I am initializing BLE server, its basically the code taken from BLE example given in the ESP32.

Sketch

#include "wled.h"
/*
 * This v1 usermod file allows you to add own functionality to WLED more easily
 * See: https://github.com/Aircoookie/WLED/wiki/Add-own-functionality
 * EEPROM bytes 2750+ are reserved for your custom use case. (if you extend #define EEPSIZE in const.h)
 * If you just need 8 bytes, use 2551-2559 (you do not need to increase EEPSIZE)
 *
 * Consider the v2 usermod API if you need a more advanced feature set!
 */

//Use userVar0 and userVar1 (API calls &U0=,&U1=, uint16_t)

#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>

#define SERVICE_UUID           "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" // UART service UUID
#define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
#define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"

static BLEServer *pServer = NULL;
static BLECharacteristic * pTxCharacteristic;
static bool deviceConnected = false;
static bool oldDeviceConnected = false;
static uint8_t txValue = 0;

class MyServerCallbacks: public BLEServerCallbacks {
    void onConnect(BLEServer* pServer) {
      deviceConnected = true;
    };

    void onDisconnect(BLEServer* pServer) {
      deviceConnected = false;
    }
};

class MyCallbacks: public BLECharacteristicCallbacks {
    void onWrite(BLECharacteristic *pCharacteristic) {
      std::string rxValue = pCharacteristic->getValue();

      if (rxValue.length() > 0) {
        Serial.println("*********");
        Serial.print("Received Value: ");
        for (int i = 0; i < rxValue.length(); i++)
          Serial.print(rxValue[i]);

        Serial.println();
        Serial.println("*********");
      }
    }
};

void ble_init()
{
  // Create the BLE Device
  BLEDevice::init("wled ble Service");
  Serial.println("BLE init called");
  // delay(2000);

  // Create the BLE Server
  pServer = BLEDevice::createServer();
  pServer->setCallbacks(new MyServerCallbacks());

  // Create the BLE Service
  BLEService *pService = pServer->createService(SERVICE_UUID);

  // Create a BLE Characteristic
  pTxCharacteristic = pService->createCharacteristic(
										CHARACTERISTIC_UUID_TX,
										BLECharacteristic::PROPERTY_NOTIFY
									);

  pTxCharacteristic->addDescriptor(new BLE2902());

  BLECharacteristic * pRxCharacteristic = pService->createCharacteristic(
											 CHARACTERISTIC_UUID_RX,
											BLECharacteristic::PROPERTY_WRITE
										);

  pRxCharacteristic->setCallbacks(new MyCallbacks());

  // Start the service
  pService->start();

  // Start advertising
  pServer->getAdvertising()->start();
  Serial.println("Waiting a client connection to notify...");
  // delay(2000);
}

void handle_ble_connection()
{
  if (deviceConnected) {
    pTxCharacteristic->setValue(&txValue, 1);
    pTxCharacteristic->notify();
    txValue++;
		delay(100); // bluetooth stack will go into congestion, if too many packets are sent
	}
  // disconnecting
  if (!deviceConnected && oldDeviceConnected) {
    delay(500); // give the bluetooth stack the chance to get things ready
    pServer->startAdvertising(); // restart advertising
    Serial.println("start advertising");
    oldDeviceConnected = deviceConnected;
  }
  // connecting
  if (deviceConnected && !oldDeviceConnected) {
    // do stuff here on connecting
    oldDeviceConnected = deviceConnected;
  }
}

//gets called once at boot. Do all initialization that doesn't depend on network here
void userSetup()
{
  Serial.println("going to init ble");
  ble_init();
}

//gets called every time WiFi is (re-)connected. Initialize own network interfaces here
void userConnected()
{

}

//loop. You can use "if (WLED_CONNECTED)" to check for successful connection
void userLoop()
{
  // handle_ble_connection();
}

Debug Message



####When BLE is initialized first:

Connecting to WFH...
abort() was called at PC 0x401f8477 on core 0

ELF file SHA256: 0000000000000000

Backtrace: 0x40090370:0x3fff0580 0x400906cd:0x3fff05a0 0x401f8477:0x3fff05c0 0x401d7ca0:0x3fff05e0 0x401d6fde:0x3fff0600 0x401fb012:0x3fff0620 0x4009217a:0x3fff0650
  #0  0x40090370:0x3fff0580 in invoke_abort at /home/cschwinne/esp32-arduino-lib-builder/esp-idf/components/esp32/panic.c:648
  #1  0x400906cd:0x3fff05a0 in abort at /home/cschwinne/esp32-arduino-lib-builder/esp-idf/components/esp32/panic.c:648
  #2  0x401f8477:0x3fff05c0 in pm_set_sleep_type at ??:?
  #3  0x401d7ca0:0x3fff05e0 in wifi_set_ps_process at ??:?
  #4  0x401d6fde:0x3fff0600 in ieee80211_ioctl_process at ??:?
  #5  0x401fb012:0x3fff0620 in ppTask at ??:?
  #6  0x4009217a:0x3fff0650 in vPortTaskWrapper at /home/cschwinne/esp32-arduino-lib-builder/esp-idf/components/freertos/port.c:355 (discriminator 1)

Rebooting...
ets Jun  8 2016 00:22:57

rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DOUT, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:1084
load:0x40078000,len:11220
load:0x40080400,len:5360
entry 0x4008067c










#### When WiFi is connected first and then BLE is connected:

Connecting to WFH...

Connected! IP address: 192.168.10.21
Init STA interfaces
mDNS started
going to init ble
abort() was called at PC 0x401a8332 on core 1

ELF file SHA256: 0000000000000000

Backtrace: 0x40090370:0x3ffd3170 0x400906cd:0x3ffd3190 0x401a8332:0x3ffd31b0 0x401aca70:0x3ffd31d0 0x401acc4a:0x3ffd31f0 0x4011905a:0x3ffd3220 0x40114ae2:0x3ffd3240 0x4010d711:0x3ffd3280 0x400f55a2:0x3ffd32b0 0x400f56a4:0x3ffd32f0 0x400f6b03:0x3ffd3310 0x400f6b4a:0x3ffd33d0 0x400f6f62:0x3ffd3410 0x40116e2c:0x3ffd3430 0x4009217a:0x3ffd3450
  #0  0x40090370:0x3ffd3170 in invoke_abort at /home/cschwinne/esp32-arduino-lib-builder/esp-idf/components/esp32/panic.c:648
  #1  0x400906cd:0x3ffd3190 in abort at /home/cschwinne/esp32-arduino-lib-builder/esp-idf/components/esp32/panic.c:648
  #2  0x401a8332:0x3ffd31b0 in coex_init at ??:?
  #3  0x401aca70:0x3ffd31d0 in esp_phy_rf_init at /home/cschwinne/esp32-arduino-lib-builder/esp-idf/components/esp32/phy_init.c:524
  #4  0x401acc4a:0x3ffd31f0 in esp_phy_load_cal_and_init at /home/cschwinne/esp32-arduino-lib-builder/esp-idf/components/esp32/phy_init.c:673
  #5  0x4011905a:0x3ffd3220 in esp_bt_controller_enable at /home/cschwinne/esp32-arduino-lib-builder/esp-idf/components/bt/bt.c:1704
  #6  0x40114ae2:0x3ffd3240 in btStart at C:\Users\umer.arshad\.platformio\packages\framework-arduinoespressif32\cores\esp32/esp32-hal-bt.c:45
  #7  0x4010d711:0x3ffd3280 in BLEDevice::init(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >) at C:/Users/umer.arshad/.platformio/packages/framework-arduinoespressif32@src-e9b1fbd6563a55e19ddae15e1fc09589/libraries/BLE/src/BLEDevice.cpp:591
  #8  0x400f55a2:0x3ffd32b0 in ble_init() at wled00/usermod.cpp:110
  #9  0x400f56a4:0x3ffd32f0 in userConnected() at wled00/usermod.cpp:123
  #10 0x400f6b03:0x3ffd3310 in WLED::handleConnection() at wled00/bus_manager.h:149
  #11 0x400f6b4a:0x3ffd33d0 in WLED::loop() at wled00/bus_manager.h:149
  #12 0x400f6f62:0x3ffd3410 in loop() at D:/MyStuff/WattsLights/WLED/wled00/wled00.ino:20
  #13 0x40116e2c:0x3ffd3430 in loopTask(void*) at C:\Users\umer.arshad\.platformio\packages\framework-arduinoespressif32\cores\esp32/main.cpp:23
  #14 0x4009217a:0x3ffd3450 in vPortTaskWrapper at /home/cschwinne/esp32-arduino-lib-builder/esp-idf/components/freertos/port.c:355 (discriminator 1)

Rebooting...
ets Jun  8 2016 00:22:57

rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DOUT, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:1084
load:0x40078000,len:11220
load:0x40080400,len:5360
entry 0x4008067c

Other Steps to Reproduce

No response

I have checked existing issues, online documentation and the Troubleshooting Guide

  • I confirm I have checked existing issues, online documentation and Troubleshooting guide.
@umerm64 umerm64 added the Status: Awaiting triage Issue is waiting for triage label Apr 5, 2022
@Jason2866
Copy link
Collaborator

Jason2866 commented Apr 6, 2022

Not directly solving your issue, for Tasmota we use h2zero/NimBLE since the beginning of using BLE because it has a lower footprint for RAM and flash usage. It does work fine with latest core.
Maybe you give it a try. Afaik WLED uses Tasmota Platformio Arduino Esp32 core build. You should use latest Tasmota version build 2.0.3-rc1 for your work.

@VojtechBartoska VojtechBartoska added the Area: BT&Wifi BT & Wifi related issues label Apr 7, 2022
@umerm64
Copy link
Author

umerm64 commented Apr 10, 2022

@Jason2866 I tried this library and the results are a bit better.
So it works correctly with the AccessPoint configuration of ESP32 but the issue occurs when the following configuration is used:
BLE init -> WiFi network connect -> AsyncWebServer creation.
Basically when WiFi is connected it remains ok but as soon as the webServer is initialized then the following exception occurs:

abort() was called at PC 0x40199fff on core 0


ELF file SHA256: 0000000000000000


Backtrace: 0x400902f8:0x3ffdba20 0x40090655:0x3ffdba40 0x40199fff:0x3ffdba60 0x40179764:0x3ffdba80 0x40178aa2:0x3ffdbaa0 0x4019cb8e:0x3ffdbac0 0x4009209e:0x3ffdbaf0
#0 0x400902f8:0x3ffdba20 in invoke_abort at /home/cschwinne/esp32-arduino-lib-builder/esp-idf/components/esp32/panic.c:648
#1 0x40090655:0x3ffdba40 in abort at /home/cschwinne/esp32-arduino-lib-builder/esp-idf/components/esp32/panic.c:648
#2 0x40199fff:0x3ffdba60 in pm_set_sleep_type at ??:?
#3 0x40179764:0x3ffdba80 in wifi_set_ps_process at ??:?
#4 0x40178aa2:0x3ffdbaa0 in ieee80211_ioctl_process at ??:?
#5 0x4019cb8e:0x3ffdbac0 in ppTask at ??:?
#6 0x4009209e:0x3ffdbaf0 in vPortTaskWrapper at /home/cschwinne/esp32-arduino-lib-builder/esp-idf/components/freertos/port.c:355 (discriminator 1)



Rebooting...
ets Jul 29 2019 12:21:46


rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DOUT, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:1084
load:0x40078000,len:11220
load:0x40080400,len:5360
entry 0x4008067c

@Jason2866
Copy link
Collaborator

Jason2866 commented Apr 10, 2022

You have to search the bug. This combination does work without issues for Tasmota.
If you are using AsyncWebserver or LittleFS you are hitted by this bug #6502 #6507
To solve apply not merge reverting PR #6516
You can use my fork without the bug with platformio

platform = https://github.com/tasmota/platform-espressif32/releases/download/v2.0.3rc1/platform-espressif32-2.0.3.zip

@VojtechBartoska
Copy link
Contributor

Can you please retest this with 2.0.3 Version? Thanks!

@VojtechBartoska VojtechBartoska added Resolution: Awaiting response Waiting for response of author and removed Status: Awaiting triage Issue is waiting for triage labels May 5, 2022
@VojtechBartoska
Copy link
Contributor

Closing as expired, if needed please reopen the issue.

@VojtechBartoska VojtechBartoska added Resolution: Expired More info wasn't provided and removed Resolution: Awaiting response Waiting for response of author labels May 26, 2022
@jacobk
Copy link

jacobk commented Feb 1, 2024

@umerm64 Did you ever resolve this issue? (running into the same problem)

@Jason2866
Copy link
Collaborator

AsyncWebserver is outdated and not maintained. When used a lot of weird issues can happen.

@jacobk
Copy link

jacobk commented Feb 2, 2024

AsyncWebserver is outdated and not maintained. When used a lot of weird issues can happen.

I see @Jason2866 . I was encountering this using the latest version of WLED. Are you saying that the error is caused by AsyncWebserver (I guess WLED is still using an old version https://github.com/Aircoookie/ESPAsyncWebServer)

@Jason2866
Copy link
Collaborator

Jason2866 commented Feb 2, 2024

Not saying definitely ESPAsyncWebserver, observed weird issues when used in complex programs. Adding BLE to an already complex piece of code makes the situation worse.
Race conditions, hidden bugs and what else...

What i am saying trying to add another function to an currently working piece of code does not mean the error is always to search in the new added code part.

To track down the issue it has to be analysed where the issues happens and what's causing it. Everything else than easy in a complex project. No easy and fast answers possible.

@umerm64
Copy link
Author

umerm64 commented Feb 2, 2024

@jacobk I used h2zero/NimBLE for adding support of BLE in my code since with BLE lib provided by sdk was crashing.
After that, the issue did not occur.
But I didn't exactly find the solution.
But I would advise you to not use BLE and WiFi simultaneously.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: BT&Wifi BT & Wifi related issues Resolution: Expired More info wasn't provided
Projects
None yet
Development

No branches or pull requests

4 participants