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.

Committer:
JMF
Date:
Wed Nov 23 00:48:12 2016 +0000
Revision:
7:beefa072a165
Parent:
6:562eed9eca87
Child:
9:667c078c40c6
Intermediate update for HTTPSClient support;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JMF 0:50979ffa39b6 1
JMF 0:50979ffa39b6 2 #include "mbed.h"
JMF 0:50979ffa39b6 3 #include "WNCInterface.h"
JMF 0:50979ffa39b6 4
JMF 0:50979ffa39b6 5 #include "Socket/Socket.h"
JMF 0:50979ffa39b6 6 #include "Socket/TCPSocketConnection.h"
JMF 0:50979ffa39b6 7 #include "Socket/UDPSocket.h"
JMF 0:50979ffa39b6 8
JMF 0:50979ffa39b6 9 #define DEBUG
JMF 0:50979ffa39b6 10 #define MBED_PLATFORM
JMF 0:50979ffa39b6 11 #include "HTTPClient.h"
JMF 0:50979ffa39b6 12
JMF 0:50979ffa39b6 13 #define CRLF "\n\r"
JMF 0:50979ffa39b6 14
JMF 0:50979ffa39b6 15 int main() {
JMF 0:50979ffa39b6 16 int ret;
JMF 0:50979ffa39b6 17 WNCInterface wnc;
JMF 2:8fb99c9f5075 18 MODSERIAL pc(USBTX,USBRX,256,256);
JMF 2:8fb99c9f5075 19
JMF 2:8fb99c9f5075 20 pc.baud(115200);
JMF 3:5cc9de44aea5 21 pc.printf("STARTING WNCInterface & Socket Test" CRLF);
JMF 0:50979ffa39b6 22
JMF 0:50979ffa39b6 23 ret = wnc.init();
JMF 3:5cc9de44aea5 24 pc.printf("WNC Module %s initialized (%02X)." CRLF, ret?"IS":"IS NOT", ret);
JMF 0:50979ffa39b6 25 if( !ret ) {
JMF 0:50979ffa39b6 26 printf(" - - - - - - - ALL DONE - - - - - - - " CRLF);
JMF 0:50979ffa39b6 27 while(1);
JMF 0:50979ffa39b6 28 }
JMF 0:50979ffa39b6 29
JMF 0:50979ffa39b6 30 ret = wnc.connect();
JMF 0:50979ffa39b6 31 printf("IP Address: %s " CRLF CRLF, wnc.getIPAddress());
JMF 0:50979ffa39b6 32
JMF 0:50979ffa39b6 33 //
JMF 0:50979ffa39b6 34 //demonstrate HTTPClient operations
JMF 0:50979ffa39b6 35 //
JMF 7:beefa072a165 36 HTTPClient http;
JMF 0:50979ffa39b6 37 char str[512];
JMF 0:50979ffa39b6 38
JMF 0:50979ffa39b6 39 printf("\n\n\n\n-------------------------------------" CRLF);
JMF 0:50979ffa39b6 40 printf( "starting HTTPClient Socket Test..." CRLF);
JMF 0:50979ffa39b6 41
JMF 0:50979ffa39b6 42 //GET data
JMF 0:50979ffa39b6 43 printf(">>>>Fetch a page..." CRLF);
JMF 0:50979ffa39b6 44
JMF 7:beefa072a165 45 ret = http.get("https://developer.mbed.org/media/uploads/mbed_official/hello.txt", str, strlen(str));
JMF 0:50979ffa39b6 46 if (!ret)
JMF 0:50979ffa39b6 47 {
JMF 0:50979ffa39b6 48 printf("Page fetched successfully - read %d characters" CRLF, strlen(str));
JMF 0:50979ffa39b6 49 printf("<----->" CRLF "Result: %s" CRLF "<----->" CRLF, str);
JMF 0:50979ffa39b6 50 }
JMF 0:50979ffa39b6 51 else
JMF 0:50979ffa39b6 52 {
JMF 0:50979ffa39b6 53 printf("Error - ret = %d - HTTP return code = %d" CRLF, ret, http.getHTTPResponseCode());
JMF 0:50979ffa39b6 54 }
JMF 0:50979ffa39b6 55
JMF 0:50979ffa39b6 56 //POST data
JMF 0:50979ffa39b6 57 HTTPMap map;
JMF 0:50979ffa39b6 58 HTTPText inText(str, 512);
JMF 0:50979ffa39b6 59 map.put("Hello", "World");
JMF 0:50979ffa39b6 60 map.put("test", "1234");
JMF 0:50979ffa39b6 61
JMF 0:50979ffa39b6 62 printf(CRLF CRLF ">>>>Post data..." CRLF);
JMF 0:50979ffa39b6 63 ret = http.post("http://httpbin.org/post", map, &inText);
JMF 0:50979ffa39b6 64 if (!ret)
JMF 0:50979ffa39b6 65 {
JMF 0:50979ffa39b6 66 printf("Executed POST successfully - read %d characters" CRLF, strlen(str));
JMF 0:50979ffa39b6 67 printf("<----->" CRLF );
JMF 0:50979ffa39b6 68 printf("Result: %s" CRLF "<----->" CRLF, str);
JMF 0:50979ffa39b6 69 }
JMF 0:50979ffa39b6 70 else
JMF 0:50979ffa39b6 71 {
JMF 0:50979ffa39b6 72 printf("Error - ret = %d - HTTP return code = %d" CRLF, ret, http.getHTTPResponseCode());
JMF 0:50979ffa39b6 73 }
JMF 0:50979ffa39b6 74
JMF 0:50979ffa39b6 75 //PUT data
JMF 0:50979ffa39b6 76 strcpy(str, "This is a PUT test!");
JMF 0:50979ffa39b6 77 HTTPText outText(str);
JMF 0:50979ffa39b6 78 printf(CRLF CRLF ">>>>Put data..." CRLF);
JMF 0:50979ffa39b6 79 ret = http.put("http://httpbin.org/put", outText, &inText);
JMF 0:50979ffa39b6 80 if (!ret)
JMF 0:50979ffa39b6 81 {
JMF 0:50979ffa39b6 82 printf("Executed PUT successfully - read %d characters" CRLF, strlen(str));
JMF 0:50979ffa39b6 83 printf("<----->" CRLF );
JMF 0:50979ffa39b6 84 printf("Result: %s" CRLF "<----->" CRLF, str);
JMF 0:50979ffa39b6 85 }
JMF 0:50979ffa39b6 86 else
JMF 0:50979ffa39b6 87 {
JMF 0:50979ffa39b6 88 printf("Error - ret = %d - HTTP return code = %d" CRLF, ret, http.getHTTPResponseCode());
JMF 0:50979ffa39b6 89 }
JMF 0:50979ffa39b6 90
JMF 0:50979ffa39b6 91 //DELETE data
JMF 0:50979ffa39b6 92 printf(CRLF CRLF ">>>>Delete data..." CRLF);
JMF 0:50979ffa39b6 93 ret = http.del("http://httpbin.org/delete", &inText);
JMF 0:50979ffa39b6 94 if (!ret)
JMF 0:50979ffa39b6 95 {
JMF 0:50979ffa39b6 96 printf("Executed DELETE successfully - read %d characters" CRLF, strlen(str));
JMF 0:50979ffa39b6 97 printf("<----->" CRLF );
JMF 0:50979ffa39b6 98 printf("Result: %s" CRLF "<----->" CRLF, str);
JMF 0:50979ffa39b6 99 }
JMF 0:50979ffa39b6 100 else
JMF 0:50979ffa39b6 101 {
JMF 0:50979ffa39b6 102 printf("Error - ret = %d - HTTP return code = %d" CRLF, ret, http.getHTTPResponseCode());
JMF 0:50979ffa39b6 103 }
JMF 0:50979ffa39b6 104
JMF 0:50979ffa39b6 105 wnc.disconnect();
JMF 0:50979ffa39b6 106 printf(" - - - - - - - ALL DONE - - - - - - - " CRLF);
JMF 0:50979ffa39b6 107 while(1) {}
JMF 0:50979ffa39b6 108 }
JMF 0:50979ffa39b6 109