Health monitoring and heartbeat help check if a system or task is working well. It tells us early if something stops or slows down.
0
0
Health monitoring and heartbeat in FreeRTOS
Introduction
To know if a FreeRTOS task is still running without problems.
To detect if a device or sensor is alive and sending data.
To restart a system part automatically if it stops responding.
To monitor system health in real-time during operation.
To send alerts when a system component fails or hangs.
Syntax
FreeRTOS
void vTaskHeartbeat(void *pvParameters) {
for (;;) {
// Send heartbeat signal or update status
vTaskDelay(pdMS_TO_TICKS(heartbeat_interval_ms));
}
}The heartbeat function usually runs in a FreeRTOS task that loops forever.
Use vTaskDelay() to wait between heartbeats without blocking other tasks.
Examples
This example prints a heartbeat message every 1 second.
FreeRTOS
void vTaskHeartbeat(void *pvParameters) {
for (;;) {
printf("Heartbeat sent\n");
vTaskDelay(pdMS_TO_TICKS(1000));
}
}This example toggles an LED every 0.5 seconds as a heartbeat signal.
FreeRTOS
void vTaskHeartbeat(void *pvParameters) {
for (;;) {
// Toggle an LED to show system is alive
ToggleLED();
vTaskDelay(pdMS_TO_TICKS(500));
}
}Sample Program
This program creates a FreeRTOS task that prints a heartbeat message every 2 seconds. The scheduler runs the task repeatedly.
FreeRTOS
#include "FreeRTOS.h" #include "task.h" #include <stdio.h> void vTaskHeartbeat(void *pvParameters) { for (;;) { printf("Heartbeat: system is alive\n"); vTaskDelay(pdMS_TO_TICKS(2000)); } } int main(void) { xTaskCreate(vTaskHeartbeat, "HeartbeatTask", configMINIMAL_STACK_SIZE, NULL, 1, NULL); vTaskStartScheduler(); return 0; }
OutputSuccess
Important Notes
Heartbeat intervals should be chosen carefully to balance responsiveness and CPU load.
Use watchdog timers with heartbeat to reset system if heartbeat stops.
Heartbeat can be a simple message, LED toggle, or signal to another system part.
Summary
Heartbeat tasks run repeatedly to show system health.
Use vTaskDelay() to wait between heartbeats without blocking.
Heartbeat helps detect failures early and keep systems reliable.