TCP Echo Server Example using WizFi310
Dependencies: NetworkSocketAPI WizFi310Interface mbed
Prerequisite
This example shows that Wizwiki-W7500 and WizFi310 operates as a TCP server. When receiving data from a TCP client, it returns same data.
To implement this function, you need a Platform board and Wi-Fi board. Below are what we used.
- WIZwiki-W7500 from WIZnet (Platform board)
- WizFi310 from WIZnet (Wi-Fi board)
WIZwiki-W7500 Pin map
- D0 is for RXD, D1 is for TXD
- D6 is for CTS, D7 is for RTS
- D9 is for RESET
WizFi310 Pin map
- J1 is for RXD, J3 is for TXD
- SW6-1 is connected to D6 for RTS, SW6-2 is connected to D7 for CTS
- SW5-3 is connected to D9 for RESET
Software
Connect to Wi-Fi
wifi.connect(AP_SSID, AP_PASSWORD, AP_SECURITY);
Get information
const char *ip = wifi.get_ip_address(); const char *mac = wifi.get_mac_address();
Receive data and return to TCP client
while (true) { int n = clt_sock.recv(buffer, sizeof(buffer)); if( n < 0 ) { clt_sock.close(); srv.close(); } if( n > 0 ) { buffer[n] = '\0'; printf("length : %d\r\n", n); clt_sock.send(buffer, n); } }
Caution
TCP client and server should be in a same network.
main.cpp@1:5981ba4a8323, 2017-04-18 (annotated)
- Committer:
- stkim92
- Date:
- Tue Apr 18 00:57:35 2017 +0000
- Revision:
- 1:5981ba4a8323
- Parent:
- 0:50932ef4b276
revise Code description (TCP client -> server)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kaizen | 0:50932ef4b276 | 1 | /* NetworkSocketAPI Example Program |
kaizen | 0:50932ef4b276 | 2 | * Copyright (c) 2015 ARM Limited |
kaizen | 0:50932ef4b276 | 3 | * |
kaizen | 0:50932ef4b276 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
kaizen | 0:50932ef4b276 | 5 | * you may not use this file except in compliance with the License. |
kaizen | 0:50932ef4b276 | 6 | * You may obtain a copy of the License at |
kaizen | 0:50932ef4b276 | 7 | * |
kaizen | 0:50932ef4b276 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
kaizen | 0:50932ef4b276 | 9 | * |
kaizen | 0:50932ef4b276 | 10 | * Unless required by applicable law or agreed to in writing, software |
kaizen | 0:50932ef4b276 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
kaizen | 0:50932ef4b276 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
kaizen | 0:50932ef4b276 | 13 | * See the License for the specific language governing permissions and |
kaizen | 0:50932ef4b276 | 14 | * limitations under the License. |
kaizen | 0:50932ef4b276 | 15 | */ |
kaizen | 0:50932ef4b276 | 16 | |
kaizen | 0:50932ef4b276 | 17 | #include "mbed.h" |
kaizen | 0:50932ef4b276 | 18 | #include "TCPSocket.h" |
kaizen | 0:50932ef4b276 | 19 | #include "mbed.h" |
kaizen | 0:50932ef4b276 | 20 | #include "WizFi310Interface.h" |
kaizen | 0:50932ef4b276 | 21 | #include "TCPSocket.h" |
kaizen | 0:50932ef4b276 | 22 | #include "TCPServer.h" |
kaizen | 0:50932ef4b276 | 23 | |
kaizen | 0:50932ef4b276 | 24 | #define TARGET_NUCLEO_F411RE |
kaizen | 0:50932ef4b276 | 25 | |
kaizen | 0:50932ef4b276 | 26 | |
kaizen | 0:50932ef4b276 | 27 | #if defined(TARGET_NUCLEO_F411RE) |
stkim92 | 1:5981ba4a8323 | 28 | //Serial pc(USBTX, USBRX); |
kaizen | 0:50932ef4b276 | 29 | //WizFi310Internet wifi(PA_11, PA_12, D6, D7, D3, NC, 115200); |
stkim92 | 1:5981ba4a8323 | 30 | //WizFi310Interface wifi(D8, D2, D7, D6, D9, NC, 115200); |
kaizen | 0:50932ef4b276 | 31 | #endif |
kaizen | 0:50932ef4b276 | 32 | |
kaizen | 0:50932ef4b276 | 33 | #if defined(TARGET_WIZwiki_W7500) |
kaizen | 0:50932ef4b276 | 34 | Serial pc(USBTX, USBRX); |
kaizen | 0:50932ef4b276 | 35 | WizFi310Interface wifi(D1, D0, D7, D6, D8, NC, 115200); |
kaizen | 0:50932ef4b276 | 36 | #endif |
kaizen | 0:50932ef4b276 | 37 | |
stkim92 | 1:5981ba4a8323 | 38 | #define AP_SSID "wizms1" |
stkim92 | 1:5981ba4a8323 | 39 | #define AP_PASSWORD "maker0701" |
kaizen | 0:50932ef4b276 | 40 | #define AP_SECURITY NSAPI_SECURITY_WPA2 |
kaizen | 0:50932ef4b276 | 41 | |
kaizen | 0:50932ef4b276 | 42 | int main() |
kaizen | 0:50932ef4b276 | 43 | { |
kaizen | 0:50932ef4b276 | 44 | pc.baud(115200); |
stkim92 | 1:5981ba4a8323 | 45 | printf("WizFi310 NetworkSocketAPI TCP Server Example\r\n"); |
kaizen | 0:50932ef4b276 | 46 | |
kaizen | 0:50932ef4b276 | 47 | wifi.connect(AP_SSID, AP_PASSWORD, AP_SECURITY); |
kaizen | 0:50932ef4b276 | 48 | |
kaizen | 0:50932ef4b276 | 49 | const char *ip = wifi.get_ip_address(); |
kaizen | 0:50932ef4b276 | 50 | const char *mac = wifi.get_mac_address(); |
kaizen | 0:50932ef4b276 | 51 | printf("IP address is: %s\r\n", ip ? ip : "No IP"); |
kaizen | 0:50932ef4b276 | 52 | printf("MAC address is: %s\r\n", mac ? mac : "No MAC"); |
kaizen | 0:50932ef4b276 | 53 | |
kaizen | 0:50932ef4b276 | 54 | TCPServer srv; |
kaizen | 0:50932ef4b276 | 55 | TCPSocket clt_sock; |
kaizen | 0:50932ef4b276 | 56 | SocketAddress clt_addr; |
kaizen | 0:50932ef4b276 | 57 | |
kaizen | 0:50932ef4b276 | 58 | srv.open(&wifi); |
kaizen | 0:50932ef4b276 | 59 | srv.set_blocking(true); |
kaizen | 0:50932ef4b276 | 60 | srv.bind(80); |
kaizen | 0:50932ef4b276 | 61 | srv.listen(); |
kaizen | 0:50932ef4b276 | 62 | srv.accept(&clt_sock); |
kaizen | 0:50932ef4b276 | 63 | |
kaizen | 0:50932ef4b276 | 64 | char buffer[512]; |
kaizen | 0:50932ef4b276 | 65 | |
kaizen | 0:50932ef4b276 | 66 | while (true) |
kaizen | 0:50932ef4b276 | 67 | { |
kaizen | 0:50932ef4b276 | 68 | int n = clt_sock.recv(buffer, sizeof(buffer)); |
kaizen | 0:50932ef4b276 | 69 | if( n < 0 ) |
kaizen | 0:50932ef4b276 | 70 | { |
kaizen | 0:50932ef4b276 | 71 | clt_sock.close(); |
kaizen | 0:50932ef4b276 | 72 | srv.close(); |
kaizen | 0:50932ef4b276 | 73 | } |
kaizen | 0:50932ef4b276 | 74 | if( n > 0 ) |
kaizen | 0:50932ef4b276 | 75 | { |
kaizen | 0:50932ef4b276 | 76 | buffer[n] = '\0'; |
kaizen | 0:50932ef4b276 | 77 | printf("length : %d\r\n", n); |
kaizen | 0:50932ef4b276 | 78 | clt_sock.send(buffer, n); |
kaizen | 0:50932ef4b276 | 79 | } |
kaizen | 0:50932ef4b276 | 80 | } |
kaizen | 0:50932ef4b276 | 81 | } |