// Thư viện cần thiết
#include <Wire.h>
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
#include <MAX30100_PulseOximeter.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// Cấu hình OLED
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// Cấu hình Blynk
#define BLYNK_TEMPLATE_ID "TMPLxxxxxx" // Thay thế bằng Template ID của bạn
#define BLYNK_DEVICE_NAME "Heart Monitor"
#define BLYNK_AUTH_TOKEN "YourAuthToken" // Thay thế bằng Auth Token của bạn
// Cấu hình Wi-Fi
char ssid[] = "YourWiFiSSID"; // Tên mạng Wi-Fi
char pass[] = "YourWiFiPassword"; // Mật khẩu Wi-Fi
// Cấu hình MAX30100
#define REPORTING_PERIOD_MS 1000 // Thời gian báo cáo (ms)
PulseOximeter pox;
uint32_t tsLastReport = 0;
// Biến lưu trữ dữ liệu
float heartRate = 0;
float spO2 = 0;
// Timer cho Blynk
BlynkTimer timer;
// Callback cho sự kiện nhịp tim mới
void onBeatDetected() {
Serial.println("Beat!");
// Có thể thêm mã để nhấp nháy LED hoặc tương tự
}
// Hàm đọc dữ liệu từ cảm biến và gửi đến Blynk
void readSensorData() {
// Cập nhật cảm biến
pox.update();
// Đảm bảo đủ thời gian giữa các lần báo cáo
if (millis() - tsLastReport > REPORTING_PERIOD_MS) {
// Lấy dữ liệu mới
heartRate = pox.getHeartRate();
spO2 = pox.getSpO2();
// In dữ liệu ra Serial Monitor
Serial.print("Heart rate: ");
Serial.print(heartRate);
Serial.print(" bpm / SpO2: ");
Serial.print(spO2);
Serial.println(" %");
// Hiển thị dữ liệu trên OLED
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println("Heart Rate Monitor");
display.setTextSize(2);
display.setCursor(0, 16);
display.print("HR: ");
display.print(heartRate, 1);
display.println(" BPM");
display.setCursor(0, 40);
display.print("SpO2: ");
display.print(spO2, 1);
display.println(" %");
display.display();
// Gửi dữ liệu đến Blynk
Blynk.virtualWrite(V0, heartRate);
Blynk.virtualWrite(V1, spO2);
// Kiểm tra ngưỡng và gửi thông báo nếu cần
if (heartRate > 100 || heartRate < 60) {
Blynk.logEvent("heart_rate_alert", "Heart rate abnormal: " + String(heartRate) + " BPM");
}
if (spO2 < 95) {
Blynk.logEvent("spo2_alert", "SpO2 level low: " + String(spO2) + " %");
}
tsLastReport = millis();
}
}
void setup() {
// Khởi tạo Serial Monitor
Serial.begin(115200);
Serial.println("Heart Rate Monitor");
// Khởi tạo màn hình OLED
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println("Initializing...");
display.display();
// Kết nối Wi-Fi và Blynk
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
// Khởi tạo cảm biến MAX30100
Serial.print("Initializing pulse oximeter..");
display.setCursor(0, 16);
display.println("Initializing sensor");
display.display();
if (!pox.begin()) {
Serial.println("FAILED");
display.setCursor(0, 32);
display.println("SENSOR FAILED!");
display.display();
for(;;);
} else {
Serial.println("SUCCESS");
display.setCursor(0, 32);
display.println("SENSOR OK!");
display.display();
}
// Cấu hình cảm biến
pox.setIRLedCurrent(MAX30100_LED_CURR_7_6MA);
// Đăng ký callback cho nhịp tim
pox.setOnBeatDetectedCallback(onBeatDetected);
// Thiết lập timer cho Blynk
timer.setInterval(1000L, readSensorData);
delay(2000);
}
void loop() {
Blynk.run();
timer.run();
pox.update();
}