Skip to content

Commit a69cfe6

Browse files
authored
Initial commit
1 parent 8922d62 commit a69cfe6

9 files changed

+246
-0
lines changed

CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
cmake_minimum_required(VERSION 3.16.0)
2+
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
3+
project(platform-test)

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Arduino_IDF_BLE_scan example using 3rd party NimBLE stack
2+
3+
BLE scan example, using the great h2zero NimBLE implementation. The needed NimBLE lib is loaded via the IDF component manager -> `idf_component.yml`.
4+
Mandantory not to forget to switch Arduino included BLE libs.
5+
Done in `sdkconfig.defaults`
6+
7+
Thx @h2zero for the great BLE library.

include/README

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
This directory is intended for project header files.
3+
4+
A header file is a file containing C declarations and macro definitions
5+
to be shared between several project source files. You request the use of a
6+
header file in your project source file (C, C++, etc) located in `src` folder
7+
by including it, with the C preprocessing directive `#include'.
8+
9+
```src/main.c
10+
11+
#include "header.h"
12+
13+
int main (void)
14+
{
15+
...
16+
}
17+
```
18+
19+
Including a header file produces the same results as copying the header file
20+
into each source file that needs it. Such copying would be time-consuming
21+
and error-prone. With a header file, the related declarations appear
22+
in only one place. If they need to be changed, they can be changed in one
23+
place, and programs that include the header file will automatically use the
24+
new version when next recompiled. The header file eliminates the labor of
25+
finding and changing all the copies as well as the risk that a failure to
26+
find one copy will result in inconsistencies within a program.
27+
28+
In C, the usual convention is to give header files names that end with `.h'.
29+
It is most portable to use only letters, digits, dashes, and underscores in
30+
header file names, and at most one dot.
31+
32+
Read more about using header files in official GCC documentation:
33+
34+
* Include Syntax
35+
* Include Operation
36+
* Once-Only Headers
37+
* Computed Includes
38+
39+
https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html

lib/README

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
This directory is intended for project specific (private) libraries.
3+
PlatformIO will compile them to static libraries and link into executable file.
4+
5+
The source code of each library should be placed in a an own separate directory
6+
("lib/your_library_name/[here are source files]").
7+
8+
For example, see a structure of the following two libraries `Foo` and `Bar`:
9+
10+
|--lib
11+
| |
12+
| |--Bar
13+
| | |--docs
14+
| | |--examples
15+
| | |--src
16+
| | |- Bar.c
17+
| | |- Bar.h
18+
| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html
19+
| |
20+
| |--Foo
21+
| | |- Foo.c
22+
| | |- Foo.h
23+
| |
24+
| |- README --> THIS FILE
25+
|
26+
|- platformio.ini
27+
|--src
28+
|- main.c
29+
30+
and a contents of `src/main.c`:
31+
```
32+
#include <Foo.h>
33+
#include <Bar.h>
34+
35+
int main (void)
36+
{
37+
...
38+
}
39+
40+
```
41+
42+
PlatformIO Library Dependency Finder will find automatically dependent
43+
libraries scanning project source files.
44+
45+
More information about PlatformIO Library Dependency Finder
46+
- https://docs.platformio.org/page/librarymanager/ldf.html

platformio.ini

+53
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 = https://github.com/pioarduino/platform-espressif32.git#poc_idf_installer
13+
;platform = https://github.com/pioarduino/platform-espressif32.git#develop
14+
platform = https://github.com/Jason2866/platform-espressif32.git#idf_installer
15+
framework = arduino, espidf
16+
monitor_speed = 115200
17+
board_build.embed_txtfiles =
18+
managed_components/espressif__esp_insights/server_certs/https_server.crt
19+
managed_components/espressif__esp_rainmaker/server_certs/rmaker_mqtt_server.crt
20+
managed_components/espressif__esp_rainmaker/server_certs/rmaker_claim_service_server.crt
21+
managed_components/espressif__esp_rainmaker/server_certs/rmaker_ota_server.crt
22+
check_skip_packages = yes
23+
lib_ignore =
24+
BLE
25+
BluetoothSerial
26+
SimpleBLE
27+
WiFiProv
28+
ArduinoOTA
29+
30+
[env:esp32]
31+
board = esp32dev
32+
check_tool = cppcheck
33+
34+
[env:esp32-s3]
35+
board = esp32-s3-devkitc-1
36+
check_tool = cppcheck
37+
38+
[env:esp32-c2]
39+
board = esp32-c2-devkitm-1
40+
board_build.embed_txtfiles =
41+
check_tool = cppcheck
42+
43+
[env:esp32-c3]
44+
board = esp32-c3-devkitm-1
45+
check_tool = cppcheck
46+
47+
[env:esp32-c6]
48+
board = esp32-c6-devkitm-1
49+
check_tool = cppcheck
50+
51+
[env:esp32-h2]
52+
board = esp32-h2-devkitm-1
53+
check_tool = cppcheck

