Artificial Intelligence Implementation in Internet of Things Embedded System for Real-Time Person Presence in Bed Detection and Sleep Behaviour Monitor

: This paper works on detecting a person in bed for sleep routine and sleep pattern monitoring based on the Micro-Electro-Mechanical Systems (MEMS) accelerometer and Internet of Things (IoT) embedded system board. This work provides sleep information, patient assessment, and elderly care for patients who live alone via tele-distance to doctors or family members. About 216,000 pieces of acceleration data were collected, including three classes: no person in bed, a static laying position, and a moving state for Artificial Intelligence (AI) application. Six well-known Machine-Learning (ML) algorithms were evaluated with precision, recall, F1-score, and accuracy in the workstation before implementing in the STM32-microcontroller for real-time state classification. The four best algorithms were selected to be programmed into the IoT board and applied for real-time testing. The results demonstrate the high accuracy of the ML performance, more than 99%, and the Classification and Regression Tree algorithm is among the best models with a light code size of 1583 bytes. The smart bed information is sent to the IoT dashboard of Node-RED via a Message Queuing Telemetry broker (MQTT).


Introduction
The incorporation of cutting-edge technologies such as Micro-Electro-Mechanical Systems (MEMS) accelerometers [1][2][3], Internet of Things (IoT) frameworks [4][5][6][7], and Machine-Learning (ML) [8][9][10] algorithms has significantly transformed healthcare monitoring, specifically in the field of sleep pattern analysis.Sleeping on time and a deep sleep are key to health maintenance [11].For restless sleep, like insomnia [12], examples of symptoms shown would be tossing and turning, and trying to get settled and comfortable [13].The capacity to autonomously identify the existence of an individual on a bed and differentiate between their stationary rest and restless movements carries great importance in diverse healthcare settings.The significance of adequately classifying sleep conditions, such as stationary lying positions or frequent motion, cannot be underestimated.Detecting variations from typical sleep patterns can assist in the early identification of sleep disorders, assess the efficacy of therapies, and reduce potential health hazards linked to inadequate sleep quality.Moreover, for persons with persistent ailments or elderly individuals who are susceptible to falls or disruptions while sleeping, prompt intervention with live monitoring can significantly improve their quality of life and autonomy.
In the article [14], movement activity was extracted from the multichannel ballistocardiography measurements based on Emfit sensor foils placed in the bed mattress, which has a total accuracy of 83%.However, the BCG data's noise, artifacts, or signal distortions can complicate the signal processing algorithms and reduce their accuracy in detecting movement events.Maintaining and calibrating Emfit sensor foils regularly is necessary to ensure their optimal performance over time.Not properly maintaining or calibrating cation by evaluating various ML algorithms, including the Extra Tree Classifier (ET) [26][27][28][29], Logistic Regression (LR) [30][31][32], Linear Discriminant Analysis (LDA) [33,34], Classification and Regression Trees (CART) [35][36][37][38][39][40], Support Vector Machines (SVMs) [41,42], and Random Forest (RF) [43][44][45].Afterward, the chosen algorithms are implemented in the STM32 embedded system [46] within the IoT framework, allowing a smooth integration into the current healthcare infrastructure for widespread adoption.This research adds to the advancement of sleep pattern monitoring and remote healthcare management by combining MEMS accelerometers, IoT frameworks, and ML algorithms with a microcontroller (MCU) platform.The impacts of our research's results spread throughout the field of sleep research, providing promising possibilities for individualized healthcare interventions and proactive wellness management.
The paper is organized as follows: First, the setup and devices will be depicted, together with the system working principle.In the next section, the ML algorithms will be described briefly; then, it will be their cross-validation and test part.The subsequent content is about the real-time test with the MCU operation and sleep state on the IoT dashboard demonstration.Finally, the conclusion and future work are at the end of the paper.

Materials and Methods
In this section, the system architecture and working principle will be shown, together with brief descriptions of the applied ML algorithms.

