Skip to content
This repository was archived by the owner on Jul 9, 2024. It is now read-only.

Commit 8d1f99a

Browse files
author
GitHub Actions
committed
1 parent 6cbe51a commit 8d1f99a

21 files changed

Lines changed: 638 additions & 25 deletions

doc/sensor_module.rst

Lines changed: 70 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,19 @@ Sensor types
2020

2121
The following table lists the sensors and sensor data types supported by the module:
2222

23-
+----------------------+-----------------+
24-
| Sensor data type | External device |
25-
+======================+=================+
26-
| Humidity | `BME680`_ |
27-
+----------------------+-----------------+
28-
| Temperature | `BME680`_ |
29-
+----------------------+-----------------+
30-
| Atmospheric Pressure | `BME680`_ |
31-
+----------------------+-----------------+
32-
| Acceleration | `ADXL362`_ |
33-
+----------------------+-----------------+
23+
+-------------------------+-----------------+
24+
| Sensor data type | External device |
25+
+=========================+=================+
26+
| Humidity | `BME680`_ |
27+
+-------------------------+-----------------+
28+
| Temperature | `BME680`_ |
29+
+-------------------------+-----------------+
30+
| Atmospheric Pressure | `BME680`_ |
31+
+-------------------------+-----------------+
32+
| Air Quality (BSEC only) | `BME680`_ |
33+
+-------------------------+-----------------+
34+
| Acceleration | `ADXL362`_ |
35+
+-------------------------+-----------------+
3436

3537
The module controls and collects data from the sensors by interacting with their :ref:`device drivers <device_model_api>` using :ref:`Zephyr's generic sensor API <sensor_api>`.
3638

@@ -74,6 +76,28 @@ The sensor module will only send out a :c:enum:`SENSOR_EVT_MOVEMENT_DATA_READY`
7476
This is beyond the scope of the general asset tracker framework this application provides.
7577
Therefore, the readings are not transmitted to the cloud and are only used to detect a binary active and inactive state.
7678

79+
.. _bosch_software_environmental_cluster_library:
80+
81+
Bosch Software Environmental Cluster (BSEC) library
82+
===================================================
83+
84+
The sensor module supports integration with the BSEC signal processing library using the external sensors, internal convenience API.
85+
If enabled, the BSEC library is used instead of the BME680 Zephyr driver to provide sensor readings from the BME680 for temperature, humidity, and atmospheric pressure.
86+
In addition, the BSEC driver provides an additional sensor reading, indoor air quality (IAQ), which is a metric given in between 0-500 range, that estimates the air quality of the environment.
87+
88+
As the BSEC library requires a separate license, it is not a default part of |NCS|, but can be downloaded externally and imported into the |NCS| source tree.
89+
90+
Perform the following steps to enable BSEC:
91+
92+
1. Download the BSEC library, using the `Bosch BSEC`_ link.
93+
#. Extract and store the folder containing the library contents in the path specified by :ref:`CONFIG_EXTERNAL_SENSORS_BME680_BSEC_PATH <CONFIG_EXTERNAL_SENSORS_BME680_BSEC_PATH>` option or update the path configuration to reference the library location.
94+
#. Disable the Zephyr BME680 driver by setting :kconfig:`CONFIG_BME680` to false.
95+
#. Enable the external sensors API BSEC integration layer by enabling :ref:`CONFIG_EXTERNAL_SENSORS_BME680_BSEC <CONFIG_EXTERNAL_SENSORS_BME680_BSEC>` option.
96+
97+
Air quality readings are provided with the :c:enumerator:`SENSOR_EVT_ENVIRONMENTAL_DATA_READY` event.
98+
99+
To check and configure the BSEC configuration options, see :ref:`external_sensor_API_BSEC_configurations` section.
100+
77101
Configuration options
78102
*********************
79103

@@ -82,6 +106,41 @@ Configuration options
82106
CONFIG_SENSOR_THREAD_STACK_SIZE - Sensor module thread stack size
83107
This option configures the sensor module's internal thread stack size.
84108

