当前位置:   article > 正文

【Esp32】用esp32和max30102制作一个血氧仪_esp32 max30102

esp32 max30102

【物资准备】

esp32-wroom

max30102

点灯科技账号or阿里云物联网平台账号

(本来是想用屏幕去显示的,但是我发现我的esp32只有一个iic引出来,而max30102模块正好是iic通讯,我又懒得弄spi的屏幕,所以用手机app或者网页去起到显示数据的一个作用了)

1、阿里云物联网平台

先上代码

  1. #include <WiFi.h>
  2. #include <PubSubClient.h>
  3. #include <ArduinoJson.h>
  4. // 这里添加要连的wifi名称和密码
  5. const char* ssid = "";
  6. const char* password = "";
  7. /* 设备证书信息,根据情况修改*/
  8. #define PRODUCT_KEY ""
  9. #define DEVICE_NAME ""
  10. #define DEVICE_SECRET ""
  11. #define REGION_ID "cn-shanghai"
  12. #define CLIENT_ID ""
  13. #define MQTT_PASSWD ""
  14. #define ALINK_BODY_FORMAT "{\"params\":%s}"
  15. #define ALINK_TOPIC_PROP_POST "/sys/" PRODUCT_KEY "/" DEVICE_NAME "/thing/event/property/post"
  16. /* 线上环境域名和端口号,不需要改 */
  17. #define MQTT_SERVER PRODUCT_KEY ".iot-as-mqtt." REGION_ID ".aliyuncs.com"
  18. #define MQTT_PORT 1883
  19. #define MQTT_USRNAME DEVICE_NAME "&" PRODUCT_KEY
  20. WiFiClient espClient;
  21. PubSubClient client(espClient);
  22. void WiFiInit()
  23. {
  24. WiFi.begin(ssid, password);
  25. while(WiFi.status() != WL_CONNECTED){
  26. delay(500);
  27. Serial.print(".");
  28. }
  29. Serial.println("WIFI CONNECTED!");
  30. }
  31. // 连接MQTT,每隔5s自动重连
  32. void mqttCheckConnect()
  33. {
  34. while(!client.connected())
  35. {
  36. Serial.println("Connecting to MQTT Server ...");
  37. if(client.connect(CLIENT_ID, MQTT_USRNAME, MQTT_PASSWD))
  38. {
  39. Serial.println("MQTT Connected!");
  40. }
  41. else
  42. {
  43. Serial.print("MQTT Connect err:");
  44. Serial.println(client.state());
  45. delay(5000);
  46. }
  47. }
  48. }
  49. // publish(ESP32发送,阿里云接收)
  50. void mqttIntervalPost()
  51. {
  52. char param[32]; // 存放参数数据
  53. char jsonBuf[128]; // 存放json格式数据
  54. sprintf(param, "{\"XY\":%d}", 32); //这是测试的发送数据
  55. sprintf(jsonBuf, ALINK_BODY_FORMAT, param);
  56. Serial.println(jsonBuf);
  57. if(client.publish(ALINK_TOPIC_PROP_POST, jsonBuf))
  58. {
  59. Serial.println("Publish success!");
  60. }
  61. else
  62. {
  63. Serial.println("Publish error!");
  64. }
  65. }
  66. void setup()
  67. {
  68. Serial.begin(115200);
  69. WiFiInit();
  70. client.setServer(MQTT_SERVER, MQTT_PORT);
  71. client.setCallback(callback);
  72. }
  73. void loop()
  74. {
  75. if(!client.connected())
  76. {
  77. mqttCheckConnect();
  78. //mqttIntervalPost();
  79. }
  80. mqttIntervalPost();//定期发送血氧数据
  81. client.loop();
  82. delay(500);
  83. }

阿里云我用的是物联网平台接收数据+物联网平台内置的lot Studio来实现网页上线显示数据

2、用点灯科技来实现app

先直接上代码,用的是blinker库

  1. #define BLINKER_PRINT Serial
  2. #define BLINKER_WIFI
  3. #include <Blinker.h>
  4. char auth[] = "";
  5. char ssid[] = "";
  6. char pswd[] = "";
  7. // 新建组件对象
  8. BlinkerNumber Number1("num-gwy"); //xueyang
  9. BlinkerNumber Number2("num-xoe"); //xinlv
  10. void setup() {
  11. // put your setup code here, to run once:
  12. // 初始化串口
  13. Serial.begin(115200);
  14. // 初始化有LED的IO
  15. pinMode(LED_BUILTIN, OUTPUT);
  16. digitalWrite(LED_BUILTIN, HIGH);
  17. // 初始化blinker
  18. Blinker.begin(auth, ssid, pswd);
  19. }
  20. void loop() {
  21. Number1.print(93); //血氧测试数据
  22. Blinker.run();
  23. }