Setup and Devices
Figure 1 shows the data acquisition diagram from MCU and MEMS Accelerometer under the IoT bed to the workstation.The STM32 B-L475-IOT01A microcontroller board [47] from the IoT kit has acquired acceleration data using Serial Peripheral Interface (SPI) communication.The data collected by the accelerometer were sent to a workstation for immediate storage in text files.STM32 B-L475-IOT01A was made by STMicroelectronics company which is headquartered in Plan-les-Ouates, Switzerland.
Electronics 2024, 13, x FOR PEER REVIEW 3 of 15 evaluating various ML algorithms, including the Extra Tree Classifier (ET) [26][27][28][29], Logistic Regression (LR) [30][31][32], Linear Discriminant Analysis (LDA) [33,34], Classification and Regression Trees (CART) [35][36][37][38][39][40], Support Vector Machines (SVMs) [41,42], and Random Forest (RF) [43][44][45].Afterward, the chosen algorithms are implemented in the STM32 embedded system [46] within the IoT framework, allowing a smooth integration into the current healthcare infrastructure for widespread adoption.This research adds to the advancement of sleep pattern monitoring and remote healthcare management by combining MEMS accelerometers, IoT frameworks, and ML algorithms with a microcontroller (MCU) platform.The impacts of our research's results spread throughout the field of sleep research, providing promising possibilities for individualized healthcare interventions and proactive wellness management.The paper is organized as follows: First, the setup and devices will be depicted, together with the system working principle.In the next section, the ML algorithms will be described briefly; then, it will be their cross-validation and test part.The subsequent content is about the real-time test with the MCU operation and sleep state on the IoT dashboard demonstration.Finally, the conclusion and future work are at the end of the paper.

Materials and Methods
In this section, the system architecture and working principle will be shown, together with brief descriptions of the applied ML algorithms.

Setup and Devices
Figure 1 shows the data acquisition diagram from MCU and MEMS Accelerometer under the IoT bed to the workstation.The STM32 B-L475-IOT01A microcontroller board [47] from the IoT kit has acquired acceleration data using Serial Peripheral Interface (SPI) communication.The data collected by the accelerometer were sent to a workstation for immediate storage in text files.STM32 B-L475-IOT01A was made by STMicroelectronics company which is headquartered in Plan-les-Ouates, Switzerland.
For ML model evaluation, these files were trained using Python programming [48], with the packages Scikit-learn [49].Then, the best models are chosen to implement into MCU via C programming language [50,51] to communicate with the IoT dashboard wirelessly as shown in Figure 2.For ML model evaluation, these files were trained using Python programming [48], with the packages Scikit-learn [49].Then, the best models are chosen to implement into MCU via C programming language [50,51] to communicate with the IoT dashboard wirelessly as shown in Figure 2.
Electronics 2024, 13, x FOR PEER REVIEW 3 of 15 evaluating various ML algorithms, including the Extra Tree Classifier (ET) [26][27][28][29], Logistic Regression (LR) [30][31][32], Linear Discriminant Analysis (LDA) [33,34], Classification and Regression Trees (CART) [35][36][37][38][39][40], Support Vector Machines (SVMs) [41,42], and Random Forest (RF) [43][44][45].Afterward, the chosen algorithms are implemented in the STM32 embedded system [46] within the IoT framework, allowing a smooth integration into the current healthcare infrastructure for widespread adoption.This research adds to the advancement of sleep pattern monitoring and remote healthcare management by combining MEMS accelerometers, IoT frameworks, and ML algorithms with a microcontroller (MCU) platform.The impacts of our research's results spread throughout the field of sleep research, providing promising possibilities for individualized healthcare interventions and proactive wellness management.The paper is organized as follows: First, the setup and devices will be depicted, together with the system working principle.In the next section, the ML algorithms will be described briefly; then, it will be their cross-validation and test part.The subsequent content is about the real-time test with the MCU operation and sleep state on the IoT dashboard demonstration.Finally, the conclusion and future work are at the end of the paper.

Materials and Methods
In this section, the system architecture and working principle will be shown, together with brief descriptions of the applied ML algorithms.

