This Socket Example Program Implements a TCP Socket, UDP Socket, and HTTPClient socket then uses each of these to perform basic I/O.
Dependencies: JSON M2XStreamClient-JMF WNCInterface mbed-rtos mbed
See the README for details on this example program. NOTE: When started, the program can take up to 40 seconds before it will respond. This delay is the time required for the WNC Data Module to connect with the network.
main.cpp
- Committer:
- JMF
- Date:
- 2016-11-23
- Revision:
- 7:beefa072a165
- Parent:
- 6:562eed9eca87
- Child:
- 9:667c078c40c6
File content as of revision 7:beefa072a165:
#include "mbed.h" #include "WNCInterface.h" #include "Socket/Socket.h" #include "Socket/TCPSocketConnection.h" #include "Socket/UDPSocket.h" #define DEBUG #define MBED_PLATFORM #include "HTTPClient.h" #define CRLF "\n\r" int main() { int ret; WNCInterface wnc; MODSERIAL pc(USBTX,USBRX,256,256); pc.baud(115200); pc.printf("STARTING WNCInterface & Socket Test" CRLF); ret = wnc.init(); pc.printf("WNC Module %s initialized (%02X)." CRLF, ret?"IS":"IS NOT", ret); if( !ret ) { printf(" - - - - - - - ALL DONE - - - - - - - " CRLF); while(1); } ret = wnc.connect(); printf("IP Address: %s " CRLF CRLF, wnc.getIPAddress()); // //demonstrate HTTPClient operations // HTTPClient http; char str[512]; printf("\n\n\n\n-------------------------------------" CRLF); printf( "starting HTTPClient Socket Test..." CRLF); //GET data printf(">>>>Fetch a page..." CRLF); ret = http.get("https://developer.mbed.org/media/uploads/mbed_official/hello.txt", str, strlen(str)); if (!ret) { printf("Page fetched successfully - read %d characters" CRLF, strlen(str)); printf("<----->" CRLF "Result: %s" CRLF "<----->" CRLF, str); } else { printf("Error - ret = %d - HTTP return code = %d" CRLF, ret, http.getHTTPResponseCode()); } //POST data HTTPMap map; HTTPText inText(str, 512); map.put("Hello", "World"); map.put("test", "1234"); printf(CRLF CRLF ">>>>Post data..." CRLF); ret = http.post("http://httpbin.org/post", map, &inText); if (!ret) { printf("Executed POST successfully - read %d characters" CRLF, strlen(str)); printf("<----->" CRLF ); printf("Result: %s" CRLF "<----->" CRLF, str); } else { printf("Error - ret = %d - HTTP return code = %d" CRLF, ret, http.getHTTPResponseCode()); } //PUT data strcpy(str, "This is a PUT test!"); HTTPText outText(str); printf(CRLF CRLF ">>>>Put data..." CRLF); ret = http.put("http://httpbin.org/put", outText, &inText); if (!ret) { printf("Executed PUT successfully - read %d characters" CRLF, strlen(str)); printf("<----->" CRLF ); printf("Result: %s" CRLF "<----->" CRLF, str); } else { printf("Error - ret = %d - HTTP return code = %d" CRLF, ret, http.getHTTPResponseCode()); } //DELETE data printf(CRLF CRLF ">>>>Delete data..." CRLF); ret = http.del("http://httpbin.org/delete", &inText); if (!ret) { printf("Executed DELETE successfully - read %d characters" CRLF, strlen(str)); printf("<----->" CRLF ); printf("Result: %s" CRLF "<----->" CRLF, str); } else { printf("Error - ret = %d - HTTP return code = %d" CRLF, ret, http.getHTTPResponseCode()); } wnc.disconnect(); printf(" - - - - - - - ALL DONE - - - - - - - " CRLF); while(1) {} }