
Example application demonstrating the TCP server API of the WiConnect Host Library
Revision 0:19d3a6600a5a, committed 2014-08-26
- Comitter:
- dan_ackme
- Date:
- Tue Aug 26 23:07:17 2014 +0000
- Child:
- 1:7d0cf8716c2c
- Commit message:
- Initial check-in
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WiConnect.lib Tue Aug 26 23:07:17 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/teams/ACKme/code/WiConnect/#7b67c3f94de9
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/example.cpp Tue Aug 26 23:07:17 2014 +0000 @@ -0,0 +1,200 @@ +/** + * @example tcp_server/example.cpp + * + * This is an example of using the TCP server API to + * send/receive data from a remote TCP client. + * + * This example is intended to be used with 'tcp_client.py' + * python script in the same directory as this example.cpp file. + * + * It works as follows: + * 1. Instantiate the WiConnect Library + * 2. Initiate Communication with WiFi Module + * 3. Join the network + * 4. Start the TCP server + * 5. Wait for clients to connect + * 6. Receive data from client + * 7. Send data to client + * 8. Close client connection + * 9. Goto sleep 5 + * + */ + + +/****************************************************************************** + * Example Variables + */ + +// The port the server listens on +#define TCP_SERVER_PORT 7 +// The maximum simultaneous client connections +// (note this example only supports 1) +#define TCP_SERVER_MAX_CLIENTS 1 + +// This is the name of your WiFi network +// Look for this name in your WiFi settings +// (e.g. your phone's list of WiFi networks in the WiFi settings menu) +// tip: add double-quotes around SSID to add spaces to name +#define NETWORK_SSID "\"<YOUR NETWORK NAME HERE>\"" + +// This is the password of your WiFi network +// Leave as empty string (e.g "") to connect to OPEN network +#define NETWORK_PASSWORD "\"<YOUR NETWORK PASSWORD HERE>\"" + + + +/****************************************************************************** + * Includes + */ + +// include C library headers +#include <stdio.h> // needed for printf + +// include target specific defines +#include "target_config.h" +// include the Wiconnect Host Library API header +#include "Wiconnect.h" + + + +/****************************************************************************** + * Local Functions + */ + + +/****************************************************************************** + * Global Defines + */ + +// Transmit/receive buffers for the remote client socket +static uint8_t clientRxBuffer[256], clientTxBuffer[256]; + +// Serial used for printfs to terminal (i.e. NOT used for WiConnect) +static Serial consoleSerial(STDIO_UART_TX, STDIO_UART_RX); + + + +//------------------------------------------------------------------------- +// STEP 1: Instantiate WiConnect Library +//------------------------------------------------------------------------- + + +// Setup wiconnect serial interface configuration +// Here we only specify the rx buffer size and not rx buffer pointer, this means +// The serial RX buffer will be dynamically allocated +SerialConfig serialConfig(WICONNECT_RX_PIN, WICONNECT_TX_PIN, 256, NULL); + +// Instantiate the Wiconnect library +// Here we specify the buffer size ONLY which means we're using dynmaic allocation +Wiconnect wiconnectIfc(serialConfig, 256, NULL, WICONNECT_RESET_PIN); + + + + + +/****************************************************************************** + * Starting point of application + */ +int main(int argc, char **argv) +{ + WiconnectResult result; + // Instantiate a client socket object with statically allocaed transmit/receive buffers + // Note: this socket object isn't valid until tcpAccept() is called with in + WiconnectSocket clientSocket(sizeof(clientRxBuffer), clientRxBuffer, sizeof(clientTxBuffer), clientTxBuffer); + + consoleSerial.baud(115200); // console terminal to 115200 baud + + + //------------------------------------------------------------------------- + // STEP 2: Initiate Communication with WiFi Module + //------------------------------------------------------------------------- + + printf("Initializing WiConnect Library...\r\n"); + + // Initialize communication with WiFi module + if(WICONNECT_FAILED(result, wiconnectIfc.init(true))) + { + printf("Failed to initialize communication with WiFi module: %s\r\n" + "Make sure the wires are connected correctly\r\n", Wiconnect::getWiconnectResultStr(result)); + for(;;); // infinite loop + } + + //------------------------------------------------------------------------- + // STEP 3: Join the network + //------------------------------------------------------------------------- + + printf("Joining WiFi network: %s\r\n", NETWORK_SSID); + + // Initialize communication with WiFi module + if(WICONNECT_FAILED(result, wiconnectIfc.join(NETWORK_SSID, NETWORK_PASSWORD))) + { + printf("Failed to join network: %s\r\n", Wiconnect::getWiconnectResultStr(result)); + for(;;); // infinite loop + } + + + //------------------------------------------------------------------------- + // STEP 4: Start the TCP server + //------------------------------------------------------------------------- + + printf("Starting TCP server, listening on: %s:%d\r\n", wiconnectIfc.getIpAddress(), TCP_SERVER_PORT); + + if(WICONNECT_FAILED(result, wiconnectIfc.tcpListen(TCP_SERVER_PORT, TCP_SERVER_MAX_CLIENTS))) + { + printf("Failed to start TCP server: %s\r\n", Wiconnect::getWiconnectResultStr(result)); + for(;;); // infinite loop + } + + + for(;;) + { + //------------------------------------------------------------------------- + // STEP 5: Wait for clients to connect + //------------------------------------------------------------------------- + + printf("Waiting for a client to connect...\r\n"); + + if(WICONNECT_FAILED(result, wiconnectIfc.tcpAccept(clientSocket))) + { + printf("Failed to accept client: %s\r\n", Wiconnect::getWiconnectResultStr(result)); + continue; + } + printf("Client connected: %s:%d\r\n", clientSocket.getHost(), clientSocket.getRemotePort()); + + //------------------------------------------------------------------------- + // STEP 6: Receive data from client + //------------------------------------------------------------------------- + + uint8_t *dataPtr; // pointer to client socket's internal RX buffer + uint16_t readSize; // will contain number of bytes available in RX buffer + if(WICONNECT_FAILED(result, clientSocket.read(&dataPtr, &readSize))) + { + printf("Failed to read data from client: %s\r\n", Wiconnect::getWiconnectResultStr(result)); + clientSocket.close(); + continue; + } + + printf("From client: %s\r\n", dataPtr); + + + //------------------------------------------------------------------------- + // STEP 7: Send data to client + //------------------------------------------------------------------------- + + if(WICONNECT_FAILED(result, clientSocket.puts("Hello client!!\r\n"))) + { + printf("Failed to send data to client: %s\r\n", Wiconnect::getWiconnectResultStr(result)); + clientSocket.close(); + continue; + } + + //------------------------------------------------------------------------- + // STEP 8: Close client connection + //------------------------------------------------------------------------- + + clientSocket.close(); + } + +} + +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Tue Aug 26 23:07:17 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/9327015d4013 \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/target_config.h Tue Aug 26 23:07:17 2014 +0000 @@ -0,0 +1,93 @@ +/** + * ACKme WiConnect Host Library is licensed under the BSD licence: + * + * Copyright (c)2014 ACKme Networks. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT + * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT + * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING + * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY + * OF SUCH DAMAGE. + */ +#pragma once + + +// The BAUD rate your PC/MAC/Linux terminal uses with the eval board +#define CONSOLE_BAUD 115200 + + +// Uncomment this to enable WiConnect serial interface hardware flow control +// NOTE: your platform must support the serial flow control api functions +//#define ENABLE_FLOW_CONTROL + + +#define WICONNECT_INTERNAL_BUFFER_SIZE (4*1024) +#define WICONNECT_SERIAL_RX_BUFFER_SIZE (4*1024) + +#define DEFAULT_CMD_GETCHAR_TIMEOUT 250 +#define DEFAULT_COMMAND_LINE_LENGTH_MAX 128 +#define DEFAULT_COMMAND_MAX_HISTORY 16 +#define DEFAULT_CMD_PROMPT_STR "> " +#define DEFAULT_COMMAND_MAX_ARGV 16 + +#define TEST_NONBLOCKING_API false +#define TEST_BUFFER_LENGTH 4*1024 + + +//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +// Seabass Target Configuration +#ifdef TARGET_SEABASS + +#define WICONNECT_TX_PIN PA_9 +#define WICONNECT_RX_PIN PA_10 +#define WICONNECT_RESET_PIN PB_0 +#define WICONNECT_WAKE_PIN NC + +#ifdef ENABLE_FLOW_CONTROL +#define WICONNECT_CTS_PIN PA_11 +#define WICONNECT_RTS_PIN PA_12 +#else +#define WICONNECT_CTS_PIN NC +#define WICONNECT_RTS_PIN NC +#endif + +#endif + + +//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +// Nucleo F401RE Target Configuration +#ifdef TARGET_NUCLEO_F401RE + +#define WICONNECT_TX_PIN PA_9 +#define WICONNECT_RX_PIN PA_10 +#define WICONNECT_RESET_PIN PC_7 +#define WICONNECT_WAKE_PIN NC + +#ifdef ENABLE_FLOW_CONTROL +#define WICONNECT_CTS_PIN PA_11 +#define WICONNECT_RTS_PIN PA_12 +#else +#define WICONNECT_CTS_PIN NC +#define WICONNECT_RTS_PIN NC +#endif + +#endif + +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tcp_client.py Tue Aug 26 23:07:17 2014 +0000 @@ -0,0 +1,12 @@ +import socket + +ECHO_SERVER_ADDRESS = "192.168.1.52" +ECHO_PORT = 7 + +s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) +s.connect((ECHO_SERVER_ADDRESS, ECHO_PORT)) + +s.sendall('Hello, world') +data = s.recv(1024) +print 'Received', repr(data) +s.close()