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

main.cpp

Committer:
PBarrett
Date:
2015-01-20
Revision:
4:d6e87aea518f
Parent:
2:8907a25944ab
Child:
5:04490f27f83f

File content as of revision 4:d6e87aea518f:

#include "mbed.h"
#include "EthernetInterface.h"
#include "HTTPClient.h"
#include "C12832_lcd.h"
#include <FileSystemLike.h>
#include <FileHandle.h>

const char VENDOR[] = "exosite";
const char MODEL[] = "mbed_lpc1768_appbrdv1";

#define WRITE_ALIAS "temp"
#define READ_ALIASES "?screen"

char EXO_CIK_HDR[] = "X-Exosite-CIK: 0000000000000000000000000000000000000000\r\n";
const char EXO_CIK_HDR_FMT[] = "X-Exosite-CIK: %s\r\n";
char EXO_ACCEPT_HDR[] = "Accept: application/x-www-form-urlencoded; charset=utf-8\r\n";
char EXO_CONTYP_HDR[] = "Content-Type: application/x-www-form-urlencoded; charset=utf-8\r\n";
const char EXO_URI[] = "https://m2.exosite.com:443/onep:v1/stack/alias" READ_ALIASES;
// NOTE: Need to specify port due to parsing bug in lib on "onep:v1"
const char EXO_ACTIVATE_URI[] = "https://m2.exosite.com/provision/activate";

#define TIMEOUT 2000
#define BUFFSIZE 1024

AnalogIn aPot1(p19);
Timer send_timer, connection_timer, run_time;

EthernetInterface eth;
HTTPClient http;

LocalFileSystem localfs("local");

static C12832_LCD lcd;

int main()
{
    run_time.start();
    send_timer.start();

    printf("\r\nStart\r\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("IP Address is %s\r\n", eth.getIPAddress());
    printf("MAC Address is %s\r\n", eth.getMACAddress());
    
    lcd.locate(0,22);
    lcd.printf("IP:    %s      ", eth.getIPAddress());
    
    printf("[%f] Running...\r\n", run_time.read());
    
    printf("[%f] Attempting Activation\r\n", run_time.read());
    {
        int ret;
        char incomming_buffer[BUFFSIZE];
        HTTPText hti = HTTPText(incomming_buffer, BUFFSIZE);
        HTTPMap hto = HTTPMap();
        hto.put("vendor", VENDOR);
        hto.put("model", MODEL);
        hto.put("sn", eth.getMACAddress());
        if ((ret = http.post(EXO_ACTIVATE_URI, hto, &hti)) == 0) {
            printf("Success, Got: %s\n", incomming_buffer);
            lcd.locate(0,0);
            sprintf(EXO_CIK_HDR, EXO_CIK_HDR_FMT, incomming_buffer);
            FILE *fp = fopen("/local/cik.txt", "w");
            fprintf(fp, "%s", incomming_buffer);
            fclose(fp);
        } else if (ret == 0) {
            printf("Couldn't Connect to Exosite, Check Network\r\n");
                lcd.locate(0,0);
                lcd.printf("Couldn't Connect to Exosite");
                lcd.locate(0,11);
                lcd.printf("Check Network");
        } else {
            printf("Error %d\r\n", http.getHTTPResponseCode());
            lcd.locate(0,0);
            lcd.printf("Act Error (%d)", http.getHTTPResponseCode());
            FILE *fp = fopen("/local/cik.txt", "r");
            if (fp !=0 && fread(incomming_buffer, 1, 40, fp) == 40) {
                sprintf(EXO_CIK_HDR, EXO_CIK_HDR_FMT, incomming_buffer);
                printf("Found cik in nv: %s\r\n", incomming_buffer);
            } else if (ret != 0) {
                printf("No CIK, Please Re-Enable Device for Activation in Portals\r\n");
                lcd.locate(0,0);
                lcd.printf("No CIK, Re-Enable in Portals");
                lcd.locate(0,22);
                lcd.printf("Then Hard-Reset Board");
                while(1);
            }
            
            if (fp != 0)
                fclose(fp);
        }
    }
    
    while (1) {
        int ret;
        char *key, *value;
        char incomming_buffer[BUFFSIZE];
        char scratch[32];
        HTTPMap read_map = HTTPMap(incomming_buffer, BUFFSIZE);
        HTTPMap write_map = HTTPMap();
        
        write_map.put("tempa", "127");
        write_map.put("uptime", "5");
    
        http.setHeader(0,EXO_CIK_HDR);
        http.setHeader(1,EXO_ACCEPT_HDR);
        //http.setHeader(2,EXO_CONTYP_HDR);
        
        printf("[%f] Making Request...\r\n", run_time.read());
        connection_timer.reset();
        connection_timer.start();
        
        // make request
        ret = http.post(EXO_URI, write_map, &read_map);
        connection_timer.stop();
        printf("[%f] Done! Status: %d, %d\r\n", run_time.read(), ret, http.getHTTPResponseCode());
        lcd.locate(0,0);
        lcd.printf("                       ");
        if (!ret) {
            // write 'screen' dp to screen
            lcd.locate(0,0);
            if (read_map.get("screen", scratch, 32)) {
                lcd.printf(scratch);
            } else {
                lcd.printf("couldn't read 'screen' dp");
            }
            
            // list returned dataports
            while(read_map.pop(key, value)) {
                printf("  %s: %s\r\n", key, value);
            }
        } else {
            lcd.locate(0,0);
            lcd.printf("Error (%d, %d)", ret, http.getHTTPResponseCode());
        }
        
        printf("[%f] Completed in %f seconds.\r\n", run_time.read(), connection_timer.read());
        
        wait(5);
    }

}