Takehiko Shimojima / Mbed 2 deprecated AmbientHeartBeat

Dependencies:   AmbientLib SimpleIoTBoardLib mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002  * while (true) {
00003  *     clear data.
00004  *     sample heart pulse every 25m seconds, 28 times.
00005  *     send data to Ambient.
00006  *     wait 2 seconds.
00007  */
00008 #include "mbed.h"
00009 #include "math.h"
00010 #include "ESP8266Interface.h"
00011 #include "TCPSocketConnection.h"
00012 #include "SoftSerialSendOnry.h"
00013 #include "Ambient.h"
00014 
00015 #define SAMPLING 25         //  Sampling period in milliseconds
00016                             //  When BPM is 100, IBI is 600msec and we have 30 samples.
00017 #define NSAMPLES 35         //  Number of Samples
00018 #define BUFSIZE  1050
00019 
00020 ESP8266Interface wifi(dp16,dp15,dp4,"ssid","password",115200); // TX,RX,Reset,SSID,Password,Baud
00021 
00022 SoftSerialSendOnry pc(dp10);
00023 DigitalOut led(dp6);
00024 
00025 unsigned int channelId = 100;
00026 const char* userKey = "ユーザーキー";
00027 const char* writeKey = "ライトキー";
00028 Ambient ambient;
00029 
00030 AnalogIn pulsePin(dp13);
00031 Ticker t2;
00032 volatile int done = false;
00033 unsigned short signal[NSAMPLES];
00034 char buffer[BUFSIZE];
00035 volatile int sampleIndex = 0;
00036 
00037 void sampling() {
00038     led = 1;
00039     signal[sampleIndex++] = pulsePin.read_u16();
00040     if (sampleIndex >= NSAMPLES) {
00041         done = true;
00042     }
00043 }
00044 
00045 int main() {
00046     TCPSocketConnection socket;
00047 
00048     pc.baud(9600);
00049     pc.printf("start\r\n");
00050     wifi.init(); //Reset
00051     wifi.connect(); //Use DHCP
00052     pc.printf("IP Address is %s\r\n", wifi.getIPAddress());
00053     ambient.init(channelId, writeKey, &socket);
00054     led = 0;
00055 
00056     while (true) {
00057         pc.printf("sampling start...");
00058         done = false;
00059         sampleIndex = 0;
00060         t2.attach_us(&sampling, SAMPLING * 1000.0f);
00061         while (!done) {
00062             ;
00063         }
00064         t2.detach();
00065         pc.printf("done\r\n");
00066         led = 0;
00067     
00068         sprintf(buffer, "{\"writeKey\":\"%s\",\"data\":[", writeKey);
00069         for (int i = 0; i < NSAMPLES; i++) {
00070             sprintf(&buffer[strlen(buffer)], "{\"created\":%d,\"d%d\":%d},", SAMPLING * i, 2, signal[i]);
00071         }
00072         buffer[strlen(buffer)-1] = '\0';
00073         sprintf(&buffer[strlen(buffer)], "]}\r\n");
00074         
00075         ambient.delete_data(userKey);
00076 
00077         int sent = ambient.bulk_send(buffer);
00078         pc.printf("%d bytes sent\r\n", sent);
00079 
00080         wait(5);
00081     }
00082 }