![](/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@0:30a991e08e77, 2014-05-02 (annotated)
- Committer:
- maanenson
- Date:
- Fri May 02 14:53:22 2014 +0000
- Revision:
- 0:30a991e08e77
- Child:
- 1:ae20607dd0c1
first commit, not working
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" |
maanenson | 0:30a991e08e77 | 3 | |
maanenson | 0:30a991e08e77 | 4 | EthernetInterface eth; |
maanenson | 0:30a991e08e77 | 5 | |
maanenson | 0:30a991e08e77 | 6 | string CIK = "996a6abfb619711dbf3532cc82a6df6513821532"; // copy Device CIK from Portals |
maanenson | 0:30a991e08e77 | 7 | |
maanenson | 0:30a991e08e77 | 8 | AnalogIn aPot1(p19); |
maanenson | 0:30a991e08e77 | 9 | Timer t; |
maanenson | 0:30a991e08e77 | 10 | |
maanenson | 0:30a991e08e77 | 11 | int main() { |
maanenson | 0:30a991e08e77 | 12 | t.start(); |
maanenson | 0:30a991e08e77 | 13 | |
maanenson | 0:30a991e08e77 | 14 | printf("\r\nStart\n"); |
maanenson | 0:30a991e08e77 | 15 | eth.init(); //Use DHCP |
maanenson | 0:30a991e08e77 | 16 | eth.connect(); |
maanenson | 0:30a991e08e77 | 17 | printf("\r\nSetting up...\r\n"); |
maanenson | 0:30a991e08e77 | 18 | printf("IP Address is %s\n", eth.getIPAddress()); |
maanenson | 0:30a991e08e77 | 19 | printf("MAC Address is %s\n", eth.getMACAddress()); |
maanenson | 0:30a991e08e77 | 20 | printf("\r\nSetup OK\r\n"); |
maanenson | 0:30a991e08e77 | 21 | |
maanenson | 0:30a991e08e77 | 22 | |
maanenson | 0:30a991e08e77 | 23 | int ping = 0; |
maanenson | 0:30a991e08e77 | 24 | |
maanenson | 0:30a991e08e77 | 25 | while (1) { |
maanenson | 0:30a991e08e77 | 26 | wait(20); |
maanenson | 0:30a991e08e77 | 27 | |
maanenson | 0:30a991e08e77 | 28 | float totalPot1 = 0; |
maanenson | 0:30a991e08e77 | 29 | float avgPot1 = 0; |
maanenson | 0:30a991e08e77 | 30 | for (int i = 0; i<30; i++) { |
maanenson | 0:30a991e08e77 | 31 | totalPot1 += abs(aPot1); |
maanenson | 0:30a991e08e77 | 32 | } |
maanenson | 0:30a991e08e77 | 33 | avgPot1 = totalPot1/30; |
maanenson | 0:30a991e08e77 | 34 | printf("Pot1 Value: %03f \r\n", avgPot1); |
maanenson | 0:30a991e08e77 | 35 | |
maanenson | 0:30a991e08e77 | 36 | float newTime = t.read(); |
maanenson | 0:30a991e08e77 | 37 | printf("Current Time: %f \r\n",newTime); |
maanenson | 0:30a991e08e77 | 38 | |
maanenson | 0:30a991e08e77 | 39 | if (newTime > 320) { |
maanenson | 0:30a991e08e77 | 40 | t.reset(); |
maanenson | 0:30a991e08e77 | 41 | ping ++; |
maanenson | 0:30a991e08e77 | 42 | if (ping > 100) { |
maanenson | 0:30a991e08e77 | 43 | ping = 1; |
maanenson | 0:30a991e08e77 | 44 | } |
maanenson | 0:30a991e08e77 | 45 | char data [70]; |
maanenson | 0:30a991e08e77 | 46 | int len = sprintf(data, "ping=%d&power=%03f",ping,avgPot1); |
maanenson | 0:30a991e08e77 | 47 | |
maanenson | 0:30a991e08e77 | 48 | TCPSocketConnection sock; |
maanenson | 0:30a991e08e77 | 49 | sock.connect("m2.exosite.com/", 80); |
maanenson | 0:30a991e08e77 | 50 | |
maanenson | 0:30a991e08e77 | 51 | char http_cmd[] = "GET /api:v1/stack/alias HTTP/1.1\r\n"; |
maanenson | 0:30a991e08e77 | 52 | sock.send_all(http_cmd, sizeof(http_cmd)-1); |
maanenson | 0:30a991e08e77 | 53 | char http_format[] = "application/x-www-form-urlencoded; charset=utf-8"; |
maanenson | 0:30a991e08e77 | 54 | sock.send_all(http_format, sizeof(http_format)-1); |
maanenson | 0:30a991e08e77 | 55 | |
maanenson | 0:30a991e08e77 | 56 | |
maanenson | 0:30a991e08e77 | 57 | char buffer[300]; |
maanenson | 0:30a991e08e77 | 58 | int ret; |
maanenson | 0:30a991e08e77 | 59 | while (true) { |
maanenson | 0:30a991e08e77 | 60 | ret = sock.receive(buffer, sizeof(buffer)-1); |
maanenson | 0:30a991e08e77 | 61 | if (ret <= 0) |
maanenson | 0:30a991e08e77 | 62 | break; |
maanenson | 0:30a991e08e77 | 63 | buffer[ret] = '\0'; |
maanenson | 0:30a991e08e77 | 64 | printf("Received %d chars from server:\n%s\n", ret, buffer); |
maanenson | 0:30a991e08e77 | 65 | } |
maanenson | 0:30a991e08e77 | 66 | |
maanenson | 0:30a991e08e77 | 67 | sock.close(); |
maanenson | 0:30a991e08e77 | 68 | |
maanenson | 0:30a991e08e77 | 69 | eth.disconnect(); |
maanenson | 0:30a991e08e77 | 70 | |
maanenson | 0:30a991e08e77 | 71 | HTTPClient http; |
maanenson | 0:30a991e08e77 | 72 | http.setRequestHeader("X-Exosite-CIK", CIK); |
maanenson | 0:30a991e08e77 | 73 | |
maanenson | 0:30a991e08e77 | 74 | HTTPText Content("application/x-www-form-urlencoded; charset=utf-8"); |
maanenson | 0:30a991e08e77 | 75 | Content.set(data); |
maanenson | 0:30a991e08e77 | 76 | string uri = "http://m2.exosite.com/api:v1/stack/alias"; // uri for post includes feed ID // uri for post includes feed ID |
maanenson | 0:30a991e08e77 | 77 | |
maanenson | 0:30a991e08e77 | 78 | printf("Sending Data to Exosite..."); |
maanenson | 0:30a991e08e77 | 79 | HTTPResult result = http.post(uri.c_str(), Content, NULL); |
maanenson | 0:30a991e08e77 | 80 | // result should be 0 and response should be 200 for successful post |
maanenson | 0:30a991e08e77 | 81 | int response = http.getHTTPResponseCode(); |
maanenson | 0:30a991e08e77 | 82 | if (result==HTTP_OK) { |
maanenson | 0:30a991e08e77 | 83 | printf("done\r\n"); |
maanenson | 0:30a991e08e77 | 84 | //printf("Response: %d\n",response); |
maanenson | 0:30a991e08e77 | 85 | } else { |
maanenson | 0:30a991e08e77 | 86 | if (response == 204) { //expected No Content (204) error |
maanenson | 0:30a991e08e77 | 87 | printf("done\r\n"); |
maanenson | 0:30a991e08e77 | 88 | //printf("Response: %d\n",response); |
maanenson | 0:30a991e08e77 | 89 | } else { |
maanenson | 0:30a991e08e77 | 90 | printf("failed\r\n"); |
maanenson | 0:30a991e08e77 | 91 | printf("Error %d\r\n", result); |
maanenson | 0:30a991e08e77 | 92 | printf("Response: %d\r\n",response); |
maanenson | 0:30a991e08e77 | 93 | } |
maanenson | 0:30a991e08e77 | 94 | } |
maanenson | 0:30a991e08e77 | 95 | } |
maanenson | 0:30a991e08e77 | 96 | } |
maanenson | 0:30a991e08e77 | 97 | |
maanenson | 0:30a991e08e77 | 98 | return 0; |
maanenson | 0:30a991e08e77 | 99 | |
maanenson | 0:30a991e08e77 | 100 | } |