for debug
Dependencies: HTTPClient PowerControl SNICInterface mbed-rtos mbed-src
Fork of HTTPClient_WiFi_HelloWorld by
main.cpp@10:87bc3840d0a5, 2015-08-15 (annotated)
- Committer:
- sunsmile2015
- Date:
- Sat Aug 15 04:02:17 2015 +0000
- Revision:
- 10:87bc3840d0a5
- Parent:
- 8:0c400a5a28db
debug version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
donatien | 0:0e0debc29569 | 1 | #include "mbed.h" |
MACRUM | 5:3dbedd084f79 | 2 | |
MACRUM | 3:837766adc429 | 3 | #include "SNIC_WifiInterface.h" |
donatien | 0:0e0debc29569 | 4 | #include "HTTPClient.h" |
MACRUM | 3:837766adc429 | 5 | #if defined(TARGET_LPC1768) |
MACRUM | 3:837766adc429 | 6 | #include "PowerControl/EthernetPowerControl.h" |
MACRUM | 3:837766adc429 | 7 | #endif |
MACRUM | 3:837766adc429 | 8 | |
sunsmile2015 | 10:87bc3840d0a5 | 9 | #define DEMO_AP_SSID "Eric 5S" |
MACRUM | 3:837766adc429 | 10 | #define DEMO_AP_SECURITY_TYPE e_SEC_WPA2_AES |
sunsmile2015 | 10:87bc3840d0a5 | 11 | #define DEMO_AP_SECUTIRY_KEY "liaoduck" |
MACRUM | 3:837766adc429 | 12 | |
MACRUM | 7:c38782801998 | 13 | #if defined(TARGET_LPC1768) |
MACRUM | 3:837766adc429 | 14 | C_SNIC_WifiInterface wifi( p9, p10, NC, NC, p30 ); |
MACRUM | 8:0c400a5a28db | 15 | #elif defined(TARGET_NUCLEO_F401RE) || defined(TARGET_NUCLEO_L152RE) |
MACRUM | 7:c38782801998 | 16 | C_SNIC_WifiInterface wifi( D8, D2, NC, NC, D3); |
MACRUM | 7:c38782801998 | 17 | #elif defined(TARGET_K64F) |
MACRUM | 7:c38782801998 | 18 | C_SNIC_WifiInterface wifi( D1, D0, NC, NC, D2); |
MACRUM | 7:c38782801998 | 19 | #endif |
MACRUM | 3:837766adc429 | 20 | |
MACRUM | 5:3dbedd084f79 | 21 | Serial pc(USBTX, USBRX); |
MACRUM | 5:3dbedd084f79 | 22 | |
donatien | 1:d263603373ac | 23 | HTTPClient http; |
donatien | 1:d263603373ac | 24 | char str[512]; |
donatien | 1:d263603373ac | 25 | |
MACRUM | 7:c38782801998 | 26 | int main() |
donatien | 0:0e0debc29569 | 27 | { |
sunsmile2015 | 10:87bc3840d0a5 | 28 | int ret = -1; |
MACRUM | 3:837766adc429 | 29 | #if defined(TARGET_LPC1768) |
MACRUM | 3:837766adc429 | 30 | PHY_PowerDown(); |
MACRUM | 3:837766adc429 | 31 | #endif |
MACRUM | 3:837766adc429 | 32 | |
MACRUM | 7:c38782801998 | 33 | pc.printf("WiFi init...\n"); |
sunsmile2015 | 10:87bc3840d0a5 | 34 | |
MACRUM | 7:c38782801998 | 35 | wifi.init(); |
donatien | 0:0e0debc29569 | 36 | |
MACRUM | 3:837766adc429 | 37 | wait(0.5); |
MACRUM | 3:837766adc429 | 38 | int s = wifi.disconnect(); |
MACRUM | 3:837766adc429 | 39 | if( s != 0 ) { |
MACRUM | 3:837766adc429 | 40 | return -1; |
MACRUM | 3:837766adc429 | 41 | } |
MACRUM | 7:c38782801998 | 42 | |
MACRUM | 3:837766adc429 | 43 | wait(0.3); |
MACRUM | 3:837766adc429 | 44 | // Connect AP |
sunsmile2015 | 10:87bc3840d0a5 | 45 | while(ret != 0) { |
sunsmile2015 | 10:87bc3840d0a5 | 46 | pc.printf("WiFi connectng...\n"); |
sunsmile2015 | 10:87bc3840d0a5 | 47 | ret = wifi.connect( DEMO_AP_SSID |
sunsmile2015 | 10:87bc3840d0a5 | 48 | , strlen(DEMO_AP_SSID) |
sunsmile2015 | 10:87bc3840d0a5 | 49 | , DEMO_AP_SECURITY_TYPE |
sunsmile2015 | 10:87bc3840d0a5 | 50 | , DEMO_AP_SECUTIRY_KEY |
sunsmile2015 | 10:87bc3840d0a5 | 51 | , strlen(DEMO_AP_SECUTIRY_KEY) ); |
sunsmile2015 | 10:87bc3840d0a5 | 52 | wait(1); |
sunsmile2015 | 10:87bc3840d0a5 | 53 | //pc.printf("ret is %d\n", ret); |
sunsmile2015 | 10:87bc3840d0a5 | 54 | }; |
MACRUM | 7:c38782801998 | 55 | wifi.setIPConfig( true ); //Use DHCP |
MACRUM | 7:c38782801998 | 56 | wait(0.5); |
MACRUM | 7:c38782801998 | 57 | pc.printf("IP Address is %s\n", wifi.getIPAddress()); |
MACRUM | 7:c38782801998 | 58 | |
sunsmile2015 | 10:87bc3840d0a5 | 59 | |
MACRUM | 7:c38782801998 | 60 | #if 0 |
MACRUM | 7:c38782801998 | 61 | // disabled this test, since the library doesn't support HTTPS connection |
donatien | 0:0e0debc29569 | 62 | //GET data |
MACRUM | 7:c38782801998 | 63 | pc.printf("\nTrying to fetch page...\n"); |
MACRUM | 7:c38782801998 | 64 | ret = http.get("http://developer.mbed.org/media/uploads/donatien/hello.txt", str, 128); |
MACRUM | 7:c38782801998 | 65 | if (!ret) { |
MACRUM | 7:c38782801998 | 66 | pc.printf("Page fetched successfully - read %d characters\n", strlen(str)); |
MACRUM | 7:c38782801998 | 67 | pc.printf("Result: %s\n", str); |
MACRUM | 7:c38782801998 | 68 | } else { |
MACRUM | 7:c38782801998 | 69 | pc.printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); |
donatien | 0:0e0debc29569 | 70 | } |
MACRUM | 7:c38782801998 | 71 | #endif |
MACRUM | 7:c38782801998 | 72 | |
donatien | 0:0e0debc29569 | 73 | //POST data |
donatien | 0:0e0debc29569 | 74 | HTTPMap map; |
donatien | 2:270e2d0bb85a | 75 | HTTPText inText(str, 512); |
donatien | 0:0e0debc29569 | 76 | map.put("Hello", "World"); |
donatien | 0:0e0debc29569 | 77 | map.put("test", "1234"); |
MACRUM | 7:c38782801998 | 78 | pc.printf("\nTrying to post data...\n"); |
donatien | 2:270e2d0bb85a | 79 | ret = http.post("http://httpbin.org/post", map, &inText); |
MACRUM | 7:c38782801998 | 80 | if (!ret) { |
MACRUM | 7:c38782801998 | 81 | pc.printf("Executed POST successfully - read %d characters\n", strlen(str)); |
MACRUM | 7:c38782801998 | 82 | pc.printf("Result: %s\n", str); |
MACRUM | 7:c38782801998 | 83 | } else { |
MACRUM | 7:c38782801998 | 84 | pc.printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); |
donatien | 0:0e0debc29569 | 85 | } |
MACRUM | 7:c38782801998 | 86 | |
donatien | 2:270e2d0bb85a | 87 | //PUT data |
donatien | 2:270e2d0bb85a | 88 | strcpy(str, "This is a PUT test!"); |
donatien | 2:270e2d0bb85a | 89 | HTTPText outText(str); |
donatien | 2:270e2d0bb85a | 90 | //HTTPText inText(str, 512); |
MACRUM | 7:c38782801998 | 91 | pc.printf("\nTrying to put resource...\n"); |
donatien | 2:270e2d0bb85a | 92 | ret = http.put("http://httpbin.org/put", outText, &inText); |
MACRUM | 7:c38782801998 | 93 | if (!ret) { |
MACRUM | 7:c38782801998 | 94 | pc.printf("Executed PUT successfully - read %d characters\n", strlen(str)); |
MACRUM | 7:c38782801998 | 95 | pc.printf("Result: %s\n", str); |
MACRUM | 7:c38782801998 | 96 | } else { |
MACRUM | 7:c38782801998 | 97 | pc.printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); |
donatien | 2:270e2d0bb85a | 98 | } |
MACRUM | 7:c38782801998 | 99 | |
donatien | 2:270e2d0bb85a | 100 | //DELETE data |
donatien | 2:270e2d0bb85a | 101 | //HTTPText inText(str, 512); |
MACRUM | 7:c38782801998 | 102 | pc.printf("\nTrying to delete resource...\n"); |
donatien | 2:270e2d0bb85a | 103 | ret = http.del("http://httpbin.org/delete", &inText); |
MACRUM | 7:c38782801998 | 104 | if (!ret) { |
MACRUM | 7:c38782801998 | 105 | pc.printf("Executed DELETE successfully - read %d characters\n", strlen(str)); |
MACRUM | 7:c38782801998 | 106 | pc.printf("Result: %s\n", str); |
MACRUM | 7:c38782801998 | 107 | } else { |
MACRUM | 7:c38782801998 | 108 | pc.printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); |
donatien | 2:270e2d0bb85a | 109 | } |
MACRUM | 7:c38782801998 | 110 | |
MACRUM | 7:c38782801998 | 111 | wifi.disconnect(); |
donatien | 0:0e0debc29569 | 112 | |
donatien | 0:0e0debc29569 | 113 | while(1) { |
donatien | 0:0e0debc29569 | 114 | } |
donatien | 0:0e0debc29569 | 115 | } |