# Nrf9160 mefault sample does not increase Switch1ToggleCount

**URL:** <https://community.memfault.com/t/nrf9160-mefault-sample-does-not-increase-switch1togglecount/668>\
**Category:** Memfault Help\
**Created:** [May 17, 2023, 10:35pm UTC](https://community.memfault.com/t/nrf9160-mefault-sample-does-not-increase-switch1togglecount/668 "2023-05-17T22:35:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![J0sh1101](https://avatars.discourse-cdn.com/v4/letter/j/e56c9b/32.png) [@J0sh1101](https://community.memfault.com/u/J0sh1101)\
**Post date:** [May 17, 2023, 10:35pm UTC](https://community.memfault.com/t/nrf9160-mefault-sample-does-not-increase-switch1togglecount/668/1 "2023-05-17T22:35:53Z")

</div>

Hi there,

I am running a sample code for nrf9160 which is available on github, [main.c](https://github.com/nrfconnect/sdk-nrf/blob/main/samples/debug/memfault/src/main.c). I made small changes and update the callbacks button. Updates this codeblock:

```auto
} else if (has_changed & DK_BTN3_MSK) {
		_switch1ToggleCount += 1;
		err = memfault_metrics_heartbeat_set_unsigned(
			MEMFAULT_METRICS_KEY(Switch1ToggleCount), _switch1ToggleCount);

		if (err) {
			LOG_ERR("Failed to update Switch1ToggleCount");
		} else {
			LOG_INF("Switch1ToggleCount incremented, value %d", _switch1ToggleCount);
		}

		// optionally, trigger a heartbeat to immediately capture the metric record
		// Trigger collection of heartbeat data. 
  		memfault_metrics_heartbeat_debug_trigger();

		// see if anything has been updated.
		memfault_metrics_heartbeat_debug_print();

		// send the data to the cloud
		memfault_zephyr_port_post_data();
} else if (has_changed & DK_BTN4_MSK) {

```

I have produced a crash with stack overflow and division by zero error. However, I cannot increase the Switch1ToggleCount variable, even after printing the heartbeat metrics, the value is zero. Also, I don’t see the metric in the dashboard in the cloud. It just says that no data is available there.

Do I need to allocate memory for the metrics or set a flag in prj.conf?

Console output:

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/memfault/original/1X/3945a65bf4375914abbda09d8986d3b31400864f.png)

_ **Thanks for the help!!!** _

---

<div class="post-metadata">

**Author:** ![eric-memfault](https://avatars.discourse-cdn.com/v4/letter/e/e9c0ed/32.png) [@eric-memfault](https://community.memfault.com/u/eric-memfault)\
**Post date:** [May 18, 2023, 2:45am UTC](https://community.memfault.com/t/nrf9160-mefault-sample-does-not-increase-switch1togglecount/668/2 "2023-05-18T02:45:23Z")

</div>

Thanks for reaching out! Let’s start with the debug printing you’re seeing. From the code you shared, what’s happening is that `memfault_metrics_heartbeat_debug_trigger` is resetting the value of the metrics before `memfault_metrics_heartbeat_debug_print` prints them to the console. The most straightforward change would be to reverse the order of those function calls. Another good place to call the `debug_print` is in `memfault_metrics_heartbeat_collect_data` if you have implemented it.

Regarding data not showing in your project, have you already uploaded your symbol file for the build?

---

<div class="post-metadata">

**Author:** ![J0sh1101](https://avatars.discourse-cdn.com/v4/letter/j/e56c9b/32.png) [@J0sh1101](https://community.memfault.com/u/J0sh1101)\
**Post date:** [May 18, 2023, 12:45pm UTC](https://community.memfault.com/t/nrf9160-mefault-sample-does-not-increase-switch1togglecount/668/3 "2023-05-18T12:45:54Z")

</div>

Hi,

thanks for the fast response.

Okay, now I understand. Every time the data is being prepared and serialized, the metric variables are resetted. This is the reason why I always got the Switch1ToggleCount zero. Is there any way to not reset the metric variable and accumulate the Switch1ToggleCount value? Or do I always have to have a duplicate/shadow of this value and update it every time heartbeat\_collect\_data() is being executed.

> [@eric-memfault](#):
>
> Another good place to call the `debug_print` is in `memfault_metrics_heartbeat_collect_data` if you have implemented it.

I try to implement it according to the MemFault docs, but I got a linker error because the function is not weakly declared:

```auto
[3/22] Linking C executable zephyr\zephyr_pre0.elf
FAILED: zephyr/zephyr_pre0.elf zephyr/zephyr_pre0.map 
cmd.exe /C "cd . && C:\toolchains\v2.3.0-rc1\opt\zephyr-sdk\arm-zephyr-eabi\bin\arm-zephyr-eabi-gcc.exe -gdwarf-4 @CMakeFiles\zephyr_pre0.rsp -o zephyr\zephyr_pre0.elf && cmd.exe /C "cd /D C:\ncs_workspace\memfault\build\zephyr && C:\toolchains\v2.3.0-rc1\opt\bin\cmake.exe -E echo ""
c:/toolchains/v2.3.0-rc1/opt/zephyr-sdk/arm-zephyr-eabi/bin/../lib/gcc/arm-zephyr-eabi/12.1.0/../../../../arm-zephyr-eabi/bin/ld.exe: modules/nrf/modules/memfault-firmware-sdk/lib.. __nrf__ modules__memfault-firmware-sdk.a(memfault_ncs_metrics.c.obj): in function `memfault_metrics_heartbeat_collect_data':
C:\v2.3.0-rc1\nrf\modules\memfault-firmware-sdk\memfault_ncs_metrics.c:100: multiple definition of `memfault_metrics_heartbeat_collect_data'; app/libapp.a(main.c.obj):C:\ncs_workspace\memfault\src\main.c:116: first defined here
collect2.exe: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.

```

Do I need add to do anything else?

When I look in the Dashboards on MemFault page, they are empty, although I got some data. Why is this the case?

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/memfault/original/1X/71af3788744e39fe512785330172149232196658.png)

---

<div class="post-metadata">

**Author:** ![eric-memfault](https://avatars.discourse-cdn.com/v4/letter/e/e9c0ed/32.png) [@eric-memfault](https://community.memfault.com/u/eric-memfault)\
**Post date:** [May 19, 2023, 3:56am UTC](https://community.memfault.com/t/nrf9160-mefault-sample-does-not-increase-switch1togglecount/668/4 "2023-05-19T03:56:06Z")

</div>

Our metrics subsystem is based around the concept of a heartbeat that occurs on a regular interval. At the end of each interval the heartbeat is reset in preparation for any changes during the next interval, so that behavior is built into the subsystem. If you’d like to keep that value across multiple intervals you will need to store a copy of that as you mentioned.

The linker error you hit is because NCS defines this function, which I forgot. You could modify the function in NCS to use the debug print if you would like for your testing.

The metrics charts do take some time to refresh which is what you might be encountering here. There are a couple of pages to check for chunk and event processing status. If you could checking out Chunks Debug and see if there are chunks as you expect and that they do not have any errors in processing? Next go to Events Debug and see if there are heartbeat events as expected. You can expand the heartbeat event to verify that the metric values you expect are there as well. Also I would double check that the symbol file for your build has been uploaded.
