Advertisement
safwan092

Untitled

Mar 12th, 2024
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.46 KB | None | 0 0
  1. #include <Wire.h>
  2. #include "MAX30105.h"
  3. #include "spo2_algorithm.h"
  4. #include <WiFiManager.h>
  5. #include <Arduino.h>
  6. #include <FirebaseESP32.h>
  7. #include <addons/TokenHelper.h>
  8. #include <addons/RTDBHelper.h>
  9.  
  10.  
  11. ///////////////firbase_setup//////////////////////////////////////////
  12.  
  13. #define API_KEY "AIzaSyBHPhlRb697I7HeNDQj3ZXelL_X20rbcLE"
  14. #define USER_EMAIL "safwan.092@gmail.com"
  15. #define USER_PASSWORD "123456789"
  16. #define DATABASE_URL "esp32-c3-heart-rate-simple-default-rtdb.firebaseio.com"
  17. #define DATABASE_SECRET "GTOXJ7U28jDiNchkwTP7dE8PCnyPkPDMyfyM3lso"
  18. FirebaseData fbdo;
  19. FirebaseAuth auth;
  20. FirebaseConfig config;
  21. unsigned long dataMillis = 0;
  22. /////////////////////////////////////////////////////////////////////
  23.  
  24.  
  25. MAX30105 particleSensor;
  26. #define MAX_BRIGHTNESS 255
  27. uint32_t irBuffer[100];
  28. uint32_t redBuffer[100];
  29. int32_t bufferLength;
  30. int32_t spo2;
  31. int8_t validSPO2;
  32. int32_t heartRate;
  33. int8_t validHeartRate;
  34. float temperature;
  35. unsigned long previousMillis = 0;
  36.  
  37. void setup() {
  38. Serial.begin(115200);
  39. init_MAX30102_Sensor();
  40. Connect_To_WiFi();
  41. firbase_setup();
  42. }
  43.  
  44. void loop() {
  45. ReConnect_To_WiFi();
  46. read_MAX30102_Sensor();
  47. }
  48.  
  49.  
  50. void init_MAX30102_Sensor() {
  51. if (!particleSensor.begin(Wire, I2C_SPEED_FAST)) //Use default I2C port, 400kHz speed
  52. {
  53. Serial.println(F("MAX30105 was not found. Please check wiring/power."));
  54. while (1);
  55. }
  56. byte ledBrightness = 60; //Options: 0=Off to 255=50mA
  57. byte sampleAverage = 4; //Options: 1, 2, 4, 8, 16, 32
  58. byte ledMode = 2; //Options: 1 = Red only, 2 = Red + IR, 3 = Red + IR + Green
  59. byte sampleRate = 100; //Options: 50, 100, 200, 400, 800, 1000, 1600, 3200
  60. int pulseWidth = 411; //Options: 69, 118, 215, 411
  61. int adcRange = 4096; //Options: 2048, 4096, 8192, 16384
  62. particleSensor.setup(ledBrightness, sampleAverage, ledMode, sampleRate, pulseWidth, adcRange); //Configure sensor with these settings
  63. particleSensor.enableDIETEMPRDY(); //Enable the temp ready interrupt. This is required.
  64. }
  65.  
  66.  
  67. void read_MAX30102_Sensor() {
  68. bufferLength = 100; //buffer length of 100 stores 4 seconds of samples running at 25sps
  69. //read the first 100 samples, and determine the signal range
  70. for (byte i = 0 ; i < bufferLength ; i++) {
  71. while (particleSensor.available() == false) //do we have new data?
  72. particleSensor.check(); //Check the sensor for new data
  73.  
  74. redBuffer[i] = particleSensor.getRed();
  75. irBuffer[i] = particleSensor.getIR();
  76. particleSensor.nextSample(); //We're finished with this sample so move to next sample
  77.  
  78. Serial.print(F("red="));
  79. Serial.print(redBuffer[i], DEC);
  80. Serial.print(F(", ir="));
  81. Serial.println(irBuffer[i], DEC);
  82. }
  83.  
  84. //calculate heart rate and SpO2 after first 100 samples (first 4 seconds of samples)
  85. maxim_heart_rate_and_oxygen_saturation(irBuffer, bufferLength, redBuffer, &spo2, &validSPO2, &heartRate, &validHeartRate);
  86.  
  87. //Continuously taking samples from MAX30102. Heart rate and SpO2 are calculated every 1 second
  88. while (1) {
  89. //dumping the first 25 sets of samples in the memory and shift the last 75 sets of samples to the top
  90. for (byte i = 25; i < 100; i++) {
  91. redBuffer[i - 25] = redBuffer[i];
  92. irBuffer[i - 25] = irBuffer[i];
  93. }
  94.  
  95. //take 25 sets of samples before calculating the heart rate.
  96. for (byte i = 75; i < 100; i++)
  97. {
  98. while (particleSensor.available() == false) //do we have new data?
  99. particleSensor.check(); //Check the sensor for new data
  100.  
  101. redBuffer[i] = particleSensor.getRed();
  102. irBuffer[i] = particleSensor.getIR();
  103. particleSensor.nextSample(); //We're finished with this sample so move to next sample
  104.  
  105. //send samples and calculation result to terminal program through UART
  106. Serial.print(F("red="));
  107. Serial.print(redBuffer[i], DEC);
  108. Serial.print(F(", ir="));
  109. Serial.print(irBuffer[i], DEC);
  110. if (millis() - previousMillis >= 10000) {
  111. temperature = particleSensor.readTemperature();
  112. Serial.print(", temperatureC=");
  113. Serial.print(temperature, 4);
  114. previousMillis = millis();
  115. }
  116. if (validSPO2) {
  117. Serial.print(F(", HR="));
  118. Serial.print(heartRate / 2, DEC);
  119.  
  120. Serial.print(F(", HRvalid="));
  121. Serial.print(validHeartRate, DEC);
  122.  
  123. Serial.print(F(", SPO2="));
  124. Serial.print(spo2, DEC);
  125.  
  126. Serial.print(F(", SPO2Valid="));
  127. Serial.print(validSPO2, DEC);
  128. Send_Data_To_Firebase();
  129. }
  130.  
  131.  
  132. Serial.println();
  133. }
  134.  
  135. //After gathering 25 new samples recalculate HR and SP02
  136. maxim_heart_rate_and_oxygen_saturation(irBuffer, bufferLength, redBuffer, &spo2, &validSPO2, &heartRate, &validHeartRate);
  137. }
  138. }
  139.  
  140. void Send_Data_To_Firebase() {
  141. if (millis() - dataMillis > 5000 && Firebase.ready()) {
  142. dataMillis = millis();
  143. Serial.printf("Set String and Counter... %s\n", Firebase.setString(fbdo, "/data/Heart_Rate", String(int(heartRate / 2))) ? "ok" : fbdo.errorReason().c_str());
  144. Serial.printf("Set String and Counter... %s\n", Firebase.setString(fbdo, "/data/Oxygen_Rate", String(int(spo2))) ? "ok" : fbdo.errorReason().c_str());
  145. Serial.printf("Set String and Counter... %s\n", Firebase.setString(fbdo, "/data/Temperature", String(int(temperature))) ? "ok" : fbdo.errorReason().c_str());
  146. }
  147. }
  148.  
  149.  
  150. void ReConnect_To_WiFi() {
  151. while (WiFi.status() != WL_CONNECTED) {
  152. Serial.print(".");
  153. Connect_To_WiFi();
  154. }
  155. }
  156. void Connect_To_WiFi() {
  157. WiFiManager wm;
  158. bool res;
  159. res = wm.autoConnect("Senior Project [Setup WiFi]"); // password protected ap
  160. if (!res) {
  161. Serial.println("Failed to Connect!!");
  162. // ESP.restart();
  163. }
  164. else {
  165. //if you get here you have connected to the WiFi
  166. Serial.println("Connected...");
  167. }
  168. }
  169.  
  170.  
  171. void firbase_setup() {
  172. Serial.printf("Firebase Client v%s\n\n", FIREBASE_CLIENT_VERSION);
  173. config.api_key = API_KEY;
  174. auth.user.email = USER_EMAIL;
  175. auth.user.password = USER_PASSWORD;
  176. config.database_url = DATABASE_URL;
  177. Firebase.reconnectWiFi(true);
  178. fbdo.setResponseSize(4096);
  179. String base_path = "/UsersData/";
  180. config.token_status_callback = tokenStatusCallback; // see addons/TokenHelper.h
  181. config.max_token_generation_retry = 5;
  182. Firebase.begin(&config, &auth);
  183. String var = "$userId";
  184. String val = "($userId === auth.uid && auth.token.premium_account === true && auth.token.admin === true)";
  185. Firebase.setReadWriteRules(fbdo, base_path, var, val, val, DATABASE_SECRET);
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement