Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Wire.h>
- #include "MAX30105.h"
- #include "spo2_algorithm.h"
- #include <WiFiManager.h>
- #include <Arduino.h>
- #include <FirebaseESP32.h>
- #include <addons/TokenHelper.h>
- #include <addons/RTDBHelper.h>
- ///////////////firbase_setup//////////////////////////////////////////
- #define API_KEY "AIzaSyBHPhlRb697I7HeNDQj3ZXelL_X20rbcLE"
- #define USER_EMAIL "safwan.092@gmail.com"
- #define USER_PASSWORD "123456789"
- #define DATABASE_URL "esp32-c3-heart-rate-simple-default-rtdb.firebaseio.com"
- #define DATABASE_SECRET "GTOXJ7U28jDiNchkwTP7dE8PCnyPkPDMyfyM3lso"
- FirebaseData fbdo;
- FirebaseAuth auth;
- FirebaseConfig config;
- unsigned long dataMillis = 0;
- /////////////////////////////////////////////////////////////////////
- MAX30105 particleSensor;
- #define MAX_BRIGHTNESS 255
- uint32_t irBuffer[100];
- uint32_t redBuffer[100];
- int32_t bufferLength;
- int32_t spo2;
- int8_t validSPO2;
- int32_t heartRate;
- int8_t validHeartRate;
- float temperature;
- unsigned long previousMillis = 0;
- void setup() {
- Serial.begin(115200);
- init_MAX30102_Sensor();
- Connect_To_WiFi();
- firbase_setup();
- }
- void loop() {
- ReConnect_To_WiFi();
- read_MAX30102_Sensor();
- }
- void init_MAX30102_Sensor() {
- if (!particleSensor.begin(Wire, I2C_SPEED_FAST)) //Use default I2C port, 400kHz speed
- {
- Serial.println(F("MAX30105 was not found. Please check wiring/power."));
- while (1);
- }
- byte ledBrightness = 60; //Options: 0=Off to 255=50mA
- byte sampleAverage = 4; //Options: 1, 2, 4, 8, 16, 32
- byte ledMode = 2; //Options: 1 = Red only, 2 = Red + IR, 3 = Red + IR + Green
- byte sampleRate = 100; //Options: 50, 100, 200, 400, 800, 1000, 1600, 3200
- int pulseWidth = 411; //Options: 69, 118, 215, 411
- int adcRange = 4096; //Options: 2048, 4096, 8192, 16384
- particleSensor.setup(ledBrightness, sampleAverage, ledMode, sampleRate, pulseWidth, adcRange); //Configure sensor with these settings
- particleSensor.enableDIETEMPRDY(); //Enable the temp ready interrupt. This is required.
- }
- void read_MAX30102_Sensor() {
- bufferLength = 100; //buffer length of 100 stores 4 seconds of samples running at 25sps
- //read the first 100 samples, and determine the signal range
- for (byte i = 0 ; i < bufferLength ; i++) {
- while (particleSensor.available() == false) //do we have new data?
- particleSensor.check(); //Check the sensor for new data
- redBuffer[i] = particleSensor.getRed();
- irBuffer[i] = particleSensor.getIR();
- particleSensor.nextSample(); //We're finished with this sample so move to next sample
- Serial.print(F("red="));
- Serial.print(redBuffer[i], DEC);
- Serial.print(F(", ir="));
- Serial.println(irBuffer[i], DEC);
- }
- //calculate heart rate and SpO2 after first 100 samples (first 4 seconds of samples)
- maxim_heart_rate_and_oxygen_saturation(irBuffer, bufferLength, redBuffer, &spo2, &validSPO2, &heartRate, &validHeartRate);
- //Continuously taking samples from MAX30102. Heart rate and SpO2 are calculated every 1 second
- while (1) {
- //dumping the first 25 sets of samples in the memory and shift the last 75 sets of samples to the top
- for (byte i = 25; i < 100; i++) {
- redBuffer[i - 25] = redBuffer[i];
- irBuffer[i - 25] = irBuffer[i];
- }
- //take 25 sets of samples before calculating the heart rate.
- for (byte i = 75; i < 100; i++)
- {
- while (particleSensor.available() == false) //do we have new data?
- particleSensor.check(); //Check the sensor for new data
- redBuffer[i] = particleSensor.getRed();
- irBuffer[i] = particleSensor.getIR();
- particleSensor.nextSample(); //We're finished with this sample so move to next sample
- //send samples and calculation result to terminal program through UART
- Serial.print(F("red="));
- Serial.print(redBuffer[i], DEC);
- Serial.print(F(", ir="));
- Serial.print(irBuffer[i], DEC);
- if (millis() - previousMillis >= 10000) {
- temperature = particleSensor.readTemperature();
- Serial.print(", temperatureC=");
- Serial.print(temperature, 4);
- previousMillis = millis();
- }
- if (validSPO2) {
- Serial.print(F(", HR="));
- Serial.print(heartRate / 2, DEC);
- Serial.print(F(", HRvalid="));
- Serial.print(validHeartRate, DEC);
- Serial.print(F(", SPO2="));
- Serial.print(spo2, DEC);
- Serial.print(F(", SPO2Valid="));
- Serial.print(validSPO2, DEC);
- Send_Data_To_Firebase();
- }
- Serial.println();
- }
- //After gathering 25 new samples recalculate HR and SP02
- maxim_heart_rate_and_oxygen_saturation(irBuffer, bufferLength, redBuffer, &spo2, &validSPO2, &heartRate, &validHeartRate);
- }
- }
- void Send_Data_To_Firebase() {
- if (millis() - dataMillis > 5000 && Firebase.ready()) {
- dataMillis = millis();
- Serial.printf("Set String and Counter... %s\n", Firebase.setString(fbdo, "/data/Heart_Rate", String(int(heartRate / 2))) ? "ok" : fbdo.errorReason().c_str());
- Serial.printf("Set String and Counter... %s\n", Firebase.setString(fbdo, "/data/Oxygen_Rate", String(int(spo2))) ? "ok" : fbdo.errorReason().c_str());
- Serial.printf("Set String and Counter... %s\n", Firebase.setString(fbdo, "/data/Temperature", String(int(temperature))) ? "ok" : fbdo.errorReason().c_str());
- }
- }
- void ReConnect_To_WiFi() {
- while (WiFi.status() != WL_CONNECTED) {
- Serial.print(".");
- Connect_To_WiFi();
- }
- }
- void Connect_To_WiFi() {
- WiFiManager wm;
- bool res;
- res = wm.autoConnect("Senior Project [Setup WiFi]"); // password protected ap
- if (!res) {
- Serial.println("Failed to Connect!!");
- // ESP.restart();
- }
- else {
- //if you get here you have connected to the WiFi
- Serial.println("Connected...");
- }
- }
- void firbase_setup() {
- Serial.printf("Firebase Client v%s\n\n", FIREBASE_CLIENT_VERSION);
- config.api_key = API_KEY;
- auth.user.email = USER_EMAIL;
- auth.user.password = USER_PASSWORD;
- config.database_url = DATABASE_URL;
- Firebase.reconnectWiFi(true);
- fbdo.setResponseSize(4096);
- String base_path = "/UsersData/";
- config.token_status_callback = tokenStatusCallback; // see addons/TokenHelper.h
- config.max_token_generation_retry = 5;
- Firebase.begin(&config, &auth);
- String var = "$userId";
- String val = "($userId === auth.uid && auth.token.premium_account === true && auth.token.admin === true)";
- Firebase.setReadWriteRules(fbdo, base_path, var, val, val, DATABASE_SECRET);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement