A basic example of using the mbed NXP LPC1768 Application Board with Exosite.

Dependencies:   C12832_lcd EthernetInterface HTTPClient LM75B MMA7660 RGBLed mbed-rtos mbed

Fork of exosite_http_example by Patrick Barrett

Revision:
1:ae20607dd0c1
Parent:
0:30a991e08e77
Child:
2:8907a25944ab
diff -r 30a991e08e77 -r ae20607dd0c1 main.cpp
--- a/main.cpp	Fri May 02 14:53:22 2014 +0000
+++ b/main.cpp	Fri May 02 22:00:13 2014 +0000
@@ -1,100 +1,85 @@
 #include "mbed.h"
 #include "EthernetInterface.h"
+#include "C12832_lcd.h"
 
 EthernetInterface eth;  
 
-string CIK = "996a6abfb619711dbf3532cc82a6df6513821532";  // copy Device CIK from Portals
+char CIK[] = "c85bec1649f3450667f918305dc33b85af024686";  // patrick's "mbed Dev Board"
 
 AnalogIn aPot1(p19);
-Timer t;
+Timer send_timer, run_time;
+
+static C12832_LCD lcd;
+
+int main()
+{
+    run_time.start();
+    send_timer.start();
 
-int main() {
-    t.start();
+    printf("\r\nStart\r\n");
+    
+    eth.init(); //Use DHCP
     
-    printf("\r\nStart\n");
-    eth.init(); //Use DHCP
+    lcd.printf("Exosite HTTP Cloud Demo");
+    lcd.locate(0,11);
+    lcd.printf("MAC: %s", eth.getMACAddress());
+    lcd.locate(0,22);
+    lcd.printf("IP:  Requesting From DHCP...");
+    
     eth.connect();
-    printf("\r\nSetting up...\r\n");
-    printf("IP Address is %s\n", eth.getIPAddress());
-    printf("MAC Address is %s\n", eth.getMACAddress());
-    printf("\r\nSetup OK\r\n");
+    
+    printf("IP Address is %s\r\n", eth.getIPAddress());
+    printf("MAC Address is %s\r\n", eth.getMACAddress());
     
-
-    int ping = 0;
+    lcd.locate(0,22);
+    lcd.printf("IP:    %s        ", eth.getIPAddress());
+    
+    printf("[%f] Running...\r\n", run_time.read());
     
     while (1) {
-        wait(20);
-
-        float totalPot1 = 0;
-        float avgPot1 = 0;
-        for (int i = 0; i<30; i++) {
-            totalPot1 += abs(aPot1);
+        int ret;
+        char post_data [70];
+        int len = snprintf(post_data, 69, "uptime=%0.0f&tempa=128",run_time.read());
+        
+        
+        printf("[%f] Connecting...\r\n", run_time.read());
+        TCPSocketConnection sock;
+        //ret = sock.connect("192.168.3.147", 8090);
+        ret = sock.connect("m2.exosite.com", 80);
+        
+        if (ret == -1) {
+            printf("[%f] Error Connecting to Server\r\n", run_time.read());
+            continue;
         }
-        avgPot1 = totalPot1/30;
-        printf("Pot1 Value: %03f \r\n", avgPot1);
-        
-        float newTime = t.read();
-        printf("Current Time: %f \r\n",newTime);
         
-        if (newTime  > 320) {
-            t.reset();
-            ping ++;
-            if (ping > 100) {
-                ping = 1;
-            }
-            char data [70];
-            int len = sprintf(data, "ping=%d&power=%03f",ping,avgPot1);
-            
-            TCPSocketConnection sock;
-            sock.connect("m2.exosite.com/", 80);
-    
-            char http_cmd[] = "GET /api:v1/stack/alias HTTP/1.1\r\n";
-            sock.send_all(http_cmd, sizeof(http_cmd)-1);
-            char http_format[] = "application/x-www-form-urlencoded; charset=utf-8";
-            sock.send_all(http_format, sizeof(http_format)-1);
-            
-            
-            char buffer[300];
-            int ret;
-            while (true) {
-                ret = sock.receive(buffer, sizeof(buffer)-1);
-                if (ret <= 0)
-                    break;
-                buffer[ret] = '\0';
-                printf("Received %d chars from server:\n%s\n", ret, buffer);
-            }
-              
-            sock.close();
-            
-            eth.disconnect();
-            
-            HTTPClient http;
-            http.setRequestHeader("X-Exosite-CIK", CIK);
-            
-            HTTPText Content("application/x-www-form-urlencoded; charset=utf-8");
-            Content.set(data);
-            string uri = "http://m2.exosite.com/api:v1/stack/alias";    // uri for post includes feed ID  // uri for post includes feed ID
-            
-            printf("Sending Data to Exosite...");
-            HTTPResult result = http.post(uri.c_str(), Content, NULL);
-            // result should be 0 and response should be 200 for successful post
-            int response = http.getHTTPResponseCode();
-            if (result==HTTP_OK) {
-                printf("done\r\n");
-                //printf("Response: %d\n",response);
-            } else {
-                if (response == 204) { //expected No Content (204) error
-                    printf("done\r\n");
-                    //printf("Response: %d\n",response);
-                } else {
-                    printf("failed\r\n");
-                    printf("Error %d\r\n", result);
-                    printf("Response: %d\r\n",response);
-                }
-            }
+        printf("[%f] Sending...\r\n", run_time.read());
+
+        char header_tmp[80] = "POST /api:v1/stack/alias?screen HTTP/1.1\r\n";
+        sock.send(header_tmp, strlen(header_tmp));
+        sock.send("Host: m2.exosite.com\r\n", strlen("Host: m2.exosite.com\r\n"));
+        snprintf(header_tmp, 79, "X-Exosite-CIK: %s\r\n", CIK);
+        sock.send(header_tmp, strlen(header_tmp));
+        sock.send("Accept: application/x-www-form-urlencoded; charset=utf-8\r\n", strlen("Accept: application/x-www-form-urlencoded; charset=utf-8\r\n"));
+        sock.send("Content-Type: application/x-www-form-urlencoded; charset=utf-8\r\n", strlen("Content-Type: application/x-www-form-urlencoded; charset=utf-8\r\n"));
+        sock.send("Connection: close\r\n", strlen("Connection: close\r\n"));
+        snprintf(header_tmp, 79, "Content-Length: %i\r\n", strlen(post_data));
+        sock.send(header_tmp, strlen(header_tmp));
+        sock.send("\r\n", strlen("\r\n"));
+        sock.send_all(post_data, strlen(post_data));
+        
+        printf("[%f] Receiving...\r\n", run_time.read());
+        
+        char buffer[300];
+        ret = sock.receive_all(buffer, 300);
+        if(ret >= 0){
+            printf("Received %d chars from server:\r\n%.*s\r\n", ret, ret, buffer);
+        }else{
+            printf("Error Receiving");
         }
+
+        sock.close();
+        
+        wait(5);
     }
 
-    return 0;
-
 }