Skip to content

Commit 9cb4b05

Browse files
committed
BLE_uart example
Combines the Notify and Write examples to create a basic UART example that works with the Nordic nRF Android app and many others. Could use a Print function and possibly read function, but basic RX/TX does work.
1 parent ed4628e commit 9cb4b05

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
Video: https://www.youtube.com/watch?v=oCMOYS71NIU
3+
Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleNotify.cpp
4+
Ported to Arduino ESP32 by Evandro Copercini
5+
6+
Create a BLE server that, once we receive a connection, will send periodic notifications.
7+
The service advertises itself as: 6E400001-B5A3-F393-E0A9-E50E24DCCA9E
8+
Has a characteristic of: 6E400002-B5A3-F393-E0A9-E50E24DCCA9E - used for receiving data with "WRITE"
9+
Has a characteristic of: 6E400003-B5A3-F393-E0A9-E50E24DCCA9E - used to send data with "NOTIFY"
10+
11+
The design of creating the BLE server is:
12+
1. Create a BLE Server
13+
2. Create a BLE Service
14+
3. Create a BLE Characteristic on the Service
15+
4. Create a BLE Descriptor on the characteristic
16+
5. Start the service.
17+
6. Start advertising.
18+
19+
In this example rxValue is the data received (only accessible inside that function).
20+
And txValue is the data to be sent, in this example just a byte incremented every second.
21+
*/
22+
#include <BLEDevice.h>
23+
#include <BLEServer.h>
24+
#include <BLEUtils.h>
25+
#include <BLE2902.h>
26+
27+
BLECharacteristic *pCharacteristic;
28+
bool deviceConnected = false;
29+
uint8_t txValue = 0;
30+
31+
// See the following for generating UUIDs:
32+
// https://www.uuidgenerator.net/
33+
34+
#define SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" // UART service UUID
35+
#define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
36+
#define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"
37+
38+
39+
class MyServerCallbacks: public BLEServerCallbacks {
40+
void onConnect(BLEServer* pServer) {
41+
deviceConnected = true;
42+
};
43+
44+
void onDisconnect(BLEServer* pServer) {
45+
deviceConnected = false;
46+
}
47+
};
48+
49+
class MyCallbacks: public BLECharacteristicCallbacks {
50+
void onWrite(BLECharacteristic *pCharacteristic) {
51+
std::string rxValue = pCharacteristic->getValue();
52+
53+
if (rxValue.length() > 0) {
54+
Serial.println("*********");
55+
Serial.print("Received Value: ");
56+
for (int i = 0; i < rxValue.length(); i++)
57+
Serial.print(rxValue[i]);
58+
59+
Serial.println();
60+
Serial.println("*********");
61+
}
62+
}
63+
};
64+
65+
66+
void setup() {
67+
Serial.begin(115200);
68+
69+
// Create the BLE Device
70+
BLEDevice::init("UART Service");
71+
72+
// Create the BLE Server
73+
BLEServer *pServer = new BLEServer();
74+
pServer->setCallbacks(new MyServerCallbacks());
75+
76+
// Create the BLE Service
77+
BLEService *pService = pServer->createService(SERVICE_UUID);
78+
79+
// Create a BLE Characteristic
80+
pCharacteristic = pService->createCharacteristic(
81+
CHARACTERISTIC_UUID_TX,
82+
BLECharacteristic::PROPERTY_NOTIFY
83+
);
84+
85+
pCharacteristic->addDescriptor(new BLE2902());
86+
87+
BLECharacteristic *pCharacteristic = pService->createCharacteristic(
88+
CHARACTERISTIC_UUID_RX,
89+
BLECharacteristic::PROPERTY_WRITE
90+
);
91+
92+
pCharacteristic->setCallbacks(new MyCallbacks());
93+
94+
// Start the service
95+
pService->start();
96+
97+
// Start advertising
98+
pServer->getAdvertising()->start();
99+
Serial.println("Waiting a client connection to notify...");
100+
}
101+
102+
void loop() {
103+
104+
if (deviceConnected) {
105+
Serial.printf("*** Sent Value: %d ***\n", txValue);
106+
pCharacteristic->setValue(&txValue, 1);
107+
pCharacteristic->notify();
108+
txValue++;
109+
}
110+
delay(1000);
111+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy