-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglucosemonitor.ino
More file actions
181 lines (152 loc) · 4.64 KB
/
glucosemonitor.ino
File metadata and controls
181 lines (152 loc) · 4.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/**
* NON-INVASIVE GLUCOSE MONITOR – FINAL VERSION
* Hardware:
* - ESP32 DevKit
* - MAX30102 / MAX30105
* - SSD1306 OLED (128x64)
*/
#include <Wire.h>
#include "MAX30105.h"
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "model_coefficients.h"
// ================= DISPLAY =================
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// ================= SENSOR =================
MAX30105 particleSensor;
// ================= CONFIG =================
#define SAMPLE_RATE 100
#define BUFFER_SIZE 200
#define FEATURE_INTERVAL 5000
float irBuffer[BUFFER_SIZE];
float redBuffer[BUFFER_SIZE];
unsigned long timeBuffer[BUFFER_SIZE];
int bufferIndex = 0;
unsigned long lastFeatureTime = 0;
float lastGlucosePrediction = 0;
// ================= ML PREDICTION =================
float predictGlucose(float ratio, float variability, float slope) {
float z_ratio = (ratio - mean_ratio) / std_ratio;
float z_variability = (variability - mean_variability) / std_variability;
float z_slope = (slope - mean_slope) / std_slope;
float glucose =
intercept +
coeff_ratio * z_ratio +
coeff_variability * z_variability +
coeff_slope * z_slope;
return glucose;
}
// ================= UTILS =================
float mean(float *buf, int n) {
float s = 0;
for (int i = 0; i < n; i++) s += buf[i];
return s / n;
}
// ================= SETUP =================
void setup() {
Serial.begin(115200);
Wire.begin(18, 19);
// OLED
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println("OLED not found");
while (1);
}
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(0, 0);
display.println("Glucose Monitor");
display.println("Initializing...");
display.display();
// MAX30102
if (!particleSensor.begin(Wire, I2C_SPEED_FAST)) {
Serial.println("MAX30102 not found");
while (1);
}
particleSensor.setup(
0x1F, // LED brightness
4, // sample avg
2, // red + IR
SAMPLE_RATE,
411, // pulse width
4096 // ADC range
);
delay(2000);
display.clearDisplay();
display.setCursor(0, 0);
display.println("Place finger");
display.display();
Serial.println("System Ready");
}
// ================= LOOP =================
void loop() {
// Capture PPG
if (bufferIndex < BUFFER_SIZE) {
irBuffer[bufferIndex] = particleSensor.getIR();
redBuffer[bufferIndex] = particleSensor.getRed();
timeBuffer[bufferIndex] = millis();
bufferIndex++;
delay(1000 / SAMPLE_RATE);
return;
}
// Shift buffer
for (int i = 1; i < BUFFER_SIZE; i++) {
irBuffer[i - 1] = irBuffer[i];
redBuffer[i - 1] = redBuffer[i];
timeBuffer[i - 1] = timeBuffer[i];
}
bufferIndex = BUFFER_SIZE - 1;
// Feature extraction every interval
if (millis() - lastFeatureTime > FEATURE_INTERVAL) {
lastFeatureTime = millis();
float irMean = mean(irBuffer, BUFFER_SIZE);
float redMean = mean(redBuffer, BUFFER_SIZE);
if (irMean < 50000) {
display.clearDisplay();
display.setCursor(0, 0);
display.println("No Finger");
display.display();
return;
}
// Feature 1: Ratio
float ratio = irMean / redMean;
// Feature 2: Variability (RMSSD)
float var = 0;
for (int i = 1; i < BUFFER_SIZE; i++) {
float d = irBuffer[i] - irBuffer[i - 1];
var += d * d;
}
var = sqrt(var / (BUFFER_SIZE - 1));
// Feature 3: Slope
int maxI = 0, minI = 0;
float maxV = irBuffer[0], minV = irBuffer[0];
for (int i = 1; i < BUFFER_SIZE; i++) {
if (irBuffer[i] > maxV) { maxV = irBuffer[i]; maxI = i; }
if (irBuffer[i] < minV) { minV = irBuffer[i]; minI = i; }
}
float slope = 0;
if (maxI > minI) {
slope = (maxV - minV) /
(timeBuffer[maxI] - timeBuffer[minI]) * 1000.0;
}
// ================= ML PREDICTION =================
float glucose = predictGlucose(ratio, var, slope);
lastGlucosePrediction = glucose;
// ================= OUTPUT =================
Serial.print("Glucose: ");
Serial.print(glucose, 1);
Serial.println(" mg/dL");
display.clearDisplay();
display.setCursor(0, 0);
display.setTextSize(1);
display.println("Predicted Glucose");
display.setTextSize(2);
display.setCursor(0, 20);
display.print(glucose, 1);
display.println(" mg/dL");
display.display();
}
}