diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml index 144526ae1..f244ae3ac 100644 --- a/.github/workflows/examples.yml +++ b/.github/workflows/examples.yml @@ -20,10 +20,10 @@ jobs: - "examples/arduino-wifiscan" - "examples/arduino-zigbee-light" - "examples/arduino-zigbee-switch" + - "examples/arduino-NimBLE-ext_client" + - "examples/arduino-matter-light" - "examples/tasmota" - - "examples/espidf-arduino-h2zero-BLE_scan" - "examples/espidf-arduino-matter-light" - - "examples/arduino-matter-light" - "examples/espidf-arduino-blink" - "examples/espidf-arduino-littlefs" - "examples/espidf-blink" diff --git a/examples/arduino-NimBLE-ext_client/README.md b/examples/arduino-NimBLE-ext_client/README.md new file mode 100644 index 000000000..be15da92d --- /dev/null +++ b/examples/arduino-NimBLE-ext_client/README.md @@ -0,0 +1,5 @@ +# NimBLE_extended_client example using h2zero Arduino NimBLE stack + +BLE 5 client example, using the great [h2zero NimBLE](https://github.com/h2zero/NimBLE-Arduino) implementation. + +Thx @h2zero for the great BLE library. diff --git a/examples/espidf-arduino-h2zero-BLE_scan/include/README b/examples/arduino-NimBLE-ext_client/include/README similarity index 100% rename from examples/espidf-arduino-h2zero-BLE_scan/include/README rename to examples/arduino-NimBLE-ext_client/include/README diff --git a/examples/espidf-arduino-h2zero-BLE_scan/lib/README b/examples/arduino-NimBLE-ext_client/lib/README similarity index 100% rename from examples/espidf-arduino-h2zero-BLE_scan/lib/README rename to examples/arduino-NimBLE-ext_client/lib/README diff --git a/examples/arduino-NimBLE-ext_client/platformio.ini b/examples/arduino-NimBLE-ext_client/platformio.ini new file mode 100644 index 000000000..0991425fe --- /dev/null +++ b/examples/arduino-NimBLE-ext_client/platformio.ini @@ -0,0 +1,53 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[env] +platform = espressif32 +framework = arduino +monitor_speed = 115200 +build_flags = + '-DCONFIG_BT_NIMBLE_EXT_ADV=1' +lib_deps = + https://github.com/h2zero/NimBLE-Arduino +lib_ignore = + BLE + BluetoothSerial + SimpleBLE + WiFiProv +custom_component_remove = + espressif/esp_hosted + espressif/esp_wifi_remote + espressif/esp-dsp + espressif/network_provisioning + espressif/esp_rainmaker + espressif/rmaker_common + espressif/esp_insights + espressif/esp_diag_data_store + espressif/esp_diagnostics + espressif/libsodium + espressif/esp-modbus + espressif/esp-cbor + espressif/esp-sr + espressif/esp32-camera + +[env:esp32s3] +board = esp32-s3-devkitc-1 + +[env:esp32c2] +board = esp32-c2-devkitm-1 + +[env:esp32c3] +board = esp32-c3-devkitm-1 + +[env:esp32c6] +board = esp32-c6-devkitm-1 + +[env:esp32h2] +board = esp32-h2-devkitm-1 diff --git a/examples/arduino-NimBLE-ext_client/src/NimBLE_extended_client.ino b/examples/arduino-NimBLE-ext_client/src/NimBLE_extended_client.ino new file mode 100644 index 000000000..5062d4039 --- /dev/null +++ b/examples/arduino-NimBLE-ext_client/src/NimBLE_extended_client.ino @@ -0,0 +1,150 @@ + +/** NimBLE Extended Client Demo: + * + * Demonstrates the Bluetooth 5.x client capabilities. + * + * Created: on April 2 2022 + * Author: H2zero + * + */ + +#include +#include +#if !CONFIG_BT_NIMBLE_EXT_ADV +# error Must enable extended advertising, see nimconfig.h file. +#endif + +#define SERVICE_UUID "ABCD" +#define CHARACTERISTIC_UUID "1234" + +static const NimBLEAdvertisedDevice* advDevice; +static bool doConnect = false; +static uint32_t scanTime = 10 * 1000; // In milliseconds, 0 = scan forever + +/** Define the PHY's to use when connecting to peer devices, can be 1, 2, or all 3 (default).*/ +static uint8_t connectPhys = BLE_GAP_LE_PHY_CODED_MASK | BLE_GAP_LE_PHY_1M_MASK /*| BLE_GAP_LE_PHY_2M_MASK */; + +/** Define a class to handle the callbacks for client connection events */ +class ClientCallbacks : public NimBLEClientCallbacks { + void onConnect(NimBLEClient* pClient) override { Serial.printf("Connected\n"); }; + + void onDisconnect(NimBLEClient* pClient, int reason) override { + Serial.printf("%s Disconnected, reason = %d - Starting scan\n", pClient->getPeerAddress().toString().c_str(), reason); + NimBLEDevice::getScan()->start(scanTime); + } +} clientCallbacks; + +/** Define a class to handle the callbacks when advertisements are received */ +class scanCallbacks : public NimBLEScanCallbacks { + void onResult(const NimBLEAdvertisedDevice* advertisedDevice) override { + Serial.printf("Advertised Device found: %s\n", advertisedDevice->toString().c_str()); + if (advertisedDevice->isAdvertisingService(NimBLEUUID("ABCD"))) { + Serial.printf("Found Our Service\n"); + doConnect = true; + /** Save the device reference in a global for the client to use*/ + advDevice = advertisedDevice; + /** stop scan before connecting */ + NimBLEDevice::getScan()->stop(); + } + } + + /** Callback to process the results of the completed scan or restart it */ + void onScanEnd(const NimBLEScanResults& results, int rc) override { Serial.printf("Scan Ended\n"); } +} scanCallbacks; + +/** Handles the provisioning of clients and connects / interfaces with the server */ +bool connectToServer() { + NimBLEClient* pClient = nullptr; + + pClient = NimBLEDevice::createClient(); + pClient->setClientCallbacks(&clientCallbacks, false); + + /** + * Set the PHY's to use for this connection. This is a bitmask that represents the PHY's: + * * 0x01 BLE_GAP_LE_PHY_1M_MASK + * * 0x02 BLE_GAP_LE_PHY_2M_MASK + * * 0x04 BLE_GAP_LE_PHY_CODED_MASK + * Combine these with OR ("|"), eg BLE_GAP_LE_PHY_1M_MASK | BLE_GAP_LE_PHY_2M_MASK | BLE_GAP_LE_PHY_CODED_MASK; + */ + pClient->setConnectPhy(connectPhys); + + /** Set how long we are willing to wait for the connection to complete (milliseconds), default is 30000. */ + pClient->setConnectTimeout(10 * 1000); + + if (!pClient->connect(advDevice)) { + /** Created a client but failed to connect, don't need to keep it as it has no data */ + NimBLEDevice::deleteClient(pClient); + Serial.printf("Failed to connect, deleted client\n"); + return false; + } + + Serial.printf("Connected to: %s RSSI: %d\n", pClient->getPeerAddress().toString().c_str(), pClient->getRssi()); + + /** Now we can read/write/subscribe the characteristics of the services we are interested in */ + NimBLERemoteService* pSvc = nullptr; + NimBLERemoteCharacteristic* pChr = nullptr; + + pSvc = pClient->getService(SERVICE_UUID); + if (pSvc) { + pChr = pSvc->getCharacteristic(CHARACTERISTIC_UUID); + if (pChr) { + if (pChr->canRead()) { + std::string value = pChr->readValue(); + Serial.printf("Characteristic value: %s\n", value.c_str()); + } + } + + } else { + Serial.printf("ABCD service not found.\n"); + } + + NimBLEDevice::deleteClient(pClient); + Serial.printf("Done with this device!\n"); + return true; +} + +void setup() { + Serial.begin(115200); + Serial.printf("Starting NimBLE Client\n"); + + /** Initialize NimBLE and set the device name */ + NimBLEDevice::init("NimBLE Extended Client"); + + /** Create aNimBLE Scan instance and set the callbacks for scan events */ + NimBLEScan* pScan = NimBLEDevice::getScan(); + pScan->setScanCallbacks(&scanCallbacks); + + /** Set scan interval (how often) and window (how long) in milliseconds */ + pScan->setInterval(97); + pScan->setWindow(67); + + /** + * Active scan will gather scan response data from advertisers + * but will use more energy from both devices + */ + pScan->setActiveScan(true); + + /** + * Start scanning for advertisers for the scan time specified (in milliseconds) 0 = forever + * Optional callback for when scanning stops. + */ + pScan->start(scanTime); + + Serial.printf("Scanning for peripherals\n"); +} + +void loop() { + /** Loop here until we find a device we want to connect to */ + if (doConnect) { + if (connectToServer()) { + Serial.printf("Success!, scanning for more!\n"); + } else { + Serial.printf("Failed to connect, starting scan\n"); + } + + doConnect = false; + NimBLEDevice::getScan()->start(scanTime); + } + + delay(10); +} diff --git a/examples/arduino-blink/platformio.ini b/examples/arduino-blink/platformio.ini index 7573b349f..ddec96cec 100644 --- a/examples/arduino-blink/platformio.ini +++ b/examples/arduino-blink/platformio.ini @@ -43,7 +43,8 @@ custom_component_remove = espressif/esp_hosted espressif/esp_diagnostics espressif/esp_rainmaker espressif/rmaker_common -custom_component_add = espressif/cmake_utilities @ 0.* +custom_component_add = + espressif/cmake_utilities @ 0.* [env:esp32-s3-arduino_nano_esp32] platform = espressif32 diff --git a/examples/espidf-arduino-h2zero-BLE_scan/.gitignore b/examples/espidf-arduino-h2zero-BLE_scan/.gitignore deleted file mode 100644 index b9f3806a2..000000000 --- a/examples/espidf-arduino-h2zero-BLE_scan/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -.pio -.vscode diff --git a/examples/espidf-arduino-h2zero-BLE_scan/CMakeLists.txt b/examples/espidf-arduino-h2zero-BLE_scan/CMakeLists.txt deleted file mode 100644 index b6275f681..000000000 --- a/examples/espidf-arduino-h2zero-BLE_scan/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ -cmake_minimum_required(VERSION 3.16.0) -include($ENV{IDF_PATH}/tools/cmake/project.cmake) -add_compile_definitions(ARDUINO_ARCH_ESP32=1) -project(Arduino_IDF_BLE_scan) diff --git a/examples/espidf-arduino-h2zero-BLE_scan/README.md b/examples/espidf-arduino-h2zero-BLE_scan/README.md deleted file mode 100644 index 5601e540c..000000000 --- a/examples/espidf-arduino-h2zero-BLE_scan/README.md +++ /dev/null @@ -1,7 +0,0 @@ -# Arduino_IDF_BLE_scan example using 3rd party NimBLE stack - -BLE scan example, using the great h2zero NimBLE implementation. The needed NimBLE lib is loaded via the IDF component manager -> `idf_component.yml`. -Mandantory not to forget to switch Arduino included BLE libs. -Done in `sdkconfig.defaults` - -Thx @h2zero for the great BLE library. diff --git a/examples/espidf-arduino-h2zero-BLE_scan/platformio.ini b/examples/espidf-arduino-h2zero-BLE_scan/platformio.ini deleted file mode 100644 index 7b9a51745..000000000 --- a/examples/espidf-arduino-h2zero-BLE_scan/platformio.ini +++ /dev/null @@ -1,27 +0,0 @@ -; PlatformIO Project Configuration File -; -; Build options: build flags, source filter -; Upload options: custom upload port, speed and extra flags -; Library options: dependencies, extra library storages -; Advanced options: extra scripting -; -; Please visit documentation for the other options and examples -; https://docs.platformio.org/page/projectconf.html - -[env] -platform = espressif32 -framework = arduino, espidf -monitor_speed = 115200 -board_build.embed_txtfiles = - managed_components/espressif__esp_insights/server_certs/https_server.crt - managed_components/espressif__esp_rainmaker/server_certs/rmaker_mqtt_server.crt - managed_components/espressif__esp_rainmaker/server_certs/rmaker_claim_service_server.crt - managed_components/espressif__esp_rainmaker/server_certs/rmaker_ota_server.crt -lib_ignore = - BLE - BluetoothSerial - SimpleBLE - WiFiProv - -[env:esp32] -board = esp32dev diff --git a/examples/espidf-arduino-h2zero-BLE_scan/sdkconfig.defaults b/examples/espidf-arduino-h2zero-BLE_scan/sdkconfig.defaults deleted file mode 100644 index 267ca7f35..000000000 --- a/examples/espidf-arduino-h2zero-BLE_scan/sdkconfig.defaults +++ /dev/null @@ -1,34 +0,0 @@ -# CONFIG_AUTOSTART_ARDUINO is not set -# CONFIG_WS2812_LED_ENABLE is not set -CONFIG_FREERTOS_HZ=1000 -CONFIG_MBEDTLS_PSK_MODES=y -CONFIG_MBEDTLS_KEY_EXCHANGE_PSK=y -CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_SIZE=y -CONFIG_COMPILER_OPTIMIZATION_SIZE=y -CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y -CONFIG_ESPTOOLPY_HEADER_FLASHSIZE_UPDATE=y - -# Override some defaults so BT stack is enabled -# in this example -# -# BT config -# -CONFIG_BT_ENABLED=y -CONFIG_BTDM_CTRL_MODE_BLE_ONLY=y -CONFIG_BTDM_CTRL_MODE_BR_EDR_ONLY=n -CONFIG_BTDM_CTRL_MODE_BTDM=n -CONFIG_BT_BLUEDROID_ENABLED=n -CONFIG_BT_NIMBLE_ENABLED=y - -# -# Arduino Configuration -# -# -# Disable all Arduino included BLE libraries -# -CONFIG_ARDUINO_SELECTIVE_COMPILATION=y -# CONFIG_ARDUINO_SELECTIVE_WiFiProv is not set -# CONFIG_ARDUINO_SELECTIVE_BLE is not set -# CONFIG_ARDUINO_SELECTIVE_BluetoothSerial is not set -# CONFIG_ARDUINO_SELECTIVE_SimpleBLE is not set -# end of Arduino Configuration \ No newline at end of file diff --git a/examples/espidf-arduino-h2zero-BLE_scan/src/CMakeLists.txt b/examples/espidf-arduino-h2zero-BLE_scan/src/CMakeLists.txt deleted file mode 100644 index 483bc0cfc..000000000 --- a/examples/espidf-arduino-h2zero-BLE_scan/src/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ -# This file was automatically generated for projects -# without default 'CMakeLists.txt' file. - -FILE(GLOB_RECURSE app_sources ${CMAKE_SOURCE_DIR}/src/*.*) - -idf_component_register(SRCS ${app_sources}) diff --git a/examples/espidf-arduino-h2zero-BLE_scan/src/idf_component.yml b/examples/espidf-arduino-h2zero-BLE_scan/src/idf_component.yml deleted file mode 100644 index 0d925b9bd..000000000 --- a/examples/espidf-arduino-h2zero-BLE_scan/src/idf_component.yml +++ /dev/null @@ -1,41 +0,0 @@ -dependencies: - # Required IDF version - idf: ">=4.4" - - esp-nimble-cpp: - git: https://github.com/h2zero/esp-nimble-cpp.git - version: 877a29a8b1d0022c5e8f67ba8b879316e67b6c3d - - - # # Defining a dependency from the registry: - # # https://components.espressif.com/component/example/cmp - # example/cmp: "^3.3.3" # Automatically update minor releases - # - # # Other ways to define dependencies - # - # # For components maintained by Espressif only name can be used. - # # Same as `espressif/cmp` - # component: "~1.0.0" # Automatically update bugfix releases - # - # # Or in a longer form with extra parameters - # component2: - # version: ">=2.0.0" - # - # # For transient dependencies `public` flag can be set. - # # `public` flag doesn't have an effect for the `main` component. - # # All dependencies of `main` are public by default. - # public: true - # - # # For components hosted on non-default registry: - # service_url: "https://componentregistry.company.com" - # - # # For components in git repository: - # test_component: - # path: test_component - # git: ssh://git@gitlab.com/user/components.git - # - # # For test projects during component development - # # components can be used from a local directory - # # with relative or absolute path - # some_local_component: - # path: ../../projects/component diff --git a/examples/espidf-arduino-h2zero-BLE_scan/src/main.cpp b/examples/espidf-arduino-h2zero-BLE_scan/src/main.cpp deleted file mode 100644 index 04b6e5f0d..000000000 --- a/examples/espidf-arduino-h2zero-BLE_scan/src/main.cpp +++ /dev/null @@ -1,54 +0,0 @@ -/* - 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 - Refactored back to IDF by H2zero -*/ - -/** NimBLE differences highlighted in comment blocks **/ - -/*******original******** -#include -#include -#include -#include -***********************/ - -#include - -#include - -extern "C"{void app_main(void);} - -int scanTime = 5 * 1000; // In milliseconds, 0 = scan forever -BLEScan* pBLEScan; - -class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks { - void onResult(BLEAdvertisedDevice* advertisedDevice) { - printf("Advertised Device: %s \n", advertisedDevice->toString().c_str()); - } -}; - -void scanTask (void * parameter){ - for(;;) { - // put your main code here, to run repeatedly: - BLEScanResults foundDevices = pBLEScan->getResults(scanTime, false); - printf("Devices found: %d\n", foundDevices.getCount()); - printf("Scan done!\n"); - pBLEScan->clearResults(); // delete results fromBLEScan buffer to release memory - vTaskDelay(2000/portTICK_PERIOD_MS); // Delay a second between loops. - } - - vTaskDelete(NULL); -} - -void app_main(void) { - printf("Scanning...\n"); - - BLEDevice::init(""); - pBLEScan = BLEDevice::getScan(); //create new scan - pBLEScan->setScanCallbacks(new MyAdvertisedDeviceCallbacks()); - pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster - pBLEScan->setInterval(100); - pBLEScan->setWindow(99); // less or equal setInterval value - xTaskCreate(scanTask, "scanTask", 5000, NULL, 1, NULL); -}