Grove ear clip sensor and ESP 8266 Wi-Fi module on FRDM-K64F over Thingspeak Server Cloud
Dependencies: mbed GroveEarbudSensor ESP8266_eduvance_shield
main.cpp@0:4c4c953ccd57, 2018-12-02 (annotated)
- Committer:
- bhshruthi92
- Date:
- Sun Dec 02 20:37:25 2018 +0000
- Revision:
- 0:4c4c953ccd57
Uses Grove ear clip sensor and ESP 8266 Wi-Fi module on FRDM-K64F and sent to patient using Thingspeak Server cloud.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bhshruthi92 | 0:4c4c953ccd57 | 1 | #include "mbed.h" |
bhshruthi92 | 0:4c4c953ccd57 | 2 | #include "ESP8266.h" |
bhshruthi92 | 0:4c4c953ccd57 | 3 | #include <string.h> |
bhshruthi92 | 0:4c4c953ccd57 | 4 | #include <stdio.h> |
bhshruthi92 | 0:4c4c953ccd57 | 5 | #include "GroveEarbudSensor.h" |
bhshruthi92 | 0:4c4c953ccd57 | 6 | |
bhshruthi92 | 0:4c4c953ccd57 | 7 | #define APIKEY SJYFHXQEDBQ75N91 //Put "Write key" of your channel in thingspeak.com |
bhshruthi92 | 0:4c4c953ccd57 | 8 | #define IP "184.106.153.149" // IP Address of "api.thingspeak.com\" |
bhshruthi92 | 0:4c4c953ccd57 | 9 | #define WIFI_SSID "BHNTG1682GF6C2" |
bhshruthi92 | 0:4c4c953ccd57 | 10 | #define WIFI_PASS "ef8df304" |
bhshruthi92 | 0:4c4c953ccd57 | 11 | #define sendnumber "3178208811" |
bhshruthi92 | 0:4c4c953ccd57 | 12 | |
bhshruthi92 | 0:4c4c953ccd57 | 13 | char snd[255],rcv[1000],snd_Data[255]; |
bhshruthi92 | 0:4c4c953ccd57 | 14 | float hrt; |
bhshruthi92 | 0:4c4c953ccd57 | 15 | |
bhshruthi92 | 0:4c4c953ccd57 | 16 | ESP8266 esp(PTC17, PTC16, 115200); // baud rate for wifi |
bhshruthi92 | 0:4c4c953ccd57 | 17 | Serial pc(USBTX, USBRX); |
bhshruthi92 | 0:4c4c953ccd57 | 18 | |
bhshruthi92 | 0:4c4c953ccd57 | 19 | // callback for receiving heartrate values |
bhshruthi92 | 0:4c4c953ccd57 | 20 | void heartrateCallback(float heartrate,void *data) |
bhshruthi92 | 0:4c4c953ccd57 | 21 | { |
bhshruthi92 | 0:4c4c953ccd57 | 22 | printf("Callback: heartrate = %.1f\r\n",heartrate); |
bhshruthi92 | 0:4c4c953ccd57 | 23 | } |
bhshruthi92 | 0:4c4c953ccd57 | 24 | |
bhshruthi92 | 0:4c4c953ccd57 | 25 | // Blinky |
bhshruthi92 | 0:4c4c953ccd57 | 26 | DigitalOut led(LED1); |
bhshruthi92 | 0:4c4c953ccd57 | 27 | |
bhshruthi92 | 0:4c4c953ccd57 | 28 | // Our sensor as an InterruptIn |
bhshruthi92 | 0:4c4c953ccd57 | 29 | InterruptIn sensor(D2); |
bhshruthi92 | 0:4c4c953ccd57 | 30 | |
bhshruthi92 | 0:4c4c953ccd57 | 31 | GroveEarbudSensor earbud(&sensor); |
bhshruthi92 | 0:4c4c953ccd57 | 32 | |
bhshruthi92 | 0:4c4c953ccd57 | 33 | void esp_initialize(void); |
bhshruthi92 | 0:4c4c953ccd57 | 34 | void esp_send(void); |
bhshruthi92 | 0:4c4c953ccd57 | 35 | |
bhshruthi92 | 0:4c4c953ccd57 | 36 | int main(void) |
bhshruthi92 | 0:4c4c953ccd57 | 37 | { |
bhshruthi92 | 0:4c4c953ccd57 | 38 | pc.baud(115200); |
bhshruthi92 | 0:4c4c953ccd57 | 39 | esp_initialize(); |
bhshruthi92 | 0:4c4c953ccd57 | 40 | |
bhshruthi92 | 0:4c4c953ccd57 | 41 | // announce |
bhshruthi92 | 0:4c4c953ccd57 | 42 | printf("Grove Earbud Sensor Example v1.0.0\r\n"); |
bhshruthi92 | 0:4c4c953ccd57 | 43 | |
bhshruthi92 | 0:4c4c953ccd57 | 44 | // allocate the earbud sensor |
bhshruthi92 | 0:4c4c953ccd57 | 45 | printf("Allocating earbud sensor instance...\r\n"); |
bhshruthi92 | 0:4c4c953ccd57 | 46 | |
bhshruthi92 | 0:4c4c953ccd57 | 47 | // register our callback function |
bhshruthi92 | 0:4c4c953ccd57 | 48 | printf("registering callback...\r\n"); |
bhshruthi92 | 0:4c4c953ccd57 | 49 | earbud.registerCallback(heartrateCallback); |
bhshruthi92 | 0:4c4c953ccd57 | 50 | |
bhshruthi92 | 0:4c4c953ccd57 | 51 | // begin main loop |
bhshruthi92 | 0:4c4c953ccd57 | 52 | printf("Beginning main loop...\r\n"); |
bhshruthi92 | 0:4c4c953ccd57 | 53 | while (true) |
bhshruthi92 | 0:4c4c953ccd57 | 54 | { |
bhshruthi92 | 0:4c4c953ccd57 | 55 | // blink... |
bhshruthi92 | 0:4c4c953ccd57 | 56 | led = !led; |
bhshruthi92 | 0:4c4c953ccd57 | 57 | wait(1); |
bhshruthi92 | 0:4c4c953ccd57 | 58 | |
bhshruthi92 | 0:4c4c953ccd57 | 59 | // we can also call directly |
bhshruthi92 | 0:4c4c953ccd57 | 60 | printf("Direct: heartrate = %.1f\r\n", earbud.getHeartRate()); |
bhshruthi92 | 0:4c4c953ccd57 | 61 | |
bhshruthi92 | 0:4c4c953ccd57 | 62 | } |
bhshruthi92 | 0:4c4c953ccd57 | 63 | } |
bhshruthi92 | 0:4c4c953ccd57 | 64 | void esp_initialize(void) |
bhshruthi92 | 0:4c4c953ccd57 | 65 | { |
bhshruthi92 | 0:4c4c953ccd57 | 66 | pc.printf("Initializing ESP\r\n"); |
bhshruthi92 | 0:4c4c953ccd57 | 67 | |
bhshruthi92 | 0:4c4c953ccd57 | 68 | pc.printf("Reset ESP\r\n"); |
bhshruthi92 | 0:4c4c953ccd57 | 69 | esp.Reset(); //RESET ESP |
bhshruthi92 | 0:4c4c953ccd57 | 70 | esp.RcvReply(rcv, 400); //receive a response from ESP |
bhshruthi92 | 0:4c4c953ccd57 | 71 | wait(2); |
bhshruthi92 | 0:4c4c953ccd57 | 72 | |
bhshruthi92 | 0:4c4c953ccd57 | 73 | strcpy(snd,"AT"); |
bhshruthi92 | 0:4c4c953ccd57 | 74 | esp.SendCMD(snd); |
bhshruthi92 | 0:4c4c953ccd57 | 75 | pc.printf(snd); |
bhshruthi92 | 0:4c4c953ccd57 | 76 | esp.RcvReply(rcv, 400); |
bhshruthi92 | 0:4c4c953ccd57 | 77 | pc.printf(rcv); |
bhshruthi92 | 0:4c4c953ccd57 | 78 | wait(0.1); |
bhshruthi92 | 0:4c4c953ccd57 | 79 | |
bhshruthi92 | 0:4c4c953ccd57 | 80 | strcpy(snd,"AT+CWMODE=1"); |
bhshruthi92 | 0:4c4c953ccd57 | 81 | esp.SendCMD(snd); |
bhshruthi92 | 0:4c4c953ccd57 | 82 | pc.printf(snd); |
bhshruthi92 | 0:4c4c953ccd57 | 83 | wait(2); |
bhshruthi92 | 0:4c4c953ccd57 | 84 | |
bhshruthi92 | 0:4c4c953ccd57 | 85 | strcpy(snd,"AT+CWJAP=\""); |
bhshruthi92 | 0:4c4c953ccd57 | 86 | strcat(snd,WIFI_SSID); |
bhshruthi92 | 0:4c4c953ccd57 | 87 | strcat(snd,"\",\""); |
bhshruthi92 | 0:4c4c953ccd57 | 88 | strcat(snd,WIFI_PASS); |
bhshruthi92 | 0:4c4c953ccd57 | 89 | strcat(snd,"\""); |
bhshruthi92 | 0:4c4c953ccd57 | 90 | |
bhshruthi92 | 0:4c4c953ccd57 | 91 | |
bhshruthi92 | 0:4c4c953ccd57 | 92 | esp.SendCMD(snd); |
bhshruthi92 | 0:4c4c953ccd57 | 93 | pc.printf(snd); |
bhshruthi92 | 0:4c4c953ccd57 | 94 | wait(5); |
bhshruthi92 | 0:4c4c953ccd57 | 95 | esp.RcvReply(rcv, 400); |
bhshruthi92 | 0:4c4c953ccd57 | 96 | pc.printf("\n %s \n", rcv); |
bhshruthi92 | 0:4c4c953ccd57 | 97 | |
bhshruthi92 | 0:4c4c953ccd57 | 98 | strcpy(snd,"AT+CIPMUX=1"); |
bhshruthi92 | 0:4c4c953ccd57 | 99 | esp.SendCMD(snd); |
bhshruthi92 | 0:4c4c953ccd57 | 100 | pc.printf(snd); |
bhshruthi92 | 0:4c4c953ccd57 | 101 | esp.RcvReply(rcv, 400); |
bhshruthi92 | 0:4c4c953ccd57 | 102 | pc.printf("\n %s \n", rcv); |
bhshruthi92 | 0:4c4c953ccd57 | 103 | |
bhshruthi92 | 0:4c4c953ccd57 | 104 | } |
bhshruthi92 | 0:4c4c953ccd57 | 105 | void esp_send(void) |
bhshruthi92 | 0:4c4c953ccd57 | 106 | { |
bhshruthi92 | 0:4c4c953ccd57 | 107 | //ESP updates the Status of Thingspeak channel// |
bhshruthi92 | 0:4c4c953ccd57 | 108 | |
bhshruthi92 | 0:4c4c953ccd57 | 109 | strcpy(snd,"AT+CIPSTART="); |
bhshruthi92 | 0:4c4c953ccd57 | 110 | strcat(snd,"\"TCP\",\""); |
bhshruthi92 | 0:4c4c953ccd57 | 111 | strcat(snd,IP); |
bhshruthi92 | 0:4c4c953ccd57 | 112 | strcat(snd,"\",80"); |
bhshruthi92 | 0:4c4c953ccd57 | 113 | |
bhshruthi92 | 0:4c4c953ccd57 | 114 | esp.SendCMD(snd); |
bhshruthi92 | 0:4c4c953ccd57 | 115 | pc.printf("Send\r\n%s",snd); |
bhshruthi92 | 0:4c4c953ccd57 | 116 | esp.RcvReply(rcv, 1000); |
bhshruthi92 | 0:4c4c953ccd57 | 117 | pc.printf("Receive\r\n%s",rcv); |
bhshruthi92 | 0:4c4c953ccd57 | 118 | wait(1); |
bhshruthi92 | 0:4c4c953ccd57 | 119 | |
bhshruthi92 | 0:4c4c953ccd57 | 120 | hrt = earbud.getHeartRate(); |
bhshruthi92 | 0:4c4c953ccd57 | 121 | pc.printf("Sending this data to thingspeak.com \r \n"); |
bhshruthi92 | 0:4c4c953ccd57 | 122 | sprintf(snd,"GET https://api.thingspeak.com/update?api_key=SJYFHXQEDBQ75N91&field1=%f\r\n", hrt); |
bhshruthi92 | 0:4c4c953ccd57 | 123 | |
bhshruthi92 | 0:4c4c953ccd57 | 124 | int i=0; |
bhshruthi92 | 0:4c4c953ccd57 | 125 | for(i=0; snd[i]!='\0'; i++); |
bhshruthi92 | 0:4c4c953ccd57 | 126 | i++; |
bhshruthi92 | 0:4c4c953ccd57 | 127 | char cmd[255]; |
bhshruthi92 | 0:4c4c953ccd57 | 128 | |
bhshruthi92 | 0:4c4c953ccd57 | 129 | sprintf(cmd,"AT+CIPSEND=%d",i); //Send Number of open connection and Characters to send |
bhshruthi92 | 0:4c4c953ccd57 | 130 | esp.SendCMD(cmd); |
bhshruthi92 | 0:4c4c953ccd57 | 131 | pc.printf("Send\r\n%s",cmd); |
bhshruthi92 | 0:4c4c953ccd57 | 132 | while(i<=20 || rcv == ">") |
bhshruthi92 | 0:4c4c953ccd57 | 133 | { |
bhshruthi92 | 0:4c4c953ccd57 | 134 | esp.RcvReply(rcv, 1000); |
bhshruthi92 | 0:4c4c953ccd57 | 135 | wait(100); |
bhshruthi92 | 0:4c4c953ccd57 | 136 | i++; |
bhshruthi92 | 0:4c4c953ccd57 | 137 | } |
bhshruthi92 | 0:4c4c953ccd57 | 138 | pc.printf("Receive\r\n%s",rcv); |
bhshruthi92 | 0:4c4c953ccd57 | 139 | |
bhshruthi92 | 0:4c4c953ccd57 | 140 | esp.SendCMD(snd); //Post value to thingspeak channel |
bhshruthi92 | 0:4c4c953ccd57 | 141 | pc.printf("Send\r\n%s",snd); |
bhshruthi92 | 0:4c4c953ccd57 | 142 | |
bhshruthi92 | 0:4c4c953ccd57 | 143 | while(i<=20 || rcv == "OK") |
bhshruthi92 | 0:4c4c953ccd57 | 144 | { |
bhshruthi92 | 0:4c4c953ccd57 | 145 | esp.RcvReply(rcv, 1000); |
bhshruthi92 | 0:4c4c953ccd57 | 146 | wait(100); |
bhshruthi92 | 0:4c4c953ccd57 | 147 | i++; |
bhshruthi92 | 0:4c4c953ccd57 | 148 | } |
bhshruthi92 | 0:4c4c953ccd57 | 149 | pc.printf("Receive\r\n%s",rcv); |
bhshruthi92 | 0:4c4c953ccd57 | 150 | } |
bhshruthi92 | 0:4c4c953ccd57 | 151 | |
bhshruthi92 | 0:4c4c953ccd57 | 152 |