gs fan
/
GSwifi_cosm_pachube
Cosm (Pachube) feed see: http://mbed.org/users/gsfan/notebook/gainspan_wifi/
main.cpp@0:0360c49c3bbe, 2013-03-29 (annotated)
- Committer:
- gsfan
- Date:
- Fri Mar 29 15:01:57 2013 +0000
- Revision:
- 0:0360c49c3bbe
- Child:
- 1:0852a9bd5365
1st build
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:0360c49c3bbe | 28 | Host host; |
gsfan | 0:0360c49c3bbe | 29 | int cid; |
gsfan | 0:0360c49c3bbe | 30 | char buf[100]; |
gsfan | 0:0360c49c3bbe | 31 | |
gsfan | 0:0360c49c3bbe | 32 | host.setName(FEED_HOST); |
gsfan | 0:0360c49c3bbe | 33 | host.setPort(80); |
gsfan | 0:0360c49c3bbe | 34 | cid = gs.open(host, GSwifi::GSPROT_TCP, &onGsReceive); |
gsfan | 0:0360c49c3bbe | 35 | if (cid < 0) return -1; |
gsfan | 0:0360c49c3bbe | 36 | |
gsfan | 0:0360c49c3bbe | 37 | sprintf(buf, "PUT /v2/feeds/" FEED_ID ".csv HTTP/1.1\r\n"); |
gsfan | 0:0360c49c3bbe | 38 | gs.send(cid, buf, strlen(buf)); |
gsfan | 0:0360c49c3bbe | 39 | sprintf(buf, "Host: " FEED_HOST "\r\n"); |
gsfan | 0:0360c49c3bbe | 40 | gs.send(cid, buf, strlen(buf)); |
gsfan | 0:0360c49c3bbe | 41 | sprintf(buf, "Connection: close\r\n"); |
gsfan | 0:0360c49c3bbe | 42 | gs.send(cid, buf, strlen(buf)); |
gsfan | 0:0360c49c3bbe | 43 | sprintf(buf, "X-ApiKey: " API_KEY "\r\n"); |
gsfan | 0:0360c49c3bbe | 44 | gs.send(cid, buf, strlen(buf)); |
gsfan | 0:0360c49c3bbe | 45 | sprintf(buf, "Content-Type: text/csv\r\n"); |
gsfan | 0:0360c49c3bbe | 46 | gs.send(cid, buf, strlen(buf)); |
gsfan | 0:0360c49c3bbe | 47 | sprintf(buf, "Content-Length: %d\r\n", strlen(data)); |
gsfan | 0:0360c49c3bbe | 48 | gs.send(cid, buf, strlen(buf)); |
gsfan | 0:0360c49c3bbe | 49 | gs.send(cid, "\r\n", 2); |
gsfan | 0:0360c49c3bbe | 50 | gs.send(cid, data, strlen(data)); |
gsfan | 0:0360c49c3bbe | 51 | |
gsfan | 0:0360c49c3bbe | 52 | gs.close(cid); |
gsfan | 0:0360c49c3bbe | 53 | return 0; |
gsfan | 0:0360c49c3bbe | 54 | } |
gsfan | 0:0360c49c3bbe | 55 | |
gsfan | 0:0360c49c3bbe | 56 | int main () { |
gsfan | 0:0360c49c3bbe | 57 | int r; |
gsfan | 0:0360c49c3bbe | 58 | IpAddr ipaddr, netmask, gateway, nameserver; |
gsfan | 0:0360c49c3bbe | 59 | char data[] = "1,123\r\n2,345\r\n"; |
gsfan | 0:0360c49c3bbe | 60 | |
gsfan | 0:0360c49c3bbe | 61 | led1 = 1; |
gsfan | 0:0360c49c3bbe | 62 | pc.baud(115200); |
gsfan | 0:0360c49c3bbe | 63 | |
gsfan | 0:0360c49c3bbe | 64 | pc.printf("connect\r\n"); |
gsfan | 0:0360c49c3bbe | 65 | if (gs.connect(SECURE, SSID, PASS, 0)) { |
gsfan | 0:0360c49c3bbe | 66 | return -1; |
gsfan | 0:0360c49c3bbe | 67 | } |
gsfan | 0:0360c49c3bbe | 68 | gs.getAddress(ipaddr, netmask, gateway, nameserver); |
gsfan | 0:0360c49c3bbe | 69 | pc.printf("ip %d.%d.%d.%d\r\n", ipaddr[0], ipaddr[1], ipaddr[2], ipaddr[3]); |
gsfan | 0:0360c49c3bbe | 70 | |
gsfan | 0:0360c49c3bbe | 71 | pc.printf("Cosm\r\n"); |
gsfan | 0:0360c49c3bbe | 72 | cosm(data); |
gsfan | 0:0360c49c3bbe | 73 | |
gsfan | 0:0360c49c3bbe | 74 | pc.printf("exit\r\n"); |
gsfan | 0:0360c49c3bbe | 75 | } |