Setup and Devices
Figure 1 shows the data acquisition diagram from MCU and MEMS Accelerometer under the IoT bed to the workstation.The STM32 B-L475-IOT01A microcontroller board [47] from the IoT kit has acquired acceleration data using Serial Peripheral Interface (SPI) communication.The data collected by the accelerometer were sent to a workstation for immediate storage in text files.STM32 B-L475-IOT01A was made by STMicroelectronics company which is headquartered in Plan-les-Ouates, Switzerland.
For ML model evaluation, these files were trained using Python programming [48], with the packages Scikit-learn [49].Then, the best models are chosen to implement into MCU via C programming language [50,51] to communicate with the IoT dashboard wirelessly as shown in Figure 2.  The MEMS accelerometer ADXL355 [52] is concealed within a protective enclosure and positioned accurately beneath the bed frame, as shown in Figure 3, and it is connected to the embedded system platform appropriately.The ADXL355 offers high-resolution measurement capabilities, allowing it to detect even subtle movements during sleep accurately.Its high sensitivity level can provide comprehensive insights into sleep quality and duration.Additionally, for applications like sleep monitoring where the device needs to operate continuously for extended periods, low power consumption is essential to prolong battery life and minimize the need for frequent recharging or replacement.The ADXL355's low power consumption ensures that it can monitor sleep patterns throughout the night without draining the device's battery quickly.ADXL355 was made by Analog Devices Company, which is headquartered in Wilmington, MA, USA.The MEMS accelerometer ADXL355 [52] is concealed within a protective enclosure and positioned accurately beneath the bed frame, as shown in Figure 3, and it is connected to the embedded system platform appropriately.The ADXL355 offers high-resolution measurement capabilities, allowing it to detect even subtle movements during sleep accurately.Its high sensitivity level can provide comprehensive insights into sleep quality and duration.Additionally, for applications like sleep monitoring where the device needs to operate continuously for extended periods, low power consumption is essential to prolong battery life and minimize the need for frequent recharging or replacement.The ADXL355's low power consumption ensures that it can monitor sleep patterns throughout the night without draining the device's battery quickly.ADXL355 was made by Analog Devices Company, which is headquartered in Wilmington, MA, USA.All the devices were mounted under the bed as shown in Figure 4.

Raw Data
During the experiment, the user lies on the bed in 4 stationary positions, prone, back, right side, and left side, to collect data for static state.In the dynamic state, the person moves and turns around.Finally, an empty state is carried out where no person is on the All the devices were mounted under the bed as shown in Figure 4.The MEMS accelerometer ADXL355 [52] is concealed within a protective enclosure and positioned accurately beneath the bed frame, as shown in Figure 3, and it is connected to the embedded system platform appropriately.The ADXL355 offers high-resolution measurement capabilities, allowing it to detect even subtle movements during sleep accurately.Its high sensitivity level can provide comprehensive insights into sleep quality and duration.Additionally, for applications like sleep monitoring where the device needs to operate continuously for extended periods, low power consumption is essential to prolong battery life and minimize the need for frequent recharging or replacement.The ADXL355's low power consumption ensures that it can monitor sleep patterns throughout the night without draining the device's battery quickly.ADXL355 was made by Analog Devices Company, which is headquartered in Wilmington, MA, USA.All the devices were mounted under the bed as shown in Figure 4.

Raw Data
During the experiment, the user lies on the bed in 4 stationary positions, prone, back, right side, and left side, to collect data for static state.In the dynamic state, the person moves and turns around.Finally, an empty state is carried out where no person is on the

Raw Data
During the experiment, the user lies on the bed in 4 stationary positions, prone, back, right side, and left side, to collect data for static state.In the dynamic state, the person moves and turns around.Finally, an empty state is carried out where no person is on the bed, and the sensor keeps acquiring the acceleration for data collection.The volunteer participant is more than 30 years old and has a sleeping issue.
Figure 5 shows the raw data in 3 concerned states on all axes: no person in bed; lying static positions (joined data from 4 lying positions), and dynamic motion in bed.It is complicated to detect a proper threshold to recognize and distinguish these states since the data conduct high variations, and their ranges are not so clearly different, especially in the case of no person in bed and lying in static positions.There are also influences from different sleep positions, which cause the data to be more intricate.Thus, it is necessary to use the ML approach to achieve high classification efficiency.
Electronics 2024, 13, x FOR PEER REVIEW 5 of 15 bed, and the sensor keeps acquiring the acceleration for data collection.The volunteer participant is more than 30 years old and has a sleeping issue.
Figure 5 shows the raw data in 3 concerned states on all axes: no person in bed; lying static positions (joined data from 4 lying positions), and dynamic motion in bed.It is complicated to detect a proper threshold to recognize and distinguish these states since the data conduct high variations, and their ranges are not so clearly different, especially in the case of no person in bed and lying in static positions.There are also influences from different sleep positions, which cause the data to be more intricate.Thus, it is necessary to use the ML approach to achieve high classification efficiency.

