TransWikia.com

ESP32 start BLE after deep sleep

Arduino Asked by hGen on January 12, 2021

I’m new to using the esp32 as a microcontroller. I’m currently working on a so-to-say remote control. Since I want to use the esp32 with an external battery I need to put it in deep-sleep after I pressed some buttons to preserve battery.

All that works fine I’ve connected a small touchpad to put the esp32 into deep sleep mode using the esp_deep_sleep_start() function . The controller itself goes to sleep and can be woken up using the following 2 lines:

  touchAttachInterrupt(T0, wakeCallback, TOUCH_THRESHOLD);
  esp_sleep_enable_touchpad_wakeup();

In order to communicate with my Raspberry Pi ‘Base Station’ I decided to use BLE.
My server is configured the following way (where bleServer, bleCharacteristic and bleService are global variables):

  BLEDevice::init("esp-32-remote");
  bleServer = BLEDevice::createServer();
  bleServer->setCallbacks(new ServerCallbacks());

  bleService = bleServer->createService(SERVICE_UUID);
  bleCharacteristic = bleService->createCharacteristic(
                        CHARACTERISTIC_UUID,
                        BLECharacteristic::PROPERTY_READ |
                        BLECharacteristic::PROPERTY_WRITE
                      );

  bleCharacteristic->addDescriptor(new BLE2902());
  bleService->start();

  BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
  pAdvertising->addServiceUUID(SERVICE_UUID);
  pAdvertising->setScanResponse(true);
  pAdvertising->setMinPreferred(0x06);
  pAdvertising->setMinPreferred(0x12);
  BLEDevice::startAdvertising();

Upon regular restart using the buttons everything works fine. I can connect to the device using python and the bluepy library.

If I now wake the controller I can’t do anything anymore. The setup() function gets called and there are no errors, nor does the controller crash due to any code errors (seemingly).

Then the loop function gets called once, and thats it. I can’t connect to the device anymore until I press the hardware reset button.

The questions that arise are:

  • might there be some error in the way I shutdown the bluetooth module?
  • Does the loop function work after being woken up from deep sleep?

Maybe one of you has an idea regarding my issue. If there are more information that I need to provide, don’t hesitate to tell me, I’m new to the whole mcu topic and I don’t know whats best to provide here.

One Answer

Does the loop function work after being woken up from deep sleep? YES

loop function isn't magical. its just a function that gets called over and over. In other words, if it gets called once, it can be called again except if

  • it did something to deactivate itself
  • never reached the end

during that first call. Check that you are not activating some sort of state inside the loop or going into a blocking call awaiting a signal that is never sent.

For good measure, make sure to reset all your variables to their expected states - expecting something to be 0 or set via the original definition may be insufficient. This is something that occurs on a power reset so if reset is making it work again, thats a likely culprit. Use a bare minimum of code in the loop for debug so you can figure it out.

Answered by Abel on January 12, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP