BLEServer - Esp32 in reboot loop after specific line of code

fearless_potato
Posts: 3
Joined: Mon Apr 13, 2020 5:46 pm

BLEServer - Esp32 in reboot loop after specific line of code

Postby fearless_potato » Mon Apr 13, 2020 5:56 pm

Hello,

I am trying to set up a BLEServer with my Esp32 module and whenever there is no more client connected to the server it should go back to sleep. I wanted to acheieve that with the function getConnectedCount() of the class BLEServer. However, whenever this specific line of code gets called, in my code the first time when it happens is in the setup() function, the esp32 gets into an endless reboot loop.
It happens just after this line in the following code block : connected pServer->getConnectedCount();

  1. #include <WiFi.h>
  2. #include <BLEDevice.h>
  3. #include <BLEUtils.h>
  4. #include <BLEServer.h>
  5. #include <stdint.h>
  6. #include "time.h"
  7.  
  8.  
  9. #define uS_TO_S_FACTOR 1000000ULL  // museconds to seconds
  10. #define BUZZER 4
  11. #define SERVICE_UUID        "144c4a01-27f0-4583-9e67-b18fd02575c8"
  12. #define CHARACTERISTIC_UUID "074db863-df24-4ffa-a05c-6ea474a88208"
  13. #define HOURS(X) (X)*60*60*uS_TO_S_FACTOR
  14. #define SECONDS(X) (X)*uS_TO_S_FACTOR
  15. #define TIMEOUT 10000
  16.  
  17.  
  18. volatile const int ping = 0xA1;
  19. volatile const int mute = 0xB1;
  20. uint32_t connected = 0;
  21.  
  22. BLEServer *pServer;
  23.  
  24. void powerSave(uint64_t sleepTime){
  25.   setCpuFrequencyMhz(80);
  26.   WiFi.disconnect(true);
  27.   WiFi.mode(WIFI_OFF);
  28.   btStop();
  29.   esp_sleep_enable_timer_wakeup(sleepTime);
  30.   esp_deep_sleep_start();
  31. }
  32.  
  33. void startBLE(){
  34.   esp_bt_controller_enable(ESP_BT_MODE_BLE);
  35.   BLEDevice::init("BluePing");
  36.   BLEServer *pServer = BLEDevice::createServer();
  37.   BLEService *pService = pServer->createService(SERVICE_UUID);
  38.   BLECharacteristic *pCharacteristic = pService->createCharacteristic(
  39.                                          CHARACTERISTIC_UUID,
  40.                                          BLECharacteristic::PROPERTY_READ |
  41.                                          BLECharacteristic::PROPERTY_WRITE
  42.                                        );
  43.  
  44.   pService->start();
  45.   BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
  46.   pAdvertising->addServiceUUID(SERVICE_UUID);
  47.   pAdvertising->setScanResponse(true);
  48.   pAdvertising->setMinPreferred(0x06);  
  49.   pAdvertising->setMinPreferred(0x12);
  50.   BLEDevice::startAdvertising();
  51. }
  52.  
  53. void checkBLE(){
  54.   connected = pServer->getConnectedCount();
  55.   while(connected != 0){
  56.     // execute commands if present
  57.     connected = pServer->getConnectedCount(); // Keine verbindung mehr -> raus
  58.   }
  59. }
  60.  
  61. void setup() {
  62.   // if(getsleepmodeform rtc memoy) powerSave(HOURS()); // sleepMode -> für jeweilige hours sleepen
  63.   Serial.begin(115200);
  64.   pinMode(BUZZER, OUTPUT);
  65.   startBLE();
  66.   connected = pServer->getConnectedCount();
  67.   unsigned long currentMil = millis();
  68.   while (connected == 0){ // ready to pair
  69.     Serial.println("looking to pair");
  70.   //connected = pServer->getConnectedCount(); // if client hat sich verbunden -> raus
  71.     if (millis() - currentMil >= (TIMEOUT * 1UL)){
  72.       Serial.println("timeout, no conn found");
  73.       break; // zeit überschritten -> raus
  74.     }
  75.   }
  76.   if (connected > 0) checkBLE();
  77.   powerSave(SECONDS(10)); //  muss nach erst ausgeführt werden wenn keine connection mehr besteht
  78. }
  79.  
  80. void loop() {}