sdkconfig.defaults

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# CONFIG_AUTOSTART_ARDUINO is not set
2+
# CONFIG_WS2812_LED_ENABLE is not set
3+
CONFIG_FREERTOS_HZ=1000
4+
CONFIG_MBEDTLS_PSK_MODES=y
5+
CONFIG_MBEDTLS_KEY_EXCHANGE_PSK=y
6+
CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_SIZE=y
7+
CONFIG_COMPILER_OPTIMIZATION_SIZE=y
8+
CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y
9+
CONFIG_ESPTOOLPY_HEADER_FLASHSIZE_UPDATE=y
10+
11+
# Override some defaults so BT stack is enabled
12+
# in this example
13+
#
14+
# BT config
15+
#
16+
CONFIG_BT_ENABLED=y
17+
CONFIG_BTDM_CTRL_MODE_BLE_ONLY=y
18+
CONFIG_BTDM_CTRL_MODE_BR_EDR_ONLY=n
19+
CONFIG_BTDM_CTRL_MODE_BTDM=n
20+
CONFIG_BT_BLUEDROID_ENABLED=n
21+
CONFIG_BT_NIMBLE_ENABLED=y
22+
23+
#
24+
# Arduino Configuration
25+
#
26+
#
27+
# Disable all Arduino included BLE libraries
28+
#
29+
CONFIG_ARDUINO_SELECTIVE_COMPILATION=y
30+
# CONFIG_ARDUINO_SELECTIVE_WiFiProv is not set
31+
# CONFIG_ARDUINO_SELECTIVE_BLE is not set
32+
# CONFIG_ARDUINO_SELECTIVE_BluetoothSerial is not set
33+
# CONFIG_ARDUINO_SELECTIVE_SimpleBLE is not set
34+
# end of Arduino Configuration

src/CMakeLists.txt

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# This file was automatically generated for projects
2+
# without default 'CMakeLists.txt' file.
3+
4+
FILE(GLOB_RECURSE app_sources ${CMAKE_SOURCE_DIR}/src/*.*)
5+
6+
idf_component_register(SRCS ${app_sources})

src/idf_component.yml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
dependencies:
2+
idf: ">=4.4,<5.4"
3+
esp-nimble-cpp:
4+
git: https://github.com/h2zero/esp-nimble-cpp.git

src/main.cpp

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
Refactored back to IDF by H2zero
5+
*/
6+
7+
/** NimBLE differences highlighted in comment blocks **/
8+
9+
/*******original********
10+
#include <BLEDevice.h>
11+
#include <BLEUtils.h>
12+
#include <BLEScan.h>
13+
#include <BLEAdvertisedDevice.h>
14+
***********************/
15+
16+
#include <Arduino.h>
17+
18+
#include <NimBLEDevice.h>
19+
20+
extern "C"{void app_main(void);}
21+
22+
int scanTime = 5 * 1000; // In milliseconds, 0 = scan forever
23+
BLEScan* pBLEScan;
24+
25+
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
26+
void onResult(BLEAdvertisedDevice* advertisedDevice) {
27+
printf("Advertised Device: %s \n", advertisedDevice->toString().c_str());
28+
}
29+
};
30+
31+
void scanTask (void * parameter){
32+
for(;;) {
33+
// put your main code here, to run repeatedly:
34+
BLEScanResults foundDevices = pBLEScan->getResults(scanTime, false);
35+
printf("Devices found: %d\n", foundDevices.getCount());
36+
printf("Scan done!\n");
37+
pBLEScan->clearResults(); // delete results fromBLEScan buffer to release memory
38+
vTaskDelay(2000/portTICK_PERIOD_MS); // Delay a second between loops.
39+
}
40+
41+
vTaskDelete(NULL);
42+
}
43+
44+
void app_main(void) {
45+
printf("Scanning...\n");
46+
47+
BLEDevice::init("");
48+
pBLEScan = BLEDevice::getScan(); //create new scan
49+
pBLEScan->setScanCallbacks(new MyAdvertisedDeviceCallbacks());
50+
pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster
51+
pBLEScan->setInterval(100);
52+
pBLEScan->setWindow(99); // less or equal setInterval value
53+
xTaskCreate(scanTask, "scanTask", 5000, NULL, 1, NULL);
54+
}

0 commit comments

Comments
 (0)