![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
HTTP Client library hello world example for Murata Type-YD WiFi module
Dependencies: HTTPClient PowerControl SNICInterface mbed-rtos mbed
Fork of HTTPClient_WiFi_HelloWorld by
main.cpp@7:c38782801998, 2015-05-10 (annotated)
- Committer:
- MACRUM
- Date:
- Sun May 10 08:51:19 2015 +0000
- Revision:
- 7:c38782801998
- Parent:
- 5:3dbedd084f79
- Child:
- 8:0c400a5a28db
Added Nucleo-F401RE support
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 | |
MACRUM | 3:837766adc429 | 9 | #define DEMO_AP_SSID "SSID" |
MACRUM | 3:837766adc429 | 10 | #define DEMO_AP_SECURITY_TYPE e_SEC_WPA2_AES |
MACRUM | 3:837766adc429 | 11 | #define DEMO_AP_SECUTIRY_KEY "PASSWORD" |
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 | 7:c38782801998 | 15 | #elif defined(TARGET_NUCLEO_F401RE) |
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 | { |
MACRUM | 3:837766adc429 | 28 | #if defined(TARGET_LPC1768) |
MACRUM | 3:837766adc429 | 29 | PHY_PowerDown(); |
MACRUM | 3:837766adc429 | 30 | #endif |
MACRUM | 3:837766adc429 | 31 | |
MACRUM | 7:c38782801998 | 32 | pc.printf("WiFi init...\n"); |
MACRUM | 7:c38782801998 | 33 | wifi.init(); |
donatien | 0:0e0debc29569 | 34 | |
MACRUM | 3:837766adc429 | 35 | wait(0.5); |
MACRUM | 3:837766adc429 | 36 | int s = wifi.disconnect(); |
MACRUM | 3:837766adc429 | 37 | if( s != 0 ) { |
MACRUM | 3:837766adc429 | 38 | return -1; |
MACRUM | 3:837766adc429 | 39 | } |
MACRUM | 7:c38782801998 | 40 | |
MACRUM | 3:837766adc429 | 41 | wait(0.3); |
MACRUM | 3:837766adc429 | 42 | // Connect AP |
MACRUM | 3:837766adc429 | 43 | wifi.connect( DEMO_AP_SSID |
MACRUM | 7:c38782801998 | 44 | , strlen(DEMO_AP_SSID) |
MACRUM | 7:c38782801998 | 45 | , DEMO_AP_SECURITY_TYPE |
MACRUM | 7:c38782801998 | 46 | , DEMO_AP_SECUTIRY_KEY |
MACRUM | 7:c38782801998 | 47 | , strlen(DEMO_AP_SECUTIRY_KEY) ); |
MACRUM | 3:837766adc429 | 48 | wait(0.5); |
MACRUM | 7:c38782801998 | 49 | wifi.setIPConfig( true ); //Use DHCP |
MACRUM | 7:c38782801998 | 50 | wait(0.5); |
MACRUM | 7:c38782801998 | 51 | pc.printf("IP Address is %s\n", wifi.getIPAddress()); |
MACRUM | 7:c38782801998 | 52 | |
MACRUM | 7:c38782801998 | 53 | int ret = 0; |
MACRUM | 7:c38782801998 | 54 | #if 0 |
MACRUM | 7:c38782801998 | 55 | // disabled this test, since the library doesn't support HTTPS connection |
donatien | 0:0e0debc29569 | 56 | //GET data |
MACRUM | 7:c38782801998 | 57 | pc.printf("\nTrying to fetch page...\n"); |
MACRUM | 7:c38782801998 | 58 | ret = http.get("http://developer.mbed.org/media/uploads/donatien/hello.txt", str, 128); |
MACRUM | 7:c38782801998 | 59 | if (!ret) { |
MACRUM | 7:c38782801998 | 60 | pc.printf("Page fetched successfully - read %d characters\n", strlen(str)); |
MACRUM | 7:c38782801998 | 61 | pc.printf("Result: %s\n", str); |
MACRUM | 7:c38782801998 | 62 | } else { |
MACRUM | 7:c38782801998 | 63 | pc.printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); |
donatien | 0:0e0debc29569 | 64 | } |
MACRUM | 7:c38782801998 | 65 | #endif |
MACRUM | 7:c38782801998 | 66 | |
donatien | 0:0e0debc29569 | 67 | //POST data |
donatien | 0:0e0debc29569 | 68 | HTTPMap map; |
donatien | 2:270e2d0bb85a | 69 | HTTPText inText(str, 512); |
donatien | 0:0e0debc29569 | 70 | map.put("Hello", "World"); |
donatien | 0:0e0debc29569 | 71 | map.put("test", "1234"); |
MACRUM | 7:c38782801998 | 72 | pc.printf("\nTrying to post data...\n"); |
donatien | 2:270e2d0bb85a | 73 | ret = http.post("http://httpbin.org/post", map, &inText); |
MACRUM | 7:c38782801998 | 74 | if (!ret) { |
MACRUM | 7:c38782801998 | 75 | pc.printf("Executed POST successfully - read %d characters\n", strlen(str)); |
MACRUM | 7:c38782801998 | 76 | pc.printf("Result: %s\n", str); |
MACRUM | 7:c38782801998 | 77 | } else { |
MACRUM | 7:c38782801998 | 78 | pc.printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); |
donatien | 0:0e0debc29569 | 79 | } |
MACRUM | 7:c38782801998 | 80 | |
donatien | 2:270e2d0bb85a | 81 | //PUT data |
donatien | 2:270e2d0bb85a | 82 | strcpy(str, "This is a PUT test!"); |
donatien | 2:270e2d0bb85a | 83 | HTTPText outText(str); |
donatien | 2:270e2d0bb85a | 84 | //HTTPText inText(str, 512); |
MACRUM | 7:c38782801998 | 85 | pc.printf("\nTrying to put resource...\n"); |
donatien | 2:270e2d0bb85a | 86 | ret = http.put("http://httpbin.org/put", outText, &inText); |
MACRUM | 7:c38782801998 | 87 | if (!ret) { |
MACRUM | 7:c38782801998 | 88 | pc.printf("Executed PUT successfully - read %d characters\n", strlen(str)); |
MACRUM | 7:c38782801998 | 89 | pc.printf("Result: %s\n", str); |
MACRUM | 7:c38782801998 | 90 | } else { |
MACRUM | 7:c38782801998 | 91 | pc.printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); |
donatien | 2:270e2d0bb85a | 92 | } |
MACRUM | 7:c38782801998 | 93 | |
donatien | 2:270e2d0bb85a | 94 | //DELETE data |
donatien | 2:270e2d0bb85a | 95 | //HTTPText inText(str, 512); |
MACRUM | 7:c38782801998 | 96 | pc.printf("\nTrying to delete resource...\n"); |
donatien | 2:270e2d0bb85a | 97 | ret = http.del("http://httpbin.org/delete", &inText); |
MACRUM | 7:c38782801998 | 98 | if (!ret) { |
MACRUM | 7:c38782801998 | 99 | pc.printf("Executed DELETE successfully - read %d characters\n", strlen(str)); |
MACRUM | 7:c38782801998 | 100 | pc.printf("Result: %s\n", str); |
MACRUM | 7:c38782801998 | 101 | } else { |
MACRUM | 7:c38782801998 | 102 | pc.printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); |
donatien | 2:270e2d0bb85a | 103 | } |
MACRUM | 7:c38782801998 | 104 | |
MACRUM | 7:c38782801998 | 105 | wifi.disconnect(); |
donatien | 0:0e0debc29569 | 106 | |
donatien | 0:0e0debc29569 | 107 | while(1) { |
donatien | 0:0e0debc29569 | 108 | } |
donatien | 0:0e0debc29569 | 109 | } |