Introduces version-specific config & condition BLE lux encoding

In the BLE service data payload, one of the reserved bits is now used to
indicate whether or not the ambient lux value is encoded in the packet.
Bit 0 of the first byte is now the `has_lux` bit. If it is set, bytes
16-17 in the service data will contain the ambient lux. If `has_lux` is
not set, bytes 16-17 may not exist (for compatibility with older
firmwares) or may hold meaningless values.

This commit also introduces version-specific configuration in
prst_config.h. Photoresistor config, for example, is only meaninful for
version 1.1.x.
This commit is contained in:
rbaron 2021-09-22 22:23:08 +02:00
parent a00559a98b
commit d8aea1d4ef
4 changed files with 83 additions and 31 deletions

View file

@ -30,16 +30,26 @@ Sensor data is encoded in the BLE advertisement packet as Service Data for the [
Sensor data is encoded in unsigned 16 bits (2 bytes), and whenever multiple
bytes are used to represent a single value, the encoding is big-endian.
| Byte index | Description |
|------------|----------------------------------------------------------------|
| 0 | 4 bits for protocol version + 4 reserved bits |
| 1 | 4 reserved bits + 4 bits wrap-around counter for deduplication |
| 2-3 | Battery voltage in millivolts |
| 4-5 | Temperature in millidegrees Celcius |
| 6-7 | Relative air humidity, scaled from 0 (0%) to 0xffff (100%) |
| 8-9 | Soil moisture, scaled from from 0 (0%) to 0xffff (100%) |
| 10-15 | b-parasite's own MAC address, big-endian format |
| 16-17 | Ambient brightness level from 0 (dark) to 0xffff (bright) |
| Byte index | Description |
|------------|-----------------------------------------------------------------|
| 0 | Protocol version (4 bits) + reserved (3 bits) + has_lux* (1 bit)|
| 1 | Reserved (4 bits) + increasing, wrap-around counter (4 bits) |
| 2-3 | Battery voltage in millivolts |
| 4-5 | Temperature in millidegrees Celcius |
| 6-7 | Relative air humidity, scaled from 0 (0%) to 0xffff (100%) |
| 8-9 | Soil moisture, scaled from from 0 (0%) to 0xffff (100%) |
| 10-15 | b-parasite's own MAC address |
| 16-17* | Ambient light in lux |
\* If the `has_lux` bit is set, bytes 16-17 shall contain the ambient light in lux.
If the `has_lux` bit is not set, bytes 16-17 may not exist or may contain
meaningless data. The reasons for this behavior are:
1. b-parasite version 1.0.x has no light sensor and its advertisement data may
have only 16 bytes if its using an older firmware. In this case, `has_lux` shall
never be set;
2. b-parasite version 1.1.x has space for an optional LDR. Users can configure
whether or not they have added the LDR by setting the `PRST_HAS_LDR` to 1 in
prst_config.h.
# Supported Modules

View file

@ -3,13 +3,15 @@
#include "nrf_gpio.h"
// Some configurations are version-specific. Uncomment the line corresponding
// the the version you're programming. The version can be found on the
// b-parasite board.
// #define PRST_VERSION_1_0_X
#define PRST_VERSION_1_1_X
// Built-in LED.
#define PRST_LED_PIN NRF_GPIO_PIN_MAP(0, 28)
// Photoresistor pins.
#define PRST_PHOTO_V NRF_GPIO_PIN_MAP(0, 29)
#define PRST_PHOTO_OUT NRF_GPIO_PIN_MAP(0, 2)
// Deep sleep.
#define PRST_DEEP_SLEEP_IN_SECONDS 2
@ -18,7 +20,6 @@
// moisture.
#define PRST_ADC_BATT_DEBUG 0
#define PRST_ADC_SOIL_DEBUG 0
#define PRST_ADC_PHOTO_DEBUG 1
// BLE.
// Prints out BLE debug info, such as the final encoded advertisement packet.
@ -50,4 +51,20 @@
// SHT3C temp/humidity sensor.
#define PRST_SHT3C_DEBUG 0
// Version-specific configuration.
#if defined(PRST_VERSION_1_1_X)
// The photoresistor (LDR) is optional in this revision. If set to 1, the LDR's
// ADC channel will be sampled and its data will be encoded in the BLE
// advertisement packet.
#define PRST_HAS_LDR 1
// Light sensor pins.
#define PRST_PHOTO_V_PIN NRF_GPIO_PIN_MAP(0, 29)
#define PRST_PHOTO_OUT_PIN NRF_GPIO_PIN_MAP(0, 2)
// Whether to produce debug messages for the LDR
#define PRST_ADC_PHOTO_DEBUG 1
#endif // End of version-specific configuration.
#endif // _PRST_CONFIG_H_

View file

@ -26,7 +26,9 @@ static void log_init(void) {
static void gpio_init(void) {
nrf_gpio_cfg_output(PRST_LED_PIN);
nrf_gpio_cfg_output(PRST_FAST_DISCH_PIN);
nrf_gpio_cfg_output(PRST_PHOTO_V);
#ifdef PRST_HAS_LDR
nrf_gpio_cfg_output(PRST_PHOTO_V_PIN);
#endif
NRF_LOG_INFO("GPIO pins inited.");
}
@ -64,13 +66,17 @@ static void rtc_callback() {
prst_pwm_stop();
nrf_gpio_pin_clear(PRST_FAST_DISCH_PIN);
nrf_gpio_pin_set(PRST_PHOTO_V);
uint16_t lux = 0;
#ifdef PRST_HAS_LDR
nrf_gpio_pin_set(PRST_PHOTO_V_PIN);
prst_adc_photo_sensor_t photo_read = prst_adc_photo_read(batt_read.voltage);
nrf_gpio_pin_clear(PRST_PHOTO_V);
nrf_gpio_pin_clear(PRST_PHOTO_V_PIN);
lux = photo_read.brightness;
#endif
prst_ble_update_adv_data(batt_read.millivolts, temp_humi.temp_millicelcius,
temp_humi.humidity, soil_read.relative,
photo_read.brightness, run_counter);
temp_humi.humidity, soil_read.relative, lux,
run_counter);
prst_adv_start();
nrf_delay_ms(PRST_BLE_ADV_TIME_IN_MS);
prst_adv_stop();

View file

@ -24,17 +24,28 @@
// Sensor data is encoded in unsigned 16 bits (2 bytes), and whenever multiple
// bytes are used to represent a single value, the encoding is big-endian:
/*
| Byte index | Description |
|------------|----------------------------------------------------------------|
| 0 | 4 bits for protocol version + 4 reserved bits |
| 1 | 4 reserved bits + 4 bits wrap-around counter for deduplication |
| 2-3 | Battery voltage in millivolts |
| 4-5 | Temperature in millidegrees Celcius |
| 6-7 | Relative air humidity, scaled from 0 (0%) to 0xffff (100%) |
| 8-9 | Soil moisture, scaled from from 0 (0%) to 0xffff (100%) |
| 10-15 | b-parasite's own MAC address |
| 16-17 | Ambient brightness level from 0 (dark) to 0xffff (bright) |
| Byte index | Description |
|------------|-----------------------------------------------------------------|
| 0 | Protocol version (4 bits) + reserved (3 bits) + has_lux* (1 bit)|
| 1 | Reserved (4 bits) + increasing, wrap-around counter (4 bits) |
| 2-3 | Battery voltage in millivolts |
| 4-5 | Temperature in millidegrees Celcius |
| 6-7 | Relative air humidity, scaled from 0 (0%) to 0xffff (100%) |
| 8-9 | Soil moisture, scaled from from 0 (0%) to 0xffff (100%) |
| 10-15 | b-parasite's own MAC address |
| 16-17* | Ambient light in lux |
* If the has_lux bit is set, bytes 16-17 shall contain the ambient light in lux.
If the has_lux bit is not set, bytes 16-17 may not exist or may contain
meaningless data. The reasons for this behavior are:
1. b-parasite version 1.0.0 has no light sensor and its advertisement data may
have only 16 bytes if its using an older firmware. In this case, has_lux shall
never be set;
2. b-parasite version 1.1.0 has space for an optional LDR. Users can configure
whether or not they have added the LDR by setting the PRST_HAS_LDR to 1 in
prst_config.h.
*/
#define SERVICE_DATA_LEN 18
static uint8_t service_data[SERVICE_DATA_LEN];
@ -99,7 +110,13 @@ static void init_advertisement_data() {
APP_ERROR_CHECK(err_code);
// Four bits for the protocol version.
service_data[0] = (PRST_BLE_PROTOCOL_VERSION << 4) & 0xf0;
service_data[0] |= (PRST_BLE_PROTOCOL_VERSION << 4) & 0xf0;
// Bit 0 of byte 0 specifies whether or not ambient light data exists in the
// payload.
#if PRST_HAS_LDR
service_data[0] |= 1;
#endif
// Parses configured MAC address from PRST_BLE_MAC_ADDR.
int mac_bytes[6];
@ -154,8 +171,10 @@ void prst_ble_update_adv_data(uint16_t batt_millivolts,
service_data[8] = soil_moisture >> 8;
service_data[9] = soil_moisture & 0xff;
#ifdef PRST_HAS_LDR
service_data[16] = brightness >> 8;
service_data[17] = brightness & 0xff;
#endif
// Encodes adv_data_ into .gap_adv_data_.
uint32_t err_code = ble_advdata_encode(