Data Processing and ML features
The output data rate of the device is 200 Sa/s.The absolute difference between 2 consecutive accelerations (Δacc) on the X, Y, and Z axes are calculated as features.There are 216000 data, containing 1080 windows of 200 samples.The sum of each window is calculated to form the final feature for each axis to classify 3 states: no person, static lying state, and moving state, as described in Table 1.70% of data were used for training and validation; 30% of the rest is used for testing the ML models.For each second, the ML model will predict the presence of a person in bed and the lying state: static or dynamic state.

Data Processing and ML features
The output data rate of the device is 200 Sa/s.The absolute difference between 2 consecutive accelerations (∆acc) on the X, Y, and Z axes are calculated as features.There are 216,000 data, containing 1080 windows of 200 samples.The sum of each window is calculated to form the final feature for each axis to classify 3 states: no person, static lying state, and moving state, as described in Table 1.70% of data were used for training and validation; 30% of the rest is used for testing the ML models.For each second, the ML model will predict the presence of a person in bed and the lying state: static or dynamic state.

ML Algorithms
There are 6 powerful algorithms in consideration for ML classification: LG is a statistical model that estimates the probability of an instance belonging to a specific class.LG does this by utilizing a logistic function, which is also referred to as the sigmoid function.This function maps any real-valued input to a value within the range of 0 to 1.This transformation is crucial as it enables LR to generate probabilities, which are subsequently utilized for binary classifications.During training, the logistic regression model learns the association between the input features and the target class by estimating coefficients for each feature.These coefficients indicate the individual impact of each attribute on the ultimate forecast.The model iteratively changes these coefficients to minimize the discrepancy between its predictions and the actual class labels in the training data.After being trained, LR utilizes the acquired coefficients to estimate the likelihood that a new instance is part of the positive class.The algorithm calculates a weighted total of the input features, multiplying each feature by its respective coefficient.The total is subsequently inputted into the logistic function to derive the likelihood.LR employs a threshold, usually set at 0.5, to determine the final binary classification based on the likelihood.If the estimated probability exceeds the specified threshold, the instance is categorized as belonging to the positive class; otherwise, it is categorized as belonging to the negative class.LR has a significant benefit in terms of interpretability.LR calculates coefficients for each feature, allowing us to understand the significance and direction of their impact on the projected probability, which assists in the comprehension of how various characteristics contribute to the determination of classification.Nevertheless, LR also possesses its constraints.It presupposes a linear correlation between the characteristics and the logarithm of the probability of the result, which may not consistently be accurate in some scenarios.LR is susceptible to the influence of outliers and multicollinearity among features, which can have a negative impact on the stability and dependability of its predictions.
LDA is a classification approach that identifies optimal linear combinations of characteristics to effectively distinguish between different classes in the data.LDA assumes that the features adhere to a normal (Gaussian) distribution and that the covariance of the features is uniform across all classes.The main objective of LDA is to identify a linear decision boundary that optimizes the distance between different classes and minimizes the variability within each class.In order to accomplish it, LDA initiates the process by creating a model that represents the distribution of features for each class.The algorithm calculates the average and covariance matrix for each class, assuming that the characteristics within each class adhere to a multivariate normal distribution.After modeling the class distributions, LDA computes the class priors, indicating the probability of meeting each class in the dataset.These prior probabilities are usually calculated by determining the relative frequencies of each class in the training data.LDA utilizes the class distributions and priors to create decision boundaries that define specific areas in the feature space for each class.The decision borders are established by optimizing a criterion to maximize the variance ratio across classes to variation within classes.When confronted with new data points, LDA employs the acquired decision boundaries to categorize them according on their feature values.The algorithm utilizes Bayes' theorem to compute the posterior probability of each class for a certain data point, considering both the class distribution and the class priors.The data point is allocated to the class with the greatest posterior probability afterward.LDA is able to handle high-dimensional data by lowering the number of dimensions while retaining most of the class-discriminating information.By mapping the data into a lower-dimensional subspace determined by the linear discriminants, LDA can effectively distinguish between classes, even when the original feature space has a high number of dimensions.
CART is a decision tree technique that iteratively divides the data space into areas according to the values of the features.The CART algorithm constructs a decision tree by dividing the feature space into subsets, each of which is assigned a particular class label.This process is carried out iteratively and recursively.The CART algorithm commences by utilizing the complete dataset, taking into account all the features and cases.The objective is to identify the characteristic that most effectively divides the data into two groups while maximizing a measure such as Gini impurity or information gain.Gini impurity quantifies the likelihood of incorrectly classifying an instance if it were assigned a random label based on the class distribution within the subset.Information gain measures the decrease in entropy (uncertainty) when splitting is carried out using a specific feature.After selecting the initial feature, CART algorithm splits the dataset into two halves using a threshold value specific to that feature.The algorithm can generate a split for each category when dealing with categorical features.On the other hand, for numerical features, it can utilize a threshold to construct binary splits.The CART algorithm assesses all potential divisions and selects the one that optimizes the criterion, such as minimizing Gini impurity or maximizing information gain.This technique is iteratively applied to each subset, thus generating a tree structure.The process of recursion persists until a specified condition for termination is fulfilled.To ensure the model's generalization and prevent overfitting to the training data, it is important to control the depth of the tree and avoid splitting nodes with insufficient instances.In order to categorize a new instance, the decision tree must be followed from the starting point to the final point, taking into account the characteristics of the instance.At every node, it evaluates the feature value of the instance and compares it to the threshold for splitting.It then proceeds to the appropriate child node based on the comparison.This procedure iterates until it reaches a leaf node, when the projected class for the new instance is determined by selecting the majority class among the instances in that node.The main advantages of CART are its simplicity, interpretability, and capability to handle numerical and categorical data.The resulting decision tree may be readily visible and comprehended, rendering it a valuable instrument for elucidating the underlying decision-making process to stakeholders.Furthermore, CART is resistant to irrelevant characteristics and has the ability to automatically choose the most informative ones for splitting, hence minimizing the necessity for feature engineering.
ET differs from regular decision trees by incorporating an extra layer of randomization in selecting splits instead of evaluating all potential splits for each characteristic at every node.The core principle of ET revolves around the idea of employing randomization while selecting splits.When seeking the most favourable division to separate the samples of a node into two groups, ET generates random splits for each of the randomly chosen features.The randomization process is carried out separately for each feature and is iterated numerous times until a specified maximum number of splits is reached.ET enhances the decision-making process by introducing a greater level of variety and diversity by generating random splits for each characteristic.This point increased the level of randomness and helped mitigate the risk of overfitting by preventing the model from excessively depending on features or patterns in the data.Extra Trees employ a different approach than traditional methods of selecting the best split based on a predetermined criterion.Instead of relying on a single split, ET considers numerous random splits and selects the one that achieves the highest performance.ET mitigates overfitting by employing multiple random splits for each feature, resulting in decision boundaries that are less prone to overfitting the training data, which is especially advantageous when working with datasets that are noisy or have a high number of dimensions, as typical decision trees may have difficulty in properly generalizing.The increased level of unpredictability in the selection of splits enables ET to effectively capture a wider range of patterns and relationships in the data.Enhancing the model's capacity to extrapolate to unfamiliar data enhances its performance on test datasets.ET exhibits greater resilience to outliers and noisy features than conventional decision trees.The technique of randomization reduces the influence of particular data points or attributes that may significantly affect the decision-making process.
SVM is a highly adaptable supervised learning model extensively employed for classification and regression problems.The classification method employed here is distinctive as it focuses on finding the most suitable hyperplane that successfully distinguishes different classes in the feature space.The objective is to maximize the distance between the hyperplane and the nearest data points, referred to as support vectors.SVM in classification aims to identify the hyperplane that effectively separates the classes and optimizes the margin, which is the distance between the hyperplane and the nearest data points from each class.SVM seeks to obtain superior generalization performance and resilience to data noise by maximizing the margin.The data points located on the margin and closest to the hyperplane are known as support vectors.These support vectors are essential in determining the decision boundary.However, real-world data frequently lack linear separability, indicating that a solitary hyperplane cannot achieve perfect separation between the classes.In such situations, SVM employs kernel functions to transform the input data into a feature space with more dimensions.This transformation increases the probability of finding a hyperplane that can effectively separate the classes.The transformation enables SVM to effectively deal with non-linear decision boundaries by implicitly mapping the input into a higher-dimensional space where a linear separator may be applied.Kernel functions are crucial for SVM's capacity to handle non-linearities in the data.These functions calculate the inner product of data points in the input space, quantifying their similarity.SVM applies a kernel function to the input data, implicitly transforming the data into a feature space of greater dimensions.This transformation enables the possibility of achieving linear separation.In this application, Gaussian Kernel Radial Basis Function captures complex decision boundaries that conduct a non-linearity within the original space of features.SVM's adaptability allows it to capture variety of classification problems.
RF is a technique in ensemble learning that builds numerous decision trees during training and merges their predictions to enhance performance.RF is a fundamental technique in ensemble learning, known for its capacity to improve predictive accuracy by aggregating the outputs of many decision trees.RF addresses the problems of overfitting and high variance commonly associated with single decision trees.It achieves this by utilizing many trees, which introduces variety and helps to minimize these concerns, resulting in more reliable and accurate predictions.RF method functions by constructing several decision trees in the training phase.Every decision tree is trained using a bootstrap sample of the data, which involves training each tree on a randomly chosen subset of the original dataset with replacement.Bootstrap sampling creates variability among the trees, guaranteeing that each tree learns from a slightly distinct viewpoint of the data.In addition, RF offers further unpredictability at each node of every decision tree by examining only a subset of features for splitting.Instead of assessing all features to identify the best split, RF randomly chooses a subset of features and evaluates them to discover the most optimal split.This procedure introduces diversity into the model, preventing it from unnecessarily relying on any specific characteristic and encouraging the examination of other data elements.RF possesses good qualities due to the combination of bootstrapping and random feature selection.RF mitigates the risk of overfitting and enhances generalization performance by training each decision tree on a distinct subset of the data and restricting the number of features examined at each node.The RF guarantees that the combined predictions of the entire forest minimize the errors made by each individual tree.RF is particularly effective in situations where the dataset has a large number of dimensions.The method of randomly selecting features enables RF to prioritize the most informative ones and ignore unnecessary or redundant ones, making it particularly suitable for datasets with a high number of dimensions.RF has the ability to capture complex correlations between features that individual decision trees may overlook, which is achieved by integrating the predictions of numerous decision trees, each trained on a distinct subset of data and features.An ensemble approach allows the RF algorithm to effectively capture and represent complex interactions and non-linear relationships within the data, improving its ability to make accurate predictions.

