Skip to content

Commit 9aa1c5c

Browse files
committed
add support for MQTT
This commit adds support for MQTT in the cloud module. Signed-off-by: Simen S. Røstad <simen.rostad@nordicsemi.no>
1 parent 0b9ab69 commit 9aa1c5c

File tree

15 files changed

+1024
-11
lines changed

15 files changed

+1024
-11
lines changed

CMakeLists.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,4 @@
44
# This CMake file is picked by the Zephyr build system because it is defined
55
# as the module CMake entry point (see zephyr/module.yml).
66

7-
8-
zephyr_include_directories(include)
9-
10-
add_subdirectory(drivers)
7+
add_subdirectory(examples/modules/cloud)

Kconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Copyright (c) 2025 Nordic Semiconductor ASA
2+
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
3+
#
4+
# This Kconfig file is picked by the Zephyr build system because it is defined
5+
# as the module Kconfig entry point (see zephyr/module.yml). You can browse
6+
# module options by going to Zephyr -> Modules in Kconfig.
7+
8+
rsource "examples/modules/cloud/Kconfig.cloud_mqtt"

app/CMakeLists.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ target_include_directories(app PRIVATE src/common)
1515
# Add main application source
1616
target_sources(app PRIVATE src/main.c)
1717

18-
# Include mandatory module source folders
18+
# Module source folders
19+
add_subdirectory(src/modules/location)
20+
add_subdirectory(src/modules/cloud)
21+
add_subdirectory(src/modules/fota)
1922
add_subdirectory(src/modules/network)
2023
add_subdirectory(src/modules/button)
2124
add_subdirectory(src/cbor)
@@ -24,6 +27,3 @@ add_subdirectory(src/cbor)
2427
add_subdirectory_ifdef(CONFIG_APP_POWER src/modules/power)
2528
add_subdirectory_ifdef(CONFIG_APP_ENVIRONMENTAL src/modules/environmental)
2629
add_subdirectory_ifdef(CONFIG_APP_LED src/modules/led)
27-
add_subdirectory_ifdef(CONFIG_APP_LOCATION src/modules/location)
28-
add_subdirectory_ifdef(CONFIG_APP_CLOUD src/modules/cloud)
29-
add_subdirectory_ifdef(CONFIG_APP_FOTA src/modules/fota)

app/prj.conf

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,3 +226,30 @@ CONFIG_TASK_WDT_MIN_TIMEOUT=10000
226226
# Device power management
227227
CONFIG_PM_DEVICE=y
228228
CONFIG_PM_DEVICE_SHELL=y
229+
230+
# Disable nRF Cloud, this will automatically patch out the template's Cloud and FOTA modules
231+
# as they depend on nRF Cloud CoAP.
232+
CONFIG_NRF_CLOUD=n
233+
# Disable LOCATION library, this will patch out the template's Location module
234+
# as it depend on on nRF Cloud CoAP.
235+
CONFIG_LOCATION=n
236+
237+
## Patch in the Cloud MQTT module and the MQTT stack and the example MQTT application module
238+
CONFIG_APP_CLOUD_MQTT=y
239+
CONFIG_APP_CLOUD_MQTT_LOG_LEVEL_DBG=y
240+
CONFIG_MQTT_HELPER=y
241+
CONFIG_MQTT_HELPER_LOG_LEVEL_DBG=y
242+
CONFIG_MQTT_LIB_TLS=y
243+
CONFIG_MQTT_CLEAN_SESSION=y
244+
CONFIG_MQTT_HELPER_PORT=8883
245+
CONFIG_MQTT_KEEPALIVE=30
246+
CONFIG_MQTT_HELPER_SEC_TAG=888
247+
248+
## Include Modem Key Mangement to provision credentials to the modem prior
249+
## to establishing a connection.
250+
CONFIG_MODEM_KEY_MGMT=y
251+
252+
## Enable the HW ID library, used to retrive modem UUID which is used as the MQTT device ID for
253+
## unique identification of the device.
254+
CONFIG_HW_ID_LIBRARY=y
255+
CONFIG_HW_ID_LIBRARY_SOURCE_IMEI=y

app/src/modules/cloud/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
55
#
66

7-
target_sources(app PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/cloud.c)
7+
target_sources_ifdef(CONFIG_APP_CLOUD app PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/cloud.c)
88
target_sources_ifdef(CONFIG_APP_CLOUD_SHELL app PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/cloud_shell.c)
99
target_include_directories(app PRIVATE .)
1010

app/src/modules/fota/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
55
#
66

7-
target_sources(app PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/fota.c)
7+
target_sources_ifdef(CONFIG_APP_FOTA app PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/fota.c)
88
target_include_directories(app PRIVATE .)

app/src/modules/location/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
55
#
66

