Advertisement
safwan092

Untitled

Mar 4th, 2024
15
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.40 KB | None | 0 0
  1. #include <Wire.h>
  2. #include "MAX30105.h"
  3. #include "heartRate.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. //********************************************************************
  12. String EPC_Number_string = "E280699500604F7332A7";
  13. //********************************************************************
  14.  
  15.  
  16. ///////////////firbase_setup//////////////////////////////////////////
  17.  
  18. #define API_KEY "AIzaSyD-Vb49XEMVPtauYmnyJ7MRu39cQ5zFqL4"
  19. #define USER_EMAIL "tawaaf8@gmail.com"
  20. #define USER_PASSWORD "123456789"
  21. #define DATABASE_URL "tawaf-dc7b8-default-rtdb.firebaseio.com"
  22. #define DATABASE_SECRET "sgmO3Dwiwu9gkcdfkVOzbMjEIb3HBcj9GombYj4d"
  23. FirebaseData fbdo;
  24. FirebaseAuth auth;
  25. FirebaseConfig config;
  26. unsigned long dataMillis = 0;
  27. /////////////////////////////////////////////////////////////////////
  28.  
  29.  
  30. MAX30105 particleSensor;
  31. const byte RATE_SIZE = 4;
  32. byte rates[RATE_SIZE];
  33. byte rateSpot = 0;
  34. long lastBeat = 0;
  35. float beatsPerMinute;
  36. int beatAvg;
  37. long irValue;
  38. long delta;
  39.  
  40. void setup() {
  41. Serial.begin(115200);
  42. init_heart_rate_sensor();
  43. Connect_To_WiFi();
  44. firbase_setup();
  45. Serial.println("Hello world.");
  46. }
  47.  
  48. void loop() {
  49. ReConnect_To_WiFi();
  50. read_heart_rate_sensor();
  51. }
  52.  
  53.  
  54.  
  55. void Send_Data_To_Firebase(int Heart_Rate_AVERAGE) {
  56. if (millis() - dataMillis > 10000 && Firebase.ready()) {
  57. dataMillis = millis();
  58. String PATH = "/tags/";
  59. PATH += EPC_Number_string;
  60. PATH += "/Heart_Rate_Average";
  61. Serial.printf("Set String and Counter... %s\n", Firebase.setString(fbdo, PATH, String(Heart_Rate_AVERAGE)) ? "ok" : fbdo.errorReason().c_str());
  62. }
  63. }
  64.  
  65.  
  66. void init_heart_rate_sensor() {
  67. Serial.println("Initializing...");
  68. // Initialize sensor
  69. if (!particleSensor.begin(Wire, I2C_SPEED_FAST)) //Use default I2C port, 400kHz speed
  70. {
  71. Serial.println("MAX30105 was not found. Please check wiring/power. ");
  72. while (1);
  73. }
  74. Serial.println("Place your index finger on the sensor with steady pressure.");
  75.  
  76. particleSensor.setup(); //Configure sensor with default settings
  77. particleSensor.enableDIETEMPRDY(); //Enable the temp ready interrupt. This is required.
  78. particleSensor.setPulseAmplitudeRed(0x0A); //Turn Red LED to low to indicate sensor is running
  79. particleSensor.setPulseAmplitudeGreen(0); //Turn off Green LED
  80. }
  81.  
  82.  
  83.  
  84. void read_heart_rate_sensor() {
  85. irValue = particleSensor.getIR();
  86. if (checkForBeat(irValue) == true) {
  87. //We sensed a beat!
  88. delta = millis() - lastBeat;
  89. lastBeat = millis();
  90. beatsPerMinute = 60 / (delta / 1000.0);
  91. if (beatsPerMinute < 255 && beatsPerMinute > 20) {
  92. rates[rateSpot++] = (byte)beatsPerMinute; //Store this reading in the array
  93. rateSpot %= RATE_SIZE; //Wrap variable
  94.  
  95. //Take average of readings
  96. beatAvg = 0;
  97. for (byte x = 0 ; x < RATE_SIZE ; x++)
  98. beatAvg += rates[x];
  99. beatAvg /= RATE_SIZE;
  100. }
  101. }
  102.  
  103. Serial.print("IR=");
  104. Serial.print(irValue);
  105. Serial.print(", BPM=");
  106. Serial.print(beatsPerMinute);
  107. Serial.print(", Avg BPM=");
  108. Serial.print(beatAvg);
  109. Send_Data_To_Firebase(beatAvg);
  110. if (irValue < 50000)
  111. Serial.print(" No finger?");
  112. Serial.println();
  113. }
  114.  
  115.  
  116.  
  117. void ReConnect_To_WiFi() {
  118. while (WiFi.status() != WL_CONNECTED) {
  119. Serial.print(".");
  120. Connect_To_WiFi();
  121. }
  122. }
  123. void Connect_To_WiFi() {
  124. WiFiManager wm;
  125. bool res;
  126. res = wm.autoConnect("Senior Project [Setup WiFi]"); // password protected ap
  127. if (!res) {
  128. Serial.println("Failed to Connect!!");
  129. // ESP.restart();
  130. }
  131. else {
  132. //if you get here you have connected to the WiFi
  133. Serial.println("Connected...");
  134. }
  135. }
  136.  
  137.  
  138. void firbase_setup() {
  139. Serial.printf("Firebase Client v%s\n\n", FIREBASE_CLIENT_VERSION);
  140. config.api_key = API_KEY;
  141. auth.user.email = USER_EMAIL;
  142. auth.user.password = USER_PASSWORD;
  143. config.database_url = DATABASE_URL;
  144. Firebase.reconnectWiFi(true);
  145. fbdo.setResponseSize(4096);
  146. String base_path = "/UsersData/";
  147. config.token_status_callback = tokenStatusCallback; // see addons/TokenHelper.h
  148. config.max_token_generation_retry = 5;
  149. Firebase.begin(&config, &auth);
  150. String var = "$userId";
  151. String val = "($userId === auth.uid && auth.token.premium_account === true && auth.token.admin === true)";
  152. Firebase.setReadWriteRules(fbdo, base_path, var, val, val, DATABASE_SECRET);
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement