|
| 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 | + |
0 commit comments