7-
target_sources(app PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/location.c)
7+
target_sources_ifdef(CONFIG_APP_LOCATION app PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/location.c)
88
target_include_directories(app PRIVATE .)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#
2+
# Copyright (c) 2025 Nordic Semiconductor ASA
3+
#
4+
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
#
6+
7+
# Prevent this example module from being included in images that doesn't enable the MQTT cloud module
8+
if (NOT CONFIG_APP_CLOUD_MQTT)
9+
return()
10+
endif()
11+
12+
add_subdirectory(creds)
13+
14+
zephyr_sources(${CMAKE_CURRENT_SOURCE_DIR}/cloud_mqtt.c)
15+
zephyr_sources(${CMAKE_CURRENT_SOURCE_DIR}/cloud_mqtt_shell.c)
16+
17+
if (CONFIG_NRF_CLOUD_COAP_SEC_TAG GREATER_EQUAL 2147483648 AND CONFIG_NRF_CLOUD_COAP_SEC_TAG LESS_EQUAL 2147483667)
18+
message(WARNING "CONFIG_NRF_CLOUD_COAP_SEC_TAG is set to a developer security tag. "
19+
"TLS traffic can now be decrypted with Nordic tools. "
20+
"This should only be used during development and testing.")
21+
endif()
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
#
2+
# Copyright (c) 2025 Nordic Semiconductor
3+
#
4+
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
#
6+
7+
menuconfig APP_CLOUD_MQTT
8+
bool "Cloud module"
9+
depends on MQTT_HELPER
10+
depends on HW_ID_LIBRARY
11+
12+
if APP_CLOUD_MQTT
13+
14+
config APP_CLOUD_MQTT_SEC_TAG
15+
int "MQTT security tag"
16+
default 888
17+
help
18+
Security tag used to store the MQTT credentials in the modem using Modem Key Management API
19+
20+
config APP_CLOUD_MQTT_SHELL
21+
bool "Enable cloud shell"
22+
default y if SHELL
23+
help
24+
Enable cloud shell commands.
25+
26+
config APP_CLOUD_PAYLOAD_BUFFER_MAX_SIZE
27+
int "Payload maximum buffer size"
28+
default 128
29+
help
30+
Maximum size of the buffer sent over the payload channel when sending RAW JSON messages
31+
to the cloud.
32+
33+
config APP_CLOUD_SHADOW_RESPONSE_BUFFER_MAX_SIZE
34+
int "Payload maximum buffer size"
35+
default 512
36+
help
37+
Maximum size of the buffer used to receive shadow responses from the cloud.
38+
39+
config APP_CLOUD_BACKOFF_INITIAL_SECONDS
40+
int "Reconnection backoff time in seconds"
41+
default 60
42+
help
43+
Time in between reconnection attempts to the MQTT server.
44+
The timer starts after the last failed attempt.
45+
46+
choice APP_CLOUD_BACKOFF_TYPE
47+
prompt "Reconnection backoff type"
48+
default APP_CLOUD_BACKOFF_TYPE_LINEAR
49+
50+
config APP_CLOUD_BACKOFF_TYPE_EXPONENTIAL
51+
bool "Exponential backoff"
52+
help
53+
Exponential backoff doubles the reconnection timeout after each failed attempt.
54+
The maximum reconnection timeout is defined by APP_CLOUD_BACKOFF_MAX_SECONDS.
55+
56+
config APP_CLOUD_BACKOFF_TYPE_LINEAR
57+
bool "Linear backoff"
58+
help
59+
Linear backoff adds a fixed amount of time to the reconnection timeout after each failed attempt,
60+
as defined by APP_CLOUD_BACKOFF_LINEAR_INCREMENT_SECONDS.
61+
62+
config APP_CLOUD_BACKOFF_TYPE_NONE
63+
bool "No backoff"
64+
help
65+
No backoff means that the reconnection timeout is constant at the value defined by
66+
APP_CLOUD_BACKOFF_INITIAL_SECONDS.
67+
68+
endchoice
69+
70+
config APP_CLOUD_BACKOFF_LINEAR_INCREMENT_SECONDS
71+
int "Reconnection backoff time increment"
72+
default 60
73+
help
74+
Time added to the reconnection timeout after each failed attempt in seconds.
75+
The maximum reconnection timeout is defined by APP_CLOUD_BACKOFF_MAX_SECONDS.
76+
77+
config APP_CLOUD_BACKOFF_MAX_SECONDS
78+
int "Maximum reconnection timeout"
79+
default 3600
80+
help
81+
Maximum reconnection backoff value in seconds.
82+
83+
config APP_CLOUD_THREAD_STACK_SIZE
84+
int "Thread stack size"
85+
default 3328
86+
87+
config APP_CLOUD_MESSAGE_QUEUE_SIZE
88+
int "Message queue size"
89+
default 5
90+
help
91+
ZBus subscriber message queue size.
92+
93+
config APP_CLOUD_WATCHDOG_TIMEOUT_SECONDS
94+
int "Watchdog timeout"
95+
default 180
96+
help
97+
Timeout in seconds for the cloud module watchdog.
98+
The timeout given in this option covers both:
99+
* Waiting for an incoming message in zbus_sub_wait_msg().
100+
* Time spent processing the message, defined by
101+
CONFIG_APP_CLOUD_MSG_PROCESSING_TIMEOUT_SECONDS.
102+
Ensure that this value exceeds CONFIG_APP_CLOUD_MSG_PROCESSING_TIMEOUT_SECONDS.
103+
A small difference between the two can mean more frequent watchdog feeds, which increases
104+
power consumption.
105+
106+
107+
config APP_CLOUD_MSG_PROCESSING_TIMEOUT_SECONDS
108+
int "Maximum message processing time"
109+
default 120
110+
help
111+
Maximum time allowed for processing a single message in the module's state machine.
112+
The value must be smaller than CONFIG_APP_CLOUD_WATCHDOG_TIMEOUT_SECONDS.
113+
114+
module = APP_CLOUD_MQTT
115+
module-str = Cloud MQTT
116+
source "subsys/logging/Kconfig.template.log_config"
117+
118+
endif # APP_CLOUD_MQTT

0 commit comments

Comments
 (0)