IOT Implementation

parent 483ee781
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "MAX30100_PulseOximeter.h"
#include <OneWire.h>
#include <DallasTemperature.h>
#include <ESP8266WiFi.h>
#include <FirebaseESP8266.h>
#include <addons/TokenHelper.h>
#include <addons/RTDBHelper.h>
#define WIFI_SSID "Dialog 4G"
#define WIFI_PASSWORD "BT35T1A9T6N"
#define API_KEY "AIzaSyDtA109jeQlkwuhd8dXV5cIa2AErZV9h_c"
#define DATABASE_URL "covidtracer-acf52-default-rtdb.firebaseio.com"
#define USER_EMAIL "nodemcu@gmail.com"
#define USER_PASSWORD "nodemcu"
FirebaseData fbdo;
FirebaseAuth auth;
FirebaseConfig config;
LiquidCrystal_I2C lcd(0x27, 16, 4);
const int oneWireBus = 12;
OneWire oneWire(oneWireBus);
DallasTemperature tempsensor(&oneWire);
PulseOximeter pox;
bool fingerDetect=false;
float finalBpm;
float finalSpo2;
float finalTemp;
void setup()
{
lcd.init();
lcd.backlight();
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
lcd.clear();
lcd.setCursor(6, 0);
lcd.print("WiFi");
lcd.setCursor(0, 2);
Serial.print("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED)
{
lcd.print(".");
delay(300);
}
config.api_key = API_KEY;
auth.user.email = USER_EMAIL;
auth.user.password = USER_PASSWORD;
config.database_url = DATABASE_URL;
config.token_status_callback = tokenStatusCallback;
Firebase.begin(&config, &auth);
Firebase.reconnectWiFi(true);
Firebase.setDoubleDigits(5);
pinMode(16, OUTPUT);
delay(1000);
pox_start();
tempsensor.begin();
}
void loop()
{
pox.update();
if(true){//network check
if (!fingerDetect){//finger check
lcd.clear();
lcd.setCursor(0, 1);
lcd.print("-- Ready to scan ---");
}
else{
lcd.clear();
if(scan()){
pox.shutdown();
lcd.setCursor(0, 0);
lcd.print("--- Sending Data ---");
if (Firebase.ready()){
if(Firebase.getInt(fbdo, F("/device/streamCount"))){
int index = fbdo.to<int>() + 1;
if(Firebase.setInt(fbdo, F("/device/streamCount"), index)){
FirebaseJson json;
//json.set(String(index)+"/ts", F("timestamp"));
json.set(String(index)+"/ts/.sv", F("timestamp"));
json.set(String(index)+"/bpm", finalBpm);
json.set(String(index)+"/spo2", finalSpo2);
json.set(String(index)+"/temp", finalTemp);
if(Firebase.updateNode(fbdo, F("/device/stream"), json)){
lcd.clear();
lcd.setCursor(0, 1);
lcd.print("-- Data Uploaded ---");
}
}
}
}
}
else
{
pox.shutdown();
lcd.setCursor(0, 0);
lcd.print("Scaning Error!");
}
// if (Firebase.ready()){
// if(Firebase.getInt(fbdo, F("/device/streamCount"))){
// int index = fbdo.to<int>() + 1;
// if(Firebase.setInt(fbdo, F("/device/streamCount"), index)){
// FirebaseJson json;
// //json.set(String(index)+"/ts", F("timestamp"));
// json.set(String(index)+"/ts/.sv", F("timestamp"));
// json.set(String(index)+"/bpm", finalBpm);
// json.set(String(index)+"/spo2", finalSpo2);
// json.set(String(index)+"/temp", finalTemp);
//
// if(Firebase.updateNode(fbdo, F("/device/stream"), json)){
// lcd.clear();
// lcd.setCursor(1, 1);
// lcd.print("Data Uploaded");
// }
// }
//
// }
// }
delay(6000);
fingerDetect=false;
pox_start();
}
}
delay(100);
}
bool scan(){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("----- Scaning ------");
float _bpmBuffer[5];
float _spo2Buffer[5];
float _tempBuffer[5];
bool scanBreak = false;
int bloodScanCurrent=0;
int scanBreakindex=0;
while (bloodScanCurrent<5) // take sensor valus
{
pox.update();
float _bpm = pox.getHeartRate();
float _spo2 = pox.getSpO2();
if (_bpm>60.0 && _spo2>60.0 && _spo2<101.0)
{
tempsensor.requestTemperatures();
float _temp = tempsensor.getTempCByIndex(0);
_bpmBuffer[bloodScanCurrent] = _bpm;
_spo2Buffer[bloodScanCurrent] = _spo2;
_tempBuffer[bloodScanCurrent] = _temp;
bloodScanCurrent++;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("----- Scaning ------");
lcd.setCursor(0, 1);
lcd.print("BPM - ");lcd.print(_bpm);
lcd.setCursor(0, 2);
lcd.print("SpO2 - ");lcd.print(_spo2);
lcd.setCursor(0, 3);
lcd.print("Temp - ");lcd.print(_temp);
}
delay(100);
if (scanBreakindex>200)//breaking scan if time reachs 21s
{
bloodScanCurrent=5;
scanBreak=true;
}
scanBreakindex++;
}
if (scanBreak)
{
return false;
}
else
{
finalBpm = _bpmBuffer[0];
finalSpo2 = _spo2Buffer[0];
finalTemp = _tempBuffer[0];
for (int i = 1; i < 5; i++) //Avaraging scanded values
{
finalBpm = ( finalBpm + _bpmBuffer[i])/2;
finalSpo2 = ( finalSpo2 + _spo2Buffer[i])/2;
finalTemp = ( finalTemp + _tempBuffer[i])/2;
}
return true;
}
}
void pox_start(){
if (!pox.begin()){
for (;;);
}
pox.setIRLedCurrent(MAX30100_LED_CURR_7_6MA);
pox.setOnBeatDetectedCallback(onBeatDetected);
}
void onBeatDetected()
{
fingerDetect=!fingerDetect;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment