# Anomaly Detection in Elderly Health Monitoring via IoT for Timely Interventions
## 2 Materials and Methods
### Proposed Embedded System
Future versions will consider integrating the automatic communication mechanism between Wi-Fi and the mobile connection, depending on network availability. In this way, the continuity of real-time monitoring and the resilience of the system in diverse environments will be ensured. The pseudocode for calculating AR, mR, and MR involves initializing a list of successive heart rate readings from the heart rate bit sensor, followed by the standard initialization of the total number of readings and the minimum and maximum values that need to be calculated. Subsequently, the list of readings is built, and the average, maximum, and minimum values are updated accordingly (Heart_Rate.txt file from the Supplementary Material). The prototype used in this study employs the M5 Stack module, which integrates the ESP32 microcontroller, enabling operation in five modes that optimize energy consumption. In the standard version, it operates in active mode, with all peripherals functioning. The Modern Sleep variant keeps the processor active while deactivating the communication modules. For light sleep, the Central Processing Unit (CPU) is turned off, whereas the random-access memory (RAM) and peripherals are retained. It can be reactivated via General Purpose Input/Output (GPIO) or a timer. Regarding the deep sleep mode, it  
Figure 2: Device operation flowchart. minimizes energy consumption by keeping only the Real-Time Clock (RTC) and wake-up sensors active, which is the operating mode chosen for the proposed prototype. The Hibernation option has even lower power consumption than deep sleep, but all data associated with RAM is lost. For these reasons, the deep sleep operating mode was considered the most suitable for the proposed variant in this work, and this operating mode is exclusively available in the architecture that includes the M5 Stack hardware ecosystem with the ESP32 board. The pseudocode in the Steps.txt file from the Supplementary Material calculates the parameter S, which reads the values of the coordinates ax, ay, and az from the MPU 6070 accelerometer. Subsequently, it calculates the magnitude of acceleration for each reading. A step is considered if the difference between the current reading and the previous one exceeds a predefined threshold (STEP_THRESHOLD). The pseudocode in the Sleep_Phases.txt file from the Supplementary Material calculates the DST and SST parameters. For the determination of DST and SST parameters, the authors propose that the system use data obtained from the MPU-605 accelerometer. In the proposed algorithm, variations in the magnitude of acceleration on the three axes lead to a value that must be below a preset threshold (DEEP_SLEEP_THRESHOLD). The respective period is classified as deep sleep. If the calculated value falls between two predefined thresholds, it is considered shallow sleep (DEEP_SLEEP_THRESHOLD < acceleration < SHALLOW_SLEEP_THRESHOLD). The intervals are accumulated over a monitoring period, and the total time of each sleep phase is calculated in minutes. Threshold values can be studied in new research to be customized according to each user's sleep quality. The analog pin reads the battery voltage to calculate the battery level. This value is converted into an actual voltage relative to the analog-to-digital converter (ADC) reference. Subsequently, the measured voltage is mapped between the values of MIN_BATTERY_VOLTAGE (e.g., 3.0 V for a discharged battery) and MAX_BATTERY_VOLTAGE (e.g., 4.2 V for a fully charged battery). If the percentage exceeds 100%, it is set to 100%. If the value is below 0% (error or heavily discharged battery), the value is set to 0% (Battery_Level.txt file from the Supplementary Material). The analysis of threshold values is presented in the Results section, where the final values established in the algorithm and the method of their determination will be detailed.