LCC 04 April V1

mail@pastecode.io avatar
unknown
c_cpp
3 years ago
11 kB
2
Indexable
Never
#include <TFT_eSPI.h> // Graphics and font library for ST7735 driver chip
#include <SPI.h>
#include <HTTPClient.h>
 
/*
#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "TMPLCJUUTVGm"
#define BLYNK_DEVICE_NAME "Quickstart Template"
#define BLYNK_AUTH_TOKEN "uTyPBxDwUc1BFFz3RQwQbLOVXK7PWXGx"
*/
 
#include <WiFi.h>
#include <WiFiClient.h>
//#include <BlynkSimpleEsp32.h>
//char auth[] = "uTyPBxDwUc1BFFz3RQwQbLOVXK7PWXGx";//this is where your token from Blynk
 
#include "twilio.hpp"
 
// Values from Twilio (find them on the dashboard)
static const char *account_sid = "ACbda8054aef93310774f7ef47b4f19c02";
static const char *auth_token = "114532f90b58eb88a8945943480a4733";
// Phone number should start with "+<countrycode>"
static const char *from_number = "+18567334846";
// You choose!
// Phone number should start with "+<countrycode>"
//static const char *to_number = "+918692864044";
static const char *to_number = "+919768026868";
 
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Anonymous";//your wifi network
char pass[] = "0 Zxq@1234";//your password
 
String server = "http://maker.ifttt.com";
String eventName = "esp32ToWeb";
String IFTTT_Key = "e396Rc2__Q1EM-gZJlbHn";
String IFTTTUrl="http://maker.ifttt.com/trigger/temp_data/with/key/e272MXJrh4_et5KUm56LmYHjJrNRtj9BjxUT5u6Njr7";
 
TFT_eSPI tft = TFT_eSPI();  // Invoke library, pins defined in User_Setup.h
 
 
const int analogInPin = 33;  //change this if you want to use another analog pin--works on this model of esp32
float area_1 = 0.0004908; //these are the areas taken carefully from the 3D printed venturi 2M before constriction
float area_2 = 0.0001179;// this is area within the venturi
float rho = 1.225; //Demsity of air in kg/m3;
float dt = 0;
int logCount = 0;
float timerBreath;
float secondsBreath;
float minuteTotal;
int newBreath = 0;
float TimerNow = 0.0;
int totalBreath = 0;
float massFlow = 0;
float volFlow = 0;
float maxFlow = 0.0;
float volumeTotal = 0;
float sensorValue = 0;        // value read from the pot
int outputValue = 0;        // value output to the PWM (analog out)
float voltage = 0.0;
float Pa = 0.0;
 
float fev1 = 0.0;
float vol[30]; //this is the total volume that stored with time...
float volSec[30];//this is the the liter/sec messured
int snatch = 0;
int fundex = 0;
void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600);
  minuteTotal = millis();
  /*Blynk.begin(auth, ssid, pass);
  delay(5000);
  */
  for(int p = 0;p < 30; p++){
    vol[p] = 0; //clears out matrix
    volSec[p] = 0;
  }
  tft.init();
  tft.setRotation(1);
  tft.fillScreen(TFT_BLACK);
 
  tft.setTextColor(TFT_YELLOW, TFT_BLACK);
   tft.setRotation(1);
    tft.fillScreen(TFT_BLACK);
    tft.setTextColor(TFT_GREEN, TFT_BLACK);
 tft.drawCentreString("BLOW.....",125,55,4);
 delay(5000);
}
 
