From c2e3723eb08d11a6d1748f6e23a3bb745e1971fd Mon Sep 17 00:00:00 2001 From: rbaron Date: Fri, 26 Mar 2021 10:49:28 +0100 Subject: [PATCH] Adds temp and humidity values to the BLE advertisement packet --- code/b-parasite/config/prst_config.h | 2 +- code/b-parasite/src/main.c | 7 +++---- code/b-parasite/src/prst/ble.c | 14 +++++++++++++- code/b-parasite/src/prst/ble.h | 4 +++- code/b-parasite/src/prst/shtc3.c | 7 ++++--- code/b-parasite/src/prst/shtc3.h | 7 +++++-- 6 files changed, 29 insertions(+), 12 deletions(-) diff --git a/code/b-parasite/config/prst_config.h b/code/b-parasite/config/prst_config.h index 9611a43..7e4a03a 100644 --- a/code/b-parasite/config/prst_config.h +++ b/code/b-parasite/config/prst_config.h @@ -10,7 +10,7 @@ #define PRST_DEEP_SLEEP_IN_SECONDS 2 // BLE. -#define PRST_BLE_ADV_NAME "Prst" +#define PRST_BLE_ADV_NAME "prst" // PWM. #define PRST_PWM_PIN NRF_GPIO_PIN_MAP(0, 29) diff --git a/code/b-parasite/src/main.c b/code/b-parasite/src/main.c index 4b4ffaf..0f57e7a 100644 --- a/code/b-parasite/src/main.c +++ b/code/b-parasite/src/main.c @@ -66,19 +66,18 @@ static void rtc_callback() { prst_pwm_start(); prst_adc_batt_read_t batt_read = prst_adc_batt_read(); prst_pwm_stop(); - prst_ble_update_adv_data(batt_read.millivolts); + prst_ble_update_adv_data(batt_read.millivolts, temp_humi.temp_millicelcius, temp_humi.humidity, 0); prst_adv_start(); nrf_delay_ms(200); prst_adv_stop(); nrf_gpio_pin_clear(PRST_LED_PIN); UNUSED_VARIABLE(batt_read); - // UNUSED_VARIABLE(temp_humi); NRF_LOG_INFO("Read batt: " NRF_LOG_FLOAT_MARKER " V (%d), %u mV", NRF_LOG_FLOAT(batt_read.voltage), batt_read.raw, batt_read.millivolts); NRF_LOG_INFO("Read temp: " NRF_LOG_FLOAT_MARKER " oC", - NRF_LOG_FLOAT(temp_humi.temp_c)); + NRF_LOG_FLOAT((float) temp_humi.temp_millicelcius / 1000.0)); NRF_LOG_INFO("Read humi: " NRF_LOG_FLOAT_MARKER " %%", - NRF_LOG_FLOAT(temp_humi.humidity)); + NRF_LOG_FLOAT(100.0 * temp_humi.humidity / (1 << 16))); NRF_LOG_FLUSH(); } diff --git a/code/b-parasite/src/prst/ble.c b/code/b-parasite/src/prst/ble.c index 7aae519..dd3fa6e 100644 --- a/code/b-parasite/src/prst/ble.c +++ b/code/b-parasite/src/prst/ble.c @@ -11,6 +11,7 @@ // TODO(rbaron): More info in the adv packet: // - Software version +// - Counter // We need to pick a service UUID for broadcasting our sensor data. // 0x181a is defined as "environmental sensing", which seems appopriate. @@ -101,10 +102,21 @@ void prst_ble_init() { init_advertisement_data(); } -void prst_ble_update_adv_data(uint16_t batt_millivolts) { +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; + service_data[2] = temp_millicelcius >> 8; + service_data[3] = temp_millicelcius & 0xff; + + service_data[4] = humidity >> 8; + service_data[5] = humidity & 0xff; + + service_data[6] = soil_moisture >> 8; + service_data[7] = soil_moisture & 0xff; + // Encodes adv_data_ into .gap_adv_data_. uint32_t err_code = ble_advdata_encode( &adv_data_, gap_adv_data_.adv_data.p_data, &gap_adv_data_.adv_data.len); diff --git a/code/b-parasite/src/prst/ble.h b/code/b-parasite/src/prst/ble.h index 382550a..851cd99 100644 --- a/code/b-parasite/src/prst/ble.h +++ b/code/b-parasite/src/prst/ble.h @@ -11,6 +11,8 @@ void prst_adv_start(); void prst_adv_stop(); -void prst_ble_update_adv_data(uint16_t batt_millivolts); +void prst_ble_update_adv_data(uint16_t batt_millivolts, + uint16_t temp_millicelcius, uint16_t humidity, + uint16_t soil_moisture); #endif // _PRST_BLE_H_ \ No newline at end of file diff --git a/code/b-parasite/src/prst/shtc3.c b/code/b-parasite/src/prst/shtc3.c index 80a63f0..fd9bd29 100644 --- a/code/b-parasite/src/prst/shtc3.c +++ b/code/b-parasite/src/prst/shtc3.c @@ -46,9 +46,10 @@ prst_shtc3_read_t prst_shtc3_read() { NRF_LOG_INFO("Computing..."); double temp_c = - -45 + 175 * ((double)((buff[0] << 8) | buff[1])) / ((1 << 16) - 1); - double humi = 100 * ((double)((buff[3] << 8) | buff[4])) / ((1 << 16) - 1); + -45 + 175 * ((double)((buff[0] << 8) | buff[1])) / (1 << 16); + // double humi = ((double)((buff[3] << 8) | buff[4])) / ((1 << 16) - 1); + uint16_t humi = (buff[3] << 8) | buff[4]; - prst_shtc3_read_t ret = {.temp_c = temp_c, .humidity = humi}; + prst_shtc3_read_t ret = {.temp_millicelcius = temp_c * 1000, .humidity = humi }; return ret; } \ No newline at end of file diff --git a/code/b-parasite/src/prst/shtc3.h b/code/b-parasite/src/prst/shtc3.h index e19951c..720b468 100644 --- a/code/b-parasite/src/prst/shtc3.h +++ b/code/b-parasite/src/prst/shtc3.h @@ -13,8 +13,11 @@ #define PRST_SHTC3_CMD_MEASURE_TFIRST_LOW_POWER 0x609c typedef struct prst_shtc3_values { - double temp_c; - double humidity; + // Temperature in millicelcius. To get the temp in Celcius, divide this by + // 1000.0. + int16_t temp_millicelcius; + // Relative humidity, from 0 to 2^16. + uint16_t humidity; } prst_shtc3_read_t; void prst_shtc3_init();