From 3c7180fefad58254f8813845bf8142634254bd56 Mon Sep 17 00:00:00 2001 From: rbaron Date: Sun, 28 Mar 2021 10:44:33 +0200 Subject: [PATCH] Adds run_counter to ble advertising packet --- code/b-parasite/config/prst_config.h | 15 +++++++++----- code/b-parasite/src/main.c | 9 ++++++--- code/b-parasite/src/prst/ble.c | 30 ++++++++++++++++++---------- code/b-parasite/src/prst/ble.h | 3 ++- 4 files changed, 37 insertions(+), 20 deletions(-) diff --git a/code/b-parasite/config/prst_config.h b/code/b-parasite/config/prst_config.h index e8e1f01..ff4e331 100644 --- a/code/b-parasite/config/prst_config.h +++ b/code/b-parasite/config/prst_config.h @@ -7,7 +7,7 @@ #define PRST_LED_PIN NRF_GPIO_PIN_MAP(0, 28) // Deep sleep. -#define PRST_DEEP_SLEEP_IN_SECONDS 3 +#define PRST_DEEP_SLEEP_IN_SECONDS 300 // Analog to digital converter (ADC). // Prints out ADC debug info, such as the values read for battery and soil @@ -17,10 +17,15 @@ // BLE. // Prints out BLE debug info, such as the final encoded advertisement packet. #define PRST_BLE_DEBUG 0 -#define PRST_BLE_MAC_ADDR_LSB0 0xaa -#define PRST_BLE_MAC_ADDR_LSB1 0xbb -#define PRST_BLE_ADV_TIME_IN_MS 200 -#define PRST_BLE_ADV_NAME "prst aabb" +#define PRST_BLE_PROTOCOL_VERSION 1 +#define PRST_BLE_MAC_ADDR_LSB1 0x00 +#define PRST_BLE_MAC_ADDR_LSB0 0x01 +#define PRST_BLE_ADV_NAME "prst" +// Total time spend advertising. +#define PRST_BLE_ADV_TIME_IN_MS 1000 +// Interval between advertising packets. +// From the specs, this value has to be greater or equal 20ms. +#define PRST_BLE_ADV_INTERVAL_IN_MS 30 // PWM. #define PRST_PWM_PIN NRF_GPIO_PIN_MAP(0, 5) diff --git a/code/b-parasite/src/main.c b/code/b-parasite/src/main.c index 6b12d1a..d88473a 100644 --- a/code/b-parasite/src/main.c +++ b/code/b-parasite/src/main.c @@ -22,12 +22,14 @@ #include "prst/shtc3.h" #include "prst_config.h" -#define DEAD_BEEF 0xDEADBEEF - +// #define DEAD_BEEF 0xDEADBEEF // void assert_nrf_callback(uint16_t line_num, const uint8_t *p_file_name) { // app_error_handler(DEAD_BEEF, line_num, p_file_name); // } +// A small wrap-around counter for deduplicating BLE packets on the receiver. +static uint8_t run_counter = 0; + static void log_init(void) { ret_code_t err_code = NRF_LOG_INIT(NULL); APP_ERROR_CHECK(err_code); @@ -68,13 +70,14 @@ static void rtc_callback() { prst_pwm_stop(); nrf_gpio_pin_clear(PRST_FAST_DISCH_PIN); prst_ble_update_adv_data(batt_read.millivolts, temp_humi.temp_millicelcius, - temp_humi.humidity, soil_read.relative); + temp_humi.humidity, soil_read.relative, run_counter); NRF_LOG_FLUSH(); prst_adv_start(); nrf_delay_ms(PRST_BLE_ADV_TIME_IN_MS); prst_adv_stop(); nrf_gpio_pin_clear(PRST_LED_PIN); NRF_LOG_FLUSH(); + run_counter++; } int main(void) { diff --git a/code/b-parasite/src/prst/ble.c b/code/b-parasite/src/prst/ble.c index b465cdb..f7aac3a 100644 --- a/code/b-parasite/src/prst/ble.c +++ b/code/b-parasite/src/prst/ble.c @@ -20,7 +20,8 @@ // The connection to configure. We only have the one. #define PRST_CONN_CFG_TAG 1 -#define NON_CONNECTABLE_ADV_INTERVAL MSEC_TO_UNITS(100, UNIT_0_625_MS) +#define NON_CONNECTABLE_ADV_INTERVAL \ + MSEC_TO_UNITS(PRST_BLE_ADV_INTERVAL_IN_MS, UNIT_0_625_MS) // Sensor data payload that will go into the advertisement message. #define SERVICE_DATA_LEN 12 @@ -93,7 +94,8 @@ static void init_advertisement_data() { sd_ble_gap_adv_set_configure(&adv_handle_, &gap_adv_data_, &adv_params_); APP_ERROR_CHECK(err_code); - APP_ERROR_CHECK(sd_ble_gap_addr_set(&gap_addr_)); + // Four bits for the protocol version + service_data[0] = (PRST_BLE_PROTOCOL_VERSION << 4) & 0xf0; } void prst_ble_init() { @@ -112,23 +114,29 @@ void prst_ble_init() { err_code = nrf_sdh_ble_enable(&ram_start); APP_ERROR_CHECK(err_code); + APP_ERROR_CHECK(sd_ble_gap_addr_set(&gap_addr_)); + init_advertisement_data(); } void prst_ble_update_adv_data(uint16_t batt_millivolts, uint16_t temp_millicelcius, uint16_t humidity, - uint16_t soil_moisture) { - service_data[0] = batt_millivolts >> 8; - service_data[1] = batt_millivolts & 0xff; + uint16_t soil_moisture, uint8_t run_counter) { + // 4 bits for a small wrap-around counter for deduplicating messages on the + // receiver. + service_data[1] = run_counter & 0x0f; - service_data[2] = temp_millicelcius >> 8; - service_data[3] = temp_millicelcius & 0xff; + service_data[2] = batt_millivolts >> 8; + service_data[3] = batt_millivolts & 0xff; - service_data[4] = humidity >> 8; - service_data[5] = humidity & 0xff; + service_data[4] = temp_millicelcius >> 8; + service_data[5] = temp_millicelcius & 0xff; - service_data[6] = soil_moisture >> 8; - service_data[7] = soil_moisture & 0xff; + service_data[6] = humidity >> 8; + service_data[7] = humidity & 0xff; + + service_data[8] = soil_moisture >> 8; + service_data[9] = soil_moisture & 0xff; // Encodes adv_data_ into .gap_adv_data_. uint32_t err_code = ble_advdata_encode( diff --git a/code/b-parasite/src/prst/ble.h b/code/b-parasite/src/prst/ble.h index 851cd99..df7bdb9 100644 --- a/code/b-parasite/src/prst/ble.h +++ b/code/b-parasite/src/prst/ble.h @@ -13,6 +13,7 @@ void prst_adv_stop(); void prst_ble_update_adv_data(uint16_t batt_millivolts, uint16_t temp_millicelcius, uint16_t humidity, - uint16_t soil_moisture); + uint16_t soil_moisture, + uint8_t run_counter); #endif // _PRST_BLE_H_ \ No newline at end of file