Skip to content
This repository has been archived by the owner on Feb 5, 2021. It is now read-only.

Commit

Permalink
feat: added new sensors
Browse files Browse the repository at this point in the history
Added temperature and humidity sensors for the
nRF91:Thingy. Added common library for all external sensors.
Closes #42. Board not supporting external sensors will
still publish to cloud with zeroed sensor values.

Signed-off-by: Simen S. Røstad <simen.rostad@nordicsemi.no>
  • Loading branch information
simensrostad committed May 12, 2020
1 parent 3e88b6b commit ecee5a5
Show file tree
Hide file tree
Showing 10 changed files with 435 additions and 166 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ zephyr_include_directories(src)
add_subdirectory(src/gps_controller)
add_subdirectory(src/ui)
add_subdirectory(src/cloud_codec)
add_subdirectory(src/ext_sensors)
add_subdirectory_ifdef(CONFIG_WATCHDOG src/watchdog)
17 changes: 13 additions & 4 deletions Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,22 @@ config CLOUD_RECONNECT_RETRIES

endmenu # Cloud socket poll

menu "Accelerometer"
menu "External sensors"

config ADXL362_DEV_NAME
config EXTERNAL_SENSORS
bool "Enable external sensors"
default y if BOARD_NRF9160_PCA20035NS || BOARD_NRF9160_PCA10015NS

config ACCELEROMETER_DEV_NAME
string "Accelerometer device name"
default "ADXL362"

endmenu # Accelerometer
config MULTISENSOR_DEV_NAME
string "Multisensor device name"

config ACCELEROMETER_TRIGGER
bool "Accelerometer trigger"

endmenu # External sensors

menu "Cloud codec"

Expand Down
12 changes: 10 additions & 2 deletions prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,24 @@ CONFIG_AWS_IOT_SEC_TAG=42
CONFIG_NRF9160_GPS=y
CONFIG_NRF9160_GPS_LOG_LEVEL_DBG=y

# ADXL362
# Sensor API
CONFIG_SENSOR=y

# Accelerometer
CONFIG_ACCELEROMETER_DEV_NAME="ADXL362"
CONFIG_ADXL362=y
CONFIG_SPI=y
CONFIG_SENSOR=y
CONFIG_ACCELEROMETER_TRIGGER=y
CONFIG_ADXL362_TRIGGER_GLOBAL_THREAD=y
CONFIG_ADXL362_INTERRUPT_MODE=1
CONFIG_ADXL362_ABS_REF_MODE=1
CONFIG_ADXL362_ACTIVITY_THRESHOLD=400
CONFIG_ADXL362_INACTIVITY_THRESHOLD=300

# Temperature and humidity
CONFIG_MULTISENSOR_DEV_NAME="BME680"
CONFIG_BME680=y