Evaluation Metrics
In order to validate the described models, the ML factors of precision, recall, and F1-score were computed.These factors were based on the values of true positive (TPA), false positive (FPA), and false negative (FNA) for class A.
• TP A refers to the count of predictions made by a classifier that accurately forecast class A;

•
Recall is a metric that measures the proportion of positive class predictions produced correctly out of all the positive examples in the dataset.Unlike precision, which only evaluates the accuracy of correctly predicted positive outcomes out of all positive predictions, recall measures the number of positive predictions that were missed.In the context of multiple categorizations, recall is calculated by adding up the number of true positives for each category and dividing it by the sum of true positives and false negatives across all categories: F1-score is a unified metric that takes into account both precision and recall, providing a single score.The F-score provides a method to merge precision and recall into a single metric that encompasses both attributes.After obtaining the values for precision and recall in the multiclass classification issue, these two scores can be merged to calculate the F-Measure.Similar to precision and recall, an F-Measure score of 0.0 indicates poor performance, whereas a score of 1.0 represents the best or flawless performance: • Accuracy is the ratio of correct predictions to the total number of predictions.

Accuracy =
Correct predictions Total predictions

Results and Discussion
This section shows the performances of ML models via cross-validation.The best models were programmed into the microcontroller for further analysis in real time and code size.

