K64f, ESP8266, Heartrate Senosr, Alcohol sensor

Dependencies:   mbed GroveEarbudSensor ESP8266

Committer:
qxy
Date:
Mon Apr 22 19:21:04 2019 +0000
Revision:
0:0a8dbe4c0447
Here is driver health system monitor project code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
qxy 0:0a8dbe4c0447 1 #include "mbed.h"
qxy 0:0a8dbe4c0447 2 #include "ESP8266.h"
qxy 0:0a8dbe4c0447 3 #include <string>
qxy 0:0a8dbe4c0447 4 #include <stdio.h>
qxy 0:0a8dbe4c0447 5 // Grove Earbud Sensor include
qxy 0:0a8dbe4c0447 6 #include "GroveEarbudSensor.h"
qxy 0:0a8dbe4c0447 7
qxy 0:0a8dbe4c0447 8 #define APIKEY SQ43V2ERAB2S42I3 //Put "Write key" of your channel in thingspeak.com
qxy 0:0a8dbe4c0447 9 #define IP "184.106.153.149" // IP Address of "api.thingspeak.com\"
qxy 0:0a8dbe4c0447 10 #define WIFI_SSID "ATTwXwjd7i" // Home WIFI ID
qxy 0:0a8dbe4c0447 11 #define WIFI_PASS "cmvbfbjrw=+w" // Home WIFI PASS WORD
qxy 0:0a8dbe4c0447 12
qxy 0:0a8dbe4c0447 13 Serial pc(USBTX,USBRX);
qxy 0:0a8dbe4c0447 14
qxy 0:0a8dbe4c0447 15 ESP8266 esp(PTC17, PTC16, 115200); // baud rate for wifi
qxy 0:0a8dbe4c0447 16
qxy 0:0a8dbe4c0447 17 char snd[255],rcv[1000],snd_Data[255]; //snd= string used to send command to ESP 8266 wii and rcv = string used to receive response from ESP8266 wifi module
qxy 0:0a8dbe4c0447 18
qxy 0:0a8dbe4c0447 19 void esp_initialize(void); // Function used to initialize ESP8266 wifi module
qxy 0:0a8dbe4c0447 20 void esp_send(void); // Function used to connect with thingspeak.com and update channel using ESP8266 wifi module
qxy 0:0a8dbe4c0447 21
qxy 0:0a8dbe4c0447 22 // Our sensor as an InterruptIn
qxy 0:0a8dbe4c0447 23 InterruptIn sensor(D2);
qxy 0:0a8dbe4c0447 24
qxy 0:0a8dbe4c0447 25 // callback for receiving heartrate values
qxy 0:0a8dbe4c0447 26 void heartrateCallback(float heartrate,void *data) {
qxy 0:0a8dbe4c0447 27 printf("Callback: heartrate = %.1f\r\n",heartrate);
qxy 0:0a8dbe4c0447 28 }
qxy 0:0a8dbe4c0447 29
qxy 0:0a8dbe4c0447 30 // allocate the earbud sensor
qxy 0:0a8dbe4c0447 31 GroveEarbudSensor earbud(&sensor);
qxy 0:0a8dbe4c0447 32
qxy 0:0a8dbe4c0447 33 float RateofHeart; // Define Global variable for Heatrate detection
qxy 0:0a8dbe4c0447 34
qxy 0:0a8dbe4c0447 35
qxy 0:0a8dbe4c0447 36
qxy 0:0a8dbe4c0447 37 DigitalOut heater(A1); // Set DigitalOut port for Alcohol Sensor
qxy 0:0a8dbe4c0447 38 AnalogIn Asensor(A0); // Set AnalogIn port for Alcohol Sensor
qxy 0:0a8dbe4c0447 39
qxy 0:0a8dbe4c0447 40 float value = 0.0f; // Define Global variable for Alcohol detection
qxy 0:0a8dbe4c0447 41
qxy 0:0a8dbe4c0447 42
qxy 0:0a8dbe4c0447 43 int main(void)
qxy 0:0a8dbe4c0447 44 {
qxy 0:0a8dbe4c0447 45
qxy 0:0a8dbe4c0447 46 pc.baud(115200); // Baud rate used for communicating with Tera-term on PC
qxy 0:0a8dbe4c0447 47
qxy 0:0a8dbe4c0447 48 heater = 1;
qxy 0:0a8dbe4c0447 49
qxy 0:0a8dbe4c0447 50 pc.printf("START\r\n"); // Starting point
qxy 0:0a8dbe4c0447 51
qxy 0:0a8dbe4c0447 52 esp_initialize();
qxy 0:0a8dbe4c0447 53
qxy 0:0a8dbe4c0447 54 // register our callback function
qxy 0:0a8dbe4c0447 55 earbud.registerCallback(heartrateCallback);
qxy 0:0a8dbe4c0447 56
qxy 0:0a8dbe4c0447 57 while (1)
qxy 0:0a8dbe4c0447 58 {
qxy 0:0a8dbe4c0447 59
qxy 0:0a8dbe4c0447 60 wait(5);
qxy 0:0a8dbe4c0447 61
qxy 0:0a8dbe4c0447 62 esp_send(); //Excute Send function to deliver detected data to Thingspeak website
qxy 0:0a8dbe4c0447 63
qxy 0:0a8dbe4c0447 64 }
qxy 0:0a8dbe4c0447 65 }
qxy 0:0a8dbe4c0447 66
qxy 0:0a8dbe4c0447 67
qxy 0:0a8dbe4c0447 68 void esp_initialize(void) // This is the Initialization function as we defined above.
qxy 0:0a8dbe4c0447 69 {
qxy 0:0a8dbe4c0447 70 pc.printf("Initializing ESP\r\n");
qxy 0:0a8dbe4c0447 71
qxy 0:0a8dbe4c0447 72 pc.printf("Reset ESP\r\n");
qxy 0:0a8dbe4c0447 73 esp.Reset(); //RESET ESP
qxy 0:0a8dbe4c0447 74 esp.RcvReply(rcv, 400); //receive a response from ESP
qxy 0:0a8dbe4c0447 75 //pc.printf(rcv); //Print the response onscreen
qxy 0:0a8dbe4c0447 76 wait(2);
qxy 0:0a8dbe4c0447 77
qxy 0:0a8dbe4c0447 78 strcpy(snd,"AT");
qxy 0:0a8dbe4c0447 79 esp.SendCMD(snd); // Sends command to ESP8266. Receives the command string
qxy 0:0a8dbe4c0447 80 pc.printf(snd);
qxy 0:0a8dbe4c0447 81 //wait(2);
qxy 0:0a8dbe4c0447 82 esp.RcvReply(rcv, 400); // Receive reply until no character is received after a given timeout in miliseconds
qxy 0:0a8dbe4c0447 83 pc.printf(rcv);
qxy 0:0a8dbe4c0447 84 wait(0.1);
qxy 0:0a8dbe4c0447 85
qxy 0:0a8dbe4c0447 86 strcpy(snd,"AT+CWMODE=1");
qxy 0:0a8dbe4c0447 87 esp.SendCMD(snd);
qxy 0:0a8dbe4c0447 88 pc.printf(snd);
qxy 0:0a8dbe4c0447 89 wait(2);
qxy 0:0a8dbe4c0447 90
qxy 0:0a8dbe4c0447 91 strcpy(snd,"AT+CWJAP=\"");
qxy 0:0a8dbe4c0447 92 strcat(snd,WIFI_SSID); // Send WIFI Account ID
qxy 0:0a8dbe4c0447 93 strcat(snd,"\",\"");
qxy 0:0a8dbe4c0447 94 strcat(snd,WIFI_PASS); // Send WIFI Password
qxy 0:0a8dbe4c0447 95 strcat(snd,"\"");
qxy 0:0a8dbe4c0447 96
qxy 0:0a8dbe4c0447 97 esp.SendCMD(snd);
qxy 0:0a8dbe4c0447 98 pc.printf(snd);
qxy 0:0a8dbe4c0447 99 wait(5);
qxy 0:0a8dbe4c0447 100 esp.RcvReply(rcv, 400);
qxy 0:0a8dbe4c0447 101 pc.printf("\n %s \n", rcv);
qxy 0:0a8dbe4c0447 102
qxy 0:0a8dbe4c0447 103 strcpy(snd,"AT+CIPMUX=0");
qxy 0:0a8dbe4c0447 104 esp.SendCMD(snd);
qxy 0:0a8dbe4c0447 105 pc.printf(snd);
qxy 0:0a8dbe4c0447 106 //wait(2);
qxy 0:0a8dbe4c0447 107 esp.RcvReply(rcv, 400);
qxy 0:0a8dbe4c0447 108 pc.printf("\n %s \n", rcv);
qxy 0:0a8dbe4c0447 109
qxy 0:0a8dbe4c0447 110 }
qxy 0:0a8dbe4c0447 111
qxy 0:0a8dbe4c0447 112
qxy 0:0a8dbe4c0447 113 void esp_send(void) // This is the Initialization function as we defined above.
qxy 0:0a8dbe4c0447 114 {
qxy 0:0a8dbe4c0447 115 //Get the rate of Heart by earbud,getHeartRate function
qxy 0:0a8dbe4c0447 116 RateofHeart = earbud.getHeartRate();
qxy 0:0a8dbe4c0447 117
qxy 0:0a8dbe4c0447 118 //Set the pulse value to 0 to get the data
qxy 0:0a8dbe4c0447 119 heater = 0;
qxy 0:0a8dbe4c0447 120 //Get the value of Alcohol rate sensed by Alcohol sensor
qxy 0:0a8dbe4c0447 121 value = 1-Asensor;
qxy 0:0a8dbe4c0447 122
qxy 0:0a8dbe4c0447 123
qxy 0:0a8dbe4c0447 124
qxy 0:0a8dbe4c0447 125 //ESP updates the Status of Thingspeak channel//
qxy 0:0a8dbe4c0447 126
qxy 0:0a8dbe4c0447 127 strcpy(snd,"AT+CIPSTART=");
qxy 0:0a8dbe4c0447 128 strcat(snd,"\"TCP\",\"");
qxy 0:0a8dbe4c0447 129 strcat(snd,IP);
qxy 0:0a8dbe4c0447 130 strcat(snd,"\",80");
qxy 0:0a8dbe4c0447 131
qxy 0:0a8dbe4c0447 132 esp.SendCMD(snd);
qxy 0:0a8dbe4c0447 133 pc.printf("S\r\n%s",snd);
qxy 0:0a8dbe4c0447 134 //wait(2);
qxy 0:0a8dbe4c0447 135 esp.RcvReply(rcv, 1000);
qxy 0:0a8dbe4c0447 136 pc.printf("R\r\n%s",rcv);
qxy 0:0a8dbe4c0447 137 wait(1);
qxy 0:0a8dbe4c0447 138
qxy 0:0a8dbe4c0447 139 pc.printf("Alcohol concentration is: %2.2f\n", value); // Print out the Alcohol concentration on the Tera Term
qxy 0:0a8dbe4c0447 140 pc.printf("heart rate is: %f\r\n", RateofHeart); // Print out the Heart Rate on the Tera Term
qxy 0:0a8dbe4c0447 141 sprintf(snd,"GET https://api.thingspeak.com/update?api_key=SQ43V2ERAB2S42I3&field1=%f&field2=%f\r\n",RateofHeart);
qxy 0:0a8dbe4c0447 142 //Transfer Heart rate and Alcohol concentration to designated Thingspeak ID and display on field 1 chart and field 2 chart
qxy 0:0a8dbe4c0447 143
qxy 0:0a8dbe4c0447 144
qxy 0:0a8dbe4c0447 145 int i=0;
qxy 0:0a8dbe4c0447 146 for(i=0;snd[i]!='\0';i++);
qxy 0:0a8dbe4c0447 147 i++;
qxy 0:0a8dbe4c0447 148 char cmd[255];
qxy 0:0a8dbe4c0447 149
qxy 0:0a8dbe4c0447 150 sprintf(cmd,"AT+CIPSEND=%d",i); //Send Number of open connection and Characters to send
qxy 0:0a8dbe4c0447 151 esp.SendCMD(cmd);
qxy 0:0a8dbe4c0447 152 pc.printf("S\r\n%s",cmd);
qxy 0:0a8dbe4c0447 153 while(i<=20 || rcv == ">")
qxy 0:0a8dbe4c0447 154 {
qxy 0:0a8dbe4c0447 155 esp.RcvReply(rcv, 1000);
qxy 0:0a8dbe4c0447 156 wait(100);
qxy 0:0a8dbe4c0447 157 i++;
qxy 0:0a8dbe4c0447 158 }
qxy 0:0a8dbe4c0447 159 pc.printf("R\r\n%s",rcv);
qxy 0:0a8dbe4c0447 160
qxy 0:0a8dbe4c0447 161 esp.SendCMD(snd); //Post value to thingspeak channel
qxy 0:0a8dbe4c0447 162 pc.printf("S\r\n%s",snd);
qxy 0:0a8dbe4c0447 163
qxy 0:0a8dbe4c0447 164 while(i<=20 || rcv == "OK")
qxy 0:0a8dbe4c0447 165 {
qxy 0:0a8dbe4c0447 166 esp.RcvReply(rcv, 1000);
qxy 0:0a8dbe4c0447 167 wait(100);
qxy 0:0a8dbe4c0447 168 i++;
qxy 0:0a8dbe4c0447 169 }
qxy 0:0a8dbe4c0447 170 pc.printf("R\r\n%s",rcv);
qxy 0:0a8dbe4c0447 171
qxy 0:0a8dbe4c0447 172 }