![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
h
Dependencies: mbed EthernetNetIf
main.cpp@2:764ecec3dc59, 2011-07-28 (annotated)
- Committer:
- okini3939
- Date:
- Thu Jul 28 16:35:09 2011 +0000
- Revision:
- 2:764ecec3dc59
- Parent:
- 0:f2bf5f966801
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
okini3939 | 0:f2bf5f966801 | 1 | #include "mbed.h" |
okini3939 | 0:f2bf5f966801 | 2 | #include "EthernetNetIf.h" |
okini3939 | 0:f2bf5f966801 | 3 | #include "TCPSocket.h" |
okini3939 | 0:f2bf5f966801 | 4 | #include "TinyHTTP.h" |
okini3939 | 0:f2bf5f966801 | 5 | |
okini3939 | 0:f2bf5f966801 | 6 | DigitalOut myled(LED1); |
okini3939 | 0:f2bf5f966801 | 7 | Serial pc(USBTX, USBRX); |
okini3939 | 0:f2bf5f966801 | 8 | EthernetNetIf eth; |
okini3939 | 0:f2bf5f966801 | 9 | |
okini3939 | 2:764ecec3dc59 | 10 | |
okini3939 | 2:764ecec3dc59 | 11 | int pachube (int feedid, char *apikey, char *buf) { |
okini3939 | 2:764ecec3dc59 | 12 | Host host; |
okini3939 | 2:764ecec3dc59 | 13 | char uri[40], head[160]; |
okini3939 | 2:764ecec3dc59 | 14 | |
okini3939 | 2:764ecec3dc59 | 15 | // header |
okini3939 | 2:764ecec3dc59 | 16 | snprintf(head, sizeof(head), "Content-type: text/csv\r\nX-PachubeApiKey: %s\r\n", apikey); |
okini3939 | 2:764ecec3dc59 | 17 | |
okini3939 | 2:764ecec3dc59 | 18 | // uri |
okini3939 | 2:764ecec3dc59 | 19 | snprintf(uri, sizeof(uri), "/v1/feeds/%d.csv?_method=put", feedid); |
okini3939 | 2:764ecec3dc59 | 20 | |
okini3939 | 2:764ecec3dc59 | 21 | host.setName("api.pachube.com"); |
okini3939 | 2:764ecec3dc59 | 22 | host.setPort(HTTP_PORT); |
okini3939 | 2:764ecec3dc59 | 23 | return httpRequest(METHOD_POST, &host, uri, head, buf) == 200 ? 0 : -1; |
okini3939 | 2:764ecec3dc59 | 24 | } |
okini3939 | 2:764ecec3dc59 | 25 | |
okini3939 | 2:764ecec3dc59 | 26 | int twitter (char *msg, char *user, char *pwd) { |
okini3939 | 2:764ecec3dc59 | 27 | Host host; |
okini3939 | 2:764ecec3dc59 | 28 | char buf[300], head[160]; |
okini3939 | 2:764ecec3dc59 | 29 | |
okini3939 | 2:764ecec3dc59 | 30 | // header |
okini3939 | 2:764ecec3dc59 | 31 | createauth(user, pwd, head, sizeof(head)); |
okini3939 | 2:764ecec3dc59 | 32 | strncat(head, "Content-type: application/x-www-form-urlencoded\r\n", sizeof(head) - strlen(head)); |
okini3939 | 2:764ecec3dc59 | 33 | |
okini3939 | 2:764ecec3dc59 | 34 | // post data |
okini3939 | 2:764ecec3dc59 | 35 | strcpy(buf, "status="); |
okini3939 | 2:764ecec3dc59 | 36 | urlencode(msg, &buf[strlen(buf)], sizeof(buf) - strlen(buf)); |
okini3939 | 2:764ecec3dc59 | 37 | |
okini3939 | 2:764ecec3dc59 | 38 | host.setName("api.supertweet.net"); |
okini3939 | 2:764ecec3dc59 | 39 | host.setPort(HTTP_PORT); |
okini3939 | 2:764ecec3dc59 | 40 | return httpRequest(METHOD_POST, &host, "/1/statuses/update.xml", head, buf) == 200 ? 0 : -1; |
okini3939 | 2:764ecec3dc59 | 41 | } |
okini3939 | 2:764ecec3dc59 | 42 | |
okini3939 | 0:f2bf5f966801 | 43 | int main () { |
okini3939 | 0:f2bf5f966801 | 44 | EthernetErr ethErr; |
okini3939 | 0:f2bf5f966801 | 45 | Host host; |
okini3939 | 0:f2bf5f966801 | 46 | int r; |
okini3939 | 0:f2bf5f966801 | 47 | |
okini3939 | 0:f2bf5f966801 | 48 | myled = 1; |
okini3939 | 2:764ecec3dc59 | 49 | // pc.baud(115200); |
okini3939 | 0:f2bf5f966801 | 50 | |
okini3939 | 0:f2bf5f966801 | 51 | ethErr = eth.setup(); |
okini3939 | 0:f2bf5f966801 | 52 | if(ethErr) { |
okini3939 | 0:f2bf5f966801 | 53 | return -1; |
okini3939 | 0:f2bf5f966801 | 54 | } |
okini3939 | 0:f2bf5f966801 | 55 | |
okini3939 | 0:f2bf5f966801 | 56 | host.setName("mbed.org"); |
okini3939 | 0:f2bf5f966801 | 57 | r = httpRequest(METHOD_GET, &host, "/", NULL, NULL); |
okini3939 | 2:764ecec3dc59 | 58 | |
okini3939 | 2:764ecec3dc59 | 59 | // r = twitter("test from #mbed TinyHTTP", "username", "password"); |
okini3939 | 2:764ecec3dc59 | 60 | |
okini3939 | 2:764ecec3dc59 | 61 | // r = pachube(99999, "api key", "1000,30,70"); |
okini3939 | 2:764ecec3dc59 | 62 | |
okini3939 | 0:f2bf5f966801 | 63 | /* |
okini3939 | 0:f2bf5f966801 | 64 | host.setName("www.domain.name"); |
okini3939 | 0:f2bf5f966801 | 65 | r = httpRequest(METHOD_POST, &host, "/xxx.cgi", "Content-Type: application/x-www-form-urlencoded\r\n", "key=value&key2=value2"); |
okini3939 | 0:f2bf5f966801 | 66 | */ |
okini3939 | 0:f2bf5f966801 | 67 | printf("status %d\r\n", r); |
okini3939 | 0:f2bf5f966801 | 68 | |
okini3939 | 0:f2bf5f966801 | 69 | myled = 0; |
okini3939 | 0:f2bf5f966801 | 70 | return 0; |
okini3939 | 0:f2bf5f966801 | 71 | } |