senseshift / arduino-ble-serial

Customizable Arduino and ESP32 BLE Serial library, compliant with Nordic UART Service and others

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Arduino Serial BLE

This library allows using Nordic UART Service (NUS) with ESP32 Arduino.

Get involved: πŸ’¬ Discord β€’ 🌐 Website β€’ πŸ› Issues β€’ πŸ“’ Twitter β€’ πŸ’Ž Patreon

Discord Widget

PlatformIO Registry Arduino Library GitHub release

PlatformIO CI PlatformIO CI

MIT GitHub contributors GitHub

Features

Installation

PlatformIO

lib_deps =
+  senseshift/Serial_BLE

Client-side usage

Usage

For all examples, take a look at the examples folder.

Basic Example

#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());
    }
}

Custom UART characteristics

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"
    );

    // ...
}

// ...

Alternative

// ...

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);

    // ...
}

// ...

Custom Read Buffer

ETL (Embedded Template Library)

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;

NimBLE

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)

Arduino IDE

Change the following line in BLESerial.h:

- #    define BLESERIAL_USE_NIMBLE false
+ #    define BLESERIAL_USE_NIMBLE true

PlatformIO

Change your platformio.ini file to the following settings:

lib_deps = 
+  h2zero/NimBLE-Arduino@^1.4.0

build_flags = 
+  -D BLESERIAL_USE_NIMBLE=true

About

Customizable Arduino and ESP32 BLE Serial library, compliant with Nordic UART Service and others

License:MIT License


Languages

Language:C++ 100.0%