Model Comparison and Selection
K-fold cross-validation is a widely used technique in ML that validates the performance of a predictive model and mitigates the risk of overfitting.The process entails dividing the dataset into K subsets or folds, training, and evaluating the model K times.For each iteration, one specific fold is set aside as the test set, while the rest of the K-1 folds are used for training.The outcomes are calculated by averaging across K iterations, guaranteeing a more robust and reliable performance estimation.In this instance, the K-fold cross-validation was performed using a value of k equal to 10.
The AI models underwent training on the host computer, which was equipped with an NVIDIA Quadro P620 featuring a Pascal GPU boasting 512 CUDA cores (NVIDIA, Santa Clara, CA, USA).The system is equipped with 2 GB of GDDR5 memory, an Intel Core i7 vPro-10850H Processor operating at a speed of 2.70 GHz, and 32 GB of RAM.The machine is manufactured by Intel, located in Santa Clara, CA, USA.
As shown in Figure 6 and Table 2, ET, LR, CART, and RF have the highest accuracy among all the models.Among these four models, the LR and RF possess the lowest standard deviation (std), showing the small variance between all 10 results.LDA has a weaker prediction capability for this specific case with an accuracy of 0.8, and SVM has a poor accuracy of 0.6.It is essential to achieve more than 0.9 of accuracy for this sleepmonitoring task to handle the proper information, so the four models with an accuracy of 0.99 are selected for a further test procedure.
Electronics 2024, 13, x FOR PEER REVIEW 10 of 15 The AI models underwent training on the host computer, which was equipped with an NVIDIA Quadro P620 featuring a Pascal GPU boasting 512 CUDA cores (NVIDIA, Santa Clara, CA, USA).The system is equipped with 2 GB of GDDR5 memory, an Intel Core i7 vPro-10850H Processor operating at a speed of 2.70 GHz, and 32 GB of RAM.The machine is manufactured by Intel, located in Santa Clara, CA, USA.
As shown in Figure 6 and Table 2, ET, LR, CART, and RF have the highest accuracy among all the models.Among these four models, the LR and RF possess the lowest standard deviation (std), showing the small variance between all 10 results.LDA has a weaker prediction capability for this specific case with an accuracy of 0.8, and SVM has a poor accuracy of 0.6.It is essential to achieve more than 0.9 of accuracy for this sleep-monitoring task to handle the proper information, so the four models with an accuracy of 0.99 are selected for a further test procedure.

