![](/media/cache/group/logo_thTzkVQ.jpg.50x50_q85.jpg)
NuMaker WiFi TCP Example
main.cpp@13:be6ec74c5987, 2018-03-12 (annotated)
- Committer:
- cyliang
- Date:
- Mon Mar 12 14:19:07 2018 +0800
- Revision:
- 13:be6ec74c5987
- Parent:
- 12:7accee7d9220
- Child:
- 15:32a6a29ffcb3
Upgrade lib revision to suppotr wifi API with encryption type
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cyliang | 0:2198c8de64fe | 1 | |
cyliang | 0:2198c8de64fe | 2 | #include <algorithm> |
cyliang | 0:2198c8de64fe | 3 | #include "mbed.h" |
cyliang | 0:2198c8de64fe | 4 | #include "TCPSocket.h" |
cyliang | 0:2198c8de64fe | 5 | #include "unity/unity.h" |
cyliang | 0:2198c8de64fe | 6 | |
cyliang | 0:2198c8de64fe | 7 | #ifndef __CC_ARM |
cyliang | 0:2198c8de64fe | 8 | #ifdef __GNUC__ |
cyliang | 0:2198c8de64fe | 9 | extern "C" caddr_t _sbrk(int incr); |
cyliang | 0:2198c8de64fe | 10 | #endif |
cyliang | 0:2198c8de64fe | 11 | #endif |
cyliang | 0:2198c8de64fe | 12 | |
cyliang | 0:2198c8de64fe | 13 | |
cyliang | 0:2198c8de64fe | 14 | //#define LOCAL_LAN |
cyliang | 0:2198c8de64fe | 15 | |
cyliang | 0:2198c8de64fe | 16 | #define ETHERNET 1 |
cyliang | 0:2198c8de64fe | 17 | #define WIFI 2 |
cyliang | 0:2198c8de64fe | 18 | #define MESH_LOWPAN_ND 3 |
cyliang | 0:2198c8de64fe | 19 | #define MESH_THREAD 4 |
cyliang | 0:2198c8de64fe | 20 | |
cyliang | 0:2198c8de64fe | 21 | #if MBED_CONF_APP_NETWORK_INTERFACE == WIFI |
cyliang | 0:2198c8de64fe | 22 | #include "ESP8266Interface.h" |
cyliang | 0:2198c8de64fe | 23 | ESP8266Interface esp(D1, D0); |
cyliang | 0:2198c8de64fe | 24 | #elif MBED_CONF_APP_NETWORK_INTERFACE == ETHERNET |
cyliang | 0:2198c8de64fe | 25 | #include "EthernetInterface.h" |
cyliang | 0:2198c8de64fe | 26 | EthernetInterface eth; |
cyliang | 0:2198c8de64fe | 27 | #elif MBED_CONF_APP_NETWORK_INTERFACE == MESH_LOWPAN_ND |
cyliang | 0:2198c8de64fe | 28 | #define MESH |
cyliang | 0:2198c8de64fe | 29 | #include "NanostackInterface.h" |
cyliang | 0:2198c8de64fe | 30 | LoWPANNDInterface mesh; |
cyliang | 0:2198c8de64fe | 31 | #elif MBED_CONF_APP_NETWORK_INTERFACE == MESH_THREAD |
cyliang | 0:2198c8de64fe | 32 | #define MESH |
cyliang | 0:2198c8de64fe | 33 | #include "NanostackInterface.h" |
cyliang | 0:2198c8de64fe | 34 | ThreadInterface mesh; |
cyliang | 0:2198c8de64fe | 35 | #endif |
cyliang | 0:2198c8de64fe | 36 | |
cyliang | 0:2198c8de64fe | 37 | namespace { |
cyliang | 0:2198c8de64fe | 38 | // Test connection information |
cyliang | 0:2198c8de64fe | 39 | #ifndef LOCAL_LAN |
ccli8 |
11:658411c71934 | 40 | const char *HTTP_SERVER_NAME = "os.mbed.com"; |
cyliang | 0:2198c8de64fe | 41 | #else |
cyliang | 0:2198c8de64fe | 42 | const char *HTTP_SERVER_NAME = "pt22_winserver2.nuvoton.com"; |
cyliang | 0:2198c8de64fe | 43 | #endif |
cyliang | 0:2198c8de64fe | 44 | |
cyliang | 0:2198c8de64fe | 45 | #ifndef LOCAL_LAN |
cyliang | 0:2198c8de64fe | 46 | const char *HTTP_SERVER_FILE_PATH = "/media/uploads/mbed_official/hello.txt"; |
cyliang | 0:2198c8de64fe | 47 | const int HTTP_SERVER_PORT = 80; |
cyliang | 0:2198c8de64fe | 48 | #else |
cyliang | 0:2198c8de64fe | 49 | const char *HTTP_SERVER_FILE_PATH = "/examples/arm_mbed/hello.txt"; |
cyliang | 0:2198c8de64fe | 50 | const int HTTP_SERVER_PORT = 8080; |
cyliang | 0:2198c8de64fe | 51 | #endif |
cyliang | 0:2198c8de64fe | 52 | |
cyliang | 0:2198c8de64fe | 53 | |
cyliang | 0:2198c8de64fe | 54 | const int RECV_BUFFER_SIZE = 512; |
cyliang | 0:2198c8de64fe | 55 | |
cyliang | 0:2198c8de64fe | 56 | // Test related data |
cyliang | 0:2198c8de64fe | 57 | const char *HTTP_OK_STR = "200 OK"; |
cyliang | 0:2198c8de64fe | 58 | const char *HTTP_HELLO_STR = "Hello world!"; |
cyliang | 0:2198c8de64fe | 59 | |
cyliang | 0:2198c8de64fe | 60 | // Test buffers |
cyliang | 0:2198c8de64fe | 61 | char buffer[RECV_BUFFER_SIZE] = {0}; |
cyliang | 0:2198c8de64fe | 62 | } |
cyliang | 0:2198c8de64fe | 63 | |
cyliang | 0:2198c8de64fe | 64 | bool find_substring(const char *first, const char *last, const char *s_first, const char *s_last) { |
cyliang | 0:2198c8de64fe | 65 | const char *f = std::search(first, last, s_first, s_last); |
cyliang | 0:2198c8de64fe | 66 | return (f != last); |
cyliang | 0:2198c8de64fe | 67 | } |
cyliang | 0:2198c8de64fe | 68 | |
cyliang | 0:2198c8de64fe | 69 | |
cyliang | 0:2198c8de64fe | 70 | Serial output(USBTX, USBRX); |
cyliang | 0:2198c8de64fe | 71 | |
cyliang | 0:2198c8de64fe | 72 | int main() { |
cyliang | 0:2198c8de64fe | 73 | // Sets the console baud-rate |
cyliang | 0:2198c8de64fe | 74 | output.baud(115200); |
cyliang | 0:2198c8de64fe | 75 | output.printf(" Start WiFi test \r\n"); |
cyliang | 0:2198c8de64fe | 76 | |
cyliang | 0:2198c8de64fe | 77 | bool result = true; |
cyliang | 0:2198c8de64fe | 78 | int rc = 0; |
cyliang | 0:2198c8de64fe | 79 | |
cyliang | 0:2198c8de64fe | 80 | #ifndef __CC_ARM |
cyliang | 0:2198c8de64fe | 81 | #ifdef __GNUC__ |
cyliang | 0:2198c8de64fe | 82 | output.printf("sbrk=%x:\r\n", (unsigned int)_sbrk(0)); |
cyliang | 0:2198c8de64fe | 83 | #endif |
cyliang | 0:2198c8de64fe | 84 | #endif |
cyliang | 0:2198c8de64fe | 85 | output.printf(" Start Connection ... \r\n"); |
cyliang | 0:2198c8de64fe | 86 | |
cyliang | 0:2198c8de64fe | 87 | |
cyliang | 0:2198c8de64fe | 88 | NetworkInterface *network_interface = 0; |
cyliang | 0:2198c8de64fe | 89 | |
cyliang | 0:2198c8de64fe | 90 | #if MBED_CONF_APP_NETWORK_INTERFACE == WIFI |
cyliang | 0:2198c8de64fe | 91 | output.printf("\n\rUsing WiFi \r\n"); |
cyliang | 0:2198c8de64fe | 92 | output.printf("\n\rConnecting to WiFi..\r\n"); |
cyliang | 13:be6ec74c5987 | 93 | rc = esp.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2); |
cyliang | 0:2198c8de64fe | 94 | network_interface = &esp; |
cyliang | 0:2198c8de64fe | 95 | #elif MBED_CONF_APP_NETWORK_INTERFACE == ETHERNET |
cyliang | 0:2198c8de64fe | 96 | output.printf("Using Ethernet\r\n"); |
cyliang | 0:2198c8de64fe | 97 | rc = eth.connect(); |
cyliang | 0:2198c8de64fe | 98 | network_interface = ð |
cyliang | 0:2198c8de64fe | 99 | #endif |
cyliang | 0:2198c8de64fe | 100 | #ifdef MESH |
cyliang | 0:2198c8de64fe | 101 | output.printf("Using Mesh\r\n"); |
cyliang | 0:2198c8de64fe | 102 | output.printf("\n\rConnecting to Mesh..\r\n"); |
cyliang | 0:2198c8de64fe | 103 | rc = mesh.connect(); |
cyliang | 0:2198c8de64fe | 104 | network_interface = &mesh; |
cyliang | 0:2198c8de64fe | 105 | #endif |
cyliang | 0:2198c8de64fe | 106 | |
cyliang | 0:2198c8de64fe | 107 | if(rc == 0) { |
cyliang | 0:2198c8de64fe | 108 | output.printf("\n\rConnected to Network successfully\r\n"); |
cyliang | 0:2198c8de64fe | 109 | } else { |
cyliang | 0:2198c8de64fe | 110 | output.printf("\n\rConnection to Network Failed %d! Exiting application....\r\n", rc); |
cyliang | 0:2198c8de64fe | 111 | return 0; |
cyliang | 0:2198c8de64fe | 112 | } |
cyliang | 0:2198c8de64fe | 113 | |
cyliang | 0:2198c8de64fe | 114 | output.printf("TCP client IP Address is %s\r\n", network_interface->get_ip_address()); |
cyliang | 0:2198c8de64fe | 115 | |
cyliang | 0:2198c8de64fe | 116 | TCPSocket sock(network_interface); |
cyliang | 0:2198c8de64fe | 117 | output.printf(" HTTP Connection ... \r\n"); |
cyliang | 0:2198c8de64fe | 118 | if (sock.connect(HTTP_SERVER_NAME, HTTP_SERVER_PORT) == 0) { |
cyliang | 0:2198c8de64fe | 119 | output.printf("HTTP: Connected to %s:%d\r\n", HTTP_SERVER_NAME, HTTP_SERVER_PORT); |
cyliang | 0:2198c8de64fe | 120 | |
cyliang | 0:2198c8de64fe | 121 | // We are constructing GET command like this: |
cyliang | 0:2198c8de64fe | 122 | // GET http://developer.mbed.org/media/uploads/mbed_official/hello.txt HTTP/1.0\n\n |
cyliang | 0:2198c8de64fe | 123 | strcpy(buffer, "GET http://"); |
cyliang | 0:2198c8de64fe | 124 | strcat(buffer, HTTP_SERVER_NAME); |
cyliang | 0:2198c8de64fe | 125 | strcat(buffer, HTTP_SERVER_FILE_PATH); |
cyliang | 0:2198c8de64fe | 126 | strcat(buffer, " HTTP/1.0\n\n"); |
cyliang | 0:2198c8de64fe | 127 | // Send GET command |
cyliang | 0:2198c8de64fe | 128 | sock.send(buffer, strlen(buffer)); |
cyliang | 0:2198c8de64fe | 129 | |
cyliang | 0:2198c8de64fe | 130 | // Server will respond with HTTP GET's success code |
cyliang | 0:2198c8de64fe | 131 | const int ret = sock.recv(buffer, sizeof(buffer) - 1); |
cyliang | 0:2198c8de64fe | 132 | buffer[ret] = '\0'; |
cyliang | 0:2198c8de64fe | 133 | |
cyliang | 0:2198c8de64fe | 134 | // Find 200 OK HTTP status in reply |
cyliang | 0:2198c8de64fe | 135 | bool found_200_ok = find_substring(buffer, buffer + ret, HTTP_OK_STR, HTTP_OK_STR + strlen(HTTP_OK_STR)); |
cyliang | 0:2198c8de64fe | 136 | // Find "Hello World!" string in reply |
cyliang | 0:2198c8de64fe | 137 | bool found_hello = find_substring(buffer, buffer + ret, HTTP_HELLO_STR, HTTP_HELLO_STR + strlen(HTTP_HELLO_STR)); |
cyliang | 0:2198c8de64fe | 138 | |
cyliang | 0:2198c8de64fe | 139 | if (!found_200_ok) result = false; |
cyliang | 0:2198c8de64fe | 140 | if (!found_hello) result = false; |
cyliang | 0:2198c8de64fe | 141 | |
cyliang | 0:2198c8de64fe | 142 | output.printf("HTTP: Received %d chars from server\r\n", ret); |
cyliang | 0:2198c8de64fe | 143 | output.printf("HTTP: Received 200 OK status ... %s\r\n", found_200_ok ? "[OK]" : "[FAIL]"); |
cyliang | 0:2198c8de64fe | 144 | output.printf("HTTP: Received '%s' status ... %s\r\n", HTTP_HELLO_STR, found_hello ? "[OK]" : "[FAIL]"); |
cyliang | 0:2198c8de64fe | 145 | output.printf("HTTP: Received massage:\r\n\r\n"); |
cyliang | 0:2198c8de64fe | 146 | output.printf("%s", buffer); |
cyliang | 0:2198c8de64fe | 147 | } |
cyliang | 0:2198c8de64fe | 148 | |
cyliang | 0:2198c8de64fe | 149 | #ifndef __CC_ARM |
cyliang | 0:2198c8de64fe | 150 | #ifdef __GNUC__ |
cyliang | 0:2198c8de64fe | 151 | output.printf("sbrk=%x:\r\n", (unsigned int)_sbrk(0)); |
cyliang | 0:2198c8de64fe | 152 | #endif |
cyliang | 0:2198c8de64fe | 153 | #endif |
cyliang | 0:2198c8de64fe | 154 | output.printf(" Close socket & disconnect ... \r\n"); |
cyliang | 0:2198c8de64fe | 155 | sock.close(); |
cyliang | 0:2198c8de64fe | 156 | |
cyliang | 0:2198c8de64fe | 157 | #if MBED_CONF_APP_NETWORK_INTERFACE == WIFI |
cyliang | 0:2198c8de64fe | 158 | ((ESP8266Interface *)network_interface)->disconnect(); |
cyliang | 0:2198c8de64fe | 159 | #elif MBED_CONF_APP_NETWORK_INTERFACE == ETHERNET |
cyliang | 0:2198c8de64fe | 160 | ((EthernetInterface *)network_interface)->disconnect(); |
cyliang | 0:2198c8de64fe | 161 | #elif MBED_CONF_APP_NETWORK_INTERFACE == MESH_LOWPAN_ND |
cyliang | 0:2198c8de64fe | 162 | ((LoWPANNDInterface *)network_interface)->disconnect(); |
cyliang | 0:2198c8de64fe | 163 | #elif MBED_CONF_APP_NETWORK_INTERFACE == MESH_THREAD |
cyliang | 0:2198c8de64fe | 164 | ((ThreadInterface *)network_interface)->disconnect(); |
cyliang | 0:2198c8de64fe | 165 | #endif |
cyliang | 0:2198c8de64fe | 166 | output.printf(" End \r\n"); |
cyliang | 0:2198c8de64fe | 167 | } |