This is an example of using the TCP server API to send/receive data from a remote TCP client.
#define TCP_SERVER_PORT 7
#define TCP_SERVER_MAX_CLIENTS 1
#define NETWORK_SSID "\"<YOUR NETWORK NAME HERE>\""
#define NETWORK_PASSWORD "\"<YOUR NETWORK PASSWORD HERE>\""
#include <stdio.h>
#include "target_config.h"
#include "Wiconnect.h"
static uint8_t clientRxBuffer[256], clientTxBuffer[256];
static Serial consoleSerial(STDIO_UART_TX, STDIO_UART_RX);
SerialConfig serialConfig(WICONNECT_RX_PIN, WICONNECT_TX_PIN, 256, NULL);
Wiconnect wiconnectIfc(serialConfig, 256, NULL, WICONNECT_RESET_PIN);
int main(int argc, char **argv)
{
WiconnectSocket clientSocket(sizeof(clientRxBuffer), clientRxBuffer, sizeof(clientTxBuffer), clientTxBuffer);
consoleSerial.baud(115200);
printf("Initializing WiConnect Library...\r\n");
{
printf("Failed to initialize communication with WiFi module: %s\r\n"
"Make sure the wires are connected correctly\r\n", Wiconnect::getWiconnectResultStr(result));
for(;;);
}
printf("Joining WiFi network: %s\r\n", NETWORK_SSID);
{
printf("Failed to join network: %s\r\n", Wiconnect::getWiconnectResultStr(result));
for(;;);
}
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(;;);
}
for(;;)
{
printf("Waiting for a client to connect...\r\n");
{
printf("Failed to accept client: %s\r\n", Wiconnect::getWiconnectResultStr(result));
continue;
}
printf("Client connected: %s:%d\r\n", clientSocket.getHost(), clientSocket.getRemotePort());
uint8_t *dataPtr;
uint16_t readSize;
{
printf("Failed to read data from client: %s\r\n", Wiconnect::getWiconnectResultStr(result));
clientSocket.close();
continue;
}
printf("From client: %s\r\n", dataPtr);
{
printf("Failed to send data to client: %s\r\n", Wiconnect::getWiconnectResultStr(result));
clientSocket.close();
continue;
}
clientSocket.close();
}
}