Issue with Disconnecting BLE from ESP32-S2 - AT Commands Not Working When BLE Is Connected and cannot reset
Hello,
I’m working on a project where I have an ESP32-S2 Mini and a BLE AT09 module (similar to the HM10) for Bluetooth communication. Here’s the setup:
-
BLE module is configured and set up correctly when the ESP32 starts, using a series of AT commands to make it ready for connections.
-
After a short delay (N seconds), the ESP32-S2 Mini goes into deep sleep mode, while the BLE module stays powered (from the battery) and waits for a device to connect to it.
-
When a device connects to the BLE module, an interrupt is triggered (via the STATE pin), waking the ESP32 to handle further actions.
This setup works as expected, but I’m running into an issue when trying to force a disconnect between the BLE module and the external device after the connection is established. AT commands are not accepted by the AT09 module once it’s connected to an external device (probably because it’s in data transfer mode).
I’ve read that the EN pin on the BLE module can be used to reset or restart the module. I connected Pin 5 (from ESP32) to the EN pin of the BLE module, toggling it LOW and then HIGH, but the BLE module doesn’t reset.
Here’s where the confusion lies: Some datasheets mention that the EN pin resets the module, while others state that it puts it to sleep, which has led to some conflicting information.
My Questions:
-
Has anyone had experience with the AT09 module or similar HM10 modules, specifically around forcing a disconnect or reset while the module is connected?
-
Is there any correct way to toggle the EN pin to reset the module, or is there another method to force a disconnect without the AT commands being accepted?
-
Does the EN pin really reset the module, or does it only put it to sleep, and is there a way to wake it back up properly?
I’d appreciate any insights or recommendations from anyone who has worked with similar BLE modules or encountered similar problems.
Thanks in advance for your help!
My code (removed the non relevant functions):
#include <HardwareSerial.h>
HardwareSerial btSerial(1); // Use UART1 for Bluetooth module
int baudRate = 9600;
// We chose pin 33 on the ESP32 as the receiving pin from the Bluetooth module. This wire is connected to the TDX pin on the Bluetooth. Pin 35 is the sending pin that sends bits to the Bluetooth module to its RDX pin
int rxPin = 35;
int txPin = 33;
int wakeESPPin = 3; //Pin that wakes the ESP up once a BLE connects to an external device
unsigned long wakeTime = 0; // Store wake-up timestamp
unsigned long wakeTimeMax = 200000; // 60000 == 6 sec
const int bleEnPin = 5; // GPIO connected to BLE's EN pin (should be able to control the BLE mode or reset it)
void setup() {
pinMode(bleEnPin, OUTPUT);
initializeSerialConnections();
handleESPWakeUpReason();
}
...
void loop() {
// Go to sleep after N seconds
if (millis() - wakeTime > wakeTimeMax) {
String seconds = String(wakeTimeMax / 1000);
Serial.println("⏳ "+seconds+ "s elapsed. Going to sleep...");
putESPToSleep();
}
// Check if data is available on the Serial Monitor and forward it to the BLE module through the btSerial connection
if (Serial.available()) {
String command = Serial.readStringUntil('\n'); // Read the command from Serial Monitor
btSerial.println(command); // Send the command to the Bluetooth module
}
// Read incoming data from BLE
if (!ATCommandInProgress && btSerial.available()) {
String response = btSerial.readStringUntil('\n');
response.trim(); // Remove any trailing newline characters
processIncomingCommand(response);
}
}
....
void putESPToSleep() {
Serial.println("💤 Preparing to enter Deep Sleep...");
// Check GPIO wakeESPPin state before sleep
Serial.print("GPIO wakeESPPin state before sleep: ");
Serial.println(digitalRead(wakeESPPin));
resetBLE();
pinMode(wakeESPPin, INPUT); // Ensure GPIO wakeESPPin is input and is driven LOW before sleep to force a reset:
esp_sleep_enable_ext0_wakeup(static_cast<gpio_num_t>(wakeESPPin), 1);
delay(300);
Serial.println("💤 Going to sleep NOW.");
esp_deep_sleep_start();
}
...
void resetBLE() {
Serial.println("💤 Resetting BLE...");
// Step 1: Pull EN LOW to disable BLE
digitalWrite(bleEnPin, LOW);
Serial.println("Pulling the bleEnPin LOW");
// pinMode(bleEnPin, OUTPUT); // Drive the pin actively
delay(3000); // Hold LOW for 150ms (adjust as needed)
digitalWrite(bleEnPin, HIGH);
Serial.println("Pulling the bleEnPin HIGH (turn off)");
delay(3000); // Hold LOW for 150ms (adjust as needed)
Serial.println("Pulling the bleEnPin LOW (turn the BLE on");
digitalWrite(bleEnPin, LOW);
// Step 2: Pull EN HIGH to re-enable BLE
// digitalWrite(bleEnPin, HIGH);
// delay(500); // Let BLE reboot
}
