I went through a lot of documentation and tutorials, but I could not find yet what is the correct way of shutting down BLE before a light sleep and how to restart if after wakeup. Could you guys give me any ideas of what is wrong with the following code?
Code: Select all
/*
Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleWrite.cpp
Ported to Arduino ESP32 by Evandro Copercini
*/
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
#include "esp_wifi.h"
#include "esp_bt_main.h"
#define uS_TO_S_FACTOR 1000000ULL /* Conversion factor for micro seconds to seconds */
#define TIME_TO_SLEEP 2
// See the following for generating UUIDs:
// https://www.uuidgenerator.net/
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
#define LED_PIN 2
#define BTN_PIN 12
int deviceConnected = false;
int counter = 0;
class MyCallbacks: public BLECharacteristicCallbacks {
void onConnect(BLEServer* pServer) {
deviceConnected = true;
Serial.println("device connected");
BLEDevice::startAdvertising();
};
void onDisconnect(BLEServer* pServer) {
deviceConnected = false;
Serial.println("device disconnected");
}
void onWrite(BLECharacteristic *pCharacteristic) {
std::string value = pCharacteristic->getValue();
if (value.length() > 0) {
Serial.print("*********");
Serial.print(value.c_str());
Serial.print("-");
Serial.print(atoi(value.c_str()));
Serial.print("-");
if(atoi(value.c_str())==1)
{
digitalWrite(LED_PIN,HIGH);
Serial.print("LEDON");
}
else
{
digitalWrite(LED_PIN,LOW);
Serial.print("LEDOFF");
}
Serial.println("*********");
}
}
};
BLECharacteristic *pCharacteristic;
void startBluetooth() {
BLEDevice::init("BLE_DEVICE");
BLEServer *pServer = BLEDevice::createServer();
BLEService *pService = pServer->createService(SERVICE_UUID);
pCharacteristic= pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE |
BLECharacteristic::PROPERTY_NOTIFY
);
pCharacteristic->setCallbacks(new MyCallbacks());
pCharacteristic->setValue("Hello World");
pService->start();
BLEAdvertising *pAdvertising = pServer->getAdvertising();
pAdvertising->start();
}
void setup() {
Serial.begin(115200);
counter = 0;
esp_wifi_set_mode(WIFI_MODE_NULL);
pinMode(LED_PIN,OUTPUT);
digitalWrite(LED_PIN,LOW);
startBluetooth();
}
int prevVal = LOW;
bool wasSleeping = false;
void loop() {
// put your main code here, to run repeatedly:
if(wasSleeping) {
wasSleeping = false;
startBluetooth();
}
int currentVal = digitalRead(BTN_PIN);
if(currentVal!=prevVal)
{
prevVal=currentVal;
if(currentVal==HIGH)
{
int value = 10;
pCharacteristic->setValue((uint8_t*)&value, 4);
pCharacteristic->notify();
}
else
{
int value = 0;
pCharacteristic->setValue((uint8_t*)&value, 4);
pCharacteristic->notify();
}
}
// delay(100);
// counter++;
if(counter > 160) {
counter = 0;
esp_bluedroid_disable();
esp_bluedroid_deinit();
btStop();
delay(1000);
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
Serial.println("Going to light sleep now");
esp_light_sleep_start();
esp_bluedroid_init();
esp_bluedroid_enable();
btStart();
}
}