# An IoT-Based Glucose Monitoring Algorithm to Prevent Diabetes Complications
## 3 System Architecture  
According to the literature review, recently, IoT architectures have shown remarkable results when they are used for glucose monitoring. Our proposal is supported by an architecture of this type, as shown in Figure 1. The principle of operation is based on a wireless sensor worn by an elderly patient. No intervention of other people is required. Due to restrictions, the readings were obtained from a patient using a traditional glucometer and the values were send to the sensing stage. It is necessary to define the period for each reading. The sensor is connected to an IoT device (NodeMCU) using a wired connection. We configured our device and router to create a home area network. Once the chip is connected to the network, it handles the readings obtained by the sensor and sends them using Wi-Fi to a router with an Internet connection. The information is received by a server based on Raspberry Pi, where the algorithm is stored. Here the data is processed to produce a prognosis using historical values. Each reading is sent to a web server to be displayed. Finally, the glucose level is shown to relatives and caregivers through a web page. Depending on the readings values, the system sends the pertinent notifications. For a better understanding, we divide the proposal into the following three layers: (1) perception, (2) network, and (3) application. The perception layer is responsible for obtaining the patient's glucose levels. A noninvasive wireless sensor is proposed. The glucose sensor is connected to an open-source platform, such as NodeMCU. This device has a microcontroller and a 10-bit resolution A/D converter. With the A/D converter it is possible to transform the readings to digital values. The connection is shown in Figure 2.  
Figure 1: System architecture. The network layer takes advantage of the NodeMCU Wi-Fi feature to communicate wirelessly to the router. The function of this device is to provide communication over the Internet and to send glucose values to the server. The application layer has a web server based on Raspberry Pi and a database developed in SQL (structured query language). The elements that make up the server, as well as the suggested sequence of installation of each of the packages, are presented in Figure 3. All these elements are supported in an open environment such as Linux. We can access the database locally and remotely to consult the records of different users, as well as the history of the readings without depending on a hosting service. The prognosis algorithm is also stored in the server and will be responsible for analyzing the history of the appraisals. With the algorithm, we can predict a trend and determine if it is necessary to send an alert via SMS to the doctor and relatives or if it is simply a peak that could be a false alarm. The system can take readings of the glucose level of diverse patients for a given period previously determined by the doctor (minutes or hours). The values obtained are stored in the data base. The algorithm uses the readings (\(Y_{t}\)), to calculate an average of the historical readings through the double moving average technique. First, it performs an average (\(\hat{Y}_{t+1}\)) over n periods from the readings. \[\hat{Y}_{t+1}\ =\ \frac{Y_{t}+Y_{t-1}+Y_{t-2}\ldots\ldots+Y_{t-n+1}}{n} \tag{1}\]  
Given \(M_{1}=\hat{Y}_{t+1}\). Next, a second average \(M^{\prime}_{t}\) is calculated as the average of the first. \[M^{\prime}_{t}\ =\ \frac{M_{t}+M_{t-1}+M_{t-2}+M_{t-2}\ldots\ldots+M_{t-n+1}}{n} \tag{2}\]  
The following equation is used to calculate the difference between the averages:  
\[a\ =\ 2M_{t}-M^{\prime}_{t}, \tag{3}\]  
Subsequently, an additional adjustment factor is calculated, such as the measurement of a slope that changes over time. \[b_{t}\ =\ \frac{2}{n-1}\ (M_{t}-M^{\prime}_{t}), \tag{4}\]  
Finally, the following equation is used to make the prognosis of p periods in the future. \[\hat{Y}_{t+p}\ =\ a+b_{t}p, \tag{5}\]  
where  
\(n=\) number of periods defined to calculate the average.