ban4jp - / Mbed 2 deprecated SNIC-httpclient-example

Dependencies:   HTTPClient SNICInterface_PullReq mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "SNIC_WifiInterface.h"
00003 #include "HTTPClient.h"
00004 
00005 #define WIFI_SSID           "mbed_ap"
00006 #define WIFI_SECUTIRY_KEY   "password1234"
00007 #define WIFI_SECURITY_TYPE  e_SEC_WPA2_AES
00008 
00009 #if defined(TARGET_LPC1768)
00010 C_SNIC_WifiInterface wifi( p9, p10, NC, NC, p30 );
00011 Serial pc(USBTX, USBRX);    // This is required when defined "_DEBUG"
00012 #else
00013 #error no defined pin.
00014 #endif
00015 
00016 HTTPClient http;
00017 char str[512];
00018 
00019 int main()
00020 {
00021     pc.baud( 115200 );
00022     pc.printf("----------------\n");
00023     pc.printf("Murata TypeYD - HTTPClient example\n");
00024 
00025     int ret = wifi.init();
00026     if (!ret) {
00027         printf("Initialized\n");
00028         //printf("Initialized, MAC: %s\n", wifi.getMACAddress());
00029     } else {
00030         printf("Error wifi.init() - ret = %d\n", ret);
00031         return -1;
00032     }
00033 
00034     wait(0.5);
00035 
00036     ret = wifi.disconnect();
00037 
00038     wait(0.5);
00039 
00040     ret = wifi.getFWVersion((unsigned char *)str);
00041     if (!ret) {
00042         printf("Firmware version: %s\n", str);
00043     } else {
00044         printf("Error wifi.getFWVersion() - ret = %d\n", ret);
00045         return -1;
00046     }
00047 
00048     wait(0.5);
00049 
00050     ret = wifi.connect( WIFI_SSID, strlen(WIFI_SSID)
00051                         , WIFI_SECURITY_TYPE
00052                         , WIFI_SECUTIRY_KEY, strlen(WIFI_SECUTIRY_KEY) );
00053     if (!ret) {
00054         printf("Connected\n");
00055         /*
00056         printf("Connected, IP: %s, MASK: %s, GW: %s\n",
00057                wifi.getIPAddress(), wifi.getNetworkMask(), wifi.getGateway());
00058         */
00059     } else {
00060         printf("Error wifi.connect() - ret = %d\n", ret);
00061         return -1;
00062     }
00063 
00064     // Use DHCP
00065     wifi.setIPConfig( true );
00066     // Use Static IP
00067     //wifi.setIPConfig( false, "192.168.0.48", "255.255.255.0", "192.168.0.1" );
00068 
00069     wait(0.5);
00070 
00071     // GET data
00072     {
00073         printf("\nTrying to GET request...\n");
00074         ret = http.get("http://developer.mbed.org/media/uploads/donatien/hello.txt", str, sizeof(str));
00075         if (!ret) {
00076             printf("Page fetched successfully - read %d characters\n", strlen(str));
00077             printf("Result: %s\n", str);
00078         } else {
00079             printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
00080         }
00081     }
00082 
00083     // POST data
00084     {
00085         HTTPMap map;
00086         HTTPText inText(str, sizeof(str));
00087         map.put("Hello", "World");
00088         map.put("test", "1234");
00089 
00090         printf("\nTrying to POST request...\n");
00091         ret = http.post("http://httpbin.org/post", map, &inText);
00092         if (!ret) {
00093             printf("Executed POST successfully - read %d characters\n", strlen(str));
00094             printf("Result: %s\n", str);
00095         } else {
00096             printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
00097         }
00098     }
00099 
00100     // PUT data
00101     {
00102         strcpy(str, "This is a PUT test!");
00103         HTTPText outText(str);
00104         HTTPText inText(str, sizeof(str));
00105 
00106         printf("\nTrying to PUT request...\n");
00107         ret = http.put("http://httpbin.org/put", outText, &inText);
00108         if (!ret) {
00109             printf("Executed PUT successfully - read %d characters\n", strlen(str));
00110             printf("Result: %s\n", str);
00111         } else {
00112             printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
00113         }
00114     }
00115 
00116     // DELETE data
00117     {
00118         HTTPText inText(str, sizeof(str));
00119 
00120         printf("\nTrying to DELETE request...\n");
00121         ret = http.del("http://httpbin.org/delete", &inText);
00122         if (!ret) {
00123             printf("Executed DELETE successfully - read %d characters\n", strlen(str));
00124             printf("Result: %s\n", str);
00125         } else {
00126             printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
00127         }
00128     }
00129 
00130     printf("\n");
00131 
00132     ret = wifi.disconnect();
00133     if (!ret) {
00134         printf("Disconnected\n");
00135     } else {
00136         printf("Error wifi.disconnect() - ret = %d\n", ret);
00137     }
00138 
00139     while(1) {
00140     }
00141 }