Turning Off BLE Radio in ESP32?

aimthiazz
Posts: 1
Joined: Thu Feb 08, 2018 5:41 pm

Turning Off BLE Radio in ESP32?

Postby aimthiazz » Thu Feb 08, 2018 6:20 pm

I'm using Arduino IDE to program ESP32. I'm trying to implement BLE uart and found an example in ESP 32 BLE Library from nkolban. (https://github.com/nkolban/ESP32_BLE_Arduino). I am trying to modify the code in a way, when ESP32 receives data from an App (nRF Connect in my case) the ESP32 should turn-off/disable BLE radio to reduce current consumption. The Modified code can be found below (I have added btStop() when ESP32 receives data from nrF Connect)

Code: Select all

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

BLECharacteristic *pCharacteristic;
bool deviceConnected = false;
uint8_t txValue = 0;

// See the following for generating UUIDs:
// https://www.uuidgenerator.net/

#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"


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("*********");
      }
      btStop(); //I have added this line to the existing example.
      
    }
};


void setup() {
  Serial.begin(115200);

  // Create the BLE Device
  BLEDevice::init("UART Service");

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

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

  // Create a BLE Characteristic
  pCharacteristic = pService->createCharacteristic(
                      CHARACTERISTIC_UUID_TX,
                      BLECharacteristic::PROPERTY_NOTIFY
                    );
                      
  pCharacteristic->addDescriptor(new BLE2902());

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

  pCharacteristic->setCallbacks(new MyCallbacks());

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

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

void loop() {

  if (deviceConnected) {
    Serial.printf("*** Sent Value: %d ***\n", txValue);
    pCharacteristic->setValue(&txValue, 1);
    pCharacteristic->notify();
    txValue++;
  }
  delay(1000);
}
The Code is working fine when it is sending data to nRF Connect, but upon receiving data from nRF Connect the ESP throws following error

Code: Select all

Guru Meditation Error: Core  0 panic'ed (LoadProhibited)
. Exception was unhandled.
Register dump:
PC      : 0x4004baaa  PS      : 0x00060031  A0      : 0x80085a7d  A1      : 0x3ffc0550  
A2      : 0x00000000  A3      : 0x00000000  A4      : 0x3ffc41bc  A5      : 0x00000000  
A6      : 0x00000000  A7      : 0x3ffdc634  A8      : 0x00000000  A9      : 0x3ffb00d8  
A10     : 0x3ffdfff9  A11     : 0x00000000  A12     : 0x80088214  A13     : 0x3ffd5ae0  
A14     : 0x00000003  A15     : 0x00060423  SAR     : 0x0000001c  EXCCAUSE: 0x0000001c  
EXCVADDR: 0x00000048  LBEG    : 0x00000000  LEND    : 0x00000000  LCOUNT  : 0x00000000  

Backtrace: 0x4004baaa:0x3ffc0550 0x40085a7a:0x3ffc0570 0x400156a5:0x3ffc05a0 0x400552cd:0x3ffc05c0 0x40086abb:0x3ffc05e0 0x40081609:0x3ffc0600 0x4012110f:0x00000000
I would like to know is there any other way to turn-off BLE Radio in ESP32?

Thanks In Advance.

Who is online

Users browsing this forum: No registered users and 56 guests