Nrf9160 mefault sample does not increase Switch1ToggleCount

Hi there,

I am running a sample code for nrf9160 which is available on github, main.c. I made small changes and update the callbacks button. Updates this codeblock:

} 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:

Thanks for the help!!!

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?

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.

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

[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?

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.