This library allows using Nordic UART Service (NUS) with ESP32 Arduino.
Get involved: π¬ Discord β’ π Website β’ π Issues β’ π’ Twitter β’ π Patreon
-
HardwareSerial
-compatible API - ETL (Embedded Template Library) support
- NimBLE support through NimBLE-Arduino library.
- Custom Server and Characteristics configuration
lib_deps =
+ senseshift/Serial_BLE
For all examples, take a look at the examples
folder.
#include <BLESerial.h>
BLESerial<> SerialBLE;
void setup() {
Serial.begin(9600);
SerialBLE.begin("ESP32-BLE-Slave");
}
void loop() {
if (Serial.available()) {
SerialBLE.write(Serial.read());
SerialBLE.flush();
}
if (SerialBLE.available()) {
Serial.write(SerialBLE.read());
}
}
Using custom UUIDs for Microchip BM70/RN4870 Transparent UART
// ...
void setup() {
// ...
SerialBLE.begin(
"ESP32-BLE-Slave",
"49535343-FE7D-4AE5-8FA9-9FAFD205E455",
"49535343-1E4D-4BD9-BA61-23C647249616",
"49535343-8841-43F4-A8D4-ECBE34729BB3"
);
// ...
}
// ...
// ...
void setup() {
// ...
BLEDevice::init("ESP32-BLE-Slave");
BLEServer* pServer = BLEDevice::createServer();
auto pService = pServer->createService("49535343-FE7D-4AE5-8FA9-9FAFD205E455");
auto pRxCharacteristic = pService->createCharacteristic("49535343-1E4D-4BD9-BA61-23C647249616", BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_WRITE_NR | BLECharacteristic::PROPERTY_NOTIFY);
auto pTxCharacteristic = pService->createCharacteristic("49535343-8841-43F4-A8D4-ECBE34729BB3", BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY);
SerialBLE.begin(pRxCharacteristic, pTxCharacteristic);
// ...
}
// ...
Using ETL provides a way to use this library without dynamic memory allocation.
#include <Embedded_Template_Library.h>
#include <etl/queue.h>
#include <etl/circular_buffer.h>
BLESerial<etl::queue<uint8_t, 255, etl::memory_model::MEMORY_MODEL_SMALL>> SerialBLE;
BLESerial<etl::circular_buffer<uint8_t, 255>> SerialBLE;
Using the NimBLE library saves a significant amount of RAM and Flash memory.
ESP32 BLE |
RAM: [= ] 11.9% (used 39124 bytes from 327680 bytes) Flash: [========= ] 85.9% (used 1125553 bytes from 1310720 bytes) |
NimBLE-Arduino |
RAM: [= ] 9.3% (used 30548 bytes from 327680 bytes) Flash: [==== ] 44.2% (used 579158 bytes from 1310720 bytes) |
Change the following line in BLESerial.h
:
- # define BLESERIAL_USE_NIMBLE false
+ # define BLESERIAL_USE_NIMBLE true
Change your platformio.ini
file to the following settings:
lib_deps =
+ h2zero/NimBLE-Arduino@^1.4.0
build_flags =
+ -D BLESERIAL_USE_NIMBLE=true