![](/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@1:ae20607dd0c1, 2014-05-02 (annotated)
- Committer:
- PBarrett
- Date:
- Fri May 02 22:00:13 2014 +0000
- Revision:
- 1:ae20607dd0c1
- Parent:
- 0:30a991e08e77
- Child:
- 2:8907a25944ab
Basic HTTP Write & Read with LCD
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 | 1:ae20607dd0c1 | 3 | #include "C12832_lcd.h" |
maanenson | 0:30a991e08e77 | 4 | |
maanenson | 0:30a991e08e77 | 5 | EthernetInterface eth; |
maanenson | 0:30a991e08e77 | 6 | |
PBarrett | 1:ae20607dd0c1 | 7 | char CIK[] = "c85bec1649f3450667f918305dc33b85af024686"; // patrick's "mbed Dev Board" |
maanenson | 0:30a991e08e77 | 8 | |
maanenson | 0:30a991e08e77 | 9 | AnalogIn aPot1(p19); |
PBarrett | 1:ae20607dd0c1 | 10 | Timer send_timer, run_time; |
PBarrett | 1:ae20607dd0c1 | 11 | |
PBarrett | 1:ae20607dd0c1 | 12 | static C12832_LCD lcd; |
PBarrett | 1:ae20607dd0c1 | 13 | |
PBarrett | 1:ae20607dd0c1 | 14 | int main() |
PBarrett | 1:ae20607dd0c1 | 15 | { |
PBarrett | 1:ae20607dd0c1 | 16 | run_time.start(); |
PBarrett | 1:ae20607dd0c1 | 17 | send_timer.start(); |
maanenson | 0:30a991e08e77 | 18 | |
PBarrett | 1:ae20607dd0c1 | 19 | printf("\r\nStart\r\n"); |
PBarrett | 1:ae20607dd0c1 | 20 | |
PBarrett | 1:ae20607dd0c1 | 21 | eth.init(); //Use DHCP |
maanenson | 0:30a991e08e77 | 22 | |
PBarrett | 1:ae20607dd0c1 | 23 | lcd.printf("Exosite HTTP Cloud Demo"); |
PBarrett | 1:ae20607dd0c1 | 24 | lcd.locate(0,11); |
PBarrett | 1:ae20607dd0c1 | 25 | lcd.printf("MAC: %s", eth.getMACAddress()); |
PBarrett | 1:ae20607dd0c1 | 26 | lcd.locate(0,22); |
PBarrett | 1:ae20607dd0c1 | 27 | lcd.printf("IP: Requesting From DHCP..."); |
PBarrett | 1:ae20607dd0c1 | 28 | |
maanenson | 0:30a991e08e77 | 29 | eth.connect(); |
PBarrett | 1:ae20607dd0c1 | 30 | |
PBarrett | 1:ae20607dd0c1 | 31 | printf("IP Address is %s\r\n", eth.getIPAddress()); |
PBarrett | 1:ae20607dd0c1 | 32 | printf("MAC Address is %s\r\n", eth.getMACAddress()); |
maanenson | 0:30a991e08e77 | 33 | |
PBarrett | 1:ae20607dd0c1 | 34 | lcd.locate(0,22); |
PBarrett | 1:ae20607dd0c1 | 35 | lcd.printf("IP: %s ", eth.getIPAddress()); |
PBarrett | 1:ae20607dd0c1 | 36 | |
PBarrett | 1:ae20607dd0c1 | 37 | printf("[%f] Running...\r\n", run_time.read()); |
maanenson | 0:30a991e08e77 | 38 | |
maanenson | 0:30a991e08e77 | 39 | while (1) { |
PBarrett | 1:ae20607dd0c1 | 40 | int ret; |
PBarrett | 1:ae20607dd0c1 | 41 | char post_data [70]; |
PBarrett | 1:ae20607dd0c1 | 42 | int len = snprintf(post_data, 69, "uptime=%0.0f&tempa=128",run_time.read()); |
PBarrett | 1:ae20607dd0c1 | 43 | |
PBarrett | 1:ae20607dd0c1 | 44 | |
PBarrett | 1:ae20607dd0c1 | 45 | printf("[%f] Connecting...\r\n", run_time.read()); |
PBarrett | 1:ae20607dd0c1 | 46 | TCPSocketConnection sock; |
PBarrett | 1:ae20607dd0c1 | 47 | //ret = sock.connect("192.168.3.147", 8090); |
PBarrett | 1:ae20607dd0c1 | 48 | ret = sock.connect("m2.exosite.com", 80); |
PBarrett | 1:ae20607dd0c1 | 49 | |
PBarrett | 1:ae20607dd0c1 | 50 | if (ret == -1) { |
PBarrett | 1:ae20607dd0c1 | 51 | printf("[%f] Error Connecting to Server\r\n", run_time.read()); |
PBarrett | 1:ae20607dd0c1 | 52 | continue; |
maanenson | 0:30a991e08e77 | 53 | } |
maanenson | 0:30a991e08e77 | 54 | |
PBarrett | 1:ae20607dd0c1 | 55 | printf("[%f] Sending...\r\n", run_time.read()); |
PBarrett | 1:ae20607dd0c1 | 56 | |
PBarrett | 1:ae20607dd0c1 | 57 | char header_tmp[80] = "POST /api:v1/stack/alias?screen HTTP/1.1\r\n"; |
PBarrett | 1:ae20607dd0c1 | 58 | sock.send(header_tmp, strlen(header_tmp)); |
PBarrett | 1:ae20607dd0c1 | 59 | sock.send("Host: m2.exosite.com\r\n", strlen("Host: m2.exosite.com\r\n")); |
PBarrett | 1:ae20607dd0c1 | 60 | snprintf(header_tmp, 79, "X-Exosite-CIK: %s\r\n", CIK); |
PBarrett | 1:ae20607dd0c1 | 61 | sock.send(header_tmp, strlen(header_tmp)); |
PBarrett | 1:ae20607dd0c1 | 62 | 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")); |
PBarrett | 1:ae20607dd0c1 | 63 | 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")); |
PBarrett | 1:ae20607dd0c1 | 64 | sock.send("Connection: close\r\n", strlen("Connection: close\r\n")); |
PBarrett | 1:ae20607dd0c1 | 65 | snprintf(header_tmp, 79, "Content-Length: %i\r\n", strlen(post_data)); |
PBarrett | 1:ae20607dd0c1 | 66 | sock.send(header_tmp, strlen(header_tmp)); |
PBarrett | 1:ae20607dd0c1 | 67 | sock.send("\r\n", strlen("\r\n")); |
PBarrett | 1:ae20607dd0c1 | 68 | sock.send_all(post_data, strlen(post_data)); |
PBarrett | 1:ae20607dd0c1 | 69 | |
PBarrett | 1:ae20607dd0c1 | 70 | printf("[%f] Receiving...\r\n", run_time.read()); |
PBarrett | 1:ae20607dd0c1 | 71 | |
PBarrett | 1:ae20607dd0c1 | 72 | char buffer[300]; |
PBarrett | 1:ae20607dd0c1 | 73 | ret = sock.receive_all(buffer, 300); |
PBarrett | 1:ae20607dd0c1 | 74 | if(ret >= 0){ |
PBarrett | 1:ae20607dd0c1 | 75 | printf("Received %d chars from server:\r\n%.*s\r\n", ret, ret, buffer); |
PBarrett | 1:ae20607dd0c1 | 76 | }else{ |
PBarrett | 1:ae20607dd0c1 | 77 | printf("Error Receiving"); |
maanenson | 0:30a991e08e77 | 78 | } |
PBarrett | 1:ae20607dd0c1 | 79 | |
PBarrett | 1:ae20607dd0c1 | 80 | sock.close(); |
PBarrett | 1:ae20607dd0c1 | 81 | |
PBarrett | 1:ae20607dd0c1 | 82 | wait(5); |
maanenson | 0:30a991e08e77 | 83 | } |
maanenson | 0:30a991e08e77 | 84 | |
maanenson | 0:30a991e08e77 | 85 | } |