Code

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

////ESP8266

#include <math.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH110X.h>
#define OLED_RESET -1 // This is a dummy pin, as the display I have only has 4 Pins-
SDA,SCL,GND,Vcc...
// Set the Screen for the Pulse display
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

const int WIDTH = 128;


const int HEIGHT = 64;
const int LENGTH = WIDTH;
Adafruit_SH1106G display = Adafruit_SH1106G(WIDTH, HEIGHT, &Wire, OLED_RESET);

String strArr[15];
uint32_t last =0;

#define BLYNK_PRINT Serial

// You should get Auth Token in the Blynk App.


// Go to the Project Settings (nut icon).
char auth[] = "KaeGGF0q1RmNZrg2Q6uNX5TvX0c_XHGM";

// Your WiFi credentials.


// Set password to "" for open networks.
char ssid[] = "Hotspot";
char pass[] = "12345678";

//BlynkTimer timer;

// VARIABLES
int blinkPin = LED_BUILTIN; // pin to blink led at each beat
//int fadePin = 12; // pin to do fancy classy fading blink at each beat
//int fadeRate = 0; // used to fade LED on with PWM on fadePin
// these variables are volatile because they are used during the interrupt service routine!
volatile int BPM, val,temp; // used to hold the pulse rate
volatile int Signal; // holds the incoming raw data
volatile int IBI = 123; // holds the time between beats, must be seeded!
volatile boolean fever = 0, ECG = 0;
int mx = 0;
// For the display

int x;
int y[LENGTH];

/*
BLYNK_CONNECTED() {
Blynk.syncVirtual(V0, V1);
}

BLYNK_WRITE(V0)
{
//KEkwh = param[0].asDouble();
}

BLYNK_WRITE(V1)
{
//LOADkwh = param[0].asDouble();
}
*/

void clearY()
{
for (int i = 0; i < LENGTH; i++)
{
y[i] = -1;
}
}

void drawY()
{
display.drawPixel(0, y[0], SH110X_WHITE);
for (int i = 1; i < LENGTH; i++)
{
if (y[i] != -1)
{
display.drawLine(i - 1, y[i - 1], i, y[i], SH110X_WHITE);
} else
{
break;
}
}
}

void setup()
{
Serial.begin(9600); // we agree to talk fast!

//Blynk.begin(auth, ssid, pass, "blynk.cloud", 80);


Serial.println("INITIALIZE");

Wire.begin();
display.begin(0x3C, true); // initialize with the I2C addr 0x3C (for the 128x64)
delay(20);
// Clear the buffer.
display.clearDisplay();

x = 0;
clearY();
pinMode(blinkPin, OUTPUT); // pin that will blink to your heartbeat!

display.clearDisplay();
display.setTextSize(2);
display.setCursor(0,28);
display.print("DUET");
display.display();
delay(1000);
}

void loop()
{
Blynk.run();
///////
String rxString = "";
if (Serial.available()) {
//Keep looping until there is something in the buffer.
while (Serial.available()) {
//Delay to allow byte to arrive in input buffer.
delay(2);
//Read a single character from the buffer.
char ch = Serial.read();
//Append that single character to a string.
rxString += ch;
}
int stringStart = 0;
int arrayIndex = 0;
for (int i = 0; i < rxString.length(); i++) {
//Get character and check if it's our "special" character.
if (rxString.charAt(i) == ',') {
//Clear previous values from array.
strArr[arrayIndex] = "";
//Save substring into array.
strArr[arrayIndex] = rxString.substring(stringStart, i);
//Set new string starting point.
stringStart = (i + 1);
arrayIndex++;
}
}
//Put values from the array into the variables.
Signal = int(strArr[0].toInt());
IBI = int(strArr[1].toInt());
BPM = int(strArr[2].toInt());
temp = int(strArr[3].toInt());
}

///////

//Signal = random(100, 800);


//BPM = random(60, 65);
y[x] = map(Signal, 0, 1023, HEIGHT - 10, 0); // Leave some screen for the text.....
drawY();
x++;
if(Signal > mx) mx = Signal;
if (x >= WIDTH)
{
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SH110X_WHITE);
display.setCursor(0, 0);
display.print(" Heart Rate = ");
display.print(BPM);
display.println(" BPM");
//Serial.print(" BPM = ");
//Serial.write(BPM);
display.setTextSize(0);
display.setCursor(0, 52);
display.print("SpO2:");
display.print(IBI);
display.print("%");
//display.setTextSize(0);
// display.setTextColor(WHITE);
display.setCursor(60, 52);
display.print("Temp:");
display.print(temp);
display.print((char)247);
display.print("F");
x = 0;
clearY();
}
display.display();
delay(10); // take a break

