Skip to content

Commit 37d0d86

Browse files
committed
modules: sensor: add configurable UTC timestamps to sensor data
Add CONFIG_SENSOR_MODULE_TIMESTAMP option to conditionally include UTC epoch timestamps in sensor messages using date_time_now() instead of k_uptime_get(). Update controller logging to display human-readable timestamp format. Refs: #21 Signed-off-by: Natalia Pluta <pluta.natalia.m@gmail.com>
1 parent 771f54e commit 37d0d86

4 files changed

Lines changed: 28 additions & 3 deletions

File tree

app/src/modules/controller/controller_module.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,10 @@ static void log_sensor_data(const struct sensor_msg *msg)
359359
LOG_INF("SEN0466: CO: %d ppm, Temp: %d.%06d C", msg->co.val1, msg->temperature_sen0466.val1,
360360
msg->temperature_sen0466.val2);
361361

362-
LOG_INF("Sensor data timestamp: %lld ms", msg->timestamp);
362+
#if defined(CONFIG_SENSOR_MODULE_TIMESTAMP)
363+
/* Log timestamp if enabled */
364+
LOG_INF("Sensor data timestamp: %lld seconds since epoch (UTC)", msg->timestamp);
365+
#endif
363366
}
364367

365368
/* Helper function to set state and keep current_state field in sync */

app/src/modules/sensor/Kconfig.sensor

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@ config SENSOR_MODULE_MAX_RETRIES
4444
help
4545
Maximum number of recovery attempts before giving up.
4646

47+
config SENSOR_MODULE_TIMESTAMP
48+
bool "Include timestamps in sensor data"
49+
depends on DATE_TIME
50+
default y
51+
help
52+
Enable inclusion of timestamps in sensor messages sent via the sensor_chan ZBUS channel.
53+
The timestamp format is UTC, which is seconds since the epoch (January 1, 1970, 00:00:00 UTC),
54+
as provided by date_time_now().
55+
4756
# Sensor-specific warmup configurations
4857
config SENSOR_MODULE_WARMUP_ENABLE
4958
bool "Enable sensor warmup periods"

app/src/modules/sensor/sensor_module.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
#include <zephyr/logging/log.h>
1010
#include <zephyr/smf.h>
1111

12+
#if defined(CONFIG_SENSOR_MODULE_TIMESTAMP)
13+
#include <date_time.h>
14+
#endif
15+
1216
LOG_MODULE_REGISTER(sensor_module, CONFIG_SENSOR_MODULE_LOG_LEVEL);
1317

1418
/* Macro to validate sensor type enum values */
@@ -311,8 +315,15 @@ static void sensor_state_reading_run(void *obj)
311315

312316
/* Initialize response */
313317
ctx->current_data.type = SENSOR_SAMPLE_RESPONSE;
314-
ctx->current_data.timestamp = k_uptime_get();
315-
ctx->last_read_time = ctx->current_data.timestamp;
318+
ctx->last_read_time = k_uptime_get();
319+
#if defined(CONFIG_SENSOR_MODULE_TIMESTAMP)
320+
ret = date_time_now(&ctx->current_data.timestamp);
321+
if (ret < 0) {
322+
LOG_ERR("Sensor SM: Failed to get current timestamp (%d)", ret);
323+
LOG_WRN("Sensor SM: Setting timestamp to 0");
324+
ctx->current_data.timestamp = 0;
325+
}
326+
#endif
316327

317328
int successful_reads = 0;
318329

app/src/modules/sensor/sensor_module.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,10 @@ struct sensor_msg {
6969
struct sensor_value co;
7070
struct sensor_value temperature_sen0466;
7171

72+
#if defined(CONFIG_SENSOR_MODULE_TIMESTAMP)
7273
/* Timestamp - only valid for SENSOR_SAMPLE_RESPONSE */
7374
int64_t timestamp;
75+
#endif
7476
};
7577

7678
/* ZBUS channel declaration */

0 commit comments

Comments
 (0)