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
Diff: main.cpp
- Revision:
- 4:d6e87aea518f
- Parent:
- 2:8907a25944ab
- Child:
- 5:04490f27f83f
diff -r 718c1c38f1e4 -r d6e87aea518f main.cpp --- a/main.cpp Tue Jan 20 14:16:54 2015 +0000 +++ b/main.cpp Tue Jan 20 23:38:51 2015 +0000 @@ -2,20 +2,22 @@ #include "EthernetInterface.h" #include "HTTPClient.h" #include "C12832_lcd.h" +#include <FileSystemLike.h> +#include <FileHandle.h> -EthernetInterface eth; -HTTPClient http; +const char VENDOR[] = "exosite"; +const char MODEL[] = "mbed_lpc1768_appbrdv1"; - -#define CIK "0000000000000000000000000000000000000000" #define WRITE_ALIAS "temp" #define READ_ALIASES "?screen" -char EXO_CIK_HDR[] = "X-Exosite-CIK: " CIK "\r\n"; +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 @@ -23,13 +25,17 @@ 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(); - connection_timer.start(); printf("\r\nStart\r\n"); @@ -47,41 +53,98 @@ printf("MAC Address is %s\r\n", eth.getMACAddress()); lcd.locate(0,22); - lcd.printf("IP: %s ", eth.getIPAddress()); + 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 outgoing_buffer[BUFFSIZE]; - HTTPText read_text = HTTPText(incomming_buffer, BUFFSIZE); - HTTPText write_map = HTTPText(outgoing_buffer); + char scratch[32]; + HTTPMap read_map = HTTPMap(incomming_buffer, BUFFSIZE); + HTTPMap write_map = HTTPMap(); - snprintf(outgoing_buffer, BUFFSIZE, "tempf=%0.0f", 42.37); - - connection_timer.reset(); - printf("[%f] Sending...\r\n", run_time.read()); + 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); + //http.setHeader(2,EXO_CONTYP_HDR); + + printf("[%f] Making Request...\r\n", run_time.read()); + connection_timer.reset(); + connection_timer.start(); - ret = http.post(EXO_URI, write_map, &read_text); - printf("Done! Status: %d, %d\r\n", ret, http.getHTTPResponseCode()); + // 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(" "); + lcd.printf(" "); if (!ret) { - printf("Success, Got: %s\n", incomming_buffer); + // write 'screen' dp to screen lcd.locate(0,0); - lcd.printf(incomming_buffer); + 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 { - printf("Error, Got: %s\n", incomming_buffer); lcd.locate(0,0); - lcd.printf(incomming_buffer); + lcd.printf("Error (%d, %d)", ret, http.getHTTPResponseCode()); } - printf("Completed in %f seconds.\r\n", connection_timer.read()); + printf("[%f] Completed in %f seconds.\r\n", run_time.read(), connection_timer.read()); wait(5); }