Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
107 lines (100 loc) · 3.44 KB
/
Copy pathmain.cpp
File metadata and controls
107 lines (100 loc) · 3.44 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
#include <Arduino.h>
#include <Wire.h>
#include <DFRobot_BloodOxygen_S.h>
// WiFi/webserver removed per user request
// Hardware pins
const int FSR_PIN = 33; // ADC1_CH6 on ESP32 (input-only)
// I2C for PPG
const int I2C_SDA = 21;
const int I2C_SCL = 22;
// DFRobot PPG instance (I2C address default 0x57)
DFRobot_BloodOxygen_S_I2C dfpg(&Wire, 0x57);
bool ppgFound = false;
uint32_t latestIR = 0;
uint32_t latestRed = 0;
void setup() {
Serial.begin(9600);
// Initialize PPG sensor (DFRobot library)
Wire.begin(I2C_SDA, I2C_SCL);
if (dfpg.begin()) {
ppgFound = true;
Serial.println("DFRobot BloodOxygen sensor found");
dfpg.sensorStartCollect();
} else {
ppgFound = false;
Serial.println("DFRobot BloodOxygen sensor NOT found");
// I2C scan hint
Serial.print("I2C scan: ");
bool any = false;
for (uint8_t addr = 1; addr < 127; ++addr) {
Wire.beginTransmission(addr);
if (Wire.endTransmission() == 0) {
Serial.print("0x"); if (addr < 16) Serial.print('0');
Serial.print(addr, HEX); Serial.print(' ');
any = true;
}
}
if (!any) Serial.print("(no devices found)");
Serial.println();
}
Serial.println("Sensor initialized.");
}
// Helper: read raw registers directly via I2C (similar to library's readReg)
int readI2CRegs(uint8_t reg_addr, uint8_t *data_buf, uint8_t len) {
int i = 0;
Wire.beginTransmission(0x57);
Wire.write(reg_addr);
if (Wire.endTransmission() != 0) {
return -1;
}
Wire.requestFrom((uint8_t)0x57, (uint8_t)len);
while (Wire.available() && i < len) {
data_buf[i++] = Wire.read();
}
return i;
}
void loop() {
// Read FSR (analog) - single sample, minimal code
int fsrRaw = analogRead(FSR_PIN); // 0-4095
float fsrVoltage = (fsrRaw / 4095.0) * 3.3;
// Read PPG values using DFRobot API (do not change FSR logic)
int spo2 = -1;
int heartbeat = -1;
if (ppgFound) {
dfpg.getHeartbeatSPO2();
spo2 = dfpg._sHeartbeatSPO2.SPO2;
heartbeat = dfpg._sHeartbeatSPO2.Heartbeat;
// For plotting also try to get raw IR/Red if available via underlying registers
uint8_t rbuf[32];
int got = readI2CRegs(0x0C, rbuf, 16); // read more than the 8 bytes the lib reads
if (got >= 12) {
// heuristic: interpret bytes 8..11 as IR and 12..15 as RED (if present)
uint32_t ir = 0;
uint32_t red = 0;
if (got >= 12) {
ir = ((uint32_t)rbuf[8] << 24) | ((uint32_t)rbuf[9] << 16) | ((uint32_t)rbuf[10] << 8) | ((uint32_t)rbuf[11]);
}
if (got >= 16) {
red = ((uint32_t)rbuf[12] << 24) | ((uint32_t)rbuf[13] << 16) | ((uint32_t)rbuf[14] << 8) | ((uint32_t)rbuf[15]);
}
latestIR = ir;
latestRed = red;
// Print a small diagnostic if values look sensible
if (ir != 0 || red != 0) {
Serial.print(" IR:"); Serial.print(ir);
Serial.print(" RED:"); Serial.print(red);
}
}
}
// Webserver removed — no remote globals to update
// Print a single labeled line and overwrite it each loop using carriage return
// Example: "FSR raw: 1023 V: 0.826 SPO2: 98 HR: 72\r"
char buf[160];
// Start with carriage return so the terminal cursor returns to line start
int n = snprintf(buf, sizeof(buf), "\rFSR raw:%4d V:%.3f SPO2:%3d HR:%3d IR:%lu RED:%lu", fsrRaw, fsrVoltage, spo2, heartbeat, latestIR, latestRed);
// Pad the rest to clear previous longer content
for (int i = n; i < 156; ++i) buf[i] = ' ';
buf[156] = '\0';
Serial.print(buf);
delay(200);
}