Skip to content

Commit 209d8dd

Browse files
authored
Upload arduino examples
1 parent d6cd7dd commit 209d8dd

File tree

4 files changed

+238
-0
lines changed

4 files changed

+238
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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: 4fafc201-1fb5-459e-8fcc-c5c9c331914b
8+
And has a characteristic of: beb5483e-36e1-4688-b7f5-ea07361b26a8
9+
10+
The design of creating the BLE server is:
11+
1. Create a BLE Server
12+
2. Create a BLE Service
13+
3. Create a BLE Characteristic on the Service
14+
4. Create a BLE Descriptor on the characteristic
15+
5. Start the service.
16+
6. Start advertising.
17+
18+
A connect hander associated with the server starts a background task that performs notification
19+
every couple of seconds.
20+
*/
21+
#include <BLEDevice.h>
22+
#include <BLEServer.h>
23+
#include <BLEUtils.h>
24+
#include <BLE2902.h>
25+
26+
BLECharacteristic *pCharacteristic;
27+
bool deviceConnected = false;
28+
uint8_t value = 0;
29+
30+
// See the following for generating UUIDs:
31+
// https://www.uuidgenerator.net/
32+
33+
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
34+
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
35+
36+
37+
class MyServerCallbacks: public BLEServerCallbacks {
38+
void onConnect(BLEServer* pServer) {
39+
deviceConnected = true;
40+
};
41+
42+
void onDisconnect(BLEServer* pServer) {
43+
deviceConnected = false;
44+
}
45+
};
46+
47+
48+
49+
void setup() {
50+
Serial.begin(115200);
51+
52+
// Create the BLE Device
53+
BLEDevice::init("MyESP32");
54+
55+
// Create the BLE Server
56+
BLEServer *pServer = new BLEServer();
57+
pServer->setCallbacks(new MyServerCallbacks());
58+
59+
// Create the BLE Service
60+
BLEService *pService = pServer->createService(SERVICE_UUID);
61+
62+
// Create a BLE Characteristic
63+
pCharacteristic = pService->createCharacteristic(
64+
CHARACTERISTIC_UUID,
65+
BLECharacteristic::PROPERTY_READ |
66+
BLECharacteristic::PROPERTY_WRITE |
67+
BLECharacteristic::PROPERTY_NOTIFY |
68+
BLECharacteristic::PROPERTY_INDICATE
69+
);
70+
71+
// https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.descriptor.gatt.client_characteristic_configuration.xml
72+
// Create a BLE Descriptor
73+
pCharacteristic->addDescriptor(new BLE2902());
74+
75+
// Start the service
76+
pService->start();
77+
78+
// Start advertising
79+
pServer->getAdvertising()->start();
80+
Serial.println("Waiting a client connection to notify...");
81+
}
82+
83+
void loop() {
84+
85+
if (deviceConnected) {
86+
Serial.printf("*** NOTIFY: %d ***\n", value);
87+
pCharacteristic->setValue(&value, 1);
88+
pCharacteristic->notify();
89+
//pCharacteristic->indicate();
90+
value++;
91+
}
92+
delay(2000);
93+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleScan.cpp
3+
Ported to Arduino ESP32 by Evandro Copercini
4+
*/
5+
6+
#include <BLEDevice.h>
7+
#include <BLEUtils.h>
8+
#include <BLEScan.h>
9+
#include <BLEAdvertisedDevice.h>
10+
11+
int scanTime = 30; //In seconds
12+
13+
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
14+
void onResult(BLEAdvertisedDevice advertisedDevice) {
15+
Serial.printf("Advertised Device: %s \n", advertisedDevice.toString().c_str());
16+
}
17+
};
18+
19+
void setup() {
20+
Serial.begin(115200);
21+
Serial.println("Scanning...");
22+
23+
BLEDevice::init("");
24+
BLEScan* pBLEScan = BLEDevice::getScan(); //create new scan
25+
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
26+
pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster
27+
BLEScanResults foundDevices = pBLEScan->start(scanTime);
28+
Serial.print("Devices found: ");
29+
Serial.println(foundDevices.getCount());
30+
Serial.println("Scan done!");
31+
}
32+
33+
void loop() {
34+
// put your main code here, to run repeatedly:
35+
delay(2000);
36+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleServer.cpp
3+
Ported to Arduino ESP32 by Evandro Copercini
4+
*/
5+
6+
#include <BLEDevice.h>
7+
#include <BLEUtils.h>
8+
#include <BLEServer.h>
9+
10+
BLEDevice ble;
11+
12+
// See the following for generating UUIDs:
13+
// https://www.uuidgenerator.net/
14+
15+
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
16+
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
17+
18+
void setup() {
19+
Serial.begin(115200);
20+
Serial.println("Starting BLE work!");
21+
22+
ble.init("MyESP32");
23+
BLEServer *pServer = new BLEServer();
24+
BLEService *pService = pServer->createService(SERVICE_UUID);
25+
BLECharacteristic *pCharacteristic = pService->createCharacteristic(
26+
CHARACTERISTIC_UUID,
27+
BLECharacteristic::PROPERTY_READ |
28+
BLECharacteristic::PROPERTY_WRITE
29+
);
30+
31+
pCharacteristic->setValue("Hello World says Neil");
32+
pService->start();
33+
BLEAdvertising *pAdvertising = pServer->getAdvertising();
34+
pAdvertising->start();
35+
Serial.println("Characteristic defined! Now you can read it in your phone!");
36+
}
37+
38+
void loop() {
39+
// put your main code here, to run repeatedly:
40+
delay(2000);
41+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleWrite.cpp
3+
Ported to Arduino ESP32 by Evandro Copercini
4+
*/
5+
6+
#include <BLEDevice.h>
7+
#include <BLEUtils.h>
8+
#include <BLEServer.h>
9+
10+
BLEDevice ble;
11+
12+
// See the following for generating UUIDs:
13+
// https://www.uuidgenerator.net/
14+
15+
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
16+
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
17+
18+
19+
class MyCallbacks: public BLECharacteristicCallbacks {
20+
void onWrite(BLECharacteristic *pCharacteristic) {
21+
std::string value = pCharacteristic->getValue();
22+
23+
if (value.length() > 0) {
24+
Serial.println("*********");
25+
Serial.print("New value: ");
26+
for (int i = 0; i < value.length(); i++)
27+
Serial.print(value[i]);
28+
29+
Serial.println();
30+
Serial.println("*********");
31+
}
32+
}
33+
};
34+
35+
void setup() {
36+
Serial.begin(115200);
37+
38+
Serial.println("1- Download and install an BLE scanner app in your phone");
39+
Serial.println("2- Scan for BLE devices in the app");
40+
Serial.println("3- Connect to MyESP32");
41+
Serial.println("4- Go to CUSTOM CHARACTERISTIC in CUSTOM SERVICE and write something");
42+
Serial.println("5- See the magic =)");
43+
44+
//ble.begin("MyESP32");
45+
ble.init("MyESP32");
46+
BLEServer *pServer = new BLEServer();
47+
48+
BLEService *pService = pServer->createService(SERVICE_UUID);
49+
50+
BLECharacteristic *pCharacteristic = pService->createCharacteristic(
51+
CHARACTERISTIC_UUID,
52+
BLECharacteristic::PROPERTY_READ |
53+
BLECharacteristic::PROPERTY_WRITE
54+
);
55+
56+
pCharacteristic->setCallbacks(new MyCallbacks());
57+
58+
pCharacteristic->setValue("Hello World");
59+
pService->start();
60+
61+
BLEAdvertising *pAdvertising = pServer->getAdvertising();
62+
pAdvertising->start();
63+
}
64+
65+
void loop() {
66+
// put your main code here, to run repeatedly:
67+
delay(2000);
68+
}

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