
HTTPClient Hello World with the WiflyInterface
Dependencies: HTTPClient WiflyInterface mbed
main.cpp@0:cba63cb4bbf9, 2012-08-24 (annotated)
- Committer:
- samux
- Date:
- Fri Aug 24 15:21:55 2012 +0000
- Revision:
- 0:cba63cb4bbf9
HTTPClient with the mbed_official WiflyInterface
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
samux | 0:cba63cb4bbf9 | 1 | #include "mbed.h" |
samux | 0:cba63cb4bbf9 | 2 | #include "WiflyInterface.h" |
samux | 0:cba63cb4bbf9 | 3 | #include "HTTPClient.h" |
samux | 0:cba63cb4bbf9 | 4 | |
samux | 0:cba63cb4bbf9 | 5 | /* wifly interface: |
samux | 0:cba63cb4bbf9 | 6 | * - p9 and p10 are for the serial communication |
samux | 0:cba63cb4bbf9 | 7 | * - p19 is for the reset pin |
samux | 0:cba63cb4bbf9 | 8 | * - p26 is for the connection status |
samux | 0:cba63cb4bbf9 | 9 | * - "mbed" is the ssid of the network |
samux | 0:cba63cb4bbf9 | 10 | * - "password" is the password |
samux | 0:cba63cb4bbf9 | 11 | * - WPA is the security |
samux | 0:cba63cb4bbf9 | 12 | */ |
samux | 0:cba63cb4bbf9 | 13 | WiflyInterface wifly(p9, p10, p19, p26, "mbed", "password", WPA); |
samux | 0:cba63cb4bbf9 | 14 | |
samux | 0:cba63cb4bbf9 | 15 | HTTPClient http; |
samux | 0:cba63cb4bbf9 | 16 | char str[512]; |
samux | 0:cba63cb4bbf9 | 17 | |
samux | 0:cba63cb4bbf9 | 18 | int main() |
samux | 0:cba63cb4bbf9 | 19 | { |
samux | 0:cba63cb4bbf9 | 20 | wifly.init(); //Use DHCP |
samux | 0:cba63cb4bbf9 | 21 | |
samux | 0:cba63cb4bbf9 | 22 | wifly.connect(); |
samux | 0:cba63cb4bbf9 | 23 | |
samux | 0:cba63cb4bbf9 | 24 | //GET data |
samux | 0:cba63cb4bbf9 | 25 | printf("Trying to fetch page...\n"); |
samux | 0:cba63cb4bbf9 | 26 | int ret = http.get("http://mbed.org/media/uploads/donatien/hello.txt", str, 128); |
samux | 0:cba63cb4bbf9 | 27 | if (!ret) { |
samux | 0:cba63cb4bbf9 | 28 | printf("Page fetched successfully - read %d characters\n", strlen(str)); |
samux | 0:cba63cb4bbf9 | 29 | printf("Result: %s\n", str); |
samux | 0:cba63cb4bbf9 | 30 | } else { |
samux | 0:cba63cb4bbf9 | 31 | printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); |
samux | 0:cba63cb4bbf9 | 32 | } |
samux | 0:cba63cb4bbf9 | 33 | |
samux | 0:cba63cb4bbf9 | 34 | //POST data |
samux | 0:cba63cb4bbf9 | 35 | HTTPMap map; |
samux | 0:cba63cb4bbf9 | 36 | HTTPText text(str, 512); |
samux | 0:cba63cb4bbf9 | 37 | map.put("Hello", "World"); |
samux | 0:cba63cb4bbf9 | 38 | map.put("test", "1234"); |
samux | 0:cba63cb4bbf9 | 39 | printf("Trying to post data...\n"); |
samux | 0:cba63cb4bbf9 | 40 | ret = http.post("http://httpbin.org/post", map, &text); |
samux | 0:cba63cb4bbf9 | 41 | if (!ret) { |
samux | 0:cba63cb4bbf9 | 42 | printf("Executed POST successfully - read %d characters\n", strlen(str)); |
samux | 0:cba63cb4bbf9 | 43 | printf("Result: %s\n", str); |
samux | 0:cba63cb4bbf9 | 44 | } else { |
samux | 0:cba63cb4bbf9 | 45 | printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); |
samux | 0:cba63cb4bbf9 | 46 | } |
samux | 0:cba63cb4bbf9 | 47 | |
samux | 0:cba63cb4bbf9 | 48 | wifly.disconnect(); |
samux | 0:cba63cb4bbf9 | 49 | |
samux | 0:cba63cb4bbf9 | 50 | while(1) { |
samux | 0:cba63cb4bbf9 | 51 | } |
samux | 0:cba63cb4bbf9 | 52 | } |