A ShipIoT tutorial for connecting a FRDM-K64F to bip.io

Dependencies:   EthernetInterface FXOS8700Q mbed-rtos mbed

Committer:
cthulhuology
Date:
Mon Aug 17 14:46:26 2015 +0000
Revision:
0:c07fa8c822d2
Initial Release

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cthulhuology 0:c07fa8c822d2 1 #include <mbed.h>
cthulhuology 0:c07fa8c822d2 2 #include <EthernetInterface.h>
cthulhuology 0:c07fa8c822d2 3 #include "FXOS8700Q.h"
cthulhuology 0:c07fa8c822d2 4
cthulhuology 0:c07fa8c822d2 5 int main(int argc, char** argv) {
cthulhuology 0:c07fa8c822d2 6
cthulhuology 0:c07fa8c822d2 7 int16_t X, Y, Z;
cthulhuology 0:c07fa8c822d2 8
cthulhuology 0:c07fa8c822d2 9 Serial pc(USBTX, USBRX);
cthulhuology 0:c07fa8c822d2 10 pc.baud(115200);
cthulhuology 0:c07fa8c822d2 11
cthulhuology 0:c07fa8c822d2 12 I2C i2c(PTE25,PTE24);
cthulhuology 0:c07fa8c822d2 13 FXOS8700QAccelerometer acc(i2c, FXOS8700CQ_SLAVE_ADDR1);
cthulhuology 0:c07fa8c822d2 14 acc.enable();
cthulhuology 0:c07fa8c822d2 15
cthulhuology 0:c07fa8c822d2 16 pc.printf("Welcome to the FRDM + Bipio\r\n");
cthulhuology 0:c07fa8c822d2 17
cthulhuology 0:c07fa8c822d2 18 EthernetInterface eth;
cthulhuology 0:c07fa8c822d2 19 eth.init();
cthulhuology 0:c07fa8c822d2 20 eth.connect();
cthulhuology 0:c07fa8c822d2 21
cthulhuology 0:c07fa8c822d2 22 pc.printf("IP Addr: %s\r\n", eth.getIPAddress());
cthulhuology 0:c07fa8c822d2 23
cthulhuology 0:c07fa8c822d2 24 while(true) {
cthulhuology 0:c07fa8c822d2 25 pc.printf("Polling x,y,z\r\n");
cthulhuology 0:c07fa8c822d2 26 TCPSocketConnection sock;
cthulhuology 0:c07fa8c822d2 27 sock.connect("david.api.shipiot.net",80);
cthulhuology 0:c07fa8c822d2 28
cthulhuology 0:c07fa8c822d2 29 acc.getX(X);
cthulhuology 0:c07fa8c822d2 30 acc.getY(Y);
cthulhuology 0:c07fa8c822d2 31 acc.getZ(Z);
cthulhuology 0:c07fa8c822d2 32
cthulhuology 0:c07fa8c822d2 33 char http_request[] = "POST /bip/http/accel HTTP/1.1\r\n"
cthulhuology 0:c07fa8c822d2 34 "Host: username.api.shipiot.net\r\n" // change the username to your username
cthulhuology 0:c07fa8c822d2 35 "Content-Type: application/json\r\n"
cthulhuology 0:c07fa8c822d2 36 "Content-Length: %d\r\n"
cthulhuology 0:c07fa8c822d2 37 "Authorization: Basic dGVzdDp0ZXN0\r\n" // change the dGVzdDp0ZXN0 to your auth token
cthulhuology 0:c07fa8c822d2 38 "\r\n"
cthulhuology 0:c07fa8c822d2 39 "%s";
cthulhuology 0:c07fa8c822d2 40
cthulhuology 0:c07fa8c822d2 41 char buffer[255];
cthulhuology 0:c07fa8c822d2 42 char json[] = "{ \"x\": %d, \"y\": %d, \"z\": %d }";
cthulhuology 0:c07fa8c822d2 43
cthulhuology 0:c07fa8c822d2 44 sprintf(buffer,json,X,Y,Z);
cthulhuology 0:c07fa8c822d2 45
cthulhuology 0:c07fa8c822d2 46 char request[512];
cthulhuology 0:c07fa8c822d2 47
cthulhuology 0:c07fa8c822d2 48 sprintf(request,http_request,strlen(buffer), buffer);
cthulhuology 0:c07fa8c822d2 49
cthulhuology 0:c07fa8c822d2 50 sock.send_all(request, strlen(request));
cthulhuology 0:c07fa8c822d2 51
cthulhuology 0:c07fa8c822d2 52 pc.printf(buffer);
cthulhuology 0:c07fa8c822d2 53
cthulhuology 0:c07fa8c822d2 54 sock.close();
cthulhuology 0:c07fa8c822d2 55
cthulhuology 0:c07fa8c822d2 56 wait(1.0);
cthulhuology 0:c07fa8c822d2 57
cthulhuology 0:c07fa8c822d2 58 }
cthulhuology 0:c07fa8c822d2 59 // should never get here...
cthulhuology 0:c07fa8c822d2 60 }