![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
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:
- 2:8907a25944ab
- Parent:
- 1:ae20607dd0c1
- Child:
- 4:d6e87aea518f
--- a/main.cpp Fri May 02 22:00:13 2014 +0000 +++ b/main.cpp Tue Jan 20 14:15:12 2015 +0000 @@ -1,13 +1,27 @@ #include "mbed.h" #include "EthernetInterface.h" +#include "HTTPClient.h" #include "C12832_lcd.h" -EthernetInterface eth; +EthernetInterface eth; +HTTPClient http; + + +#define CIK "0000000000000000000000000000000000000000" +#define WRITE_ALIAS "temp" +#define READ_ALIASES "?screen" -char CIK[] = "c85bec1649f3450667f918305dc33b85af024686"; // patrick's "mbed Dev Board" +char EXO_CIK_HDR[] = "X-Exosite-CIK: " CIK "\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" + +#define TIMEOUT 2000 +#define BUFFSIZE 1024 AnalogIn aPot1(p19); -Timer send_timer, run_time; +Timer send_timer, connection_timer, run_time; static C12832_LCD lcd; @@ -15,6 +29,7 @@ { run_time.start(); send_timer.start(); + connection_timer.start(); printf("\r\nStart\r\n"); @@ -38,46 +53,35 @@ while (1) { int ret; - char post_data [70]; - int len = snprintf(post_data, 69, "uptime=%0.0f&tempa=128",run_time.read()); + char incomming_buffer[BUFFSIZE]; + char outgoing_buffer[BUFFSIZE]; + HTTPText read_text = HTTPText(incomming_buffer, BUFFSIZE); + HTTPText write_map = HTTPText(outgoing_buffer); + snprintf(outgoing_buffer, BUFFSIZE, "tempf=%0.0f", 42.37); - 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); + connection_timer.reset(); + printf("[%f] Sending...\r\n", run_time.read()); + + http.setHeader(0,EXO_CIK_HDR); + http.setHeader(1,EXO_ACCEPT_HDR); + http.setHeader(2,EXO_CONTYP_HDR); - if (ret == -1) { - printf("[%f] Error Connecting to Server\r\n", run_time.read()); - continue; + ret = http.post(EXO_URI, write_map, &read_text); + printf("Done! Status: %d, %d\r\n", ret, http.getHTTPResponseCode()); + lcd.locate(0,0); + lcd.printf(" "); + if (!ret) { + printf("Success, Got: %s\n", incomming_buffer); + lcd.locate(0,0); + lcd.printf(incomming_buffer); + } else { + printf("Error, Got: %s\n", incomming_buffer); + lcd.locate(0,0); + lcd.printf(incomming_buffer); } - 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(); + printf("Completed in %f seconds.\r\n", connection_timer.read()); wait(5); }