Heart rate monitoring system design using FRDM K64F and MIT App Inventor II

Dependencies:   ESP8266 GroveEarbudSensor mbed

Fork of Heart-rate-monitor by Sarada Gajjala

main.cpp

Committer:
spriyanka
Date:
2017-05-04
Revision:
6:37829af7bfbd
Parent:
5:f1b55ff3cda0

File content as of revision 6:37829af7bfbd:

#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"


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(D2);

GroveEarbudSensor earbud(&sensor); 

void esp_initialize(void);
void esp_send(void);

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");
    
     // register our callback function     
     printf("registering callback...\r\n");     
     earbud.registerCallback(heartrateCallback); 
    
    // begin main loop
    printf("Beginning main loop...\r\n");
    while (true) 
    {
        // blink... 
        led = !led; 
        wait(1);
           
    // we can also call directly 
    printf("Direct: heartrate = %.1f\r\n", earbud.getHeartRate());
    
    esp_send();
    }
}

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);
    
}