# Console
CONFIG_CONSOLE_SUBSYS=y
CONFIG_CONSOLE_HANDLER=y
Expand Down
79 changes: 66 additions & 13 deletions src/cloud_codec/cloud_codec.c
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ int cloud_encode_cfg_data(struct cloud_msg *output,
output->buf = buffer;
output->len = strlen(buffer);

change_gpst = false;
change_gpst = false;
change_active = false;
change_active_wait = false;
change_passive_wait = false;
Expand Down Expand Up @@ -398,6 +398,8 @@ int cloud_encode_sensor_data(struct cloud_msg *output,
cloud_data->acc_ts = 0;
cloud_data->bat_ts = 0;
cir_buf_gps->gps_ts = 0;
cloud_data->hmt_ts = 0;
cloud_data->tmp_ts = 0;
return err;
}

Expand All @@ -408,6 +410,8 @@ int cloud_encode_sensor_data(struct cloud_msg *output,
cloud_data->acc_ts = 0;
cloud_data->bat_ts = 0;
cir_buf_gps->gps_ts = 0;
cloud_data->hmt_ts = 0;
cloud_data->tmp_ts = 0;
return err;
}

Expand All @@ -418,6 +422,8 @@ int cloud_encode_sensor_data(struct cloud_msg *output,
cloud_data->acc_ts = 0;
cloud_data->bat_ts = 0;
cir_buf_gps->gps_ts = 0;
cloud_data->hmt_ts = 0;
cloud_data->tmp_ts = 0;
return err;
}

Expand All @@ -428,28 +434,57 @@ int cloud_encode_sensor_data(struct cloud_msg *output,
cloud_data->acc_ts = 0;
cloud_data->bat_ts = 0;
cir_buf_gps->gps_ts = 0;
cloud_data->hmt_ts = 0;
cloud_data->tmp_ts = 0;
return err;
}

cJSON *root_obj = cJSON_CreateObject();
cJSON *state_obj = cJSON_CreateObject();
cJSON *reported_obj = cJSON_CreateObject();
cJSON *bat_obj = cJSON_CreateObject();
cJSON *acc_obj = cJSON_CreateObject();
cJSON *acc_v_obj = cJSON_CreateObject();
cJSON *gps_obj = cJSON_CreateObject();
cJSON *gps_val_obj = cJSON_CreateObject();
err = date_time_uptime_to_unix_time_ms(&cloud_data->hmt_ts);
if (err) {
LOG_ERR("date_time_uptime_to_unix_time_ms, error: %d", err);
cloud_data->mod_ts = 0;
cloud_data->acc_ts = 0;
cloud_data->bat_ts = 0;
cir_buf_gps->gps_ts = 0;
cloud_data->hmt_ts = 0;
cloud_data->tmp_ts = 0;
return err;
}

err = date_time_uptime_to_unix_time_ms(&cloud_data->tmp_ts);
if (err) {
LOG_ERR("date_time_uptime_to_unix_time_ms, error: %d", err);
cloud_data->mod_ts = 0;
cloud_data->acc_ts = 0;
cloud_data->bat_ts = 0;
cir_buf_gps->gps_ts = 0;
cloud_data->hmt_ts = 0;
cloud_data->tmp_ts = 0;
return err;
}

cJSON *root_obj = cJSON_CreateObject();
cJSON *state_obj = cJSON_CreateObject();
cJSON *reported_obj = cJSON_CreateObject();
cJSON *bat_obj = cJSON_CreateObject();
cJSON *acc_obj = cJSON_CreateObject();
cJSON *acc_v_obj = cJSON_CreateObject();
cJSON *gps_obj = cJSON_CreateObject();
cJSON *gps_val_obj = cJSON_CreateObject();
cJSON *static_m_data = cJSON_CreateObject();
cJSON *static_m_data_v = cJSON_CreateObject();
cJSON *dynamic_m_data = cJSON_CreateObject();
cJSON *dynamic_m_data_v = cJSON_CreateObject();
cJSON *tmp_obj = cJSON_CreateObject();
cJSON *hmt_obj = cJSON_CreateObject();

if (root_obj == NULL || state_obj == NULL ||
reported_obj == NULL || gps_obj == NULL ||
bat_obj == NULL || acc_obj == NULL ||
bat_obj == NULL || acc_obj == NULL ||
gps_val_obj == NULL || static_m_data == NULL ||
dynamic_m_data == NULL || dynamic_m_data_v == NULL ||
static_m_data_v == NULL || acc_v_obj == NULL) {
static_m_data_v == NULL || acc_v_obj == NULL ||
hmt_obj == NULL || tmp_obj == NULL) {
cJSON_Delete(root_obj);
cJSON_Delete(state_obj);
cJSON_Delete(reported_obj);
Expand All @@ -462,6 +497,8 @@ int cloud_encode_sensor_data(struct cloud_msg *output,
cJSON_Delete(static_m_data_v);
cJSON_Delete(dynamic_m_data);
cJSON_Delete(dynamic_m_data_v);
cJSON_Delete(tmp_obj);
cJSON_Delete(hmt_obj);
return -ENOMEM;
}

Expand Down Expand Up @@ -501,6 +538,14 @@ int cloud_encode_sensor_data(struct cloud_msg *output,
err += json_add_obj(reported_obj, "roam", dynamic_m_data);
}

/*TMP*/
err += json_add_number(tmp_obj, "v", cloud_data->tmp);
err += json_add_number(tmp_obj, "ts", cloud_data->tmp_ts);

/*HMD*/
err += json_add_number(hmt_obj, "v", cloud_data->hmt);
err += json_add_number(hmt_obj, "ts", cloud_data->hmt_ts);

/*BAT*/
err += json_add_number(bat_obj, "v", modem_info->device.battery.value);
err += json_add_number(bat_obj, "ts", cloud_data->mod_ts);
Expand All @@ -523,26 +568,32 @@ int cloud_encode_sensor_data(struct cloud_msg *output,
/*Parameters included depending on mode and obtained gps fix*/
if (cloud_data->active && !cloud_data->gps_found) {
err += json_add_obj(reported_obj, "bat", bat_obj);
err += json_add_obj(reported_obj, "tmp", tmp_obj);
err += json_add_obj(reported_obj, "hmt", hmt_obj);
}

if (cloud_data->active && cloud_data->gps_found) {
err += json_add_obj(reported_obj, "bat", bat_obj);
err += json_add_obj(reported_obj, "tmp", tmp_obj);
err += json_add_obj(reported_obj, "hmt", hmt_obj);
err += json_add_obj(gps_obj, "v", gps_val_obj);
err += json_add_number(gps_obj, "ts", cir_buf_gps->gps_ts);
err += json_add_obj(reported_obj, "gps", gps_obj);
}

if (!cloud_data->active && !cloud_data->gps_found) {
err += json_add_obj(reported_obj, "bat", bat_obj);

err += json_add_obj(reported_obj, "tmp", tmp_obj);
err += json_add_obj(reported_obj, "hmt", hmt_obj);
if (cloud_data->acc_trig) {
err += json_add_obj(reported_obj, "acc", acc_obj);
}
}

if (!cloud_data->active && cloud_data->gps_found) {
err += json_add_obj(reported_obj, "bat", bat_obj);

err += json_add_obj(reported_obj, "tmp", tmp_obj);
err += json_add_obj(reported_obj, "hmt", hmt_obj);
if (cloud_data->acc_trig) {
err += json_add_obj(reported_obj, "acc", acc_obj);
}
Expand Down Expand Up @@ -608,6 +659,8 @@ int cloud_encode_sensor_data(struct cloud_msg *output,
cloud_data->acc_ts = 0;
cloud_data->bat_ts = 0;
cir_buf_gps->gps_ts = 0;
cloud_data->hmt_ts = 0;
cloud_data->tmp_ts = 0;

return err;
}
Expand Down
4 changes: 4 additions & 0 deletions src/cloud_codec/cloud_codec.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ struct cloud_data {
s64_t acc_ts;
s64_t mod_ts;
s64_t btn_ts;
s64_t tmp_ts;
s64_t hmt_ts;
double acc[3];
bool active;
int gps_timeout;
Expand All @@ -50,6 +52,8 @@ struct cloud_data {
int acc_thres;
int btn_number;
int rsrp;
double tmp;
double hmt;
bool gps_found;
bool acc_trig;
bool synch;
Expand Down
8 changes: 8 additions & 0 deletions src/ext_sensors/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#
# Copyright (c) 2019 Nordic Semiconductor
#
# SPDX-License-Identifier: LicenseRef-BSD-5-Clause-Nordic
#

zephyr_include_directories(.)
target_sources(app PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/ext_sensors.c)
Loading

0 comments on commit ecee5a5

Please sign in to comment.