Skip to content

Commit e0d2645

Browse files
authored
Add Memfault demo (#11)
1 parent 118ebd2 commit e0d2645

26 files changed

+1278
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ Follow this guide to:
2222
* Build, flash, and run Secure CoAP Application.
2323

2424
### Prerequisites
25-
* [nRF Connect SDK (v2.2.0)](https://developer.nordicsemi.com/nRF_Connect_SDK/doc/2.2.0/nrf/gs_assistant.html)
25+
* For Memfault demo: [nRF Connect SDK (v2.8.0)](https://docs.nordicsemi.com/bundle/ncs-2.8.0/page/nrf/gsg_guides.html)
26+
* For Other demos: [nRF Connect SDK (v2.2.0)](https://developer.nordicsemi.com/nRF_Connect_SDK/doc/2.2.0/nrf/gs_assistant.html)
2627
* [VS Code)](https://code.visualstudio.com/)
2728
* [West](https://docs.zephyrproject.org/3.1.0/develop/west/install.html)
2829

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#
2+
# Copyright (c) 2024 1NCE
3+
#
4+
5+
cmake_minimum_required(VERSION 3.20.0)
6+
7+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
8+
project(nce_debug_memfault_demo)
9+
10+
# NORDIC SDK APP START
11+
add_subdirectory(src)
12+
# NORDIC SDK APP END
13+
14+
zephyr_include_directories(src)
15+
zephyr_include_directories(config)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#
2+
# Copyright (c) 2024 1NCE
3+
#
4+
5+
menu "Zephyr Kernel"
6+
source "Kconfig.zephyr"
7+
endmenu
8+
9+
config NCE_MEMFAULT_DEMO_PERIODIC_UPDATE
10+
bool "Enable periodic Memfault updates"
11+
default y
12+
13+
config NCE_MEMFAULT_DEMO_PERIODIC_UPDATE_FREQUENCY_SECONDS
14+
int "Update frequency (in seconds)"
15+
depends on NCE_MEMFAULT_DEMO_PERIODIC_UPDATE
16+
default 30
17+
18+
config NCE_MEMFAULT_DEMO_CONNECTIVITY_METRICS
19+
bool "Collect Additional connectivity metrics"
20+
default y
21+
22+
config NCE_MEMFAULT_DEMO_COAP_SYNC_METRICS
23+
bool "Enable sync success/failure metrics"
24+
default y
25+
26+
config NCE_MEMFAULT_DEMO_PRINT_HEARTBEAT_METRICS
27+
bool "Print the collected heartbeat metrics"
28+
default y
29+
30+
config NCE_MEMFAULT_DEMO_DISCONNECT_DURATION_SECONDS
31+
int "Disconnect command duration (in seconds)"
32+
default 20
33+
34+
config NCE_MEMFAULT_DEMO_ENABLE_DTLS
35+
bool "Enable DTLS for CoAP Proxy communication"
36+
default n
37+
38+
module = NCE_MEMFAULT_DEMO
39+
module-str = NCE Memfault Demo
40+
source "${ZEPHYR_BASE}/subsys/logging/Kconfig.template.log_config"
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
# 1NCE Zephyr blueprint - 1NCE Memfault Demo
2+
3+
## Overview
4+
5+
This demo code showcases how to send Memfault diagnostics data from a Zephyr-based device over CoAP (Constrained Application Protocol) using 1NCE CoAP Proxy. \
6+
This is useful for IoT devices where periodic error reporting and fault diagnostics are critical.\
7+
The communication can be secured using DTLS (Datagram Transport Layer Security) depending on configuration.
8+
9+
To install Memfault plugin, follow the instructions in [Memfault Plugin dev-hub documentation](https://help.1nce.com/dev-hub/docs/1nce-os-plugins-device-observability-memfault).
10+
11+
This demo requires [nRF Connect SDK (v2.8.0)](https://docs.nordicsemi.com/bundle/ncs-2.8.0/page/nrf/gsg_guides.html)
12+
13+
For `Thingy:91`, the LED colors are used to indicate the following:
14+
15+
- `BLUE:` A network connection has been successfully established.
16+
- `GREEN:` Memfault data was successfully sent via the 1NCE CoAP Proxy.
17+
- `RED:` Failed to send Memfault data.
18+
19+
## Running the demo
20+
21+
1. Build the demo for `nrf9160dk_nrf9160_ns` or `thingy91_nrf9160_ns` and flash it.
22+
* Note: On Windows, file path length limitations may cause errors during the build process. To resolve this, consider moving the demo folder to a directory with a shorter path (e.g., `C:\dev`).
23+
* For `nrf9160dk_nrf9160_ns`, the demo can be flashed directly from VS code using the "Flash" option in nrf Connect tab.
24+
* For `thingy91_nrf9160_ns`, the file `build/nce_debug_memfault_demo/zephyr/zephyr.signed.hex` should be flashed using the programmer app from [nrf Connect for Desktop](https://www.nordicsemi.com/Products/Development-tools/nRF-Connect-for-Desktop).
25+
26+
2. Upload the symbol file `build/nce_debug_memfault_demo/zephyr/zephyr.elf` to Memfault (Symbol files tab > Upload symbol file). This file is required by Memfault to process the metrics sent from the device. For more information about symbol files, refer to the documentation [here](https://docs.memfault.com/docs/mcu/symbol-file-build-ids).
27+
<p align="center"><img src="./images/symbol_file_1.png"><br></p>
28+
<p align="center"><img src="./images/symbol_file_2.png"><br></p>
29+
30+
2. When starting the demo for the first time, a new device will appear in Memfault UI, the device serial is set to ICCID of the SIM by default.
31+
<p align="center"><img src="./images/device.png"><br></p>
32+
33+
3. Two events will be sent by the device on boot, a heartbeat event containg predefined metrics, and a reboot event including the reboot cause.
34+
35+
4. [Memfault Zephyr CLI](https://docs.memfault.com/docs/mcu/demo-cli-cmds-ncs-zephyr#command-reference) can be used to generate test events.
36+
37+
5. The Demo also contains the following additional commands:
38+
```
39+
nce post_chunks : Post Memfault data to cloud via 1NCE CoAP Proxy
40+
nce divby0 : Trigger Division by zero
41+
nce sw1 : Increment switch_1_toggle_count
42+
nce sw2 : Trigger switch_2_toggled event
43+
nce disconnect : Simulates a network disconnection and reconnection sequence
44+
```
45+
The overview dashboard shows a summary of recent device issues:
46+
<p align="center"><img src="./images/overview.png"><br></p>
47+
48+
## Fault Injection via Buttons
49+
50+
Memfault uses buttons and switches on the board to demonstrate the following:
51+
`Button 1`: Triggers a stack overflow.
52+
53+
`Button 2`: Triggers division by zero.
54+
55+
`Switch 1`: Increments a custom metric (switch_1_toggle_count).
56+
57+
`Switch 2`: Logs and traces a toggle event (switch_2_toggled).
58+
59+
Note: for `Thingy:91`, only `Button 1` is available, other actions can be simulated using `nce` cli commands.
60+
61+
## Connectivity Metrics
62+
63+
Connectivity metrics collected by Memfault include:
64+
- `ncs_lte_time_to_connect_ms`: Time taken from starting the LTE search to network registration.
65+
- `ncs_lte_connection_loss_count`: Number of LTE connection losses after initial registration.
66+
- `ncs_lte_tx_kilobytes`: Amount of data sent (in KB).
67+
- `ncs_lte_rx_kilobytes`: Amount of data received (in KB).
68+
69+
Enabled (by default) with:
70+
```
71+
CONFIG_MEMFAULT_NCS_LTE_METRICS=y
72+
```
73+
In addition, sync metrics include:
74+
- `sync_memfault_successful`: Count of successful Memfault sync attempts.
75+
- `sync_memfault_failure`: Count of failed Memfault sync attempts.
76+
77+
Enabled (by default) with:
78+
```
79+
CONFIG_NCE_MEMFAULT_DEMO_COAP_SYNC_METRICS=y
80+
```
81+
82+
The demo also adds the following connectivity metrics:
83+
- `ncs_lte_nce_operator`: LTE operator code (MCC and MNC).
84+
- `ncs_lte_nce_bands`: Supported LTE bands.
85+
- `ncs_lte_nce_current_band`: Current LTE band.
86+
- `ncs_lte_nce_apn`: APN (Access Point Name).
87+
- `ncs_lte_nce_rsrp_dbm`: LTE RSRP (Signal strength).
88+
89+
Enabled (by default) with:
90+
```
91+
CONFIG_NCE_MEMFAULT_DEMO_CONNECTIVITY_METRICS=y
92+
```
93+
### Sample Connectivity dashboard configuration:
94+
<p align="center"><img src="./images/connectivity_metrics.png"><br></p>
95+
96+
#### Sync Succes chart configuration:
97+
<p align="center"><img src="./images/sync_success_config.png"><br></p>
98+
99+
#### To create a new metrics chart:
100+
<p align="center"><img src="./images/create_chart.png"><br></p>
101+
102+
#### Signal quality chart configuration:
103+
<p align="center"><img src="./images/signal_quality_config.png"><br></p>
104+
105+
#### Sent KB chart configuration:
106+
<p align="center"><img src="./images/sent_kb_config.png"><br></p>
107+
108+
## DTLS Configuration
109+
110+
To enable DTLS, add the following configuration:
111+
```
112+
CONFIG_NCE_MEMFAULT_DEMO_ENABLE_DTLS=y
113+
CONFIG_NCE_SDK_ENABLE_DTLS=y
114+
CONFIG_NCE_DEVICE_AUTHENTICATOR=y
115+
CONFIG_NCE_SDK_DTLS_SECURITY_TAG=<security_tag>
116+
```
117+
- `CONFIG_NCE_SDK_DTLS_SECURITY_TAG` defines a secure modem storage slot for saving the PSK persistently, ensuring it remains available across device reboots.
118+
- If the device is previously onboarded, `CONFIG_NCE_SDK_DTLS_SECURITY_TAG` can be set to the tag that was used for onboarding.
119+
- If not, `CONFIG_NCE_SDK_DTLS_SECURITY_TAG` should be set to an empty tag. The demo will then use 1NCE Device Authenticator ( `os_auth()` function from the SDK ) to onboard the device.
120+
- If DTLS connection fails three times due to an invalid PSK, the device will reinitiate onboarding to update the PSK.
121+
122+
## Demo Configuration
123+
124+
The configuration options for this sample are:
125+
126+
`CONFIG_NCE_MEMFAULT_DEMO_PERIODIC_UPDATE` Enable periodic Memfault updates.
127+
- Note: If periodic updates are disabled, the RAM buffer used to store events may fill up unless the chunks are uploaded manually. By default, the buffer size is 1024 bytes, and it can be adjusted using the `CONFIG_MEMFAULT_EVENT_STORAGE_SIZE`.
128+
129+
`CONFIG_NCE_MEMFAULT_DEMO_PERIODIC_UPDATE_FREQUENCY_SECONDS` Update frequency (in seconds).
130+
131+
The heartbeat collection interval can be configured using `MEMFAULT_METRICS_HEARTBEAT_INTERVAL_SECS` in `config/memfault_platform_config.h`
132+
133+
`CONFIG_NCE_MEMFAULT_DEMO_CONNECTIVITY_METRICS` Collect Additional connectivity metrics.
134+
135+
`CONFIG_NCE_MEMFAULT_DEMO_COAP_SYNC_METRICS` Enable sync success/failure metrics.
136+
137+
`CONFIG_NCE_MEMFAULT_DEMO_PRINT_HEARTBEAT_METRICS` Print the collected heartbeat metrics.
138+
139+
`CONFIG_NCE_MEMFAULT_DEMO_DISCONNECT_DURATION_SECONDS` Disconnect command duration (in seconds).
140+
141+
`CONFIG_NCE_MEMFAULT_DEMO_ENABLE_DTLS` Enable DTLS for CoAP Proxy communication.
142+
143+
## Asking for Help
144+
145+
The most effective communication with our team is through GitHub. Simply create a [new issue](https://github.com/1NCE-GmbH/blueprint-zephyr/issues/new/choose) and select from a range of templates covering bug reports, feature requests, documentation issue, or Gerneral Question.
146+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#
2+
# Copyright (c) 2024 Nordic Semiconductor ASA
3+
#
4+
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
#
6+
7+
# Network
8+
CONFIG_NET_SOCKETS=y
9+
CONFIG_NET_SOCKETS_OFFLOAD=y
10+
11+
# Certificate management
12+
CONFIG_MEMFAULT_ROOT_CERT_STORAGE_NRF9160_MODEM=y
13+
CONFIG_MODEM_KEY_MGMT=y
14+
15+
# AT command capability in shell
16+
CONFIG_AT_SHELL=y
17+
CONFIG_SHELL_WILDCARD=n
18+
19+
# Disable Duplicate Address Detection (DAD)
20+
# due to not being properly implemented for offloaded interfaces.
21+
CONFIG_NET_IPV6_NBR_CACHE=n
22+
CONFIG_NET_IPV6_MLD=n
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#
2+
# Copyright (c) 2023 Nordic Semiconductor ASA
3+
#
4+
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
#
6+
7+
# Network
8+
CONFIG_NET_SOCKETS=y
9+
CONFIG_NET_SOCKETS_OFFLOAD=y
10+
11+
# Certificate management
12+
CONFIG_MODEM_KEY_MGMT=y
13+
14+
# AT command capability in shell
15+
CONFIG_AT_SHELL=y
16+
CONFIG_SHELL_WILDCARD=n
17+
18+
# Disable Duplicate Address Detection (DAD)
19+
# due to not being properly implemented for offloaded interfaces.
20+
CONFIG_NET_IPV6_NBR_CACHE=n
21+
CONFIG_NET_IPV6_MLD=n
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#
2+
# Copyright (c) 2023 Nordic Semiconductor ASA
3+
#
4+
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
#
6+
7+
# Network
8+
CONFIG_NET_SOCKETS=y
9+
CONFIG_NET_SOCKETS_OFFLOAD=y
10+
11+
# Certificate management
12+
CONFIG_MODEM_KEY_MGMT=y
13+
14+
# AT command capability in shell
15+
CONFIG_AT_SHELL=y
16+
CONFIG_SHELL_WILDCARD=n
17+
18+
19+
# Disable Duplicate Address Detection (DAD)
20+
# due to not being properly implemented for offloaded interfaces.
21+
CONFIG_NET_IPV6_NBR_CACHE=n
22+
CONFIG_NET_IPV6_MLD=n
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#
2+
# Copyright (c) 2023 Nordic Semiconductor ASA
3+
#
4+
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
#
6+
7+
# Network
8+
CONFIG_NET_SOCKETS=y
9+
CONFIG_NET_SOCKETS_OFFLOAD=y
10+
11+
# Certificate management
12+
CONFIG_MODEM_KEY_MGMT=y
13+
14+
# AT command capability in shell
15+
CONFIG_AT_SHELL=y
16+
CONFIG_SHELL_WILDCARD=n
17+
18+
# Disable Duplicate Address Detection (DAD)
19+
# due to not being properly implemented for offloaded interfaces.
20+
CONFIG_NET_IPV6_NBR_CACHE=n
21+
CONFIG_NET_IPV6_MLD=n
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#
2+
# Copyright (c) 2024 Nordic Semiconductor ASA
3+
#
4+
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
#
6+
7+
# Network
8+
CONFIG_NET_SOCKETS=y
9+
CONFIG_NET_SOCKETS_OFFLOAD=y
10+
11+
# Certificate management
12+
CONFIG_MODEM_KEY_MGMT=y
13+
14+
# AT command capability in shell
15+
CONFIG_AT_SHELL=y
16+
CONFIG_SHELL_WILDCARD=n
17+
18+
# Disable Duplicate Address Detection (DAD)
19+
# due to not being properly implemented for offloaded interfaces.
20+
CONFIG_NET_IPV6_NBR_CACHE=n
21+
CONFIG_NET_IPV6_MLD=n
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/* Application-specific metrics can be defined here.
2+
* Please refer to https://docs.memfault.com/docs/embedded/metrics-api for more details.
3+
*/
4+
5+
MEMFAULT_METRICS_KEY_DEFINE(switch_1_toggle_count, kMemfaultMetricType_Unsigned)
6+
7+
#if defined(CONFIG_NCE_MEMFAULT_DEMO_COAP_SYNC_METRICS)
8+
MEMFAULT_METRICS_KEY_DEFINE(sync_memfault_successful, kMemfaultMetricType_Unsigned)
9+
MEMFAULT_METRICS_KEY_DEFINE(sync_memfault_failure, kMemfaultMetricType_Unsigned)
10+
#endif /* CONFIG_NCE_MEMFAULT_DEMO_COAP_SYNC_METRICS */
11+
12+
#if defined(CONFIG_NCE_MEMFAULT_DEMO_CONNECTIVITY_METRICS)
13+
MEMFAULT_METRICS_STRING_KEY_DEFINE(ncs_lte_nce_operator, 50)
14+
MEMFAULT_METRICS_STRING_KEY_DEFINE(ncs_lte_nce_bands, 50)
15+
MEMFAULT_METRICS_STRING_KEY_DEFINE(ncs_lte_nce_apn, 50)
16+
MEMFAULT_METRICS_KEY_DEFINE(ncs_lte_nce_rsrp_dbm, kMemfaultMetricType_Signed)
17+
MEMFAULT_METRICS_KEY_DEFINE(ncs_lte_nce_current_band, kMemfaultMetricType_Unsigned)
18+
#endif /* CONFIG_NCE_MEMFAULT_DEMO_CONNECTIVITY_METRICS */

0 commit comments

Comments
 (0)