109+
.. _external_sensor_API_BSEC_configurations:
110+
111+
External sensors API BSEC configurations
112+
========================================
113+
114+
.. _CONFIG_EXTERNAL_SENSORS_BME680_BSEC:
115+
116+
CONFIG_EXTERNAL_SENSORS_BME680_BSEC
117+
This option configures the Bosch BSEC library for the BME680.
118+
119+
.. _CONFIG_EXTERNAL_SENSORS_BME680_BSEC_PATH:
120+
121+
CONFIG_EXTERNAL_SENSORS_BME680_BSEC_PATH
122+
This option sets the path for the Bosch BSEC library folder.
123+
124+
.. _CONFIG_EXTERNAL_SENSORS_BSEC_SAMPLE_MODE_ULTRA_LOW_POWER:
125+
126+
CONFIG_EXTERNAL_SENSORS_BSEC_SAMPLE_MODE_ULTRA_LOW_POWER
127+
This option configures the BSEC ultra Low Power Mode. In this mode, the BME680 is sampled every 300 seconds.
128+
129+
.. _CONFIG_EXTERNAL_SENSORS_BSEC_SAMPLE_MODE_LOW_POWER:
130+
131+
CONFIG_EXTERNAL_SENSORS_BSEC_SAMPLE_MODE_LOW_POWER
132+
This option configures BSEC Low Power Mode. In this mode, the BME680 is sampled every 3 seconds.
133+
134+
.. _CONFIG_EXTERNAL_SENSORS_BSEC_SAMPLE_MODE_CONTINUOUS:
135+
136+
CONFIG_EXTERNAL_SENSORS_BSEC_SAMPLE_MODE_CONTINUOUS
137+
This option configures BSEC continuous Mode. In this mode, the BME680 is sampled every second.
138+
139+
.. _CONFIG_EXTERNAL_SENSORS_BSEC_TEMPERATURE_OFFSET:
140+
141+
CONFIG_EXTERNAL_SENSORS_BSEC_TEMPERATURE_OFFSET
142+
This option configures BSEC temperature offset in degree celsius multiplied by 100.
143+
85144
Module states
86145
*************
87146

src/cloud/cloud_codec/aws_iot/json_protocol_names_aws_iot.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#define DATA_TEMPERATURE "temp"
3737
#define DATA_HUMIDITY "hum"
3838
#define DATA_PRESSURE "atmp"
39+
#define DATA_BSEC_IAQ "bsec_iaq"
3940
#define DATA_ENVIRONMENTALS "env"
4041
#define DATA_BUTTON "btn"
4142
#define DATA_CONFIG "cfg"

src/cloud/cloud_codec/azure_iot_hub/json_protocol_names_azure_iot_hub.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#define DATA_TEMPERATURE "temp"
3737
#define DATA_HUMIDITY "hum"
3838
#define DATA_PRESSURE "atmp"
39+
#define DATA_BSEC_IAQ "bsec_iaq"
3940
#define DATA_ENVIRONMENTALS "env"
4041
#define DATA_BUTTON "btn"
4142
#define DATA_CONFIG "cfg"

src/cloud/cloud_codec/cloud_codec.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ struct cloud_data_sensors {
129129
double humidity;
130130
/** Atmospheric pressure in kilopascal. */
131131
double pressure;
132+
/** BSEC Air quality in Indoor-Air-Quality (IAQ) index.
133+
* If -1, the value is not provided.
134+
*/
135+
int bsec_air_quality;
132136
/** Flag signifying that the data entry is to be encoded. */
133137
bool queued : 1;
134138
};

src/cloud/cloud_codec/json_common.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,15 @@ int json_common_sensor_data_add(cJSON *parent,
315315
goto exit;
316316
}
317317

318+
/* If air quality is negative, the value is not provided. */
319+
if (data->bsec_air_quality >= 0) {
320+
err = json_add_number(sensor_val_obj, DATA_BSEC_IAQ, data->bsec_air_quality);
321+
if (err) {
322+
LOG_ERR("Encoding error: %d returned at %s:%d", err, __FILE__, __LINE__);
323+
goto exit;
324+
}
325+
}
326+
318327
json_add_obj(sensor_obj, DATA_VALUE, sensor_val_obj);
319328

320329
err = json_add_number(sensor_obj, DATA_TIMESTAMP, data->env_ts);

src/cloud/cloud_codec/nrf_cloud/json_protocol_names_nrf_cloud.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#define DATA_TEMPERATURE "temp"
2424
#define DATA_HUMIDITY "hum"
2525
#define DATA_PRESSURE "atmp"
26+
#define DATA_BSEC_IAQ "bsec_iaq"
2627
#define DATA_ENVIRONMENTALS "env"
2728
#define DATA_BUTTON "btn"
2829
#define DATA_CONFIG "config"
@@ -42,6 +43,7 @@
4243
#define APP_ID_GPS "GPS"
4344
#define APP_ID_HUMIDITY "HUMID"
4445
#define APP_ID_AIR_PRESS "AIR_PRESS"
46+
#define APP_ID_AIR_QUAL "AIR_QUAL"
4547
#define APP_ID_TEMPERATURE "TEMP"
4648
#define APP_ID_RSRP "RSRP"
4749
#define APP_ID_CELL_POS "CELL_POS"

