Xively (Cosm, Pachube) feed see: http://mbed.org/users/gsfan/notebook/gainspan_wifi/

Dependencies:   GSwifi mbed

Committer:
gsfan
Date:
Wed Apr 03 08:48:41 2013 +0000
Revision:
1:0852a9bd5365
Parent:
0:0360c49c3bbe
Child:
2:a8ab83e57440
fix

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gsfan 0:0360c49c3bbe 1 #include "mbed.h"
gsfan 0:0360c49c3bbe 2 #include "GSwifi.h"
gsfan 0:0360c49c3bbe 3
gsfan 0:0360c49c3bbe 4 #define SECURE GSwifi::GSSEC_WPA_PSK
gsfan 0:0360c49c3bbe 5 #define SSID "SSID"
gsfan 0:0360c49c3bbe 6 #define PASS "passkey"
gsfan 0:0360c49c3bbe 7
gsfan 0:0360c49c3bbe 8 #define FEED_HOST "api.cosm.com"
gsfan 0:0360c49c3bbe 9 #define FEED_ID "000000"
gsfan 0:0360c49c3bbe 10 #define API_KEY "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
gsfan 0:0360c49c3bbe 11
gsfan 0:0360c49c3bbe 12 GSwifi gs(p13, p14, p20); // TX, RX, Reset (no flow control)
gsfan 0:0360c49c3bbe 13 //GSwifi gs(p13, p14, p12, P0_22, p20, NC, 115200); // TX, RX, CTS, RTS, Reset, Alarm
gsfan 0:0360c49c3bbe 14 Serial pc(USBTX, USBRX);
gsfan 0:0360c49c3bbe 15 DigitalOut led1(LED1), led2(LED2);
gsfan 0:0360c49c3bbe 16
gsfan 0:0360c49c3bbe 17 void onGsReceive (int cid, int len) {
gsfan 0:0360c49c3bbe 18 int i;
gsfan 0:0360c49c3bbe 19 char buf[GS_DATA_SIZE + 1];
gsfan 0:0360c49c3bbe 20
gsfan 0:0360c49c3bbe 21 led2 = 1;
gsfan 0:0360c49c3bbe 22 i = gs.recv(cid, buf, len);
gsfan 0:0360c49c3bbe 23 buf[i] = 0;
gsfan 0:0360c49c3bbe 24 pc.printf(buf);
gsfan 0:0360c49c3bbe 25 }
gsfan 0:0360c49c3bbe 26
gsfan 0:0360c49c3bbe 27 int cosm (char *data) {
gsfan 1:0852a9bd5365 28 int i;
gsfan 0:0360c49c3bbe 29 Host host;
gsfan 0:0360c49c3bbe 30 int cid;
gsfan 0:0360c49c3bbe 31 char buf[100];
gsfan 0:0360c49c3bbe 32
gsfan 0:0360c49c3bbe 33 host.setName(FEED_HOST);
gsfan 0:0360c49c3bbe 34 host.setPort(80);
gsfan 0:0360c49c3bbe 35 cid = gs.open(host, GSwifi::GSPROT_TCP, &onGsReceive);
gsfan 0:0360c49c3bbe 36 if (cid < 0) return -1;
gsfan 0:0360c49c3bbe 37
gsfan 0:0360c49c3bbe 38 sprintf(buf, "PUT /v2/feeds/" FEED_ID ".csv HTTP/1.1\r\n");
gsfan 0:0360c49c3bbe 39 gs.send(cid, buf, strlen(buf));
gsfan 0:0360c49c3bbe 40 sprintf(buf, "Host: " FEED_HOST "\r\n");
gsfan 0:0360c49c3bbe 41 gs.send(cid, buf, strlen(buf));
gsfan 0:0360c49c3bbe 42 sprintf(buf, "Connection: close\r\n");
gsfan 0:0360c49c3bbe 43 gs.send(cid, buf, strlen(buf));
gsfan 0:0360c49c3bbe 44 sprintf(buf, "X-ApiKey: " API_KEY "\r\n");
gsfan 0:0360c49c3bbe 45 gs.send(cid, buf, strlen(buf));
gsfan 0:0360c49c3bbe 46 sprintf(buf, "Content-Type: text/csv\r\n");
gsfan 0:0360c49c3bbe 47 gs.send(cid, buf, strlen(buf));
gsfan 0:0360c49c3bbe 48 sprintf(buf, "Content-Length: %d\r\n", strlen(data));
gsfan 0:0360c49c3bbe 49 gs.send(cid, buf, strlen(buf));
gsfan 0:0360c49c3bbe 50 gs.send(cid, "\r\n", 2);
gsfan 0:0360c49c3bbe 51 gs.send(cid, data, strlen(data));
gsfan 0:0360c49c3bbe 52
gsfan 1:0852a9bd5365 53 for (i = 0; i < 10; i ++) {
gsfan 1:0852a9bd5365 54 if (! gs.isConnected(cid)) break;
gsfan 1:0852a9bd5365 55 wait(100);
gsfan 1:0852a9bd5365 56 }
gsfan 0:0360c49c3bbe 57 gs.close(cid);
gsfan 0:0360c49c3bbe 58 return 0;
gsfan 0:0360c49c3bbe 59 }
gsfan 0:0360c49c3bbe 60
gsfan 0:0360c49c3bbe 61 int main () {
gsfan 0:0360c49c3bbe 62 IpAddr ipaddr, netmask, gateway, nameserver;
gsfan 0:0360c49c3bbe 63 char data[] = "1,123\r\n2,345\r\n";
gsfan 0:0360c49c3bbe 64
gsfan 0:0360c49c3bbe 65 led1 = 1;
gsfan 0:0360c49c3bbe 66 pc.baud(115200);
gsfan 0:0360c49c3bbe 67
gsfan 0:0360c49c3bbe 68 pc.printf("connect\r\n");
gsfan 0:0360c49c3bbe 69 if (gs.connect(SECURE, SSID, PASS, 0)) {
gsfan 0:0360c49c3bbe 70 return -1;
gsfan 0:0360c49c3bbe 71 }
gsfan 0:0360c49c3bbe 72 gs.getAddress(ipaddr, netmask, gateway, nameserver);
gsfan 0:0360c49c3bbe 73 pc.printf("ip %d.%d.%d.%d\r\n", ipaddr[0], ipaddr[1], ipaddr[2], ipaddr[3]);
gsfan 0:0360c49c3bbe 74
gsfan 0:0360c49c3bbe 75 pc.printf("Cosm\r\n");
gsfan 0:0360c49c3bbe 76 cosm(data);
gsfan 0:0360c49c3bbe 77
gsfan 0:0360c49c3bbe 78 pc.printf("exit\r\n");
gsfan 0:0360c49c3bbe 79 }