HTTP/HTTPS client Hello World application running with X-NUCLEO-IDW01M1v2 wifi board.
Dependencies: HTTPClient NetworkSocketAPI X_NUCLEO_IDW01M1v2 mbed
Fork of HTTPClient_HelloWorld by
Example of HTTP and HTTPS connections using X-NUCLEO-IDW01M1 Wi-Fi expansion board.
The application is meant to be used with mbed OS 2 ("Classic") only (no mbedOS 5 support).
For HTTPS connection it uses the TLS/SSL feature provided natively by the Wi-Fi module and performs secure connection to the server also verifying the server identity.
To avoid expired CA certificates, system time (in epoch) must be manually entered (e..g. using http://www.epochconverter.com/ ) .
Retrieval of current time from an NTP server is shown by this example.
main.cpp
- Committer:
- mapellil
- Date:
- 2016-11-17
- Revision:
- 8:1b5c28fe5e94
- Parent:
- 5:526c0df8cbff
- Child:
- 9:b54448c7f712
File content as of revision 8:1b5c28fe5e94:
//#define TESTV2 #ifdef TESTV2 #include "mbed.h" #include "SpwfInterface.h" #include "NSAPITests.h" //------------------------------------ // Hyperterminal configuration // 9600 bauds, 8-bit data, no parity //------------------------------------ Serial pc(USBTX, USBRX); DigitalOut myled(LED1); SpwfSAInterface spwf(D8, D2, false); int main() { int32_t err; // char * ssid = "STM"; // char * seckey = "STMdemoPWD"; char * ssid = "crespan"; char * seckey = "Elfrontal1"; pc.printf("\r\nSpwf Interface NSAPI Tests\r\n"); pc.printf("\r\nconnecting to AP\r\n"); err = spwf.connect(ssid, seckey, NSAPI_SECURITY_WPA); if (!err) { printf("Interface failed to connect with code %d\r\n", err); } else { do { nsapi_tests("SPWF Tests", &spwf, "192.168.1.19", 32001); } while (1); spwf.disconnect(); } while(1) { wait(1); myled = !myled; } } #else #include "mbed.h" //#include "EthernetInterface.h" #include "TCPSocket.h" #include "HTTPClient.h" #ifdef LICIO #else EthernetInterface eth; #endif char str[512]; int main() { #ifdef LICIO SpwfSAInterface spwf(D8, D2, false); spwf.connect("crespan","Elfrontal1", NSAPI_SECURITY_WPA2); HTTPClient http(spwf); #else eth.init(); //Use DHCP eth.connect(); #endif int ret; //GET data printf("\nTrying to fetch page...\n\r"); ret = http.get("http://mbed.org/media/uploads/donatien/hello.txt", str, 128, HTTP_CLIENT_DEFAULT_TIMEOUT); // ret = http.get("http://httpstat.us", str, 128, HTTP_CLIENT_DEFAULT_TIMEOUT); if (!ret) { printf("Page fetched successfully - read %d characters\n\r", strlen(str)); printf("Result: %s\n", str); } else { printf("Error - ret = %d - HTTP return code = %d\n\r", ret, http.getHTTPResponseCode()); } //POST data HTTPMap map; HTTPText inText(str, 512); map.put("Hello", "World"); map.put("test", "1234"); printf("\nTrying to post data...\n\r"); ret = http.post("http://httpbin.org/post", map, &inText, HTTP_CLIENT_DEFAULT_TIMEOUT); // ret = http.post("http://posttestserver.com/post.php", map, &inText, HTTP_CLIENT_DEFAULT_TIMEOUT); if (!ret) { printf("Executed POST successfully - read %d characters\n\r", strlen(str)); printf("Result: %s\n\r", str); } else { printf("Error - ret = %d - HTTP return code = %d\n\r", ret, http.getHTTPResponseCode()); } //PUT data strcpy(str, "This is a PUT test!"); HTTPText outText(str); //HTTPText inText(str, 512); printf("\nTrying to put resource...\n\r"); ret = http.put("http://httpbin.org/put", outText, &inText); if (!ret) { printf("Executed PUT successfully - read %d characters\n\r", strlen(str)); printf("Result: %s\n\r", str); } else { printf("Error - ret = %d - HTTP return code = %d\n\r", ret, http.getHTTPResponseCode()); } //DELETE data //HTTPText inText(str, 512); printf("\nTrying to delete resource...\n\r"); ret = http.del("http://httpbin.org/delete", &inText); if (!ret) { printf("Executed DELETE successfully - read %d characters\n\r", strlen(str)); printf("Result: %s\n\r", str); } else { printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); } // eth.disconnect(); while(1) { } } #endif