外面的资料都比较少,只能看官方文档

3、MAX30102

直接上代码

  1. void setup() {
  2. // put your setup code here, to run once:
  3. // 初始化串口
  4. Serial.begin(115200);
  5. #if defined(BLINKER_PRINT)
  6. BLINKER_DEBUG.stream(BLINKER_PRINT);
  7. #endif
  8. // 初始化有LED的IO
  9. pinMode(LED_BUILTIN, OUTPUT);
  10. digitalWrite(LED_BUILTIN, HIGH);
  11. // 初始化blinker
  12. Blinker.begin(auth, ssid, pswd);
  13. // Initialize sensor
  14. if (!particleSensor.begin(Wire, I2C_SPEED_FAST)) //Use default I2C port, 400kHz speed
  15. {
  16. Serial.println(F("MAX30105 was not found. Please check wiring/power."));
  17. while (1);
  18. }
  19. byte ledBrightness = 60; //Options: 0=Off to 255=50mA
  20. byte sampleAverage = 4; //Options: 1, 2, 4, 8, 16, 32
  21. byte ledMode = 2; //Options: 1 = Red only, 2 = Red + IR, 3 = Red + IR + Green
  22. byte sampleRate = 100; //Options: 50, 100, 200, 400, 800, 1000, 1600, 3200
  23. int pulseWidth = 411; //Options: 69, 118, 215, 411
  24. int adcRange = 4096; //Options: 2048, 4096, 8192, 16384
  25. particleSensor.setup(ledBrightness, sampleAverage, ledMode, sampleRate, pulseWidth, adcRange); //Configure sensor with these settings
  26. }
  27. void loop() {
  28. // put your main code here, to run repeatedly:
  29. bufferLength = 100; //buffer length of 100 stores 4 seconds of samples running at 25sps
  30. //read the first 100 samples, and determine the signal range
  31. for (byte i = 0 ; i < bufferLength ; i++)
  32. {
  33. while (particleSensor.available() == false) //do we have new data?
  34. particleSensor.check(); //Check the sensor for new data
  35. redBuffer[i] = particleSensor.getRed();
  36. irBuffer[i] = particleSensor.getIR();
  37. particleSensor.nextSample(); //We're finished with this sample so move to next sample
  38. Serial.print(F("red="));
  39. Serial.print(redBuffer[i], DEC);
  40. Serial.print(F(", ir="));
  41. Serial.println(irBuffer[i], DEC);
  42. }
  43. //calculate heart rate and SpO2 after first 100 samples (first 4 seconds of samples)
  44. maxim_heart_rate_and_oxygen_saturation(irBuffer, bufferLength, redBuffer, &spo2, &validSPO2, &heartRate, &validHeartRate);
  45. //Continuously taking samples from MAX30102. Heart rate and SpO2 are calculated every 1 second
  46. while (1)
  47. {
  48. //dumping the first 25 sets of samples in the memory and shift the last 75 sets of samples to the top
  49. for (byte i = 25; i < 100; i++)
  50. {
  51. redBuffer[i - 25] = redBuffer[i];
  52. irBuffer[i - 25] = irBuffer[i];
  53. }
  54. //take 25 sets of samples before calculating the heart rate.
  55. for (byte i = 75; i < 100; i++)
  56. {
  57. while (particleSensor.available() == false) //do we have new data?
  58. particleSensor.check(); //Check the sensor for new data
  59. digitalWrite(readLED, !digitalRead(readLED)); //Blink onboard LED with every data read
  60. redBuffer[i] = particleSensor.getRed();
  61. irBuffer[i] = particleSensor.getIR();
  62. particleSensor.nextSample(); //We're finished with this sample so move to next sample
  63. //send samples and calculation result to terminal program through UART
  64. //Serial.print(F("red="));
  65. //Serial.print(redBuffer[i], DEC);
  66. //Serial.print(F(", ir="));
  67. //Serial.print(irBuffer[i], DEC);
  68. Serial.print(F(", HR="));
  69. Serial.print(heartRate, DEC);
  70. Serial.print(F(", HRvalid="));
  71. Serial.print(validHeartRate, DEC);
  72. Serial.print(F(", SPO2="));
  73. Serial.print(spo2, DEC);
  74. Serial.print(F(", SPO2Valid="));
  75. Serial.println(validSPO2, DEC);
  76. }
  77. //After gathering 25 new samples recalculate HR and SP02
  78. maxim_heart_rate_and_oxygen_saturation(irBuffer, bufferLength, redBuffer, &spo2, &validSPO2, &heartRate, &validHeartRate);
  79. }
  80. Number1.print(spo2);
  81. Blinker.run();
  82. }

但是不知道是不是模块问题还是我测量方式不对,好难测出准确的血氧数据= =

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/491441
推荐阅读
相关标签
  

闽ICP备14008679号