LCC-07-04-22 1622

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
14 kB
1
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[] = "lcc5";//your wifi network
char pass[] = "12345678";//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 = 0.0 ;
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 maxFlow1 = 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;


//Battry 
#include <Pangodream_18650_CL.h>
#include "SPIFFS.h" 
#include <TJpg_Decoder.h>

#define ICON_WIDTH 70
#define ICON_HEIGHT 36
#define STATUS_HEIGHT_BAR ICON_HEIGHT
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
#define ICON_POS_X (tft.width() - ICON_WIDTH)

#define MIN_USB_VOL 4.9
#define ADC_PIN 34
#define CONV_FACTOR 1.8
#define READS 20

Pangodream_18650_CL BL(ADC_PIN, CONV_FACTOR, READS);
char *batteryImages[] = {"/battery_01.jpg", "/battery_02.jpg", "/battery_03.jpg", "/battery_04.jpg", "/battery_05.jpg"};


void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600);
   
  minuteTotal = millis();
  /*Blynk.begin(auth, ssid, pass);
  delay(5000);
  */
  //for battery 
 /* pinoutInit();
  SPIFFSInit();
  displayInit();
  xTaskCreate(battery_info, "battery_info", 2048, NULL, 1, NULL);*/

  
  
  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("LCC5",125,55,4);
 delay(2000);
   tft.fillScreen(TFT_BLACK);
  tft.drawCentreString("Be Ready",125,55,4);
 delay(2000);
   tft.fillScreen(TFT_BLACK);
   tft.drawCentreString("Take Deep Breath",125,55,4);
 delay(4000);
   tft.fillScreen(TFT_BLACK);
   tft.drawCentreString("Hold It",125,55,4);
 delay(2000);
   tft.fillScreen(TFT_BLACK);
    tft.drawCentreString("Blow Now",125,55,4);
 delay(2000);
}
 
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 (voltage>2.8){
    maxFlow1=volumeTotal;}
  if((millis() - timerBreath >= 1000) && (fev1 == 0)) fev1 = volumeTotal + 800;
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();
   break;
   }
   //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 AGAIN",125,55,4);
 delay(3000);
    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,37,4);
    tft.drawCentreString("FEV1/FEVC:",67,74,4);
    tft.drawCentreString("DURATION:",67,103,4);
 
    tft.setTextColor(0xFBE0, TFT_BLACK); // Orange
    tft.drawNumber(fev1,100,5,4);
    tft.drawCentreString(" ml",180,5,4);
     tft.drawNumber(volumeTotal,100,37,4);
     tft.drawCentreString(" ml",180,37,4);
      tft.drawNumber((fev1/volumeTotal) * 100,160,74,4);
           tft.drawCentreString(" %",200,74,4);
       tft.drawNumber((timerBreath/1000.0)-11.0,160,103,4);
            tft.drawCentreString("sec",200,103,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();
 
}

void pinoutInit(){
  pinMode(14, OUTPUT);
  digitalWrite(14, HIGH);
}

void SPIFFSInit(){
  if (!SPIFFS.begin()) {
    Serial.println("SPIFFS initialisation failed!");
    while (1) yield(); // Stay here twiddling thumbs waiting
  }
  Serial.println("\r\nInitialisation done.");
}

void displayInit(){
  tft.begin();
  tft.setRotation(1);
  tft.setTextColor(TFT_WHITE,TFT_BLACK); 
  tft.fillScreen(TFT_BLACK);
  tft.setSwapBytes(true);
  tft.setTextFont(2);
  TJpgDec.setJpgScale(1);
  TJpgDec.setCallback(tft_output);
}

void battery_info(void *arg)
{
  while (1) {
    if(BL.getBatteryVolts() >= MIN_USB_VOL){
      for(int i=0; i< ARRAY_SIZE(batteryImages); i++){
        drawingBatteryIcon(batteryImages[i]);
        drawingText("Chrg");
        vTaskDelay(500);
      }
    }else{
        int imgNum = 0;
        int batteryLevel = BL.getBatteryChargeLevel();
        if(batteryLevel >=80){
          imgNum = 3;
        }else if(batteryLevel < 80 && batteryLevel >= 50 ){
          imgNum = 2;
        }else if(batteryLevel < 50 && batteryLevel >= 20 ){
          imgNum = 1;
        }else if(batteryLevel < 20 ){
          imgNum = 0;
        }  
    
        drawingBatteryIcon(batteryImages[imgNum]);    
        drawingText(String(batteryLevel) + "%");
        vTaskDelay(1000);
    }
    }  
}

void drawingBatteryIcon(String filePath){
   TJpgDec.drawFsJpg(ICON_POS_X, 0, filePath);
}

void drawingText(String text){
  tft.fillRect(0, 0, ICON_POS_X, ICON_HEIGHT,TFT_BLACK);
  tft.setTextDatum(5);
  tft.drawString(text, ICON_POS_X-2, STATUS_HEIGHT_BAR/2, 4);
}

bool tft_output(int16_t x, int16_t y, uint16_t w, uint16_t h, uint16_t* bitmap)
{
  if ( y >= tft.height() ) return 0;
  tft.pushImage(x, y, w, h, bitmap);
  return 1;
}