Test Process
In this test, 30% of the total data are used for testing the four selected models.Overall, all of them attain highly effective results.As reported in Table 3, all three tree algorithms have the same performance, with the precision, recall, and F1-score being similar.Their predictions are almost perfect, with a small misprediction between the moving state and static position.The moving state has a recall of 0.98, which means that the models correctly identified and classified 98% of the actual instances.Static position has a precision of 0.98,

Test Process
In this test, 30% of the total data are used for testing the four selected models.Overall, all of them attain highly effective results.As reported in Table 3, all three tree algorithms have the same performance, with the precision, recall, and F1-score being similar.Their predictions are almost perfect, with a small misprediction between the moving state and static position.The moving state has a recall of 0.98, which means that the models correctly identified and classified 98% of the actual instances.Static position has a precision of 0.98, showing that, out of all instances predicted as a static position by the models, 98% are actually true static position instances.Unlike the tree algorithms, LG has an inferior performance in the test process with the wrong predictions also in the case of no person, which leads to the precision, recall, and F1-score of the other classes not being as good as the ET, CART, and RF algorithms.

Real-Time Test with STM32-Microcontroller
In this stage, the trained models of the four selected algorithms are implemented in the IoT kit STM32 B-L475-IOT01A microcontroller board.The real-time test with the embedded system includes:

•
No person is in bed with 720,000 data; • A person statically lies on the bed with 1,440,000 data; • A person moves on the bed with 180,000 data.Table 4 reports the accuracy and the code size of the ML models.All the models demonstrate a good execution with an accuracy greater than 98%.The LR model has the advantage of the lightest code size, but a lower accuracy than the other models.Since the performance of the other three algorithms are almost the same, the CART model is the most suitable model for the MCU application, with the smallest code size among the tree models.

IoT Dashboard
With the MQTT [53-56] broker by HiveMQ [57], the Node Red [58][59][60][61][62] received the ML prediction about the monitoring state via Wi-Fi, which can be observed via tele-distance as a result of the IoT technology, as shown in Figure 7. Here, the numeric data means: The dashboard on Figure 7 shows a case where, in the first period, there is no person in bed (0), then the person goes to bed and turns himself to adjust the sleep position, causing a strong motion (2), then lays in a stationary position (1).In this way, the sleep routine of the concerned person will be monitored effectively from a far distance via Wi-Fi based on IoT communication.

Discussion
This paper outlines the approaches and results of our study, which entailed gathering and examining more than 216,000 pieces of acceleration data of X, Y, and Z axes points.The difference of two consecutive samples are elaborated, and each of the 200 samples formed a window.These windows are the ML instances for the purpose of training and validating the ML algorithms.The results emphasize the outstanding performance of the chosen Machine-Learning models, reaching accuracy rates that surpass 99% in tests conducted on workstations.The CART technique stood out among the best models in terms of performance, showing both a high accuracy and the lightest code size that is suitable for effective deployment on a IoT microcontroller with limited memory.The monitoring process is carried out via the MQTT protocol and Node-RED development tool.