I get this repeating reboot message:
  1.  
  2. Rebooting...
  3. ets Jun  8 2016 00:22:57
  4.  
  5. rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
  6. configsip: 0, SPIWP:0xee
  7. clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
  8. mode:DIO, clock div:1
  9. load:0x3fff0018,len:4
  10. load:0x3fff001c,len:1216
  11. ho 0 tail 12 room 4
  12. load:0x40078000,len:9720
  13. ho 0 tail 12 room 4
  14. load:0x40080400,len:6352
  15. entry 0x400806b8
  16. Guru Meditation Error: Core  1 panic'ed (LoadProhibited). Exception was unhandled.
  17. Core 1 register dump:
  18. PC      : 0x401c0f37  PS      : 0x00060f30  A0      : 0x800d1178  A1      : 0x3ffcdc50  
  19. A2      : 0x00000000  A3      : 0x3eaa9c81  A4      : 0x3f4015a5  A5      : 0x00000003  
  20. A6      : 0x00000001  A7      : 0x00000000  A8      : 0x800d10f6  A9      : 0x3ffcdc10  
  21. A10     : 0x3ffe2c68  A11     : 0x00000012  A12     : 0x3ffc5dc8  A13     : 0x3f40198c  
  22. A14     : 0xffffffff  A15     : 0x00000000  SAR     : 0x00000018  EXCCAUSE: 0x0000001c  
  23. EXCVADDR: 0x00000034  LBEG    : 0x4000c2e0  LEND    : 0x4000c2f6  LCOUNT  : 0xffffffff  
  24.  
  25. Backtrace: 0x401c0f37:0x3ffcdc50 0x400d1175:0x3ffcdc70 0x400d6d63:0x3ffcdca0 0x4008eb51:0x3ffcdcc0
Thanks in advance!

fearless_potato
Posts: 3
Joined: Mon Apr 13, 2020 5:46 pm

Re: BLEServer - Esp32 in reboot loop after specific line of code

Postby fearless_potato » Tue Apr 14, 2020 6:24 am

By the way, in numerous similar threads I have read that the Brownout protection might be triggered. However, this probably isn’t the case since it happens even with a power 5V 2A power supply.

lbernstone
Posts: 827
Joined: Mon Jul 22, 2019 3:20 pm

Re: BLEServer - Esp32 in reboot loop after specific line of code

Postby lbernstone » Tue Apr 14, 2020 3:10 pm

Decode your backtrace with https://github.com/me-no-dev/EspExceptionDecoder
The esp32 uses 3V3, so your 5V input is being reduced by a power supply which may not provide enough power. If the backtrace indicates an issue in wifi or phy init, that does indicate a power problem.

fearless_potato
Posts: 3
Joined: Mon Apr 13, 2020 5:46 pm

Re: BLEServer - Esp32 in reboot loop after specific line of code

Postby fearless_potato » Tue Apr 14, 2020 6:44 pm

Alright, did that, got the following result:
  1. PC: 0x401c0fdb: BLEServer::getConnectedCount() at C:\Users\leoni\Dokumente\ArduinoData\packages\esp32\hardware\esp32\1.0.4\libraries\BLE\src\BLEServer.cpp line 126
  2. EXCVADDR: 0x00000034
  3.  
  4. Decoding stack results
  5. 0x401c0fdb: BLEServer::getConnectedCount() at C:\Users\leoni\Dokumente\ArduinoData\packages\esp32\hardware\esp32\1.0.4\libraries\BLE\src\BLEServer.cpp line 126
  6. 0x400d1212: setup() at C:\Users\leoni\Dokumente\Arduino\esp32aktuell/esp32aktuell.ino line 98
  7. 0x400d6e07: loopTask(void*) at C:\Users\leoni\Dokumente\ArduinoData\packages\esp32\hardware\esp32\1.0.4\cores\esp32\main.cpp line 14
  8. 0x4008eb51: vPortTaskWrapper at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/freertos/port.c line 143
Currently trying to make sense of it. But it is, as I thought, caused by the getConnectedCount().

Thank you!!

Who is online

Users browsing this forum: No registered users and 57 guests