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
- Committer:
- stkim92
- Date:
- 2017-04-18
- Revision:
- 1:5981ba4a8323
- Parent:
- 0:50932ef4b276
File content as of revision 1:5981ba4a8323:
/* NetworkSocketAPI Example Program * Copyright (c) 2015 ARM Limited * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "mbed.h" #include "TCPSocket.h" #include "mbed.h" #include "WizFi310Interface.h" #include "TCPSocket.h" #include "TCPServer.h" #define TARGET_NUCLEO_F411RE #if defined(TARGET_NUCLEO_F411RE) //Serial pc(USBTX, USBRX); //WizFi310Internet wifi(PA_11, PA_12, D6, D7, D3, NC, 115200); //WizFi310Interface wifi(D8, D2, D7, D6, D9, NC, 115200); #endif #if defined(TARGET_WIZwiki_W7500) Serial pc(USBTX, USBRX); WizFi310Interface wifi(D1, D0, D7, D6, D8, NC, 115200); #endif #define AP_SSID "wizms1" #define AP_PASSWORD "maker0701" #define AP_SECURITY NSAPI_SECURITY_WPA2 int main() { pc.baud(115200); printf("WizFi310 NetworkSocketAPI TCP Server Example\r\n"); wifi.connect(AP_SSID, AP_PASSWORD, AP_SECURITY); const char *ip = wifi.get_ip_address(); const char *mac = wifi.get_mac_address(); printf("IP address is: %s\r\n", ip ? ip : "No IP"); printf("MAC address is: %s\r\n", mac ? mac : "No MAC"); TCPServer srv; TCPSocket clt_sock; SocketAddress clt_addr; srv.open(&wifi); srv.set_blocking(true); srv.bind(80); srv.listen(); srv.accept(&clt_sock); char buffer[512]; 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); } } }