if (millis() - last > 2000){


if (temp > 99) fever = 1;
else fever = 0;
if(mx < 100) ECG = 1;
else ECG = 0;
mx=0;
Blynk.virtualWrite(V0, BPM);
Blynk.virtualWrite(V1, IBI);
Blynk.virtualWrite(V2, temp);
Blynk.virtualWrite(V3, fever); //FEVER
Blynk.virtualWrite(V4, ECG); //ECG
Serial.println("UPDATED");
last = millis();
}
}
//Arduino
#include <dht.h>
#include <math.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH110X.h>
#define OLED_RESET -1 // This is a dummy pin, as the display I have only has 4 Pins-
SDA,SCL,GND,Vcc...
#include "MAX30100_PulseOximeter.h"

#define REPORTING_PERIOD_MS 1000


#define DHT22 8
// Create a PulseOximeter object
PulseOximeter pox;

// Time at which the last beat occurred


uint32_t tsLastReport = 0;
const int WIDTH = 128;
const int HEIGHT = 64;
const int LENGTH = WIDTH;
Adafruit_SH1106G display = Adafruit_SH1106G(WIDTH, HEIGHT, &Wire, OLED_RESET);
dht DHT;

// VARIABLES
int blinkPin = LED_BUILTIN; // pin to blink led at each beat
//int fadePin = 12; // pin to do fancy classy fading blink at each beat
//int fadeRate = 0; // used to fade LED on with PWM on fadePin

String msg="";

// these variables are volatile because they are used during the interrupt service routine!
int BPM=0; // used to hold the pulse rate
int SpO2=0; // holds the incoming raw data
int ECG = 000; // holds the time between beats, must be seeded!
int temp = 0;
volatile boolean Pulse = false; // true when pulse wave is high, false when it's low
volatile boolean QS = false; // becomes true when Arduoino finds a beat.
// For the display

int x;
int y[LENGTH];

// Callback routine is executed when a pulse is detected


void onBeatDetected() {
Serial.println("Beat!");
}

void setup() {
Serial.begin(9600);

Serial.print("Initializing pulse oximeter..");


pinMode(3, INPUT); // Setup for leads off detection LO +
pinMode(4, INPUT); // Setup for leads off detection LO -
pinMode(8, INPUT_PULLUP);
pinMode(LED_BUILTIN,OUTPUT);
digitalWrite(LED_BUILTIN, 1);
delay(1000);

// Initialize sensor
if (!pox.begin()) {
Serial.println("FAILED");
for(;;);
} else {
Serial.println("SUCCESS");
}

// Configure sensor to use 7.6mA for LED drive


pox.setIRLedCurrent(MAX30100_LED_CURR_50MA);

// Register a callback routine


pox.setOnBeatDetectedCallback(onBeatDetected);

}
void loop() {
// Read from the sensor
pox.update();

// Grab the updated heart rate and SpO2 levels


if (millis() - tsLastReport > REPORTING_PERIOD_MS) {
BPM = pox.getHeartRate();
SpO2 = pox.getSpO2();
tsLastReport = millis();
}
if((digitalRead(3) == 1)||(digitalRead(4) == 1)){
ECG = 0;
} else{
ECG = analogRead(A0);
}
int readData = DHT.read22(DHT22);
float t = DHT.temperature;
temp = (t * 9.0) / 5.0 + 32.0;

Serial.println(String(ECG) +","+ String(SpO2) +","+ String(BPM) + ","+ String(temp) + ",");


delay(50);
}

You might also like