From 342e640a5a6b7efc4e8d28dee3a191868f045658 Mon Sep 17 00:00:00 2001 From: Jason2866 <24528715+Jason2866@users.noreply.github.com> Date: Thu, 8 Aug 2024 11:05:04 +0200 Subject: [PATCH] add h2zero NimBLE example --- .github/workflows/examples.yml | 1 + .../espidf-arduino-h2zero-BLE_scan/.gitignore | 2 + .../CMakeLists.txt | 4 ++ .../espidf-arduino-h2zero-BLE_scan/README.md | 7 +++ .../include/README | 39 ++++++++++++++ .../espidf-arduino-h2zero-BLE_scan/lib/README | 46 ++++++++++++++++ .../platformio.ini | 22 ++++++++ .../sdkconfig.defaults | 34 ++++++++++++ .../src/CMakeLists.txt | 6 +++ .../src/idf_component.yml | 40 ++++++++++++++ .../src/main.cpp | 54 +++++++++++++++++++ 11 files changed, 255 insertions(+) create mode 100644 examples/espidf-arduino-h2zero-BLE_scan/.gitignore create mode 100644 examples/espidf-arduino-h2zero-BLE_scan/CMakeLists.txt create mode 100644 examples/espidf-arduino-h2zero-BLE_scan/README.md create mode 100644 examples/espidf-arduino-h2zero-BLE_scan/include/README create mode 100644 examples/espidf-arduino-h2zero-BLE_scan/lib/README create mode 100644 examples/espidf-arduino-h2zero-BLE_scan/platformio.ini create mode 100644 examples/espidf-arduino-h2zero-BLE_scan/sdkconfig.defaults create mode 100644 examples/espidf-arduino-h2zero-BLE_scan/src/CMakeLists.txt create mode 100644 examples/espidf-arduino-h2zero-BLE_scan/src/idf_component.yml create mode 100644 examples/espidf-arduino-h2zero-BLE_scan/src/main.cpp diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml index a9e0352..8e8a32b 100644 --- a/.github/workflows/examples.yml +++ b/.github/workflows/examples.yml @@ -18,6 +18,7 @@ jobs: - "examples/arduino-rmt-blink" - "examples/arduino-usb-keyboard" - "examples/arduino-wifiscan" + - "examples/espidf-arduino-h2zero-BLE_scan" - "examples/espidf-arduino-blink" - "examples/espidf-arduino-littlefs" - "examples/espidf-blink" diff --git a/examples/espidf-arduino-h2zero-BLE_scan/.gitignore b/examples/espidf-arduino-h2zero-BLE_scan/.gitignore new file mode 100644 index 0000000..b9f3806 --- /dev/null +++ b/examples/espidf-arduino-h2zero-BLE_scan/.gitignore @@ -0,0 +1,2 @@ +.pio +.vscode diff --git a/examples/espidf-arduino-h2zero-BLE_scan/CMakeLists.txt b/examples/espidf-arduino-h2zero-BLE_scan/CMakeLists.txt new file mode 100644 index 0000000..b6275f6 --- /dev/null +++ b/examples/espidf-arduino-h2zero-BLE_scan/CMakeLists.txt @@ -0,0 +1,4 @@ +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 new file mode 100644 index 0000000..5601e54 --- /dev/null +++ b/examples/espidf-arduino-h2zero-BLE_scan/README.md @@ -0,0 +1,7 @@ +# 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/include/README b/examples/espidf-arduino-h2zero-BLE_scan/include/README new file mode 100644 index 0000000..194dcd4 --- /dev/null +++ b/examples/espidf-arduino-h2zero-BLE_scan/include/README @@ -0,0 +1,39 @@ + +This directory is intended for project header files. + +A header file is a file containing C declarations and macro definitions +to be shared between several project source files. You request the use of a +header file in your project source file (C, C++, etc) located in `src` folder +by including it, with the C preprocessing directive `#include'. + +```src/main.c + +#include "header.h" + +int main (void) +{ + ... +} +``` + +Including a header file produces the same results as copying the header file +into each source file that needs it. Such copying would be time-consuming +and error-prone. With a header file, the related declarations appear +in only one place. If they need to be changed, they can be changed in one +place, and programs that include the header file will automatically use the +new version when next recompiled. The header file eliminates the labor of +finding and changing all the copies as well as the risk that a failure to +find one copy will result in inconsistencies within a program. + +In C, the usual convention is to give header files names that end with `.h'. +It is most portable to use only letters, digits, dashes, and underscores in +header file names, and at most one dot. + +Read more about using header files in official GCC documentation: + +* Include Syntax +* Include Operation +* Once-Only Headers +* Computed Includes + +https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html diff --git a/examples/espidf-arduino-h2zero-BLE_scan/lib/README b/examples/espidf-arduino-h2zero-BLE_scan/lib/README new file mode 100644 index 0000000..6debab1 --- /dev/null +++ b/examples/espidf-arduino-h2zero-BLE_scan/lib/README @@ -0,0 +1,46 @@ + +This directory is intended for project specific (private) libraries. +PlatformIO will compile them to static libraries and link into executable file. + +The source code of each library should be placed in a an own separate directory +("lib/your_library_name/[here are source files]"). + +For example, see a structure of the following two libraries `Foo` and `Bar`: + +|--lib +| | +| |--Bar +| | |--docs +| | |--examples +| | |--src +| | |- Bar.c +| | |- Bar.h +| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html +| | +| |--Foo +| | |- Foo.c +| | |- Foo.h +| | +| |- README --> THIS FILE +| +|- platformio.ini +|--src + |- main.c + +and a contents of `src/main.c`: +``` +#include +#include + +int main (void) +{ + ... +} + +``` + +PlatformIO Library Dependency Finder will find automatically dependent +libraries scanning project source files. + +More information about PlatformIO Library Dependency Finder +- https://docs.platformio.org/page/librarymanager/ldf.html diff --git a/examples/espidf-arduino-h2zero-BLE_scan/platformio.ini b/examples/espidf-arduino-h2zero-BLE_scan/platformio.ini new file mode 100644 index 0000000..ae81cfd --- /dev/null +++ b/examples/espidf-arduino-h2zero-BLE_scan/platformio.ini @@ -0,0 +1,22 @@ +; 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 + +[env:esp32] +board = esp32dev diff --git a/examples/espidf-arduino-h2zero-BLE_scan/sdkconfig.defaults b/examples/espidf-arduino-h2zero-BLE_scan/sdkconfig.defaults new file mode 100644 index 0000000..267ca7f --- /dev/null +++ b/examples/espidf-arduino-h2zero-BLE_scan/sdkconfig.defaults @@ -0,0 +1,34 @@ +# 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 new file mode 100644 index 0000000..483bc0c --- /dev/null +++ b/examples/espidf-arduino-h2zero-BLE_scan/src/CMakeLists.txt @@ -0,0 +1,6 @@ +# 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 new file mode 100644 index 0000000..98b4cbf --- /dev/null +++ b/examples/espidf-arduino-h2zero-BLE_scan/src/idf_component.yml @@ -0,0 +1,40 @@ +dependencies: + # Required IDF version + idf: ">=4.4" + + esp-nimble-cpp: + git: https://github.com/h2zero/esp-nimble-cpp.git + + + # # 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 new file mode 100644 index 0000000..04b6e5f --- /dev/null +++ b/examples/espidf-arduino-h2zero-BLE_scan/src/main.cpp @@ -0,0 +1,54 @@ +/* + 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); +}