Pachube feed API/v2 by json
Dependencies: NetServices mbed
main.cpp@0:9b8c657f003c, 2011-11-13 (annotated)
- Committer:
- takashikojo
- Date:
- Sun Nov 13 12:34:03 2011 +0000
- Revision:
- 0:9b8c657f003c
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
takashikojo | 0:9b8c657f003c | 1 | // includes |
takashikojo | 0:9b8c657f003c | 2 | |
takashikojo | 0:9b8c657f003c | 3 | #include "mbed.h" |
takashikojo | 0:9b8c657f003c | 4 | #include "EthernetNetIf.h" |
takashikojo | 0:9b8c657f003c | 5 | #include "HTTPClient.h" |
takashikojo | 0:9b8c657f003c | 6 | |
takashikojo | 0:9b8c657f003c | 7 | EthernetNetIf eth; |
takashikojo | 0:9b8c657f003c | 8 | HTTPClient http; |
takashikojo | 0:9b8c657f003c | 9 | |
takashikojo | 0:9b8c657f003c | 10 | // feed with 2 streams |
takashikojo | 0:9b8c657f003c | 11 | char contentTemplate[] = "{ \ |
takashikojo | 0:9b8c657f003c | 12 | \"title\":\"TITLE HERE\", \"version\":\"1.0.0\",\ |
takashikojo | 0:9b8c657f003c | 13 | \"datastreams\":[\ |
takashikojo | 0:9b8c657f003c | 14 | { \"id\":\"STREAM ID\", \"current_value\":\"%d\"},\ |
takashikojo | 0:9b8c657f003c | 15 | { \"id\":\"STREAM ID\", \"current_value\":\"%d\"}\ |
takashikojo | 0:9b8c657f003c | 16 | ]\ |
takashikojo | 0:9b8c657f003c | 17 | }" ; |
takashikojo | 0:9b8c657f003c | 18 | |
takashikojo | 0:9b8c657f003c | 19 | char content[200] ; |
takashikojo | 0:9b8c657f003c | 20 | |
takashikojo | 0:9b8c657f003c | 21 | int main() { |
takashikojo | 0:9b8c657f003c | 22 | |
takashikojo | 0:9b8c657f003c | 23 | HTTPText txt; |
takashikojo | 0:9b8c657f003c | 24 | int r ; |
takashikojo | 0:9b8c657f003c | 25 | |
takashikojo | 0:9b8c657f003c | 26 | printf("Start\n"); |
takashikojo | 0:9b8c657f003c | 27 | printf("\r\nSetting up...\r\n"); |
takashikojo | 0:9b8c657f003c | 28 | EthernetErr ethErr = eth.setup(); |
takashikojo | 0:9b8c657f003c | 29 | if (ethErr) { |
takashikojo | 0:9b8c657f003c | 30 | printf("Error %d in setup.\n", ethErr); |
takashikojo | 0:9b8c657f003c | 31 | return -1; |
takashikojo | 0:9b8c657f003c | 32 | } |
takashikojo | 0:9b8c657f003c | 33 | printf("\r\nSetup OK\r\n"); |
takashikojo | 0:9b8c657f003c | 34 | |
takashikojo | 0:9b8c657f003c | 35 | // copy API key from settings |
takashikojo | 0:9b8c657f003c | 36 | string apiKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; |
takashikojo | 0:9b8c657f003c | 37 | |
takashikojo | 0:9b8c657f003c | 38 | // use feed ID |
takashikojo | 0:9b8c657f003c | 39 | string environmentID = "xxxxx"; |
takashikojo | 0:9b8c657f003c | 40 | |
takashikojo | 0:9b8c657f003c | 41 | // for authentication, API key is set in client header |
takashikojo | 0:9b8c657f003c | 42 | HTTPClient client; |
takashikojo | 0:9b8c657f003c | 43 | client.setRequestHeader("X-PachubeApiKey", apiKey); |
takashikojo | 0:9b8c657f003c | 44 | |
takashikojo | 0:9b8c657f003c | 45 | // text object holds data to be posted |
takashikojo | 0:9b8c657f003c | 46 | HTTPText jsonContent("text/json"); |
takashikojo | 0:9b8c657f003c | 47 | |
takashikojo | 0:9b8c657f003c | 48 | while (1) { |
takashikojo | 0:9b8c657f003c | 49 | |
takashikojo | 0:9b8c657f003c | 50 | sprintf(content, contentTemplate, rand()%40, rand()%100) ; |
takashikojo | 0:9b8c657f003c | 51 | printf("Json: %s\n", content) ; |
takashikojo | 0:9b8c657f003c | 52 | jsonContent.set(content) ; |
takashikojo | 0:9b8c657f003c | 53 | |
takashikojo | 0:9b8c657f003c | 54 | // uri for post includes feed ID |
takashikojo | 0:9b8c657f003c | 55 | string uri = "http://api.pachube.com/v2/feeds/" + environmentID + ".json?_method=put"; |
takashikojo | 0:9b8c657f003c | 56 | |
takashikojo | 0:9b8c657f003c | 57 | // result should be 0 and response should be 200 for successful post |
takashikojo | 0:9b8c657f003c | 58 | HTTPResult result = client.post(uri.c_str(), jsonContent, NULL); |
takashikojo | 0:9b8c657f003c | 59 | |
takashikojo | 0:9b8c657f003c | 60 | if (result==HTTP_OK) { |
takashikojo | 0:9b8c657f003c | 61 | printf("Result :\"%s\"\n", txt.gets()); |
takashikojo | 0:9b8c657f003c | 62 | } else { |
takashikojo | 0:9b8c657f003c | 63 | printf("Error %d\n", result); |
takashikojo | 0:9b8c657f003c | 64 | } |
takashikojo | 0:9b8c657f003c | 65 | |
takashikojo | 0:9b8c657f003c | 66 | |
takashikojo | 0:9b8c657f003c | 67 | r = client.getHTTPResponseCode(); |
takashikojo | 0:9b8c657f003c | 68 | if (result==HTTP_OK) { |
takashikojo | 0:9b8c657f003c | 69 | printf("Result :\"%d\"\n", r); |
takashikojo | 0:9b8c657f003c | 70 | } else { |
takashikojo | 0:9b8c657f003c | 71 | printf("Error %d\n", r); |
takashikojo | 0:9b8c657f003c | 72 | } |
takashikojo | 0:9b8c657f003c | 73 | |
takashikojo | 0:9b8c657f003c | 74 | // wait for 60sec. |
takashikojo | 0:9b8c657f003c | 75 | wait(xx) ; |
takashikojo | 0:9b8c657f003c | 76 | } |
takashikojo | 0:9b8c657f003c | 77 | } |