Test C027 with Xively and Ethernet

Dependencies:   C027 EthernetInterface libxively mbed-rtos mbed HTTPClient

Fork of C027_Xively_Ethernet by Chau Vo

Revision:
7:b2eb8b943b63
Parent:
6:f1477813f212
Child:
8:6ba9cdff119f
--- a/main.cpp	Sun Apr 27 13:47:58 2014 +0000
+++ b/main.cpp	Mon Apr 28 20:17:23 2014 +0000
@@ -1,6 +1,8 @@
 #include "mbed.h"
 #include "C027.h"
 #include "EthernetInterface.h"
+#include "HTTPClient.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)
@@ -34,6 +36,11 @@
     DBG("IP Address is %s", eth.getIPAddress());
     
     /*
+     * HTTPClient for location
+     */
+    HTTPClient http; 
+    
+    /*
      * Setup Xively parameters
      */
     xi_feed_t feed;
@@ -58,14 +65,42 @@
         DBG("xi_context = NULL");
     }
     
-    long unsigned int c = 0; 
+    char url[64];
+    char json[256];
+    char request[512], response[64];
+    
+    // prepare url
+    sprintf(url, "https://api.xively.com/v2/feeds/%d.json", XI_FEED_ID);
+    
+    // prepare json string
+    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\": 177.03402996826173\r\n}\r\n}\r\n");
+    DBG("%s", json);
+    int length = 0;
+    while (json[length] != 0) length++;
+    DBG("json length = %d", length);
+    
+    // request header, currently set in HTTPClient
+    //http.setRequestHeader("X-ApiKey", XI_API_KEY);
+    
+    // prepare request to PUT
+    sprintf(request,"%s", json);
+    length = 0;
+    while (request[length] != 0) length++;
+    DBG("request length = %d", length);
+
+    // Loop for sending data to Xively
+    long unsigned int c = 0; // counter value
     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);
+      // location
+      HTTPText requestText(request);
+      HTTPText responseText(response,64);
+      http.post(url,requestText,&responseText);
+      wait(10);
     }
 }