summaryrefslogtreecommitdiff
path: root/ESP32_BLE_Arduino/examples/Arduino
diff options
context:
space:
mode:
Diffstat (limited to 'ESP32_BLE_Arduino/examples/Arduino')
-rw-r--r--ESP32_BLE_Arduino/examples/Arduino/BLE_client/BLE_client.ino128
-rw-r--r--ESP32_BLE_Arduino/examples/Arduino/BLE_notify/BLE_notify.ino93
-rw-r--r--ESP32_BLE_Arduino/examples/Arduino/BLE_scan/BLE_scan.ino36
-rw-r--r--ESP32_BLE_Arduino/examples/Arduino/BLE_server/BLE_server.ino41
-rw-r--r--ESP32_BLE_Arduino/examples/Arduino/BLE_uart/BLE_uart.ino111
-rw-r--r--ESP32_BLE_Arduino/examples/Arduino/BLE_write/BLE_write.ino68
6 files changed, 477 insertions, 0 deletions
diff --git a/ESP32_BLE_Arduino/examples/Arduino/BLE_client/BLE_client.ino b/ESP32_BLE_Arduino/examples/Arduino/BLE_client/BLE_client.ino
new file mode 100644
index 0000000..3fc5efa
--- /dev/null
+++ b/ESP32_BLE_Arduino/examples/Arduino/BLE_client/BLE_client.ino
@@ -0,0 +1,128 @@
+/**
+ * A BLE client example that is rich in capabilities.
+ */
+
+#include "BLEDevice.h"
+//#include "BLEScan.h"
+
+// The remote service we wish to connect to.
+static BLEUUID serviceUUID("91bad492-b950-4226-aa2b-4ede9fa42f59");
+// The characteristic of the remote service we are interested in.
+static BLEUUID charUUID("0d563a58-196a-48ce-ace2-dfec78acc814");
+
+static BLEAddress *pServerAddress;
+static boolean doConnect = false;
+static boolean connected = false;
+static BLERemoteCharacteristic* pRemoteCharacteristic;
+
+static void notifyCallback(
+ BLERemoteCharacteristic* pBLERemoteCharacteristic,
+ uint8_t* pData,
+ size_t length,
+ bool isNotify) {
+ Serial.print("Notify callback for characteristic ");
+ Serial.print(pBLERemoteCharacteristic->getUUID().toString().c_str());
+ Serial.print(" of data length ");
+ Serial.println(length);
+}
+
+void connectToServer(BLEAddress pAddress) {
+ Serial.print("Forming a connection to ");
+ Serial.println(pAddress.toString().c_str());
+
+ BLEClient* pClient = BLEDevice::createClient();
+ Serial.println(" - Created client");
+
+ // Connect to the remove BLE Server.
+ pClient->connect(pAddress);
+ Serial.println(" - Connected to server");
+
+ // Obtain a reference to the service we are after in the remote BLE server.
+ BLERemoteService* pRemoteService = pClient->getService(serviceUUID);
+ if (pRemoteService == nullptr) {
+ Serial.print("Failed to find our service UUID: ");
+ Serial.println(serviceUUID.toString().c_str());
+ return;
+ }
+
+
+ // Obtain a reference to the characteristic in the service of the remote BLE server.
+ pRemoteCharacteristic = pRemoteService->getCharacteristic(charUUID);
+ if (pRemoteCharacteristic == nullptr) {
+ Serial.print("Failed to find our characteristic UUID: ");
+ Serial.println(charUUID.toString().c_str());
+ return;
+ }
+
+ // Read the value of the characteristic.
+ std::string value = pRemoteCharacteristic->readValue();
+ Serial.print("The characteristic value was: ");
+ Serial.println(value.c_str());
+
+ pRemoteCharacteristic->registerForNotify(notifyCallback);
+}
+/**
+ * Scan for BLE servers and find the first one that advertises the service we are looking for.
+ */
+class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
+ /**
+ * Called for each advertising BLE server.
+ */
+ void onResult(BLEAdvertisedDevice advertisedDevice) {
+ Serial.print("BLE Advertised Device found: ");
+ Serial.println(advertisedDevice.toString().c_str());
+
+ // We have found a device, let us now see if it contains the service we are looking for.
+ if (advertisedDevice.haveServiceUUID() && advertisedDevice.getServiceUUID().equals(serviceUUID)) {
+
+ //
+ Serial.print("Found our device! address: ");
+ advertisedDevice.getScan()->stop();
+
+ pServerAddress = new BLEAddress(advertisedDevice.getAddress());
+ doConnect = true;
+
+ } // Found our server
+ } // onResult
+}; // MyAdvertisedDeviceCallbacks
+
+
+void setup() {
+ Serial.begin(115200);
+ Serial.println("Starting Arduino BLE Client application...");
+ BLEDevice::init("");
+
+ // Retrieve a Scanner and set the callback we want to use to be informed when we
+ // have detected a new device. Specify that we want active scanning and start the
+ // scan to run for 30 seconds.
+ BLEScan* pBLEScan = BLEDevice::getScan();
+ pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
+ pBLEScan->setActiveScan(true);
+ pBLEScan->start(30);
+} // End of setup.
+
+
+// This is the Arduino main loop function.
+void loop() {
+
+ // If the flag "doConnect" is true then we have scanned for and found the desired
+ // BLE Server with which we wish to connect. Now we connect to it. Once we are
+ // connected we set the connected flag to be true.
+ if (doConnect == true) {
+ connectToServer(*pServerAddress);
+ doConnect = false;
+ connected = true;
+ }
+
+ // If we are connected to a peer BLE Server, update the characteristic each time we are reached
+ // with the current time since boot.
+ if (connected) {
+ String newValue = "Time since boot: " + String(millis()/1000);
+ Serial.println("Setting new characteristic value to \"" + newValue + "\"");
+
+ // Set the characteristic's value to be the array of bytes that is actually a string.
+ pRemoteCharacteristic->writeValue(newValue.c_str(), newValue.length());
+ }
+
+ delay(1000); // Delay a second between loops.
+} // End of loop
diff --git a/ESP32_BLE_Arduino/examples/Arduino/BLE_notify/BLE_notify.ino b/ESP32_BLE_Arduino/examples/Arduino/BLE_notify/BLE_notify.ino
new file mode 100644
index 0000000..44506c2
--- /dev/null
+++ b/ESP32_BLE_Arduino/examples/Arduino/BLE_notify/BLE_notify.ino
@@ -0,0 +1,93 @@
+/*
+ Video: https://www.youtube.com/watch?v=oCMOYS71NIU
+ Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleNotify.cpp
+ Ported to Arduino ESP32 by Evandro Copercini
+
+ Create a BLE server that, once we receive a connection, will send periodic notifications.
+ The service advertises itself as: 4fafc201-1fb5-459e-8fcc-c5c9c331914b
+ And has a characteristic of: beb5483e-36e1-4688-b7f5-ea07361b26a8
+
+ The design of creating the BLE server is:
+ 1. Create a BLE Server
+ 2. Create a BLE Service
+ 3. Create a BLE Characteristic on the Service
+ 4. Create a BLE Descriptor on the characteristic
+ 5. Start the service.
+ 6. Start advertising.
+
+ A connect hander associated with the server starts a background task that performs notification
+ every couple of seconds.
+*/
+#include <BLEDevice.h>
+#include <BLEServer.h>
+#include <BLEUtils.h>
+#include <BLE2902.h>
+
+BLECharacteristic *pCharacteristic;
+bool deviceConnected = false;
+uint8_t value = 0;
+
+// See the following for generating UUIDs:
+// https://www.uuidgenerator.net/
+
+#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
+#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
+
+
+class MyServerCallbacks: public BLEServerCallbacks {
+ void onConnect(BLEServer* pServer) {
+ deviceConnected = true;
+ };
+
+ void onDisconnect(BLEServer* pServer) {
+ deviceConnected = false;
+ }
+};
+
+
+
+void setup() {
+ Serial.begin(115200);
+
+ // Create the BLE Device
+ BLEDevice::init("MyESP32");
+
+ // Create the BLE Server
+ BLEServer *pServer = new BLEServer();
+ pServer->setCallbacks(new MyServerCallbacks());
+
+ // Create the BLE Service
+ BLEService *pService = pServer->createService(SERVICE_UUID);
+
+ // Create a BLE Characteristic
+ pCharacteristic = pService->createCharacteristic(
+ CHARACTERISTIC_UUID,
+ BLECharacteristic::PROPERTY_READ |
+ BLECharacteristic::PROPERTY_WRITE |
+ BLECharacteristic::PROPERTY_NOTIFY |
+ BLECharacteristic::PROPERTY_INDICATE
+ );
+
+ // https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.descriptor.gatt.client_characteristic_configuration.xml
+ // Create a BLE Descriptor
+ pCharacteristic->addDescriptor(new BLE2902());
+
+ // Start the service
+ pService->start();
+
+ // Start advertising
+ pServer->getAdvertising()->start();
+ Serial.println("Waiting a client connection to notify...");
+}
+
+void loop() {
+
+ if (deviceConnected) {
+ Serial.printf("*** NOTIFY: %d ***\n", value);
+ pCharacteristic->setValue(&value, 1);
+ pCharacteristic->notify();
+ //pCharacteristic->indicate();
+ value++;
+ }
+ delay(2000);
+} \ No newline at end of file
diff --git a/ESP32_BLE_Arduino/examples/Arduino/BLE_scan/BLE_scan.ino b/ESP32_BLE_Arduino/examples/Arduino/BLE_scan/BLE_scan.ino
new file mode 100644
index 0000000..ef7d892
--- /dev/null
+++ b/ESP32_BLE_Arduino/examples/Arduino/BLE_scan/BLE_scan.ino
@@ -0,0 +1,36 @@
+/*
+ Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleScan.cpp
+ Ported to Arduino ESP32 by Evandro Copercini
+*/
+
+#include <BLEDevice.h>
+#include <BLEUtils.h>
+#include <BLEScan.h>
+#include <BLEAdvertisedDevice.h>
+
+int scanTime = 30; //In seconds
+
+class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
+ void onResult(BLEAdvertisedDevice advertisedDevice) {
+ Serial.printf("Advertised Device: %s \n", advertisedDevice.toString().c_str());
+ }
+};
+
+void setup() {
+ Serial.begin(115200);
+ Serial.println("Scanning...");
+
+ BLEDevice::init("");
+ BLEScan* pBLEScan = BLEDevice::getScan(); //create new scan
+ pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
+ pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster
+ BLEScanResults foundDevices = pBLEScan->start(scanTime);
+ Serial.print("Devices found: ");
+ Serial.println(foundDevices.getCount());
+ Serial.println("Scan done!");
+}
+
+void loop() {
+ // put your main code here, to run repeatedly:
+ delay(2000);
+} \ No newline at end of file
diff --git a/ESP32_BLE_Arduino/examples/Arduino/BLE_server/BLE_server.ino b/ESP32_BLE_Arduino/examples/Arduino/BLE_server/BLE_server.ino
new file mode 100644
index 0000000..45ebf99
--- /dev/null
+++ b/ESP32_BLE_Arduino/examples/Arduino/BLE_server/BLE_server.ino
@@ -0,0 +1,41 @@
+/*
+ Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleServer.cpp
+ Ported to Arduino ESP32 by Evandro Copercini
+*/
+
+#include <BLEDevice.h>
+#include <BLEUtils.h>
+#include <BLEServer.h>
+
+BLEDevice ble;
+
+// See the following for generating UUIDs:
+// https://www.uuidgenerator.net/
+
+#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
+#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
+
+void setup() {
+ Serial.begin(115200);
+ Serial.println("Starting BLE work!");
+
+ ble.init("MyESP32");
+ BLEServer *pServer = new BLEServer();
+ BLEService *pService = pServer->createService(SERVICE_UUID);
+ BLECharacteristic *pCharacteristic = pService->createCharacteristic(
+ CHARACTERISTIC_UUID,
+ BLECharacteristic::PROPERTY_READ |
+ BLECharacteristic::PROPERTY_WRITE
+ );
+
+ pCharacteristic->setValue("Hello World says Neil");
+ pService->start();
+ BLEAdvertising *pAdvertising = pServer->getAdvertising();
+ pAdvertising->start();
+ Serial.println("Characteristic defined! Now you can read it in your phone!");
+}
+
+void loop() {
+ // put your main code here, to run repeatedly:
+ delay(2000);
+} \ No newline at end of file
diff --git a/ESP32_BLE_Arduino/examples/Arduino/BLE_uart/BLE_uart.ino b/ESP32_BLE_Arduino/examples/Arduino/BLE_uart/BLE_uart.ino
new file mode 100644
index 0000000..a8ab2d7
--- /dev/null
+++ b/ESP32_BLE_Arduino/examples/Arduino/BLE_uart/BLE_uart.ino
@@ -0,0 +1,111 @@
+/*
+ Video: https://www.youtube.com/watch?v=oCMOYS71NIU
+ Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleNotify.cpp
+ Ported to Arduino ESP32 by Evandro Copercini
+
+ Create a BLE server that, once we receive a connection, will send periodic notifications.
+ The service advertises itself as: 6E400001-B5A3-F393-E0A9-E50E24DCCA9E
+ Has a characteristic of: 6E400002-B5A3-F393-E0A9-E50E24DCCA9E - used for receiving data with "WRITE"
+ Has a characteristic of: 6E400003-B5A3-F393-E0A9-E50E24DCCA9E - used to send data with "NOTIFY"
+
+ The design of creating the BLE server is:
+ 1. Create a BLE Server
+ 2. Create a BLE Service
+ 3. Create a BLE Characteristic on the Service
+ 4. Create a BLE Descriptor on the characteristic
+ 5. Start the service.
+ 6. Start advertising.
+
+ In this example rxValue is the data received (only accessible inside that function).
+ And txValue is the data to be sent, in this example just a byte incremented every second.
+*/
+#include <BLEDevice.h>
+#include <BLEServer.h>
+#include <BLEUtils.h>
+#include <BLE2902.h>
+
+BLECharacteristic *pCharacteristic;
+bool deviceConnected = false;
+uint8_t txValue = 0;
+
+// See the following for generating UUIDs:
+// https://www.uuidgenerator.net/
+
+#define SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" // UART service UUID
+#define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
+#define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"
+
+
+class MyServerCallbacks: public BLEServerCallbacks {
+ void onConnect(BLEServer* pServer) {
+ deviceConnected = true;
+ };
+
+ void onDisconnect(BLEServer* pServer) {
+ deviceConnected = false;
+ }
+};
+
+class MyCallbacks: public BLECharacteristicCallbacks {
+ void onWrite(BLECharacteristic *pCharacteristic) {
+ std::string rxValue = pCharacteristic->getValue();
+
+ if (rxValue.length() > 0) {
+ Serial.println("*********");
+ Serial.print("Received Value: ");
+ for (int i = 0; i < rxValue.length(); i++)
+ Serial.print(rxValue[i]);
+
+ Serial.println();
+ Serial.println("*********");
+ }
+ }
+};
+
+
+void setup() {
+ Serial.begin(115200);
+
+ // Create the BLE Device
+ BLEDevice::init("UART Service");
+
+ // Create the BLE Server
+ BLEServer *pServer = new BLEServer();
+ pServer->setCallbacks(new MyServerCallbacks());
+
+ // Create the BLE Service
+ BLEService *pService = pServer->createService(SERVICE_UUID);
+
+ // Create a BLE Characteristic
+ pCharacteristic = pService->createCharacteristic(
+ CHARACTERISTIC_UUID_TX,
+ BLECharacteristic::PROPERTY_NOTIFY
+ );
+
+ pCharacteristic->addDescriptor(new BLE2902());
+
+ BLECharacteristic *pCharacteristic = pService->createCharacteristic(
+ CHARACTERISTIC_UUID_RX,
+ BLECharacteristic::PROPERTY_WRITE
+ );
+
+ pCharacteristic->setCallbacks(new MyCallbacks());
+
+ // Start the service
+ pService->start();
+
+ // Start advertising
+ pServer->getAdvertising()->start();
+ Serial.println("Waiting a client connection to notify...");
+}
+
+void loop() {
+
+ if (deviceConnected) {
+ Serial.printf("*** Sent Value: %d ***\n", txValue);
+ pCharacteristic->setValue(&txValue, 1);
+ pCharacteristic->notify();
+ txValue++;
+ }
+ delay(1000);
+}
diff --git a/ESP32_BLE_Arduino/examples/Arduino/BLE_write/BLE_write.ino b/ESP32_BLE_Arduino/examples/Arduino/BLE_write/BLE_write.ino
new file mode 100644
index 0000000..ed5ebc6
--- /dev/null
+++ b/ESP32_BLE_Arduino/examples/Arduino/BLE_write/BLE_write.ino
@@ -0,0 +1,68 @@
+/*
+ Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleWrite.cpp
+ Ported to Arduino ESP32 by Evandro Copercini
+*/
+
+#include <BLEDevice.h>
+#include <BLEUtils.h>
+#include <BLEServer.h>
+
+BLEDevice ble;
+
+// See the following for generating UUIDs:
+// https://www.uuidgenerator.net/
+
+#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
+#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
+
+
+class MyCallbacks: public BLECharacteristicCallbacks {
+ void onWrite(BLECharacteristic *pCharacteristic) {
+ std::string value = pCharacteristic->getValue();
+
+ if (value.length() > 0) {
+ Serial.println("*********");
+ Serial.print("New value: ");
+ for (int i = 0; i < value.length(); i++)
+ Serial.print(value[i]);
+
+ Serial.println();
+ Serial.println("*********");
+ }
+ }
+};
+
+void setup() {
+ Serial.begin(115200);
+
+ Serial.println("1- Download and install an BLE scanner app in your phone");
+ Serial.println("2- Scan for BLE devices in the app");
+ Serial.println("3- Connect to MyESP32");
+ Serial.println("4- Go to CUSTOM CHARACTERISTIC in CUSTOM SERVICE and write something");
+ Serial.println("5- See the magic =)");
+
+ //ble.begin("MyESP32");
+ ble.init("MyESP32");
+ BLEServer *pServer = new BLEServer();
+
+ BLEService *pService = pServer->createService(SERVICE_UUID);
+
+ BLECharacteristic *pCharacteristic = pService->createCharacteristic(
+ CHARACTERISTIC_UUID,
+ BLECharacteristic::PROPERTY_READ |
+ BLECharacteristic::PROPERTY_WRITE
+ );
+
+ pCharacteristic->setCallbacks(new MyCallbacks());
+
+ pCharacteristic->setValue("Hello World");
+ pService->start();
+
+ BLEAdvertising *pAdvertising = pServer->getAdvertising();
+ pAdvertising->start();
+}
+
+void loop() {
+ // put your main code here, to run repeatedly:
+ delay(2000);
+} \ No newline at end of file