Conclusions
In conclusion, this study demonstrates the significant potential of integrating MEMS accelerometers, IoT frameworks, and Machine-Learning algorithms for automated sleep pattern analysis.By accurately detecting individuals in a bed and classifying their sleep states in real time, this approach offers promising avenues for enhancing patient outcomes, particularly in remote monitoring and elderly care scenarios.The findings underscore the transformative impact of technology-driven healthcare solutions and highlight the importance of continued innovation in this field to realize its full potential in improving healthcare delivery and patient well-being.
Three algorithms, ET, CART, and RF, accomplish the best performances.
LG also has a high efficiency with a light code size, although its accuracy is still lower than that of the tree models.This work shows the potential of the automated sleep pattern analysis of a real-time ML model implementation.By remotely monitoring sleep patterns, healthcare professionals can tailor interventions and treatment plans based on individualized data insights, enhancing patient outcomes and fortifying elderly care.Furthermore, the scalability and accessibility afforded by IoT devices promise to democratize access to sleepmonitoring services, ensuring equitable healthcare provision across diverse populations.
As we navigate towards this future, it is imperative to harness the full potential of these technologies to realize their transformative impact on healthcare delivery and improve the lives of individuals worldwide.In future work, more algorithms with more tests will be carried out for the developed platform.Other types of beds and more patients will be tested with the developed system.

Discussion
This paper outlines the approaches and results of our study, which entailed gathering and examining more than 216,000 pieces of acceleration data of X, Y, and Z axes points.The difference of two consecutive samples are elaborated, and each of the 200 samples formed a window.These windows are the ML instances for the purpose of training and validating the ML algorithms.The results emphasize the outstanding performance of the chosen Machine-Learning models, reaching accuracy rates that surpass 99% in tests conducted on workstations.The CART technique stood out among the best models in terms of performance, showing both a high accuracy and the lightest code size that is suitable for effective deployment on a IoT microcontroller with limited memory.The monitoring process is carried out via the MQTT protocol and Node-RED development tool.

Conclusions
In conclusion, this study demonstrates the significant potential of integrating MEMS accelerometers, IoT frameworks, and Machine-Learning algorithms for automated sleep pattern analysis.By accurately detecting individuals in a bed and classifying their sleep states in real time, this approach offers promising avenues for enhancing patient outcomes, particularly in remote monitoring and elderly care scenarios.The findings underscore the transformative impact of technology-driven healthcare solutions and highlight the importance of continued innovation in this field to realize its full potential in improving healthcare delivery and patient well-being.
Three algorithms, ET, CART, and RF, accomplish the best performances.
LG also has a high efficiency with a light code size, although its accuracy is still lower than that of the tree models.This work shows the potential of the automated sleep pattern analysis of a real-time ML model implementation.By remotely monitoring sleep patterns, healthcare professionals can tailor interventions and treatment plans based on individualized data insights, enhancing patient outcomes and fortifying elderly care.Furthermore, the scalability and accessibility afforded by IoT devices promise to democratize access to sleep-monitoring services, ensuring equitable healthcare provision across diverse populations.
As we navigate towards this future, it is imperative to harness the full potential of these technologies to realize their transformative impact on healthcare delivery and improve the lives of individuals worldwide.In future work, more algorithms with more tests will be carried out for the developed platform.Other types of beds and more patients will be tested with the developed system.

Figure 3 .
Figure 3. Encapsulated accelerometer and MCU platform under the bed frame.

Figure 3 .
Figure 3. Encapsulated accelerometer and MCU platform under the bed frame.

Figure 3 .
Figure 3. Encapsulated accelerometer and MCU platform under the bed frame.
• FP A refers to the count of objects that are not part of class A, yet are incorrectly classified as class A; • FN A represents the count of objects belonging to class A that are anticipated to belong to a different class; • Precision measures the proportion of positive class predictions that truly belong to the positive class.The calculation involves adding up the number of true positives for each class and dividing it by the sum of true positives and false positives for all classes:

• 0 : 2 :
No person on bed; • 1: Person stays in static position on the bed; • Person moves on bed.

Table 1 .
ML feature and classification.

Table 3 .
Test result on the selected models.

Table 4 .
Real-time test with microcontroller.