![](/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
main.cpp@4:d6e87aea518f, 2015-01-20 (annotated)
- Committer:
- PBarrett
- Date:
- Tue Jan 20 23:38:51 2015 +0000
- Revision:
- 4:d6e87aea518f
- Parent:
- 2:8907a25944ab
- Child:
- 5:04490f27f83f
many updates, semi-final
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
maanenson | 0:30a991e08e77 | 1 | #include "mbed.h" |
maanenson | 0:30a991e08e77 | 2 | #include "EthernetInterface.h" |
PBarrett | 2:8907a25944ab | 3 | #include "HTTPClient.h" |
PBarrett | 1:ae20607dd0c1 | 4 | #include "C12832_lcd.h" |
PBarrett | 4:d6e87aea518f | 5 | #include <FileSystemLike.h> |
PBarrett | 4:d6e87aea518f | 6 | #include <FileHandle.h> |
maanenson | 0:30a991e08e77 | 7 | |
PBarrett | 4:d6e87aea518f | 8 | const char VENDOR[] = "exosite"; |
PBarrett | 4:d6e87aea518f | 9 | const char MODEL[] = "mbed_lpc1768_appbrdv1"; |
PBarrett | 2:8907a25944ab | 10 | |
PBarrett | 2:8907a25944ab | 11 | #define WRITE_ALIAS "temp" |
PBarrett | 2:8907a25944ab | 12 | #define READ_ALIASES "?screen" |
maanenson | 0:30a991e08e77 | 13 | |
PBarrett | 4:d6e87aea518f | 14 | char EXO_CIK_HDR[] = "X-Exosite-CIK: 0000000000000000000000000000000000000000\r\n"; |
PBarrett | 4:d6e87aea518f | 15 | const char EXO_CIK_HDR_FMT[] = "X-Exosite-CIK: %s\r\n"; |
PBarrett | 2:8907a25944ab | 16 | char EXO_ACCEPT_HDR[] = "Accept: application/x-www-form-urlencoded; charset=utf-8\r\n"; |
PBarrett | 2:8907a25944ab | 17 | char EXO_CONTYP_HDR[] = "Content-Type: application/x-www-form-urlencoded; charset=utf-8\r\n"; |
PBarrett | 2:8907a25944ab | 18 | const char EXO_URI[] = "https://m2.exosite.com:443/onep:v1/stack/alias" READ_ALIASES; |
PBarrett | 2:8907a25944ab | 19 | // NOTE: Need to specify port due to parsing bug in lib on "onep:v1" |
PBarrett | 4:d6e87aea518f | 20 | const char EXO_ACTIVATE_URI[] = "https://m2.exosite.com/provision/activate"; |
PBarrett | 2:8907a25944ab | 21 | |
PBarrett | 2:8907a25944ab | 22 | #define TIMEOUT 2000 |
PBarrett | 2:8907a25944ab | 23 | #define BUFFSIZE 1024 |
maanenson | 0:30a991e08e77 | 24 | |
maanenson | 0:30a991e08e77 | 25 | AnalogIn aPot1(p19); |
PBarrett | 2:8907a25944ab | 26 | Timer send_timer, connection_timer, run_time; |
PBarrett | 1:ae20607dd0c1 | 27 | |
PBarrett | 4:d6e87aea518f | 28 | EthernetInterface eth; |
PBarrett | 4:d6e87aea518f | 29 | HTTPClient http; |
PBarrett | 4:d6e87aea518f | 30 | |
PBarrett | 4:d6e87aea518f | 31 | LocalFileSystem localfs("local"); |
PBarrett | 4:d6e87aea518f | 32 | |
PBarrett | 1:ae20607dd0c1 | 33 | static C12832_LCD lcd; |
PBarrett | 1:ae20607dd0c1 | 34 | |
PBarrett | 1:ae20607dd0c1 | 35 | int main() |
PBarrett | 1:ae20607dd0c1 | 36 | { |
PBarrett | 1:ae20607dd0c1 | 37 | run_time.start(); |
PBarrett | 1:ae20607dd0c1 | 38 | send_timer.start(); |
maanenson | 0:30a991e08e77 | 39 | |
PBarrett | 1:ae20607dd0c1 | 40 | printf("\r\nStart\r\n"); |
PBarrett | 1:ae20607dd0c1 | 41 | |
PBarrett | 1:ae20607dd0c1 | 42 | eth.init(); //Use DHCP |
maanenson | 0:30a991e08e77 | 43 | |
PBarrett | 1:ae20607dd0c1 | 44 | lcd.printf("Exosite HTTP Cloud Demo"); |
PBarrett | 1:ae20607dd0c1 | 45 | lcd.locate(0,11); |
PBarrett | 1:ae20607dd0c1 | 46 | lcd.printf("MAC: %s", eth.getMACAddress()); |
PBarrett | 1:ae20607dd0c1 | 47 | lcd.locate(0,22); |
PBarrett | 1:ae20607dd0c1 | 48 | lcd.printf("IP: Requesting From DHCP..."); |
PBarrett | 1:ae20607dd0c1 | 49 | |
maanenson | 0:30a991e08e77 | 50 | eth.connect(); |
PBarrett | 1:ae20607dd0c1 | 51 | |
PBarrett | 1:ae20607dd0c1 | 52 | printf("IP Address is %s\r\n", eth.getIPAddress()); |
PBarrett | 1:ae20607dd0c1 | 53 | printf("MAC Address is %s\r\n", eth.getMACAddress()); |
maanenson | 0:30a991e08e77 | 54 | |
PBarrett | 1:ae20607dd0c1 | 55 | lcd.locate(0,22); |
PBarrett | 4:d6e87aea518f | 56 | lcd.printf("IP: %s ", eth.getIPAddress()); |
PBarrett | 1:ae20607dd0c1 | 57 | |
PBarrett | 1:ae20607dd0c1 | 58 | printf("[%f] Running...\r\n", run_time.read()); |
maanenson | 0:30a991e08e77 | 59 | |
PBarrett | 4:d6e87aea518f | 60 | printf("[%f] Attempting Activation\r\n", run_time.read()); |
PBarrett | 4:d6e87aea518f | 61 | { |
PBarrett | 4:d6e87aea518f | 62 | int ret; |
PBarrett | 4:d6e87aea518f | 63 | char incomming_buffer[BUFFSIZE]; |
PBarrett | 4:d6e87aea518f | 64 | HTTPText hti = HTTPText(incomming_buffer, BUFFSIZE); |
PBarrett | 4:d6e87aea518f | 65 | HTTPMap hto = HTTPMap(); |
PBarrett | 4:d6e87aea518f | 66 | hto.put("vendor", VENDOR); |
PBarrett | 4:d6e87aea518f | 67 | hto.put("model", MODEL); |
PBarrett | 4:d6e87aea518f | 68 | hto.put("sn", eth.getMACAddress()); |
PBarrett | 4:d6e87aea518f | 69 | if ((ret = http.post(EXO_ACTIVATE_URI, hto, &hti)) == 0) { |
PBarrett | 4:d6e87aea518f | 70 | printf("Success, Got: %s\n", incomming_buffer); |
PBarrett | 4:d6e87aea518f | 71 | lcd.locate(0,0); |
PBarrett | 4:d6e87aea518f | 72 | sprintf(EXO_CIK_HDR, EXO_CIK_HDR_FMT, incomming_buffer); |
PBarrett | 4:d6e87aea518f | 73 | FILE *fp = fopen("/local/cik.txt", "w"); |
PBarrett | 4:d6e87aea518f | 74 | fprintf(fp, "%s", incomming_buffer); |
PBarrett | 4:d6e87aea518f | 75 | fclose(fp); |
PBarrett | 4:d6e87aea518f | 76 | } else if (ret == 0) { |
PBarrett | 4:d6e87aea518f | 77 | printf("Couldn't Connect to Exosite, Check Network\r\n"); |
PBarrett | 4:d6e87aea518f | 78 | lcd.locate(0,0); |
PBarrett | 4:d6e87aea518f | 79 | lcd.printf("Couldn't Connect to Exosite"); |
PBarrett | 4:d6e87aea518f | 80 | lcd.locate(0,11); |
PBarrett | 4:d6e87aea518f | 81 | lcd.printf("Check Network"); |
PBarrett | 4:d6e87aea518f | 82 | } else { |
PBarrett | 4:d6e87aea518f | 83 | printf("Error %d\r\n", http.getHTTPResponseCode()); |
PBarrett | 4:d6e87aea518f | 84 | lcd.locate(0,0); |
PBarrett | 4:d6e87aea518f | 85 | lcd.printf("Act Error (%d)", http.getHTTPResponseCode()); |
PBarrett | 4:d6e87aea518f | 86 | FILE *fp = fopen("/local/cik.txt", "r"); |
PBarrett | 4:d6e87aea518f | 87 | if (fp !=0 && fread(incomming_buffer, 1, 40, fp) == 40) { |
PBarrett | 4:d6e87aea518f | 88 | sprintf(EXO_CIK_HDR, EXO_CIK_HDR_FMT, incomming_buffer); |
PBarrett | 4:d6e87aea518f | 89 | printf("Found cik in nv: %s\r\n", incomming_buffer); |
PBarrett | 4:d6e87aea518f | 90 | } else if (ret != 0) { |
PBarrett | 4:d6e87aea518f | 91 | printf("No CIK, Please Re-Enable Device for Activation in Portals\r\n"); |
PBarrett | 4:d6e87aea518f | 92 | lcd.locate(0,0); |
PBarrett | 4:d6e87aea518f | 93 | lcd.printf("No CIK, Re-Enable in Portals"); |
PBarrett | 4:d6e87aea518f | 94 | lcd.locate(0,22); |
PBarrett | 4:d6e87aea518f | 95 | lcd.printf("Then Hard-Reset Board"); |
PBarrett | 4:d6e87aea518f | 96 | while(1); |
PBarrett | 4:d6e87aea518f | 97 | } |
PBarrett | 4:d6e87aea518f | 98 | |
PBarrett | 4:d6e87aea518f | 99 | if (fp != 0) |
PBarrett | 4:d6e87aea518f | 100 | fclose(fp); |
PBarrett | 4:d6e87aea518f | 101 | } |
PBarrett | 4:d6e87aea518f | 102 | } |
PBarrett | 4:d6e87aea518f | 103 | |
maanenson | 0:30a991e08e77 | 104 | while (1) { |
PBarrett | 1:ae20607dd0c1 | 105 | int ret; |
PBarrett | 4:d6e87aea518f | 106 | char *key, *value; |
PBarrett | 2:8907a25944ab | 107 | char incomming_buffer[BUFFSIZE]; |
PBarrett | 4:d6e87aea518f | 108 | char scratch[32]; |
PBarrett | 4:d6e87aea518f | 109 | HTTPMap read_map = HTTPMap(incomming_buffer, BUFFSIZE); |
PBarrett | 4:d6e87aea518f | 110 | HTTPMap write_map = HTTPMap(); |
PBarrett | 1:ae20607dd0c1 | 111 | |
PBarrett | 4:d6e87aea518f | 112 | write_map.put("tempa", "127"); |
PBarrett | 4:d6e87aea518f | 113 | write_map.put("uptime", "5"); |
PBarrett | 2:8907a25944ab | 114 | |
PBarrett | 2:8907a25944ab | 115 | http.setHeader(0,EXO_CIK_HDR); |
PBarrett | 2:8907a25944ab | 116 | http.setHeader(1,EXO_ACCEPT_HDR); |
PBarrett | 4:d6e87aea518f | 117 | //http.setHeader(2,EXO_CONTYP_HDR); |
PBarrett | 4:d6e87aea518f | 118 | |
PBarrett | 4:d6e87aea518f | 119 | printf("[%f] Making Request...\r\n", run_time.read()); |
PBarrett | 4:d6e87aea518f | 120 | connection_timer.reset(); |
PBarrett | 4:d6e87aea518f | 121 | connection_timer.start(); |
PBarrett | 1:ae20607dd0c1 | 122 | |
PBarrett | 4:d6e87aea518f | 123 | // make request |
PBarrett | 4:d6e87aea518f | 124 | ret = http.post(EXO_URI, write_map, &read_map); |
PBarrett | 4:d6e87aea518f | 125 | connection_timer.stop(); |
PBarrett | 4:d6e87aea518f | 126 | printf("[%f] Done! Status: %d, %d\r\n", run_time.read(), ret, http.getHTTPResponseCode()); |
PBarrett | 2:8907a25944ab | 127 | lcd.locate(0,0); |
PBarrett | 4:d6e87aea518f | 128 | lcd.printf(" "); |
PBarrett | 2:8907a25944ab | 129 | if (!ret) { |
PBarrett | 4:d6e87aea518f | 130 | // write 'screen' dp to screen |
PBarrett | 2:8907a25944ab | 131 | lcd.locate(0,0); |
PBarrett | 4:d6e87aea518f | 132 | if (read_map.get("screen", scratch, 32)) { |
PBarrett | 4:d6e87aea518f | 133 | lcd.printf(scratch); |
PBarrett | 4:d6e87aea518f | 134 | } else { |
PBarrett | 4:d6e87aea518f | 135 | lcd.printf("couldn't read 'screen' dp"); |
PBarrett | 4:d6e87aea518f | 136 | } |
PBarrett | 4:d6e87aea518f | 137 | |
PBarrett | 4:d6e87aea518f | 138 | // list returned dataports |
PBarrett | 4:d6e87aea518f | 139 | while(read_map.pop(key, value)) { |
PBarrett | 4:d6e87aea518f | 140 | printf(" %s: %s\r\n", key, value); |
PBarrett | 4:d6e87aea518f | 141 | } |
PBarrett | 2:8907a25944ab | 142 | } else { |
PBarrett | 2:8907a25944ab | 143 | lcd.locate(0,0); |
PBarrett | 4:d6e87aea518f | 144 | lcd.printf("Error (%d, %d)", ret, http.getHTTPResponseCode()); |
maanenson | 0:30a991e08e77 | 145 | } |
maanenson | 0:30a991e08e77 | 146 | |
PBarrett | 4:d6e87aea518f | 147 | printf("[%f] Completed in %f seconds.\r\n", run_time.read(), connection_timer.read()); |
PBarrett | 1:ae20607dd0c1 | 148 | |
PBarrett | 1:ae20607dd0c1 | 149 | wait(5); |
maanenson | 0:30a991e08e77 | 150 | } |
maanenson | 0:30a991e08e77 | 151 | |
maanenson | 0:30a991e08e77 | 152 | } |