Heart rate monitoring

Dependencies:   mbed GroveEarbudSensor ESP8266

Revision:
6:37829af7bfbd
Parent:
5:f1b55ff3cda0
diff -r f1b55ff3cda0 -r 37829af7bfbd main.cpp
--- a/main.cpp	Wed Oct 01 21:35:54 2014 +0000
+++ b/main.cpp	Thu May 04 04:24:48 2017 +0000
@@ -1,60 +1,156 @@
-/* Copyright C2014 ARM, MIT License
- *
- * Author: Doug Anson (doug.anson@arm.com)
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
- * and associated documentation files the "Software", to deal in the Software without restriction,
- * including without limitation the rights to use, copy, modify, merge, publish, distribute,
- * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all copies or
- * substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
- * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
- * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- */
+#include "mbed.h"
+#include "ESP8266.h"
+#include <string>
+#include <stdio.h>
+#include "GroveEarbudSensor.h"
+#define APIKEY SJYFHXQEDBQ75N91    //Put "Write key" of your channel in thingspeak.com 
+#define IP "184.106.153.149"       // IP Address of "api.thingspeak.com\"
+#define WIFI_SSID "vivo 1601"
+#define WIFI_PASS "bschitta"
+
 
-#include "mbed.h"
+char snd[255],rcv[1000],snd_Data[255];
+float hrt;
+
+ESP8266 esp(PTC17, PTC16, 115200); // baud rate for wifi
+Serial pc(USBTX, USBRX);
+
+// callback for receiving heartrate values 
+void heartrateCallback(float heartrate,void *data) 
+{
+         printf("Callback: heartrate = %.1f\r\n",heartrate);
+}  
 
 // Blinky
 DigitalOut led(LED1);
 
 // Our sensor as an InterruptIn
-InterruptIn sensor(D0);
+InterruptIn sensor(D2);
 
-// Grove Earbud Sensor include
-#include "GroveEarbudSensor.h"
+GroveEarbudSensor earbud(&sensor); 
 
-// callback for receiving heartrate values
-void heartrateCallback(float heartrate,void *data) {
-    printf("Callback: heartrate = %.1f\r\n",heartrate);
-}
+void esp_initialize(void);
+void esp_send(void);
 
-int main()
+int main(void)
 {   
+pc.baud(115200);
+    esp_initialize();
+    
     // announce
     printf("Grove Earbud Sensor Example v1.0.0\r\n");
     
     // allocate the earbud sensor
     printf("Allocating earbud sensor instance...\r\n");
-    GroveEarbudSensor earbud(&sensor); 
     
-    // register our callback function
-    printf("registering callback...\r\n");
-    earbud.registerCallback(heartrateCallback);
+     // register our callback function     
+     printf("registering callback...\r\n");     
+     earbud.registerCallback(heartrateCallback); 
     
     // begin main loop
     printf("Beginning main loop...\r\n");
-    while (true) {
+    while (true) 
+    {
         // blink... 
         led = !led; 
-        wait(0.5);
-        
-        // we can also call directly 
-        //printf("Direct: heartrate = %.1f\r\n",earbud.getHeartRate());
+        wait(1);
+           
+    // we can also call directly 
+    printf("Direct: heartrate = %.1f\r\n", earbud.getHeartRate());
+    
+    esp_send();
     }
-}
\ No newline at end of file
+}
+
+void esp_initialize(void)
+{
+    //AT+CWJAP="vivo 1601","bschitta" ;
+    
+    pc.printf("Initializing ESP\r\n");
+    pc.printf("Reset ESP\r\n");
+    esp.Reset();                   //RESET ESP
+    esp.RcvReply(rcv, 400);        //receive a response from ESP
+    wait(2);
+
+    strcpy(snd,"AT");
+    esp.SendCMD(snd);
+    pc.printf(snd);
+    esp.RcvReply(rcv, 400);
+    pc.printf(rcv);
+    wait(2);
+
+    strcpy(snd,"AT+CWMODE=1");
+    esp.SendCMD(snd);
+    pc.printf(snd);
+    wait(2);
+
+    strcpy(snd,"AT+CWJAP=\"");
+    strcat(snd,WIFI_SSID);
+    strcat(snd,"\",\"");
+    strcat(snd,WIFI_PASS);
+    strcat(snd,"\"");
+
+    esp.SendCMD(snd);
+    pc.printf(snd);
+    wait(5);
+    esp.RcvReply(rcv, 400);
+    pc.printf("\n %s \n", rcv);
+
+    strcpy(snd,"AT+CIPMUX=1");
+    esp.SendCMD(snd);
+    pc.printf(snd);
+    esp.RcvReply(rcv, 400);
+    pc.printf("\n %s \n", rcv);
+}
+
+void esp_send(void)
+{
+    //ESP updates the Status of Thingspeak channel//
+
+    strcpy(snd,"AT+CIPSTART=");
+    strcat(snd,"\"TCP\",\"");
+    strcat(snd,IP);
+    strcat(snd,"\",80");
+
+    esp.SendCMD(snd);
+    pc.printf("Send\r\n%s",snd);
+    esp.RcvReply(rcv, 1000);
+    pc.printf("Receive\r\n%s",rcv);
+    wait(2);
+
+    hrt = earbud.getHeartRate();
+    pc.printf("Sending this information to thingspeak.com \r\n");
+    sprintf(snd,"GET https://api.thingspeak.com/update?api_key=SJYFHXQEDBQ75N91&field1=%f\r\n", hrt);
+    //wait(5);
+    //https://api.thingspeak.com/update?api_key=SJYFHXQEDBQ75N91&field1=%f\r\n", hrt
+
+    int i=0;
+    for(i=0; snd[i]!='\0'; i++);
+    i++;
+    char cmd[255];
+
+    sprintf(cmd,"AT+CIPSEND=%d",i);           //Send Number of open connection and Characters to send
+    esp.SendCMD(cmd);
+    pc.printf("Send\r\n%s",cmd);
+    while(i<=20 || rcv == ">")
+    {
+        esp.RcvReply(rcv, 1000);
+        wait(100);
+        i++;
+    }
+    pc.printf("Receive\r\n%s",rcv);
+
+    esp.SendCMD(snd);       //Post value to thingspeak channel
+    pc.printf("Send\r\n%s",snd);
+
+    while(i<=20 || rcv == "OK") 
+    {
+        esp.RcvReply(rcv, 1000);
+        wait(100);
+        i++;
+    }
+    pc.printf("Receive\r\n%s",rcv);
+    
+}
+
+