void loop() {
 //Blynk.run(); 
  if((millis()- minuteTotal) > 60000){
    minuteTotal = millis();
    //Serial.print( "Breaths per minute:");
    //Serial.println(totalBreath);
   // logfile.print("                                        ");
    //logfile.println(totalBreath);
    totalBreath = 0;
  }
  // read the analog in value:
  sensorValue = analogRead(analogInPin);
 
//Serial.print("max sens value");
//Serial.println(sensorValue);
 
  voltage =( 3.3 * (( sensorValue )/4095));//the usual voltage conversion for 12 bit esp32
 
Serial.print("CheVoltage "); // For testing only
Serial.println(voltage);
 
  Pa = (190 * voltage/3.3) - 38;  //equation from sensor data sheet
     Serial.println("pa");
     Serial.println(Pa);
  if(( Pa > 1) || (fundex >5 && fundex <28)) {   //checks to see if your breathing out and if your halfway through a blow
    if (newBreath < 1) {
      timerBreath = millis();
      fev1 = 0.0;
      snatch = 0;
      fundex = 0;
      if(volumeTotal > 100){
      totalBreath = totalBreath + 1; //counts breath
      //Serial.print("VolumeTotal= ");
      //Serial.println(volumeTotal);
      }
 
      volumeTotal = 0;
      newBreath = 1;
 
 
    }
 
    if(!(snatch%5)){
      vol[fundex] = volumeTotal;
      volSec[fundex] = volFlow;
      fundex++;
 
    }
  snatch++;
  // change the analog out value:
  //analogWrite(analogOutPin, outputValue);
  if((millis() - timerBreath >= 1400) && (fev1 == 0)) fev1 = volumeTotal +400;
massFlow = 1400*sqrt((abs(Pa)*2*rho)/((1/(pow(area_2,2)))-(1/(pow(area_1,2))))); //Bernoulli equation
volFlow = massFlow/rho; //volumetric flow of air
//Serial.print(timerBreath);
//Serial.print(volFlow);
//Serial.print(massFlow);
if(volFlow > maxFlow) maxFlow = volFlow;
 
volumeTotal = volFlow * (millis() - TimerNow) + volumeTotal;//integrates volumes over units of time to get total volume
  // print the results to the Serial Monitor:
  //Serial.print("sensor = ");
  //Serial.print(sensorValue);
  //Serial.print("VolumeTotal= ");
 //Serial.print(volumeTotal);
 
  //Serial.print("\t pressure = ");
  //Serial.println(Pa);
 
 
  } else if(newBreath){    //if your done figure out the results
    newBreath = 0;
    //Serial.print("time for breath");
    if(volumeTotal > 200){
    secondsBreath = (millis() - timerBreath)/1000;
  /*  Serial.print("FEV1  ");
    Serial.println(fev1);
    Serial.print("FVC  ");
    Serial.print(volumeTotal);
    Serial.print("FV1/FVC");
    Serial.println(fev1/volumeTotal);
    Serial.print("Duration");
    Serial.println(timerBreath/1000);
    Serial.print("MaxFlow");
    Serial.println(maxFlow);*/
    while (fev1 != 0){
    tft.setRotation(1);
    tft.fillScreen(TFT_BLACK);
    tft.setTextColor(TFT_GREEN, TFT_BLACK);
    tft.drawCentreString("FEV1",32,20,4);
    tft.setTextColor(TFT_GREEN, TFT_BLACK);
    tft.drawCentreString("FVC",32,80,4);
    tft.setTextColor(TFT_BLACK, TFT_BLACK);
    tft.drawString("888888",70,5,7);
    tft.setTextColor(0xFBE0, TFT_BLACK); // Orange
    tft.drawNumber(fev1,70,5,7);
    tft.setTextColor(TFT_BLACK, TFT_BLACK);
    tft.drawString("888888",70,70,7);
    tft.setTextColor(0xFBE0, TFT_BLACK); // Orange
    tft.drawNumber(volumeTotal,70,70,7);
    //Serial.println(secondsBreath);
    delay(5000);
    tft.fillScreen(TFT_BLACK);
    printer();
   dataDump();
   }
   //if(Blynk.connected())blynkPrint();
 
    }
    for(int p = 0;p < 30; p++){
    vol[p] = 0;
    volSec[p] = 0;
  }
   tft.setRotation(1);
    tft.fillScreen(TFT_BLACK);
    tft.setTextColor(TFT_GREENYELLOW, TFT_BLACK);
 tft.drawCentreString("BLOW.....",125,55,4);
    maxFlow = 0;
  }
 
  TimerNow = millis();
  delay(20);
}
 
