Skip to content

Commit ed6ca45

Browse files
committed
modules: cloud: Move environmental data handling to new file
Move the environemntal data handling to a separate file that is only included if CONFIG_APP_ENVIRONMENTAL is enabled. Signed-off-by: Jan Tore Guggedal <jantore.guggedal@nordicsemi.no>
1 parent 1a618d2 commit ed6ca45

5 files changed

Lines changed: 92 additions & 32 deletions

File tree

app/src/modules/cloud/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
target_sources(app PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/cloud.c)
88
target_sources(app PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/cloud_provisioning.c)
99
target_sources_ifdef(CONFIG_APP_LOCATION app PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/cloud_location.c)
10+
target_sources_ifdef(CONFIG_APP_ENVIRONMENTAL app PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/cloud_environmental.c)
1011
target_sources_ifdef(CONFIG_APP_CLOUD_SHELL app PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/cloud_shell.c)
1112
target_include_directories(app PRIVATE .)
1213

app/src/modules/cloud/cloud.c

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "cloud_internal.h"
2828
#include "cloud_provisioning.h"
2929
#include "cloud_location.h"
30+
#include "cloud_environmental.h"
3031
#include "app_common.h"
3132
#include "network.h"
3233
#include "storage.h"
@@ -339,7 +340,6 @@ static void send_request_failed(void)
339340
}
340341
}
341342

342-
343343
#if defined(CONFIG_APP_NETWORK)
344344
static void handle_network_data_message(const struct network_msg *msg)
345345
{
@@ -413,37 +413,7 @@ static int send_storage_data_to_cloud(const struct storage_data_item *item)
413413
if (item->type == STORAGE_TYPE_ENVIRONMENTAL) {
414414
const struct environmental_msg *env = &item->data.ENVIRONMENTAL;
415415

416-
err = nrf_cloud_coap_sensor_send(NRF_CLOUD_JSON_APPID_VAL_TEMP,
417-
env->temperature,
418-
timestamp_ms,
419-
confirmable);
420-
if (err) {
421-
LOG_ERR("Failed to send temperature data to cloud, error: %d", err);
422-
return err;
423-
}
424-
425-
err = nrf_cloud_coap_sensor_send(NRF_CLOUD_JSON_APPID_VAL_AIR_PRESS,
426-
env->pressure,
427-
timestamp_ms,
428-
confirmable);
429-
if (err) {
430-
LOG_ERR("Failed to send pressure data to cloud, error: %d", err);
431-
return err;
432-
}
433-
434-
err = nrf_cloud_coap_sensor_send(NRF_CLOUD_JSON_APPID_VAL_HUMID,
435-
env->humidity,
436-
timestamp_ms,
437-
confirmable);
438-
if (err) {
439-
LOG_ERR("Failed to send humidity data to cloud, error: %d", err);
440-
return err;
441-
}
442-
443-
LOG_DBG("Environmental data sent to cloud: T=%.1f°C, P=%.1fhPa, H=%.1f%%",
444-
(double)env->temperature, (double)env->pressure, (double)env->humidity);
445-
446-
return 0;
416+
return cloud_environmental_send(env, timestamp_ms, confirmable);
447417
}
448418
#endif /* CONFIG_APP_ENVIRONMENTAL */
449419

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright (c) 2025 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
*/
6+
7+
#include <zephyr/kernel.h>
8+
#include <zephyr/logging/log.h>
9+
#include <net/nrf_cloud_coap.h>
10+
11+
#include "cloud_environmental.h"
12+
13+
LOG_MODULE_DECLARE(cloud, CONFIG_APP_CLOUD_LOG_LEVEL);
14+
15+
int cloud_environmental_send(const struct environmental_msg *env,
16+
int64_t timestamp_ms,
17+
bool confirmable)
18+
{
19+
int err;
20+
21+
err = nrf_cloud_coap_sensor_send(NRF_CLOUD_JSON_APPID_VAL_TEMP,
22+
env->temperature,
23+
timestamp_ms,
24+
confirmable);
25+
if (err) {
26+
LOG_ERR("Failed to send temperature data to cloud, error: %d", err);
27+
return err;
28+
}
29+
30+
err = nrf_cloud_coap_sensor_send(NRF_CLOUD_JSON_APPID_VAL_AIR_PRESS,
31+
env->pressure,
32+
timestamp_ms,
33+
confirmable);
34+
if (err) {
35+
LOG_ERR("Failed to send pressure data to cloud, error: %d", err);
36+
return err;
37+
}
38+
39+
err = nrf_cloud_coap_sensor_send(NRF_CLOUD_JSON_APPID_VAL_HUMID,
40+
env->humidity,
41+
timestamp_ms,
42+
confirmable);
43+
if (err) {
44+
LOG_ERR("Failed to send humidity data to cloud, error: %d", err);
45+
return err;
46+
}
47+
48+
LOG_DBG("Environmental data sent to cloud: T=%.1f°C, P=%.1fhPa, H=%.1f%%",
49+
(double)env->temperature, (double)env->pressure, (double)env->humidity);
50+
51+
return 0;
52+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (c) 2025 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
*/
6+
7+
#ifndef _CLOUD_ENVIRONMENTAL_H_
8+
#define _CLOUD_ENVIRONMENTAL_H_
9+
10+
#include <stdint.h>
11+
#include "environmental.h"
12+
13+
#ifdef __cplusplus
14+
extern "C" {
15+
#endif
16+
17+
/**
18+
* @brief Send environmental data to the cloud.
19+
*
20+
* Sends temperature, pressure, and humidity data to nRF Cloud.
21+
*
22+
* @param env Pointer to environmental message containing sensor data
23+
* @param timestamp_ms Timestamp in milliseconds, or NRF_CLOUD_NO_TIMESTAMP
24+
* @param confirmable Whether to use confirmable CoAP messages
25+
*
26+
* @return 0 on success, negative error code on failure
27+
*/
28+
int cloud_environmental_send(const struct environmental_msg *env,
29+
int64_t timestamp_ms,
30+
bool confirmable);
31+
32+
#ifdef __cplusplus
33+
}
34+
#endif
35+
36+
#endif /* _CLOUD_ENVIRONMENTAL_H_ */

tests/module/cloud/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ target_sources(app
1717
../../../app/src/modules/cloud/cloud.c
1818
../../../app/src/modules/cloud/cloud_provisioning.c
1919
../../../app/src/modules/cloud/cloud_location.c
20+
../../../app/src/modules/cloud/cloud_environmental.c
2021
)
2122

2223
zephyr_include_directories(${ZEPHYR_BASE}/include/zephyr/)

0 commit comments

Comments
 (0)