src/cloud/cloud_codec/nrf_cloud/nrf_cloud_codec.c

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,10 @@ static int add_batch_data(cJSON *array, enum batch_data_type type, void *buf, si
165165
}
166166
case ENVIRONMENTALS: {
167167
int err, len;
168-
char humidity[7];
169-
char temperature[7];
170-
char pressure[7];
168+
char humidity[10];
169+
char temperature[10];
170+
char pressure[10];
171+
char bsec_air_quality[4];
171172
struct cloud_data_sensors *data = (struct cloud_data_sensors *)buf;
172173

173174
if (data[i].queued == false) {
@@ -184,21 +185,33 @@ static int add_batch_data(cJSON *array, enum batch_data_type type, void *buf, si
184185
data[i].humidity);
185186
if ((len < 0) || (len >= sizeof(humidity))) {
186187
LOG_ERR("Cannot convert humidity to string, buffer too small");
187-
return -ENOMEM;
188188
}
189189

190190
len = snprintk(temperature, sizeof(temperature), "%.2f",
191191
data[i].temperature);
192192
if ((len < 0) || (len >= sizeof(temperature))) {
193193
LOG_ERR("Cannot convert temperature to string, buffer too small");
194-
return -ENOMEM;
195194
}
196195

197196
len = snprintk(pressure, sizeof(pressure), "%.2f",
198197
data[i].pressure);
199198
if ((len < 0) || (len >= sizeof(pressure))) {
200199
LOG_ERR("Cannot convert pressure to string, buffer too small");
201-
return -ENOMEM;
200+
}
201+
202+
if (data[i].bsec_air_quality >= 0) {
203+
len = snprintk(bsec_air_quality, sizeof(bsec_air_quality), "%d",
204+
data[i].bsec_air_quality);
205+
if ((len < 0) || (len >= sizeof(bsec_air_quality))) {
206+
LOG_ERR("Cannot convert BSEC air quality to string, "
207+
"buffer too small");
208+
}
209+
210+
err = add_data(array, NULL, APP_ID_AIR_QUAL, bsec_air_quality,
211+
&data[i].env_ts, data[i].queued, NULL, false);
212+
if (err && err != -ENODATA) {
213+
return err;
214+
}
202215
}
203216

204217
err = add_data(array, NULL, APP_ID_HUMIDITY, humidity,

src/cloud/nrf_cloud_integration.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ static int send_service_info(void)
6868
#if defined(CONFIG_BOARD_THINGY91_NRF9160_NS)
6969
.humidity = true,
7070
.air_pressure = true,
71+
.air_quality = true,
7172
.temperature = true,
7273
#endif
7374
.rsrp = true,

src/events/sensor_module_event.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ struct sensor_module_data {
5959
double humidity;
6060
/** Atmospheric pressure in kilopascal. */
6161
double pressure;
62+
/** BSEC air quality in Indoor-Air-Quality (IAQ) index.
63+
* If -1, the value is not provided.
64+
*/
65+
int bsec_air_quality;
6266
};
6367

6468
/** @brief Structure used to provide acceleration data. */

src/ext_sensors/CMakeLists.txt

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,29 @@
55
#
66

77
target_include_directories(app PRIVATE .)
8-
target_sources_ifdef(CONFIG_EXTERNAL_SENSORS app PRIVATE
9-
${CMAKE_CURRENT_SOURCE_DIR}/ext_sensors.c)
8+
target_sources_ifdef(CONFIG_EXTERNAL_SENSORS app PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/ext_sensors.c)
9+
10+
if (CONFIG_EXTERNAL_SENSORS_BME680_BSEC)
11+
target_sources(app PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/ext_sensors_bsec.c)
12+
set(bsec_dir ${CONFIG_EXTERNAL_SENSORS_BME680_BSEC_PATH})
13+
set(BSEC_LIB_DIR ${bsec_dir}/algo/normal_version/bin/gcc/Cortex_M33F)
14+
15+
if(NOT EXISTS "${BSEC_LIB_DIR}/libalgobsec.a")
16+
assert(0 "Could not find BSEC library. Minimum version is 1.4.8.0_Generic_Release")
17+
endif()
18+
19+
target_include_directories(app PRIVATE ${bsec_dir}/examples/bsec_iot_example)
20+
target_include_directories(app PRIVATE ${BSEC_LIB_DIR})
21+
22+
target_sources(app PRIVATE ${bsec_dir}/examples/bsec_iot_example/bsec_integration.c)
23+
target_sources(app PRIVATE ${bsec_dir}/examples/bsec_iot_example/bme680.c)
24+
25+
add_library(bsec_lib STATIC IMPORTED GLOBAL)
26+
add_dependencies(bsec_lib math_lib bsec_target)
27+
28+
set_target_properties(bsec_lib PROPERTIES IMPORTED_LOCATION "${BSEC_LIB_DIR}/libalgobsec.a")
29+
set_target_properties(bsec_lib PROPERTIES INTERFACE_INCLUDE_DIRECTORIES ${BSEC_LIB_DIR})
30+
31+
target_link_libraries(bsec_lib INTERFACE -L${LIBC_LIBRARY_DIR})
32+
target_link_libraries(app PUBLIC bsec_lib)
33+
endif()

0 commit comments

Comments
 (0)