Twilio *twilio;
void dataDump(){
    tft.setRotation(1);
    tft.fillScreen(TFT_BLACK);
    tft.setTextColor(TFT_GREEN, TFT_BLACK);
    tft.drawCentreString("FEV1:",32,5,4);
    tft.drawCentreString("FEVC:",32,27,4);
    tft.drawCentreString("DURATION:",67,49,4);
    tft.drawCentreString("FEV1/FEVC:",70,71,4);
    tft.drawCentreString("MAXFLOW:",63,93,4);
 
    tft.setTextColor(0xFBE0, TFT_BLACK); // Orange
    tft.drawNumber(fev1,100,5,4);
    tft.drawCentreString(" ml",180,5,4);
     tft.drawNumber(volumeTotal,100,27,4);
     tft.drawCentreString(" ml",180,27,4);
      tft.drawNumber(timerBreath/1000,160,49,4);
       tft.drawNumber((fev1/volumeTotal) * 100,160,71,4);
        tft.drawNumber(maxFlow,160,93,4);
     delay(300);
 
  Serial.begin(115200);
  //Serial.print("Connecting to WiFi network ;");
  //Serial.print(ssid);
  //Serial.println("'...");
  WiFi.begin(ssid, pass);
 
  while (WiFi.status() != WL_CONNECTED) {
    Serial.println("Connecting...");
    delay(500);
  }
//  Serial.println("Connected!");
 
  twilio = new Twilio(account_sid, auth_token);
 
  String message1 = "FEV1 value is " ;
  message1 += fev1;
  String message2 = " ml. FEVC value is " ;
  message2 += volumeTotal;
  String message3 = " ml.";
  String message = message1 + message2 + message3 ;
  delay(100);
  String response;
  bool success = twilio->send_message(to_number, from_number, message, response);
   sendDataToSheet();
   
  if (success) {
    //Serial.println("Sent message successfully!");
  } else {
    //Serial.println(response); 
  }
 
}
 
    
    
void printer(){     //Draws nice little graph
   int x = 1;
  for(int p = 0; p< 30; p++){
    /* Serial.print("vol");
    Serial.println(vol[p]);
    Serial.print("volspeed");
    Serial.println(volSec[p]); */
     int x = map(vol[p],0,5000,0,20);
 
    linearMeter(x,10,p*8,5,10,3,25,1);
 
  }
  delay(3000);
}
    
/*
void blynkPrint(){             //connects to Blynk
   tft.setRotation(1);
    tft.fillScreen(TFT_BLACK);
    tft.setTextColor(TFT_GREEN, TFT_BLACK);
 tft.drawCentreString("BLYNK......",125,55,4);
 Blynk.virtualWrite(V1,timerBreath/1000);
 Blynk.virtualWrite(V2, fev1/volumeTotal);
 Blynk.virtualWrite(V3, volumeTotal);
 Blynk.virtualWrite(V4, fev1);
 
 
  for(int i = 0; i < 20; i++){
   Blynk.virtualWrite(V5, vol[i]);
    Blynk.virtualWrite(V6, volSec[i]);
   delay(500);
  }
 
}
*/
void linearMeter(int val, int x, int y, int w, int h, int g, int n, byte s)       //Does the graphing
{
  tft.setRotation(0);
  // Variable to save "value" text colour from scheme and set default
  int colour = TFT_BLUE;
  // Draw n colour blocks
  for (int b = 1; b <= n; b++) {
    if (val > 0 && b <= val) { // Fill in coloured blocks
      switch (s) {
        case 0: colour = TFT_RED; break; // Fixed colour
        case 1: colour = TFT_GREEN; break; // Fixed colour
        case 2: colour = TFT_BLUE; break; // Fixed colour
        //case 3: colour = rainbowColor(map(b, 0, n, 127,   0)); break; // Blue to red
        //case 4: colour = rainbowColor(map(b, 0, n,  63,   0)); break; // Green to red
        //case 5: colour = rainbowColor(map(b, 0, n,   0,  63)); break; // Red to green
        //case 6: colour = rainbowColor(map(b, 0, n,   0, 159)); break; // Rainbow (red to violet)
      }
      tft.fillRect(x + b*(w+g), y, w, h, colour);
    }
    else // Fill in blank segments
    {
      tft.fillRect(x + b*(w+g), y, w, h, TFT_DARKGREY);
    }
  }
}
 
WiFiClientSecure client;
 
void sendDataToSheet(void)
{
  String url = server + "/trigger/" + eventName + "/with/key/" + IFTTT_Key + "?value1=" + String((int)volumeTotal) + "&value2="+String((int)fev1);  
  Serial.println(url);
  //Start to send data to IFTTT
  HTTPClient http;
  Serial.print("[HTTP] begin...\n");
  http.begin(url); //HTTP
 
  Serial.print("[HTTP] GET...\n");
  // start connection and send HTTP header
  int httpCode = http.GET();
  // httpCode will be negative on error
  if(httpCode > 0) {
    // HTTP header has been send and Server response header has been handled
    Serial.printf("[HTTP] GET... code: %d\n", httpCode);
    // file found at server
    if(httpCode == HTTP_CODE_OK) {
      String payload = http.getString();
      Serial.println(payload);
    }
  } else {
    Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
  }
  http.end();
 
}