ArduinoBLE: Bluetooth disconnects when serial monitor is turned on

I used the ArduinoBLE library to create a perepheral server

#include <ArduinoBLE.h>

BLEService purifierService("0000180F-0000-1000-8000-00805F9B34FB");
BLEIntCharacteristic allowanceCharacteristic("00002A6E-0000-1000-8000-00805F9B34FB", BLERead | BLEWrite);

void setup() {
  if (!BLE.begin()) {
    Serial.println("starting BLE failed!");
    while (true);
  }
  pinMode(LED_BUILTIN, OUTPUT);

  // Setup characteristics
  purifierService.addCharacteristic(allowanceCharacteristic);
  allowanceCharacteristic.setEventHandler(BLEWrite, updateAllowanceCallback);
  allowanceCharacteristic.writeValue(0);

  BLE.setLocalName("BLE_Purifier");

  BLE.setEventHandler(BLEConnected, connectHandler);
  BLE.setEventHandler(BLEDisconnected, disconnectHandler);

  BLE.setAdvertisedService(purifierService);
  BLE.addService(purifierService);
  BLE.advertise();
}

void loop() {
  BLE.poll();
}

void connectHandler(BLEDevice central) {
  Serial.print("Connected event, central: ");
  Serial.println(central.address());
  digitalWrite(LED_BUILTIN, HIGH);
}

void disconnectHandler(BLEDevice central) {
  Serial.print("Disconnected event, central: ");
  Serial.println(central.address());
  digitalWrite(LED_BUILTIN, LOW);
}

void updateAllowanceCallback(BLEDevice central, BLECharacteristic characteristic) {
  const uint8_t* allowanceBytes = characteristic.value();
    unsigned int allowance = 0;

  // Convert the received bytes to an unsigned int
  if (characteristic.valueLength() == sizeof(allowance)) {
      memcpy(&allowance, allowanceBytes, sizeof(allowance));
      Serial.print("Got allowance: ");
      Serial.println(allowance);
  } else {
      Serial.println("Invalid data length for allowance.");
  }
}

I'm able to use the BLE scanner mobile app to read from and write to the allowanceCharacteristic. However when I turn on the serial monitor from tools, the esp32 gets disconnected. It doesn't show up when I try to scan for bluetooth devices. My device is connected to the PC via a USB. How do I monitor the serial output while maintaining the connection?

Specs:

  • Esp-wroom-32 devkit v1
  • uPeasy ESP32 Wroom Devkit

In order to synchronize your Arduino code with the serial monitor, your Arduino is RESET by the monitor, so the Arduino code starts over from the beginning.

1 Like

https://forum.arduino.cc/t/prevent-serial-monitor-from-resetting-the-arduino/694390/8

According to this post, you can go into the boards.txt file in the esp32 package in the Arduino15 file modify the settings.

In the boards.txt file there are settings for many different esp32 boards, so you want to modify the file for the board you are using.
Look for something like this with your board and change false to true.

xxx.serial.disableDTR=false
xxx.serial.disableRTS=false

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.