Skip to content

Commit 5ce80da

Browse files
committed
change NimBLE example
1 parent 2c337e4 commit 5ce80da

File tree

14 files changed

+210
-177
lines changed

14 files changed

+210
-177
lines changed

.github/workflows/examples.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ jobs:
2020
- "examples/arduino-wifiscan"
2121
- "examples/arduino-zigbee-light"
2222
- "examples/arduino-zigbee-switch"
23+
- "examples/arduino-NimBLE-ext_client"
24+
- "examples/arduino-matter-light"
2325
- "examples/tasmota"
24-
- "examples/espidf-arduino-h2zero-BLE_scan"
2526
- "examples/espidf-arduino-matter-light"
26-
- "examples/arduino-matter-light"
2727
- "examples/espidf-arduino-blink"
2828
- "examples/espidf-arduino-littlefs"
2929
- "examples/espidf-blink"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# NimBLE_extended_client example using h2zero Arduino NimBLE stack
2+
3+
BLE 5 client example, using the great [h2zero NimBLE](https://github.com/h2zero/NimBLE-Arduino) implementation.
4+
5+
Thx @h2zero for the great BLE library.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
; PlatformIO Project Configuration File
2+
;
3+
; Build options: build flags, source filter
4+
; Upload options: custom upload port, speed and extra flags
5+
; Library options: dependencies, extra library storages
6+
; Advanced options: extra scripting
7+
;
8+
; Please visit documentation for the other options and examples
9+
; https://docs.platformio.org/page/projectconf.html
10+
11+
[env]
12+
platform = espressif32
13+
framework = arduino
14+
monitor_speed = 115200
15+
build_flags =
16+
'-DCONFIG_BT_NIMBLE_EXT_ADV=1'
17+
lib_deps =
18+
https://github.com/h2zero/NimBLE-Arduino
19+
lib_ignore =
20+
BLE
21+
BluetoothSerial
22+
SimpleBLE
23+
WiFiProv
24+
custom_component_remove =
25+
espressif/esp_hosted
26+
espressif/esp_wifi_remote
27+
espressif/esp-dsp
28+
espressif/network_provisioning
29+
espressif/esp_rainmaker
30+
espressif/rmaker_common
31+
espressif/esp_insights
32+
espressif/esp_diag_data_store
33+
espressif/esp_diagnostics
34+
espressif/libsodium
35+
espressif/esp-modbus
36+
espressif/esp-cbor
37+
espressif/esp-sr
38+
espressif/esp32-camera
39+
40+
[env:esp32s3]
41+
board = esp32-s3-devkitc-1
42+
43+
[env:esp32c2]
44+
board = esp32-c2-devkitm-1
45+
46+
[env:esp32c3]
47+
board = esp32-c3-devkitm-1
48+
49+
[env:esp32c6]
50+
board = esp32-c6-devkitm-1
51+
52+
[env:esp32h2]
53+
board = esp32-h2-devkitm-1
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
2+
/** NimBLE Extended Client Demo:
3+
*
4+
* Demonstrates the Bluetooth 5.x client capabilities.
5+
*
6+
* Created: on April 2 2022
7+
* Author: H2zero
8+
*
9+
*/
10+
11+
#include <Arduino.h>
12+
#include <NimBLEDevice.h>
13+
#if !CONFIG_BT_NIMBLE_EXT_ADV
14+
# error Must enable extended advertising, see nimconfig.h file.
15+
#endif
16+
17+
#define SERVICE_UUID "ABCD"
18+
#define CHARACTERISTIC_UUID "1234"
19+
20+
static const NimBLEAdvertisedDevice* advDevice;
21+
static bool doConnect = false;
22+
static uint32_t scanTime = 10 * 1000; // In milliseconds, 0 = scan forever
23+
24+
/** Define the PHY's to use when connecting to peer devices, can be 1, 2, or all 3 (default).*/
25+
static uint8_t connectPhys = BLE_GAP_LE_PHY_CODED_MASK | BLE_GAP_LE_PHY_1M_MASK /*| BLE_GAP_LE_PHY_2M_MASK */;
26+
27+
/** Define a class to handle the callbacks for client connection events */
28+
class ClientCallbacks : public NimBLEClientCallbacks {
29+
void onConnect(NimBLEClient* pClient) override { Serial.printf("Connected\n"); };
30+
31+
void onDisconnect(NimBLEClient* pClient, int reason) override {
32+
Serial.printf("%s Disconnected, reason = %d - Starting scan\n", pClient->getPeerAddress().toString().c_str(), reason);
33+
NimBLEDevice::getScan()->start(scanTime);
34+
}
35+
} clientCallbacks;
36+
37+
/** Define a class to handle the callbacks when advertisements are received */
38+
class scanCallbacks : public NimBLEScanCallbacks {
39+
void onResult(const NimBLEAdvertisedDevice* advertisedDevice) override {
40+
Serial.printf("Advertised Device found: %s\n", advertisedDevice->toString().c_str());
41+
if (advertisedDevice->isAdvertisingService(NimBLEUUID("ABCD"))) {
42+
Serial.printf("Found Our Service\n");
43+
doConnect = true;
44+
/** Save the device reference in a global for the client to use*/
45+
advDevice = advertisedDevice;
46+
/** stop scan before connecting */
47+
NimBLEDevice::getScan()->stop();
48+
}
49+
}
50+
51+
/** Callback to process the results of the completed scan or restart it */
52+
void onScanEnd(const NimBLEScanResults& results, int rc) override { Serial.printf("Scan Ended\n"); }
53+
} scanCallbacks;
54+
55+
/** Handles the provisioning of clients and connects / interfaces with the server */
56+
bool connectToServer() {
57+
NimBLEClient* pClient = nullptr;
58+
59+
pClient = NimBLEDevice::createClient();
60+
pClient->setClientCallbacks(&clientCallbacks, false);
61+
62+
/**
63+
* Set the PHY's to use for this connection. This is a bitmask that represents the PHY's:
64+
* * 0x01 BLE_GAP_LE_PHY_1M_MASK
65+
* * 0x02 BLE_GAP_LE_PHY_2M_MASK
66+
* * 0x04 BLE_GAP_LE_PHY_CODED_MASK
67+
* Combine these with OR ("|"), eg BLE_GAP_LE_PHY_1M_MASK | BLE_GAP_LE_PHY_2M_MASK | BLE_GAP_LE_PHY_CODED_MASK;
68+
*/
69+
pClient->setConnectPhy(connectPhys);
70+
71+
/** Set how long we are willing to wait for the connection to complete (milliseconds), default is 30000. */
72+
pClient->setConnectTimeout(10 * 1000);
73+
74+
if (!pClient->connect(advDevice)) {
75+
/** Created a client but failed to connect, don't need to keep it as it has no data */
76+
NimBLEDevice::deleteClient(pClient);
77+
Serial.printf("Failed to connect, deleted client\n");
78+
return false;
79+
}
80+
81+
Serial.printf("Connected to: %s RSSI: %d\n", pClient->getPeerAddress().toString().c_str(), pClient->getRssi());
82+
83+
/** Now we can read/write/subscribe the characteristics of the services we are interested in */
84+
NimBLERemoteService* pSvc = nullptr;
85+
NimBLERemoteCharacteristic* pChr = nullptr;
86+
87+
pSvc = pClient->getService(SERVICE_UUID);
88+
if (pSvc) {
89+
pChr = pSvc->getCharacteristic(CHARACTERISTIC_UUID);
90+
if (pChr) {
91+
if (pChr->canRead()) {
92+
std::string value = pChr->readValue();
93+
Serial.printf("Characteristic value: %s\n", value.c_str());
94+
}
95+
}
96+
97+
} else {
98+
Serial.printf("ABCD service not found.\n");
99+
}
100+
101+
NimBLEDevice::deleteClient(pClient);
102+
Serial.printf("Done with this device!\n");
103+
return true;
104+
}
105+
106+
void setup() {
107+
Serial.begin(115200);
108+
Serial.printf("Starting NimBLE Client\n");
109+
110+
/** Initialize NimBLE and set the device name */
111+
NimBLEDevice::init("NimBLE Extended Client");
112+
113+
/** Create aNimBLE Scan instance and set the callbacks for scan events */
114+
NimBLEScan* pScan = NimBLEDevice::getScan();
115+
pScan->setScanCallbacks(&scanCallbacks);
116+
117+
/** Set scan interval (how often) and window (how long) in milliseconds */
118+
pScan->setInterval(97);
119+
pScan->setWindow(67);
120+
121+
/**
122+
* Active scan will gather scan response data from advertisers
123+
* but will use more energy from both devices
124+
*/
125+
pScan->setActiveScan(true);
126+
127+
/**
128+
* Start scanning for advertisers for the scan time specified (in milliseconds) 0 = forever
129+
* Optional callback for when scanning stops.
130+
*/
131+
pScan->start(scanTime);
132+
133+
Serial.printf("Scanning for peripherals\n");
134+
}
135+
136+
void loop() {
137+
/** Loop here until we find a device we want to connect to */
138+
if (doConnect) {
139+
if (connectToServer()) {
140+
Serial.printf("Success!, scanning for more!\n");
141+
} else {
142+
Serial.printf("Failed to connect, starting scan\n");
143+
}
144+
145+
doConnect = false;
146+
NimBLEDevice::getScan()->start(scanTime);
147+
}
148+
149+
delay(10);
150+
}

examples/espidf-arduino-h2zero-BLE_scan/.gitignore

Lines changed: 0 additions & 2 deletions
This file was deleted.

examples/espidf-arduino-h2zero-BLE_scan/CMakeLists.txt

Lines changed: 0 additions & 4 deletions
This file was deleted.

examples/espidf-arduino-h2zero-BLE_scan/README.md

Lines changed: 0 additions & 7 deletions
This file was deleted.

examples/espidf-arduino-h2zero-BLE_scan/platformio.ini

Lines changed: 0 additions & 27 deletions
This file was deleted.

examples/espidf-arduino-h2zero-BLE_scan/sdkconfig.defaults

Lines changed: 0 additions & 34 deletions
This file was deleted.

examples/espidf-arduino-h2zero-BLE_scan/src/CMakeLists.txt

Lines changed: 0 additions & 6 deletions
This file was deleted.

examples/espidf-arduino-h2zero-BLE_scan/src/idf_component.yml

Lines changed: 0 additions & 41 deletions
This file was deleted.

0 commit comments

Comments
 (0)