Test C027 with Xively and Ethernet
Dependencies: C027 EthernetInterface libxively mbed-rtos mbed HTTPClient
Fork of C027_Xively_Ethernet by
main.cpp@9:11148ee67ba0, 2014-05-03 (annotated)
- Committer:
- olympux
- Date:
- Sat May 03 13:47:05 2014 +0000
- Revision:
- 9:11148ee67ba0
- Parent:
- 8:6ba9cdff119f
Xively api with location
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
samux | 1:1c1802ec42a2 | 1 | #include "mbed.h" |
olympux | 3:259ac92c3ca8 | 2 | #include "C027.h" |
samux | 1:1c1802ec42a2 | 3 | #include "EthernetInterface.h" |
olympux | 7:b2eb8b943b63 | 4 | #include "HTTPClient.h" |
olympux | 7:b2eb8b943b63 | 5 | |
olympux | 4:0bdab35cb164 | 6 | |
olympux | 6:f1477813f212 | 7 | #define XI_FEED_ID 607813696 // set Xively Feed ID (numerical, no quoutes) |
olympux | 6:f1477813f212 | 8 | #define XI_API_KEY "DcO8pnBiF4N0xyT9cPeQLggUHuNu7g8dwYhxH6s1qjZwtZm5" // set Xively API key (double-quoted string) |
olympux | 6:f1477813f212 | 9 | |
olympux | 6:f1477813f212 | 10 | #include "xively.h" |
olympux | 6:f1477813f212 | 11 | #include "xi_err.h" |
olympux | 4:0bdab35cb164 | 12 | |
olympux | 4:0bdab35cb164 | 13 | //Debug is disabled by default |
olympux | 4:0bdab35cb164 | 14 | #if 1 |
olympux | 4:0bdab35cb164 | 15 | //Enable debug |
olympux | 4:0bdab35cb164 | 16 | #include <cstdio> |
olympux | 4:0bdab35cb164 | 17 | #define DBG(x, ...) std::printf("[main : DBG]"x"\r\n", ##__VA_ARGS__); |
olympux | 4:0bdab35cb164 | 18 | #define WARN(x, ...) std::printf("[main : WARN]"x"\r\n", ##__VA_ARGS__); |
olympux | 4:0bdab35cb164 | 19 | #define ERR(x, ...) std::printf("[main : ERR]"x"\r\n", ##__VA_ARGS__); |
olympux | 4:0bdab35cb164 | 20 | |
olympux | 4:0bdab35cb164 | 21 | #else |
olympux | 4:0bdab35cb164 | 22 | //Disable debug |
olympux | 4:0bdab35cb164 | 23 | #define DBG(x, ...) |
olympux | 4:0bdab35cb164 | 24 | #define WARN(x, ...) |
olympux | 4:0bdab35cb164 | 25 | #define ERR(x, ...) |
olympux | 4:0bdab35cb164 | 26 | |
olympux | 4:0bdab35cb164 | 27 | #endif |
olympux | 4:0bdab35cb164 | 28 | |
olympux | 6:f1477813f212 | 29 | void xively_test(void const*) { |
olympux | 6:f1477813f212 | 30 | /* |
olympux | 6:f1477813f212 | 31 | * Setup Ethernet interface |
olympux | 6:f1477813f212 | 32 | */ |
samux | 1:1c1802ec42a2 | 33 | EthernetInterface eth; |
samux | 1:1c1802ec42a2 | 34 | eth.init(); //Use DHCP |
samux | 1:1c1802ec42a2 | 35 | eth.connect(); |
olympux | 4:0bdab35cb164 | 36 | DBG("IP Address is %s", eth.getIPAddress()); |
olympux | 6:f1477813f212 | 37 | |
olympux | 6:f1477813f212 | 38 | /* |
olympux | 7:b2eb8b943b63 | 39 | * HTTPClient for location |
olympux | 7:b2eb8b943b63 | 40 | */ |
olympux | 7:b2eb8b943b63 | 41 | HTTPClient http; |
olympux | 7:b2eb8b943b63 | 42 | |
olympux | 7:b2eb8b943b63 | 43 | /* |
olympux | 6:f1477813f212 | 44 | * Setup Xively parameters |
olympux | 6:f1477813f212 | 45 | */ |
olympux | 6:f1477813f212 | 46 | xi_feed_t feed; |
olympux | 6:f1477813f212 | 47 | memset(&feed, NULL, sizeof(xi_feed_t)); |
olympux | 6:f1477813f212 | 48 | |
olympux | 6:f1477813f212 | 49 | // channels |
olympux | 6:f1477813f212 | 50 | feed.feed_id = XI_FEED_ID; |
olympux | 6:f1477813f212 | 51 | feed.datastream_count = 1; // number of channels |
olympux | 6:f1477813f212 | 52 | |
olympux | 6:f1477813f212 | 53 | // channel settings |
olympux | 6:f1477813f212 | 54 | feed.datastreams[0].datapoint_count = 1; |
olympux | 6:f1477813f212 | 55 | xi_datastream_t* counter_datastream = &feed.datastreams[0]; |
olympux | 6:f1477813f212 | 56 | strcpy(counter_datastream->datastream_id, "counter"); // name of channel |
olympux | 6:f1477813f212 | 57 | xi_datapoint_t* counter = &counter_datastream->datapoints[0]; // xively channel variable to set value |
olympux | 6:f1477813f212 | 58 | |
olympux | 6:f1477813f212 | 59 | // create the cosm library context |
olympux | 6:f1477813f212 | 60 | xi_context_t* xi_context = xi_create_context(XI_HTTP, XI_API_KEY, feed.feed_id); |
olympux | 6:f1477813f212 | 61 | |
olympux | 6:f1477813f212 | 62 | // check if everything works |
olympux | 6:f1477813f212 | 63 | if (xi_context == NULL) |
olympux | 6:f1477813f212 | 64 | { |
olympux | 6:f1477813f212 | 65 | DBG("xi_context = NULL"); |
olympux | 3:259ac92c3ca8 | 66 | } |
olympux | 6:f1477813f212 | 67 | |
olympux | 7:b2eb8b943b63 | 68 | char url[64]; |
olympux | 7:b2eb8b943b63 | 69 | char json[256]; |
olympux | 8:6ba9cdff119f | 70 | char request[256], response[128]; |
olympux | 7:b2eb8b943b63 | 71 | |
olympux | 7:b2eb8b943b63 | 72 | // prepare url |
olympux | 7:b2eb8b943b63 | 73 | sprintf(url, "https://api.xively.com/v2/feeds/%d.json", XI_FEED_ID); |
olympux | 7:b2eb8b943b63 | 74 | |
olympux | 7:b2eb8b943b63 | 75 | // prepare json string |
olympux | 8:6ba9cdff119f | 76 | sprintf(json,"{\r\n\"location\": {\r\n\"disposition\": \"mobile\",\r\n\"name\": \"Monuriki Island\",\r\n\"exposure\": \"outdoor\",\r\n\"domain\": \"physical\",\r\n\"ele\": \"370000\",\r\n\"lat\": 17.609991828964787,\r\n\"lon\": 178.03402996826273\r\n}\r\n}\r\n"); |
olympux | 7:b2eb8b943b63 | 77 | DBG("%s", json); |
olympux | 7:b2eb8b943b63 | 78 | int length = 0; |
olympux | 7:b2eb8b943b63 | 79 | while (json[length] != 0) length++; |
olympux | 7:b2eb8b943b63 | 80 | DBG("json length = %d", length); |
olympux | 7:b2eb8b943b63 | 81 | |
olympux | 7:b2eb8b943b63 | 82 | // prepare request to PUT |
olympux | 7:b2eb8b943b63 | 83 | sprintf(request,"%s", json); |
olympux | 7:b2eb8b943b63 | 84 | length = 0; |
olympux | 7:b2eb8b943b63 | 85 | while (request[length] != 0) length++; |
olympux | 7:b2eb8b943b63 | 86 | DBG("request length = %d", length); |
olympux | 7:b2eb8b943b63 | 87 | |
olympux | 7:b2eb8b943b63 | 88 | // Loop for sending data to Xively |
olympux | 7:b2eb8b943b63 | 89 | long unsigned int c = 0; // counter value |
olympux | 8:6ba9cdff119f | 90 | while(1) { |
olympux | 6:f1477813f212 | 91 | xi_set_value_i32(counter, ++c); |
olympux | 6:f1477813f212 | 92 | |
olympux | 6:f1477813f212 | 93 | DBG("Updating Xively feed %d: %d", XI_FEED_ID, c); |
olympux | 6:f1477813f212 | 94 | xi_feed_update(xi_context, &feed); |
olympux | 6:f1477813f212 | 95 | |
olympux | 7:b2eb8b943b63 | 96 | // location |
olympux | 8:6ba9cdff119f | 97 | sprintf(response, "X-ApiKey: %s", XI_API_KEY); |
olympux | 8:6ba9cdff119f | 98 | HTTPTextXively requestText(request); |
olympux | 8:6ba9cdff119f | 99 | HTTPTextXively responseText(response,128); |
olympux | 8:6ba9cdff119f | 100 | |
olympux | 8:6ba9cdff119f | 101 | // prepare specific headers to PUT, put in pDataIn |
olympux | 8:6ba9cdff119f | 102 | int ret = http.put(url,requestText,&responseText); |
olympux | 7:b2eb8b943b63 | 103 | wait(10); |
olympux | 4:0bdab35cb164 | 104 | } |
olympux | 4:0bdab35cb164 | 105 | } |
olympux | 4:0bdab35cb164 | 106 | |
olympux | 3:259ac92c3ca8 | 107 | int main() |
olympux | 3:259ac92c3ca8 | 108 | { |
olympux | 6:f1477813f212 | 109 | Thread testTask(xively_test, NULL, osPriorityNormal, 1024 * 4); |
olympux | 3:259ac92c3ca8 | 110 | DigitalOut led(LED); // on rev A you should reasign the signal to A0 |
olympux | 3:259ac92c3ca8 | 111 | while(1) { |
olympux | 3:259ac92c3ca8 | 112 | led=!led; |
olympux | 3:259ac92c3ca8 | 113 | Thread::wait(1000); |
olympux | 3:259ac92c3ca8 | 114 | } |
olympux | 3:259ac92c3ca8 | 115 | |
olympux | 3:259ac92c3ca8 | 116 | return 0; |
samux | 1:1c1802ec42a2 | 117 | } |