Skip to content

Simplify standaline BT examples #667

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 2 additions & 68 deletions pico_w/bt/standalone/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,68 +1,2 @@
# Standalone example that reads from the on board temperature sensor and sends notifications via BLE
# Flashes slowly each second to show it's running
add_executable(picow_ble_temp_sensor
server.c server_common.c
)
target_link_libraries(picow_ble_temp_sensor
pico_stdlib
pico_btstack_ble
pico_btstack_cyw43
pico_cyw43_arch_none
hardware_adc
)
target_include_directories(picow_ble_temp_sensor PRIVATE
${CMAKE_CURRENT_LIST_DIR} # For btstack config
)
pico_btstack_make_gatt_header(picow_ble_temp_sensor PRIVATE "${CMAKE_CURRENT_LIST_DIR}/temp_sensor.gatt")

pico_add_extra_outputs(picow_ble_temp_sensor)
example_auto_set_url(picow_ble_temp_sensor)

# Standalone example that connects to picow_ble_temp_sensor and reads the temperature
# Flahes once quickly each second when it's running but not connected to another device
# Flashes twice quickly each second when connected to another device and reading it's temperature
add_executable(picow_ble_temp_reader
client.c
)
target_link_libraries(picow_ble_temp_reader
pico_stdlib
pico_btstack_ble
pico_btstack_cyw43
pico_cyw43_arch_none
hardware_adc
)
target_include_directories(picow_ble_temp_reader PRIVATE
${CMAKE_CURRENT_LIST_DIR} # For btstack config
)
target_compile_definitions(picow_ble_temp_reader PRIVATE
RUNNING_AS_CLIENT=1
)

pico_add_extra_outputs(picow_ble_temp_reader)
example_auto_set_url(picow_ble_temp_reader)

if (WIFI_SSID AND WIFI_PASSWORD)
# Another version of the sensor example, but this time also runs iperf over wifi
add_executable(picow_ble_temp_sensor_with_wifi
server_with_wifi.c server_common.c
)
target_link_libraries(picow_ble_temp_sensor_with_wifi
pico_stdlib
pico_btstack_ble
pico_btstack_cyw43
pico_cyw43_arch_lwip_threadsafe_background
pico_lwip_iperf
hardware_adc
)
target_include_directories(picow_ble_temp_sensor_with_wifi PRIVATE
${CMAKE_CURRENT_LIST_DIR} # For btstack config
)
target_compile_definitions(picow_ble_temp_sensor_with_wifi PRIVATE
WIFI_SSID=\"${WIFI_SSID}\"
WIFI_PASSWORD=\"${WIFI_PASSWORD}\"
)
pico_btstack_make_gatt_header(picow_ble_temp_sensor_with_wifi PRIVATE "${CMAKE_CURRENT_LIST_DIR}/temp_sensor.gatt")

pico_add_extra_outputs(picow_ble_temp_sensor_with_wifi)
example_auto_set_url(picow_ble_temp_sensor_with_wifi)
endif()
add_subdirectory(client)
add_subdirectory(server)
22 changes: 22 additions & 0 deletions pico_w/bt/standalone/client/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Standalone example that connects to picow_ble_temp_sensor and reads the temperature
# Flahes once quickly each second when it's running but not connected to another device
# Flashes twice quickly each second when connected to another device and reading it's temperature
add_executable(picow_ble_temp_reader
client.c
)
target_link_libraries(picow_ble_temp_reader
pico_stdlib
pico_btstack_ble
pico_btstack_cyw43
pico_cyw43_arch_none
hardware_adc
)
target_include_directories(picow_ble_temp_reader PRIVATE
${CMAKE_CURRENT_LIST_DIR}/.. # For btstack config
)
Comment on lines +14 to +16
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this will actually make it harder for VS Code to import, due to dependence on files out of the directory - could this be changed to something similar to how the wifi examples have a local lwipopts.h which just includes a common lwipopts_examples_common.h, so then VS Code can copy a standard file across (like it currently does for lwipopts)?

Suggested change
target_include_directories(picow_ble_temp_reader PRIVATE
${CMAKE_CURRENT_LIST_DIR}/.. # For btstack config
)
target_include_directories(picow_ble_temp_reader PRIVATE
${CMAKE_CURRENT_LIST_DIR}
${CMAKE_CURRENT_LIST_DIR}/.. # For common btstack config
)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes - will do

target_compile_definitions(picow_ble_temp_reader PRIVATE
RUNNING_AS_CLIENT=1
)

pico_add_extra_outputs(picow_ble_temp_reader)
example_auto_set_url(picow_ble_temp_reader)
File renamed without changes.
105 changes: 0 additions & 105 deletions pico_w/bt/standalone/server.c

This file was deleted.

19 changes: 19 additions & 0 deletions pico_w/bt/standalone/server/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Standalone example that reads from the on board temperature sensor and sends notifications via BLE
# Flashes slowly each second to show it's running
add_executable(picow_ble_temp_sensor
server.c
)
target_link_libraries(picow_ble_temp_sensor
pico_stdlib
pico_btstack_ble
pico_btstack_cyw43
pico_cyw43_arch_none
hardware_adc
)
target_include_directories(picow_ble_temp_sensor PRIVATE
${CMAKE_CURRENT_LIST_DIR}/.. # For btstack config
)
pico_btstack_make_gatt_header(picow_ble_temp_sensor PRIVATE "${CMAKE_CURRENT_LIST_DIR}/temp_sensor.gatt")

