Test C027 with Xively and Ethernet

Dependencies:   C027 EthernetInterface libxively mbed-rtos mbed HTTPClient

Fork of C027_Xively_Ethernet by Chau Vo

Revision:
6:f1477813f212
Parent:
4:0bdab35cb164
Child:
7:b2eb8b943b63
--- a/main.cpp	Tue Apr 22 21:21:49 2014 +0000
+++ b/main.cpp	Sun Apr 27 13:47:58 2014 +0000
@@ -1,9 +1,12 @@
 #include "mbed.h"
 #include "C027.h"
 #include "EthernetInterface.h"
-#include "Websocket.h"
-#include "SocketIO.h"
 
+#define XI_FEED_ID 607813696 // set Xively Feed ID (numerical, no quoutes)
+#define XI_API_KEY "DcO8pnBiF4N0xyT9cPeQLggUHuNu7g8dwYhxH6s1qjZwtZm5" // set Xively API key (double-quoted string)
+ 
+#include "xively.h"
+#include "xi_err.h"
 
 //Debug is disabled by default
 #if 1
@@ -21,76 +24,54 @@
 
 #endif
 
-
-void websocket_test(void const*)
-{
-    char msg[512] = {0};
-
+void xively_test(void const*) {
+    /*
+     * Setup Ethernet interface
+     */
     EthernetInterface eth;
     eth.init(); //Use DHCP
     eth.connect();
     DBG("IP Address is %s", eth.getIPAddress());
-
-    Websocket ws("ws://sockets.mbed.org:443/ws/chauvo/wo");
-    Timer t;
-    t.start();
-
-    bool c = ws.connect();
-    DBG("Connect result: %s", c?"OK":"Failed");
-
-    for(int i = 0; i < 10000; i++) {
-        ws.connect();
-        // create json string with acc/tmp data
-        sprintf(msg, "Chau's demo loop: %d", i);
-        ws.send(msg);
-        wait(0.5f);
-        memset(msg, 0, 512);
-
-        if (ws.read(msg)) {
-            DBG("rcv: %s\r\n", msg);
-        } else {
-            DBG("Loop %d ws.read() returns 0\n \t %s, line %d @ %6.2f seconds\n", i, __FILE__, __LINE__, t.read());
-            wait(5.0f);
-        }
-        ws.close();
-    }
-
-    while(1) {
+    
+    /*
+     * Setup Xively parameters
+     */
+    xi_feed_t feed;
+    memset(&feed, NULL, sizeof(xi_feed_t));
+    
+    // channels
+    feed.feed_id = XI_FEED_ID;
+    feed.datastream_count = 1; // number of channels
+    
+    // channel settings
+    feed.datastreams[0].datapoint_count = 1;
+    xi_datastream_t* counter_datastream = &feed.datastreams[0];
+    strcpy(counter_datastream->datastream_id, "counter"); // name of channel
+    xi_datapoint_t* counter = &counter_datastream->datapoints[0]; // xively channel variable to set value
+ 
+    // create the cosm library context
+    xi_context_t* xi_context = xi_create_context(XI_HTTP, XI_API_KEY, feed.feed_id);
+ 
+    // check if everything works
+    if (xi_context == NULL)
+    {
+        DBG("xi_context = NULL");
     }
-}
-
-void socketio_test(void const*)
-{
-    char msg[512] = {0};
-
-    EthernetInterface eth;
-    eth.init(); //Use DHCP
-    eth.connect();
-    DBG("IP Address is %s", eth.getIPAddress());
-
-    SocketIO socketio("nne-server1.herokuapp.com");
-    //SocketIO socketio("websocketserver-tutorial11067089.rhcloud.com");
-    //SocketIO socketio("websocket-server.chau_vk.c9.io:8001");
-    //SocketIO socketio("websocket-server-c9-chau_vk.c9.io");
-    Timer t;
-    t.start();
-
-    bool c = socketio.connect(); // c is always false, (maybe because the connection is upgraded?)
-    DBG("Connect result: %s", c?"OK":"Failed");
-
-    while (1) {
-        int res = socketio.emit("mesage","[\"mbed SocketIO Hello World!\"]");
-        if (socketio.read(msg)) {
-            DBG("rcv: %s", msg);
-        }
-        wait(0.1);
+    
+    long unsigned int c = 0; 
+    while(1) {     
+      xi_set_value_i32(counter, ++c);
+        
+      DBG("Updating Xively feed %d: %d", XI_FEED_ID, c);
+      xi_feed_update(xi_context, &feed);
+      
+      wait(5);
     }
 }
 
 int main()
 {
-    //Thread testTask(websocket_test, NULL, osPriorityNormal, 1024 * 4);
-    Thread testTask(socketio_test, NULL, osPriorityNormal, 1024 * 4);
+    Thread testTask(xively_test, NULL, osPriorityNormal, 1024 * 4);
     DigitalOut led(LED); // on rev A you should reasign the signal to A0
     while(1) {
         led=!led;