本文主要是介绍【开源】ESP8266_MAX30102_OLED屏幕实现血氧心率检测,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
本系列,是记录自己入门嵌入式领域的学习笔记。
看看能坚持多久。
今天是2023年5月2日
源代码的来源:https://github.com/yangqingyuan-byte/MAX30102-0.96_4pin_oled-esp8266_HR_and_SPO2
项目需求:
ESP8266_MAX30102_OLED屏幕实现血氧心率检测
硬件连接:
* ESP8266 --- OLED
* GND --- GND *
* 5V --- VCC
* D1 --- SCL
* D2 --- SDA
* ESP8266 --- MAX30102
* 5V --- VIN
* G --- GND
* D0 --- INT
* D1 --- SCL
* D2 --- SDA
程序源码:
引入库
#include <U8g2lib.h>
#include <SPI.h>
#include <Wire.h>
#include "algorithm_by_RF.h"
#include "max30102.h"
这段代码引入了需要用到的库。其中 U8g2lib 是一个用于字体显示的库;SPI、Wire 分别是用于支持 SPI 和 I2C 协议的库;algorithm_by_RF 和 max30102 分别是根据最大值和最小值计算心率和血氧的自定义库。
// uncomment below line if cannot calculate readings
#define REVERSE_LED
这里定义了 REVERSE_LED
宏。如果无法计算读数,则应取消此行代码的注释。
OLED 屏幕初始化
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE);
定义了一个 U8G2_SSD1306_128X64_NONAME_F_SW_I2C 类型的对象 u8g2
,这个对象代表了 OLED 屏幕,并把屏幕连接到 I2C 总线上。
void setup()
{pinMode(oxiInt, INPUT); // pin D0 connects to the interrupt output pin of the MAX30102u8g2.begin();u8g2.enableUTF8Print();u8g2.clearBuffer();delay(2000);u8g2.setFont(u8g2_font_unifont_tr);u8g2.setCursor(0, 14);u8g2.print("Initializing...");u8g2.sendBuffer();Wire.begin();Serial.begin(115200);Serial.println("Initializing");maxim_max30102_reset(); // resets the MAX30102maxim_max30102_read_reg(REG_INTR_STATUS_1, &uch_dummy); // Reads/clears the interrupt status registermaxim_max30102_init(); // initialize the MAX30102old_n_spo2 = 0.0;Serial.println(F("Time[s]\tSpO2\tHR\tRatio\tCorr"));timeStart = millis();
}
在 setup()
函数中,首先将 oxiInt
引脚设置为输入模式(用于连接到 MAX30102 的中断输出引脚),然后对 OLED 屏幕进行初始化操作,在开机画面上输出 "Initializing..."。接着使用 Wire
库初始化 I2C 总线,打开串口输出,并分别进行了 MAX30102 的复位、中断状态清除和初始化操作。最后输出表头 "Time[s]\tSpO2\tHR\tRatio\tCorr",并记录开始时间。
显示心率和血氧值
void print_hr_spo2(int val_hr, int val_spo2){if(val_hr > 999 || val_hr < 0){return;}if(val_spo2 > 100 || val_spo2 < 0){return;}u8g2.clearBuffer();u8g2.setFont(u8g2_font_fub14_tr); u8g2.setCursor(0,14);u8g2.print(" HR");u8g2.setCursor(60,14);u8g2.print("SPO2");char str_hr[4]; u8g2.setFont(u8g2_font_fur20_tr);itoa(val_hr, str_hr, 10);if(val_hr > 99){u8g2.drawStr(0, 38, str_hr);}else if(val_hr > 9){u8g2.drawStr(14,38, str_hr);}else{u8g2.drawStr(28,38, str_hr);}char str_spo2[4];u8g2.setFont(u8g2_font_fur30_tr); itoa(val_spo2, str_spo2, 10); if(val_spo2 > 99){u8g2.drawStr(55, 48, str_spo2);}else if(val_spo2 > 9){u8g2.drawStr(70, 48, str_spo2);}else{u8g2.drawStr(85, 48, str_spo2); }u8g2.setFont(u8g2_font_fur11_tr); u8g2.setCursor(13,50);u8g2.print("bpm");u8g2.setCursor(110,64);u8g2.print("%");u8g2.sendBuffer();
}
在 print_hr_spo2()
函数中,首先判断心率和血氧值是否在有效范围内(0~999 和 0~100),然后清空 OLED 屏幕缓存,并设置字体和显示位置。接着分别将心率和血氧值转化为字符串类型,并根据值的长度和位置绘制在屏幕上,最后输出单位并发送缓存。
void print_measuring(){u8g2.clearBuffer();u8g2.setFont(u8g2_font_fub14_tr); u8g2.setCursor(0,14);u8g2.print(" HR");u8g2.setCursor(60,14);u8g2.print("SPO2");char str_hr[4]; u8g2.setFont(u8g2_font_fur20_tr);u8g2.drawStr(10, 38, "---");char str_spo2[4];u8g2.setFont(u8g2_font_fur30_tr); u8g2.drawStr(65, 48, "---");u8g2.setFont(u8g2_font_unifont_tr);u8g2.setCursor(26,62);u8g2.print("Measuring..."); u8g2.sendBuffer();
}
在 print_measuring()
函数中,清空 OLED 屏幕缓存,并设置字体和显示位置。接着绘制未测量状态的心率和血氧值,并输出 "Measuring...",最后发送缓存。
void print_press(){u8g2.clearBuffer();u8g2.setFont(u8g2_font_fub14_tr); u8g2.setCursor(0,14);u8g2.print(" HR");u8g2.setCursor(60,14);u8g2.print("SPO2");char str_hr[4]; u8g2.setFont(u8g2_font_fur20_tr);u8g2.drawStr(10, 38, "---");char str_spo2[4];u8g2.setFont(u8g2_font_fur30_tr); u8g2.drawStr(65, 48, "---");u8g2.setFont(u8g2_font_unifont_tr);u8g2.setCursor(20,62);u8g2.print("Keep pressing"); u8g2.sendBuffer();
}
在 print_press()
函数中,清空 OLED 屏幕缓存,并设置字体和显示位置。接着绘制要求将指尖放到传感器上。
完整代码如下:
#include <U8g2lib.h>
#include <SPI.h>
#include <Wire.h>
#include "algorithm_by_RF.h"
#include "max30102.h"
#define REVERSE_LEDU8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE);const byte oxiInt = 16; // pin connected to MAX30102 INTuint32_t elapsedTime, timeStart;uint32_t aun_ir, aun_red;
uint32_t aun_ir_buffer[BUFFER_SIZE]; // infrared LED sensor data
uint32_t aun_red_buffer[BUFFER_SIZE]; // red LED sensor data
float old_n_spo2; // Previous SPO2 value
uint8_t uch_dummy;
bool showMeasuring = false;void setup()
{pinMode(oxiInt, INPUT); // pin D0 connects to the interrupt output pin of the MAX30102u8g2.begin();u8g2.enableUTF8Print();u8g2.clearBuffer();delay(2000);u8g2.setFont(u8g2_font_unifont_tr);u8g2.setCursor(0, 14);u8g2.print("Initializing...");u8g2.sendBuffer();Wire.begin();Serial.begin(115200);Serial.println("Initializing");maxim_max30102_reset(); // resets the MAX30102maxim_max30102_read_reg(REG_INTR_STATUS_1, &uch_dummy); // Reads/clears the interrupt status registermaxim_max30102_init(); // initialize the MAX30102old_n_spo2 = 0.0;Serial.println(F("Time[s]\tSpO2\tHR\tRatio\tCorr"));timeStart = millis();
}void print_hr_spo2(int val_hr, int val_spo2){if(val_hr > 999 || val_hr < 0){return;}if(val_spo2 > 100 || val_spo2 < 0){return;}u8g2.clearBuffer();u8g2.setFont(u8g2_font_fub14_tr); u8g2.setCursor(0,14);u8g2.print(" HR");u8g2.setCursor(60,14);u8g2.print("SPO2");char str_hr[4]; u8g2.setFont(u8g2_font_fur20_tr);itoa(val_hr, str_hr, 10);if(val_hr > 99){u8g2.drawStr(0, 38, str_hr);}else if(val_hr > 9){u8g2.drawStr(14,38, str_hr);}else{u8g2.drawStr(28,38, str_hr);}char str_spo2[4];u8g2.setFont(u8g2_font_fur30_tr); itoa(val_spo2, str_spo2, 10); if(val_spo2 > 99){u8g2.drawStr(55, 48, str_spo2);}else if(val_spo2 > 9){u8g2.drawStr(70, 48, str_spo2);}else{u8g2.drawStr(85, 48, str_spo2); }u8g2.setFont(u8g2_font_fur11_tr); u8g2.setCursor(13,50);u8g2.print("bpm");u8g2.setCursor(110,64);u8g2.print("%");u8g2.sendBuffer();
}void print_measuring(){u8g2.clearBuffer();u8g2.setFont(u8g2_font_fub14_tr); u8g2.setCursor(0,14);u8g2.print(" HR");u8g2.setCursor(60,14);u8g2.print("SPO2");char str_hr[4]; u8g2.setFont(u8g2_font_fur20_tr);u8g2.drawStr(10, 38, "---");char str_spo2[4];u8g2.setFont(u8g2_font_fur30_tr); u8g2.drawStr(65, 48, "---");u8g2.setFont(u8g2_font_unifont_tr);u8g2.setCursor(26,62);u8g2.print("Measuring..."); u8g2.sendBuffer();
}void print_press(){u8g2.clearBuffer();u8g2.setFont(u8g2_font_fub14_tr); u8g2.setCursor(0,14);u8g2.print(" HR");u8g2.setCursor(60,14);u8g2.print("SPO2");char str_hr[4]; u8g2.setFont(u8g2_font_fur20_tr);u8g2.drawStr(10, 38, "---");char str_spo2[4];u8g2.setFont(u8g2_font_fur30_tr); u8g2.drawStr(65, 48, "---");u8g2.setFont(u8g2_font_unifont_tr);u8g2.setCursor(20,62);u8g2.print("Keep pressing"); u8g2.sendBuffer();
}void loop()
{// Serial.println("looping");float n_spo2, ratio, correl; // SPO2 valueint8_t ch_spo2_valid; // indicator to show if the SPO2 calculation is validint32_t n_heartrate; // heart rate valueint8_t ch_hr_valid; // indicator to show if the heart rate calculation is validint32_t i;// buffer length of BUFFER_SIZE stores ST seconds of samples running at FS sps// read BUFFER_SIZE samples, and determine the signal rangefor (i = 0; i < BUFFER_SIZE; i++){while (digitalRead(oxiInt) == 1); // wait until the interrupt pin assertsdelay(1);// wdt_reset();
#ifdef REVERSE_LEDmaxim_max30102_read_fifo(&aun_ir, &aun_red); // read from MAX30102 FIFO
#elsemaxim_max30102_read_fifo(&aun_red, &aun_ir); // read from MAX30102 FIFO
#endifif (aun_ir < 5000){break;}if (i == 0){if(showMeasuring){print_measuring();showMeasuring = false;}Serial.print("Measuring...");}*(aun_ir_buffer + i) = aun_ir;*(aun_red_buffer + i) = aun_red;}if (aun_ir < 5000){print_press();showMeasuring = true;Serial.print("Put On Finger");}else{// calculate heart rate and SpO2 after BUFFER_SIZE samples (ST seconds of samples) using Robert's methodrf_heart_rate_and_oxygen_saturation(aun_ir_buffer, BUFFER_SIZE, aun_red_buffer, &n_spo2, &ch_spo2_valid, &n_heartrate, &ch_hr_valid, &ratio, &correl);// Serial.println("rf_heart_rate_and_oxygen_saturation");elapsedTime = millis() - timeStart;elapsedTime /= 1000; // Time in secondsif (ch_hr_valid && ch_spo2_valid){Serial.print(elapsedTime);Serial.print("\t");Serial.print(n_spo2);Serial.print("\t");Serial.print(n_heartrate, DEC);Serial.print("\t");Serial.print(ratio);Serial.print("\t");Serial.print(correl);Serial.println("");print_hr_spo2(n_heartrate, (int)(n_spo2+0.5));old_n_spo2 = n_spo2;}}
}
这篇关于【开源】ESP8266_MAX30102_OLED屏幕实现血氧心率检测的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!