Exosite Evaluation Example on LPC1768 mbed Application Board

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

Fork of exosite_http_example by Patrick Barrett

main.cpp

Committer:
maanenson
Date:
2014-05-02
Revision:
0:30a991e08e77
Child:
1:ae20607dd0c1

File content as of revision 0:30a991e08e77:

#include "mbed.h"
#include "EthernetInterface.h"

EthernetInterface eth;  

string CIK = "996a6abfb619711dbf3532cc82a6df6513821532";  // copy Device CIK from Portals

AnalogIn aPot1(p19);
Timer t;

int main() {
    t.start();
    
    printf("\r\nStart\n");
    eth.init(); //Use 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");
    

    int ping = 0;
    
    while (1) {
        wait(20);

        float totalPot1 = 0;
        float avgPot1 = 0;
        for (int i = 0; i<30; i++) {
            totalPot1 += abs(aPot1);
        }
        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);
                }
            }
        }
    }

    return 0;

}