Test C027 with Xively and Ethernet

Dependencies:   C027 EthernetInterface libxively mbed-rtos mbed HTTPClient

Fork of C027_Xively_Ethernet by Chau Vo

Committer:
olympux
Date:
Sun Apr 27 13:47:58 2014 +0000
Revision:
6:f1477813f212
Parent:
4:0bdab35cb164
Child:
7:b2eb8b943b63
Initial

Who changed what in which revision?

UserRevisionLine numberNew 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 4:0bdab35cb164 4
olympux 6:f1477813f212 5 #define XI_FEED_ID 607813696 // set Xively Feed ID (numerical, no quoutes)
olympux 6:f1477813f212 6 #define XI_API_KEY "DcO8pnBiF4N0xyT9cPeQLggUHuNu7g8dwYhxH6s1qjZwtZm5" // set Xively API key (double-quoted string)
olympux 6:f1477813f212 7
olympux 6:f1477813f212 8 #include "xively.h"
olympux 6:f1477813f212 9 #include "xi_err.h"
olympux 4:0bdab35cb164 10
olympux 4:0bdab35cb164 11 //Debug is disabled by default
olympux 4:0bdab35cb164 12 #if 1
olympux 4:0bdab35cb164 13 //Enable debug
olympux 4:0bdab35cb164 14 #include <cstdio>
olympux 4:0bdab35cb164 15 #define DBG(x, ...) std::printf("[main : DBG]"x"\r\n", ##__VA_ARGS__);
olympux 4:0bdab35cb164 16 #define WARN(x, ...) std::printf("[main : WARN]"x"\r\n", ##__VA_ARGS__);
olympux 4:0bdab35cb164 17 #define ERR(x, ...) std::printf("[main : ERR]"x"\r\n", ##__VA_ARGS__);
olympux 4:0bdab35cb164 18
olympux 4:0bdab35cb164 19 #else
olympux 4:0bdab35cb164 20 //Disable debug
olympux 4:0bdab35cb164 21 #define DBG(x, ...)
olympux 4:0bdab35cb164 22 #define WARN(x, ...)
olympux 4:0bdab35cb164 23 #define ERR(x, ...)
olympux 4:0bdab35cb164 24
olympux 4:0bdab35cb164 25 #endif
olympux 4:0bdab35cb164 26
olympux 6:f1477813f212 27 void xively_test(void const*) {
olympux 6:f1477813f212 28 /*
olympux 6:f1477813f212 29 * Setup Ethernet interface
olympux 6:f1477813f212 30 */
samux 1:1c1802ec42a2 31 EthernetInterface eth;
samux 1:1c1802ec42a2 32 eth.init(); //Use DHCP
samux 1:1c1802ec42a2 33 eth.connect();
olympux 4:0bdab35cb164 34 DBG("IP Address is %s", eth.getIPAddress());
olympux 6:f1477813f212 35
olympux 6:f1477813f212 36 /*
olympux 6:f1477813f212 37 * Setup Xively parameters
olympux 6:f1477813f212 38 */
olympux 6:f1477813f212 39 xi_feed_t feed;
olympux 6:f1477813f212 40 memset(&feed, NULL, sizeof(xi_feed_t));
olympux 6:f1477813f212 41
olympux 6:f1477813f212 42 // channels
olympux 6:f1477813f212 43 feed.feed_id = XI_FEED_ID;
olympux 6:f1477813f212 44 feed.datastream_count = 1; // number of channels
olympux 6:f1477813f212 45
olympux 6:f1477813f212 46 // channel settings
olympux 6:f1477813f212 47 feed.datastreams[0].datapoint_count = 1;
olympux 6:f1477813f212 48 xi_datastream_t* counter_datastream = &feed.datastreams[0];
olympux 6:f1477813f212 49 strcpy(counter_datastream->datastream_id, "counter"); // name of channel
olympux 6:f1477813f212 50 xi_datapoint_t* counter = &counter_datastream->datapoints[0]; // xively channel variable to set value
olympux 6:f1477813f212 51
olympux 6:f1477813f212 52 // create the cosm library context
olympux 6:f1477813f212 53 xi_context_t* xi_context = xi_create_context(XI_HTTP, XI_API_KEY, feed.feed_id);
olympux 6:f1477813f212 54
olympux 6:f1477813f212 55 // check if everything works
olympux 6:f1477813f212 56 if (xi_context == NULL)
olympux 6:f1477813f212 57 {
olympux 6:f1477813f212 58 DBG("xi_context = NULL");
olympux 3:259ac92c3ca8 59 }
olympux 6:f1477813f212 60
olympux 6:f1477813f212 61 long unsigned int c = 0;
olympux 6:f1477813f212 62 while(1) {
olympux 6:f1477813f212 63 xi_set_value_i32(counter, ++c);
olympux 6:f1477813f212 64
olympux 6:f1477813f212 65 DBG("Updating Xively feed %d: %d", XI_FEED_ID, c);
olympux 6:f1477813f212 66 xi_feed_update(xi_context, &feed);
olympux 6:f1477813f212 67
olympux 6:f1477813f212 68 wait(5);
olympux 4:0bdab35cb164 69 }
olympux 4:0bdab35cb164 70 }
olympux 4:0bdab35cb164 71
olympux 3:259ac92c3ca8 72 int main()
olympux 3:259ac92c3ca8 73 {
olympux 6:f1477813f212 74 Thread testTask(xively_test, NULL, osPriorityNormal, 1024 * 4);
olympux 3:259ac92c3ca8 75 DigitalOut led(LED); // on rev A you should reasign the signal to A0
olympux 3:259ac92c3ca8 76 while(1) {
olympux 3:259ac92c3ca8 77 led=!led;
olympux 3:259ac92c3ca8 78 Thread::wait(1000);
olympux 3:259ac92c3ca8 79 }
olympux 3:259ac92c3ca8 80
olympux 3:259ac92c3ca8 81 return 0;
samux 1:1c1802ec42a2 82 }