![](/media/cache/group/logo_thTzkVQ.jpg.50x50_q85.jpg)
NuMaker WiFi TCP Example
main.cpp@20:41c819860b4d, 2019-05-13 (annotated)
- Committer:
- cyliang
- Date:
- Mon May 13 20:16:52 2019 +0800
- Revision:
- 20:41c819860b4d
- Parent:
- 19:79f168fa9a8a
- Child:
- 21:662058dda3b1
To Change http test server as ifconfig.io
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cyliang | 0:2198c8de64fe | 1 | #include <algorithm> |
cyliang | 0:2198c8de64fe | 2 | #include "mbed.h" |
ccli8 |
15:32a6a29ffcb3 | 3 | #include "mbed_stats.h" |
cyliang | 0:2198c8de64fe | 4 | #include "TCPSocket.h" |
cyliang | 0:2198c8de64fe | 5 | |
cyliang | 20:41c819860b4d | 6 | #define MBED_HEAP_STATS_ENABLED 1 |
cyliang | 0:2198c8de64fe | 7 | //#define LOCAL_LAN |
cyliang | 0:2198c8de64fe | 8 | |
cyliang | 0:2198c8de64fe | 9 | #define ETHERNET 1 |
cyliang | 0:2198c8de64fe | 10 | #define WIFI 2 |
cyliang | 0:2198c8de64fe | 11 | #define MESH_LOWPAN_ND 3 |
cyliang | 0:2198c8de64fe | 12 | #define MESH_THREAD 4 |
cyliang | 0:2198c8de64fe | 13 | |
cyliang | 0:2198c8de64fe | 14 | #if MBED_CONF_APP_NETWORK_INTERFACE == WIFI |
ccli8 |
16:f705110e6c04 | 15 | #define ESP8266_AT_ONBOARD 1 // On-board ESP8266 |
ccli8 |
16:f705110e6c04 | 16 | #define ESP8266_AT_EXTERN 2 // External ESP8266 through UNO D1/D0 |
ccli8 |
16:f705110e6c04 | 17 | |
ccli8 |
19:79f168fa9a8a | 18 | #ifndef ESP8266_AT_SEL |
ccli8 |
19:79f168fa9a8a | 19 | #error("ESP8266_AT_SEL missing. Select ESP8266 on-board/external.") |
ccli8 |
16:f705110e6c04 | 20 | #endif |
ccli8 |
16:f705110e6c04 | 21 | |
cyliang | 0:2198c8de64fe | 22 | #include "ESP8266Interface.h" |
ccli8 |
16:f705110e6c04 | 23 | # if ESP8266_AT_SEL == ESP8266_AT_ONBOARD |
ccli8 |
16:f705110e6c04 | 24 | # if TARGET_NUMAKER_IOT_M487 |
ccli8 |
16:f705110e6c04 | 25 | DigitalOut esp_rst(PH_3, 0); // Simulate reset button pressed |
ccli8 |
16:f705110e6c04 | 26 | ESP8266Interface esp(PH_8, PH_9); |
ccli8 |
16:f705110e6c04 | 27 | # elif TARGET_NUMAKER_PFM_M2351 |
ccli8 |
16:f705110e6c04 | 28 | DigitalIn esp_gpio0(PD_6); // Go boot mode by default |
ccli8 |
16:f705110e6c04 | 29 | // User can change to F/W update mode by short'ing ESP8266 GPIO0/GND |
ccli8 |
16:f705110e6c04 | 30 | // before power-on |
ccli8 |
16:f705110e6c04 | 31 | DigitalOut esp_pwr_off(PD_7, 1); // Disable power to on-board ESP8266 |
ccli8 |
16:f705110e6c04 | 32 | ESP8266Interface esp(PD_1, PD_0); |
ccli8 |
16:f705110e6c04 | 33 | # endif |
ccli8 |
16:f705110e6c04 | 34 | # elif ESP8266_AT_SEL == ESP8266_AT_EXTERN |
cyliang | 0:2198c8de64fe | 35 | ESP8266Interface esp(D1, D0); |
ccli8 |
16:f705110e6c04 | 36 | # endif |
cyliang | 0:2198c8de64fe | 37 | #elif MBED_CONF_APP_NETWORK_INTERFACE == ETHERNET |
cyliang | 0:2198c8de64fe | 38 | #include "EthernetInterface.h" |
cyliang | 0:2198c8de64fe | 39 | EthernetInterface eth; |
cyliang | 0:2198c8de64fe | 40 | #elif MBED_CONF_APP_NETWORK_INTERFACE == MESH_LOWPAN_ND |
cyliang | 0:2198c8de64fe | 41 | #define MESH |
cyliang | 0:2198c8de64fe | 42 | #include "NanostackInterface.h" |
cyliang | 0:2198c8de64fe | 43 | LoWPANNDInterface mesh; |
cyliang | 0:2198c8de64fe | 44 | #elif MBED_CONF_APP_NETWORK_INTERFACE == MESH_THREAD |
cyliang | 0:2198c8de64fe | 45 | #define MESH |
cyliang | 0:2198c8de64fe | 46 | #include "NanostackInterface.h" |
cyliang | 0:2198c8de64fe | 47 | ThreadInterface mesh; |
cyliang | 0:2198c8de64fe | 48 | #endif |
cyliang | 0:2198c8de64fe | 49 | |
cyliang | 0:2198c8de64fe | 50 | namespace { |
cyliang | 0:2198c8de64fe | 51 | // Test connection information |
cyliang | 0:2198c8de64fe | 52 | #ifndef LOCAL_LAN |
cyliang | 20:41c819860b4d | 53 | const char *HTTP_SERVER_NAME = "www.ifconfig.io"; |
cyliang | 0:2198c8de64fe | 54 | #else |
cyliang | 0:2198c8de64fe | 55 | const char *HTTP_SERVER_NAME = "pt22_winserver2.nuvoton.com"; |
cyliang | 0:2198c8de64fe | 56 | #endif |
cyliang | 0:2198c8de64fe | 57 | |
cyliang | 0:2198c8de64fe | 58 | #ifndef LOCAL_LAN |
cyliang | 20:41c819860b4d | 59 | const char *HTTP_SERVER_FILE_PATH = "/method"; |
cyliang | 0:2198c8de64fe | 60 | const int HTTP_SERVER_PORT = 80; |
cyliang | 0:2198c8de64fe | 61 | #else |
cyliang | 20:41c819860b4d | 62 | const char *HTTP_SERVER_FILE_PATH = "/examples/arm_mbed/method.txt"; |
cyliang | 0:2198c8de64fe | 63 | const int HTTP_SERVER_PORT = 8080; |
cyliang | 0:2198c8de64fe | 64 | #endif |
cyliang | 0:2198c8de64fe | 65 | |
cyliang | 0:2198c8de64fe | 66 | |
cyliang | 0:2198c8de64fe | 67 | const int RECV_BUFFER_SIZE = 512; |
cyliang | 0:2198c8de64fe | 68 | |
cyliang | 0:2198c8de64fe | 69 | // Test related data |
cyliang | 0:2198c8de64fe | 70 | const char *HTTP_OK_STR = "200 OK"; |
cyliang | 20:41c819860b4d | 71 | const char *HTTP_EXPECT_STR = "GET"; |
cyliang | 0:2198c8de64fe | 72 | |
cyliang | 0:2198c8de64fe | 73 | // Test buffers |
cyliang | 0:2198c8de64fe | 74 | char buffer[RECV_BUFFER_SIZE] = {0}; |
cyliang | 0:2198c8de64fe | 75 | } |
cyliang | 0:2198c8de64fe | 76 | |
cyliang | 0:2198c8de64fe | 77 | bool find_substring(const char *first, const char *last, const char *s_first, const char *s_last) { |
cyliang | 0:2198c8de64fe | 78 | const char *f = std::search(first, last, s_first, s_last); |
cyliang | 0:2198c8de64fe | 79 | return (f != last); |
cyliang | 0:2198c8de64fe | 80 | } |
cyliang | 0:2198c8de64fe | 81 | |
cyliang | 0:2198c8de64fe | 82 | int main() { |
ccli8 |
16:f705110e6c04 | 83 | #if MBED_CONF_APP_NETWORK_INTERFACE == WIFI |
ccli8 |
16:f705110e6c04 | 84 | # if ESP8266_AT_SEL == ESP8266_AT_ONBOARD |
ccli8 |
16:f705110e6c04 | 85 | # if TARGET_NUMAKER_IOT_M487 |
ccli8 |
16:f705110e6c04 | 86 | wait_ms(5); |
ccli8 |
16:f705110e6c04 | 87 | esp_rst = 1; // Simulate reset button released |
ccli8 |
16:f705110e6c04 | 88 | wait_ms(5); |
ccli8 |
16:f705110e6c04 | 89 | # elif TARGET_NUMAKER_PFM_M2351 |
ccli8 |
16:f705110e6c04 | 90 | wait_ms(50); |
ccli8 |
16:f705110e6c04 | 91 | esp_pwr_off = 0; // Turn on on-board ESP8266 |
ccli8 |
16:f705110e6c04 | 92 | wait_ms(50); |
ccli8 |
16:f705110e6c04 | 93 | # endif |
ccli8 |
16:f705110e6c04 | 94 | # endif |
ccli8 |
16:f705110e6c04 | 95 | #endif |
ccli8 |
16:f705110e6c04 | 96 | |
ccli8 |
15:32a6a29ffcb3 | 97 | #if MBED_HEAP_STATS_ENABLED |
ccli8 |
15:32a6a29ffcb3 | 98 | mbed_stats_heap_t heap_stats; |
ccli8 |
15:32a6a29ffcb3 | 99 | #endif |
ccli8 |
15:32a6a29ffcb3 | 100 | |
ccli8 |
19:79f168fa9a8a | 101 | printf("Start WiFi test \r\n"); |
cyliang | 0:2198c8de64fe | 102 | |
cyliang | 0:2198c8de64fe | 103 | bool result = true; |
ccli8 |
15:32a6a29ffcb3 | 104 | int rc = 0; |
cyliang | 0:2198c8de64fe | 105 | |
ccli8 |
19:79f168fa9a8a | 106 | printf("Start Connection ... \r\n"); |
cyliang | 0:2198c8de64fe | 107 | |
cyliang | 0:2198c8de64fe | 108 | |
cyliang | 0:2198c8de64fe | 109 | NetworkInterface *network_interface = 0; |
cyliang | 0:2198c8de64fe | 110 | |
cyliang | 0:2198c8de64fe | 111 | #if MBED_CONF_APP_NETWORK_INTERFACE == WIFI |
ccli8 |
19:79f168fa9a8a | 112 | printf("\n\rUsing WiFi \r\n"); |
ccli8 |
19:79f168fa9a8a | 113 | printf("\n\rConnecting to WiFi..\r\n"); |
cyliang | 13:be6ec74c5987 | 114 | rc = esp.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2); |
cyliang | 0:2198c8de64fe | 115 | network_interface = &esp; |
cyliang | 0:2198c8de64fe | 116 | #elif MBED_CONF_APP_NETWORK_INTERFACE == ETHERNET |
ccli8 |
19:79f168fa9a8a | 117 | printf("Using Ethernet\r\n"); |
cyliang | 0:2198c8de64fe | 118 | rc = eth.connect(); |
cyliang | 0:2198c8de64fe | 119 | network_interface = ð |
cyliang | 0:2198c8de64fe | 120 | #endif |
cyliang | 0:2198c8de64fe | 121 | #ifdef MESH |
ccli8 |
19:79f168fa9a8a | 122 | printf("Using Mesh\r\n"); |
ccli8 |
19:79f168fa9a8a | 123 | printf("\n\rConnecting to Mesh..\r\n"); |
cyliang | 0:2198c8de64fe | 124 | rc = mesh.connect(); |
cyliang | 0:2198c8de64fe | 125 | network_interface = &mesh; |
cyliang | 0:2198c8de64fe | 126 | #endif |
cyliang | 0:2198c8de64fe | 127 | |
cyliang | 0:2198c8de64fe | 128 | if(rc == 0) { |
ccli8 |
19:79f168fa9a8a | 129 | printf("\n\rConnected to Network successfully\r\n"); |
cyliang | 0:2198c8de64fe | 130 | } else { |
ccli8 |
19:79f168fa9a8a | 131 | printf("\n\rConnection to Network Failed %d! Exiting application....\r\n", rc); |
cyliang | 0:2198c8de64fe | 132 | return 0; |
cyliang | 0:2198c8de64fe | 133 | } |
cyliang | 0:2198c8de64fe | 134 | |
ccli8 |
19:79f168fa9a8a | 135 | printf("TCP client IP Address is %s\r\n", network_interface->get_ip_address()); |
cyliang | 0:2198c8de64fe | 136 | |
cyliang | 0:2198c8de64fe | 137 | TCPSocket sock(network_interface); |
ccli8 |
19:79f168fa9a8a | 138 | printf(" HTTP Connection ... \r\n"); |
cyliang | 0:2198c8de64fe | 139 | if (sock.connect(HTTP_SERVER_NAME, HTTP_SERVER_PORT) == 0) { |
ccli8 |
19:79f168fa9a8a | 140 | printf("HTTP: Connected to %s:%d\r\n", HTTP_SERVER_NAME, HTTP_SERVER_PORT); |
cyliang | 0:2198c8de64fe | 141 | |
cyliang | 0:2198c8de64fe | 142 | // We are constructing GET command like this: |
cyliang | 20:41c819860b4d | 143 | // GET http://www.ifconfig.io/method HTTP/1.0\n\n |
cyliang | 20:41c819860b4d | 144 | |
cyliang | 0:2198c8de64fe | 145 | strcpy(buffer, "GET http://"); |
cyliang | 0:2198c8de64fe | 146 | strcat(buffer, HTTP_SERVER_NAME); |
cyliang | 0:2198c8de64fe | 147 | strcat(buffer, HTTP_SERVER_FILE_PATH); |
cyliang | 0:2198c8de64fe | 148 | strcat(buffer, " HTTP/1.0\n\n"); |
cyliang | 20:41c819860b4d | 149 | |
cyliang | 0:2198c8de64fe | 150 | // Send GET command |
cyliang | 0:2198c8de64fe | 151 | sock.send(buffer, strlen(buffer)); |
cyliang | 0:2198c8de64fe | 152 | |
cyliang | 0:2198c8de64fe | 153 | // Server will respond with HTTP GET's success code |
cyliang | 0:2198c8de64fe | 154 | const int ret = sock.recv(buffer, sizeof(buffer) - 1); |
cyliang | 0:2198c8de64fe | 155 | buffer[ret] = '\0'; |
cyliang | 20:41c819860b4d | 156 | |
cyliang | 0:2198c8de64fe | 157 | // Find 200 OK HTTP status in reply |
cyliang | 0:2198c8de64fe | 158 | bool found_200_ok = find_substring(buffer, buffer + ret, HTTP_OK_STR, HTTP_OK_STR + strlen(HTTP_OK_STR)); |
cyliang | 20:41c819860b4d | 159 | // Find "deny" string in reply |
cyliang | 20:41c819860b4d | 160 | bool found_expect = find_substring(buffer, buffer + ret, HTTP_EXPECT_STR, HTTP_EXPECT_STR + strlen(HTTP_EXPECT_STR)); |
cyliang | 0:2198c8de64fe | 161 | |
cyliang | 0:2198c8de64fe | 162 | if (!found_200_ok) result = false; |
cyliang | 20:41c819860b4d | 163 | if (!found_expect) result = false; |
cyliang | 0:2198c8de64fe | 164 | |
ccli8 |
19:79f168fa9a8a | 165 | printf("HTTP: Received %d chars from server\r\n", ret); |
ccli8 |
19:79f168fa9a8a | 166 | printf("HTTP: Received 200 OK status ... %s\r\n", found_200_ok ? "[OK]" : "[FAIL]"); |
cyliang | 20:41c819860b4d | 167 | printf("HTTP: Received '%s' status ... %s\r\n", HTTP_EXPECT_STR, found_expect ? "[OK]" : "[FAIL]"); |
ccli8 |
19:79f168fa9a8a | 168 | printf("HTTP: Received massage:\r\n\r\n"); |
ccli8 |
19:79f168fa9a8a | 169 | printf("%s", buffer); |
cyliang | 0:2198c8de64fe | 170 | } |
cyliang | 0:2198c8de64fe | 171 | |
ccli8 |
15:32a6a29ffcb3 | 172 | #if MBED_HEAP_STATS_ENABLED |
ccli8 |
15:32a6a29ffcb3 | 173 | mbed_stats_heap_get(&heap_stats); |
ccli8 |
15:32a6a29ffcb3 | 174 | printf("Current heap: %lu\r\n", heap_stats.current_size); |
ccli8 |
15:32a6a29ffcb3 | 175 | printf("Max heap size: %lu\r\n", heap_stats.max_size); |
cyliang | 0:2198c8de64fe | 176 | #endif |
ccli8 |
15:32a6a29ffcb3 | 177 | |
ccli8 |
19:79f168fa9a8a | 178 | printf(" Close socket & disconnect ... \r\n"); |
cyliang | 0:2198c8de64fe | 179 | sock.close(); |
cyliang | 0:2198c8de64fe | 180 | |
cyliang | 0:2198c8de64fe | 181 | #if MBED_CONF_APP_NETWORK_INTERFACE == WIFI |
ccli8 |
15:32a6a29ffcb3 | 182 | esp.disconnect(); |
cyliang | 0:2198c8de64fe | 183 | #elif MBED_CONF_APP_NETWORK_INTERFACE == ETHERNET |
ccli8 |
15:32a6a29ffcb3 | 184 | eth.disconnect(); |
cyliang | 0:2198c8de64fe | 185 | #elif MBED_CONF_APP_NETWORK_INTERFACE == MESH_LOWPAN_ND |
ccli8 |
15:32a6a29ffcb3 | 186 | mesh.disconnect(); |
cyliang | 0:2198c8de64fe | 187 | #elif MBED_CONF_APP_NETWORK_INTERFACE == MESH_THREAD |
ccli8 |
15:32a6a29ffcb3 | 188 | mesh.disconnect(); |
cyliang | 0:2198c8de64fe | 189 | #endif |
ccli8 |
19:79f168fa9a8a | 190 | printf(" End \r\n"); |
cyliang | 0:2198c8de64fe | 191 | } |