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