pico_add_extra_outputs(picow_ble_temp_sensor)
example_auto_set_url(picow_ble_temp_sensor)
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@

#include <stdio.h>
#include "btstack.h"
#include "pico/cyw43_arch.h"
#include "pico/btstack_cyw43.h"
#include "hardware/adc.h"
#include "pico/stdlib.h"

#include "temp_sensor.h"
#include "server_common.h"

#define HEARTBEAT_PERIOD_MS 1000
#define ADC_CHANNEL_TEMPSENSOR 4
#define APP_AD_FLAGS 0x06

static uint8_t adv_data[] = {
// Flags general discoverable
0x02, BLUETOOTH_DATA_TYPE_FLAGS, APP_AD_FLAGS,
Expand All @@ -24,6 +29,10 @@ static const uint8_t adv_data_len = sizeof(adv_data);
int le_notification_enabled;
hci_con_handle_t con_handle;
uint16_t current_temp;
static btstack_timer_source_t heartbeat;
static btstack_packet_callback_registration_t hci_event_callback_registration;

extern uint8_t const profile_data[];

void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) {
UNUSED(size);
Expand Down Expand Up @@ -76,7 +85,7 @@ int att_write_callback(hci_con_handle_t connection_handle, uint16_t att_handle,
UNUSED(transaction_mode);
UNUSED(offset);
UNUSED(buffer_size);

if (att_handle != ATT_CHARACTERISTIC_ORG_BLUETOOTH_CHARACTERISTIC_TEMPERATURE_01_CLIENT_CONFIGURATION_HANDLE) return 0;
le_notification_enabled = little_endian_read_16(buffer, 0) == GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION;
con_handle = connection_handle;
Expand All @@ -97,10 +106,96 @@ void poll_temp(void) {
// ref https://github.com/raspberrypi/pico-micropython-examples/blob/master/adc/temperature.py
const float conversion_factor = 3.3 / (65535);
float reading = raw16 * conversion_factor;

// The temperature sensor measures the Vbe voltage of a biased bipolar diode, connected to the fifth ADC channel
// Typically, Vbe = 0.706V at 27 degrees C, with a slope of -1.721mV (0.001721) per degree.
// Typically, Vbe = 0.706V at 27 degrees C, with a slope of -1.721mV (0.001721) per degree.
float deg_c = 27 - (reading - 0.706) / 0.001721;
current_temp = deg_c * 100;
printf("Write temp %.2f degc\n", deg_c);
}
}

static void heartbeat_handler(struct btstack_timer_source *ts) {
static uint32_t counter = 0;
counter++;

// Update the temp every 10s
if (counter % 10 == 0) {
poll_temp();
if (le_notification_enabled) {
att_server_request_can_send_now_event(con_handle);
}
}

// Invert the led
static int led_on = true;
led_on = !led_on;
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, led_on);

// Restart timer
btstack_run_loop_set_timer(ts, HEARTBEAT_PERIOD_MS);
btstack_run_loop_add_timer(ts);
}

static volatile bool key_pressed;
void key_pressed_func(void *param) {
int key = getchar_timeout_us(0); // get any pending key press but don't wait
if (key == 's' || key == 'S') {
key_pressed = true;
}
}

int main() {
stdio_init_all();

restart:
// initialize CYW43 driver architecture (will enable BT if/because CYW43_ENABLE_BLUETOOTH == 1)
if (cyw43_arch_init()) {
printf("failed to initialise cyw43_arch\n");
return -1;
}

// Get notified if the user presses a key
printf("Press the \"S\" key to Stop bluetooth\n");
stdio_set_chars_available_callback(key_pressed_func, NULL);

// Initialise adc for the temp sensor
adc_init();
adc_select_input(ADC_CHANNEL_TEMPSENSOR);
adc_set_temp_sensor_enabled(true);

l2cap_init();
sm_init();

att_server_init(profile_data, att_read_callback, att_write_callback);

// inform about BTstack state
hci_event_callback_registration.callback = &packet_handler;
hci_add_event_handler(&hci_event_callback_registration);

// register for ATT event
att_server_register_packet_handler(packet_handler);

// set one-shot btstack timer
heartbeat.process = &heartbeat_handler;
btstack_run_loop_set_timer(&heartbeat, HEARTBEAT_PERIOD_MS);
btstack_run_loop_add_timer(&heartbeat);

// turn on bluetooth!
hci_power_control(HCI_POWER_ON);

key_pressed = false;
while(!key_pressed) {
async_context_poll(cyw43_arch_async_context());
async_context_wait_for_work_until(cyw43_arch_async_context(), at_the_end_of_time);
}

cyw43_arch_deinit();

printf("Press the \"S\" key to Start bluetooth\n");
key_pressed = false;
while(!key_pressed) {
sleep_ms(1000);
}
goto restart;
return 0;
}
22 changes: 0 additions & 22 deletions pico_w/bt/standalone/server_common.h

This file was deleted.

Loading