ACKme Logo WiConnect Host Library- API Reference Guide
 
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Groups Pages
example.cpp
1 
24 /******************************************************************************
25  * Example Variables
26  */
27 
28 // The port the server listens on
29 #define TCP_SERVER_PORT 7
30 // The maximum simultaneous client connections
31 // (note this example only supports 1)
32 #define TCP_SERVER_MAX_CLIENTS 1
33 
34 // This is the name of your WiFi network
35 // Look for this name in your WiFi settings
36 // (e.g. your phone's list of WiFi networks in the WiFi settings menu)
37 // tip: add double-quotes around SSID to add spaces to name
38 #define NETWORK_SSID "\"<YOUR NETWORK NAME HERE>\""
39 
40 // This is the password of your WiFi network
41 // Leave as empty string (e.g "") to connect to OPEN network
42 #define NETWORK_PASSWORD "\"<YOUR NETWORK PASSWORD HERE>\""
43 
44 
45 
46 /******************************************************************************
47  * Includes
48  */
49 
50 // include C library headers
51 #include <stdio.h> // needed for printf
52 
53 // include target specific defines
54 #include "target_config.h"
55 // include the Wiconnect Host Library API header
56 #include "Wiconnect.h"
57 
58 
59 
60 /******************************************************************************
61  * Local Functions
62  */
63 
64 
65 /******************************************************************************
66  * Global Defines
67  */
68 
69 // Transmit/receive buffers for the remote client socket
70 static uint8_t clientRxBuffer[256], clientTxBuffer[256];
71 
72 // Serial used for printfs to terminal (i.e. NOT used for WiConnect)
73 static Serial consoleSerial(STDIO_UART_TX, STDIO_UART_RX);
74 
75 
76 
77 //-------------------------------------------------------------------------
78 // STEP 1: Instantiate WiConnect Library
79 //-------------------------------------------------------------------------
80 
81 
82 // Setup wiconnect serial interface configuration
83 // Here we only specify the rx buffer size and not rx buffer pointer, this means
84 // The serial RX buffer will be dynamically allocated
85 SerialConfig serialConfig(WICONNECT_RX_PIN, WICONNECT_TX_PIN, 256, NULL);
86 
87 // Instantiate the Wiconnect library
88 // Here we specify the buffer size ONLY which means we're using dynmaic allocation
89 Wiconnect wiconnectIfc(serialConfig, 256, NULL, WICONNECT_RESET_PIN);
90 
91 
92 
93 
94 
95 /******************************************************************************
96  * Starting point of application
97  */
98 int main(int argc, char **argv)
99 {
100  WiconnectResult result;
101  // Instantiate a client socket object with statically allocaed transmit/receive buffers
102  // Note: this socket object isn't valid until tcpAccept() is called with in
103  WiconnectSocket clientSocket(sizeof(clientRxBuffer), clientRxBuffer, sizeof(clientTxBuffer), clientTxBuffer);
104 
105  consoleSerial.baud(115200); // console terminal to 115200 baud
106 
107 
108  //-------------------------------------------------------------------------
109  // STEP 2: Initiate Communication with WiFi Module
110  //-------------------------------------------------------------------------
111 
112  printf("Initializing WiConnect Library...\r\n");
113 
114  // Initialize communication with WiFi module
115  if(WICONNECT_FAILED(result, wiconnectIfc.init(true)))
116  {
117  printf("Failed to initialize communication with WiFi module: %s\r\n"
118  "Make sure the wires are connected correctly\r\n", Wiconnect::getWiconnectResultStr(result));
119  for(;;); // infinite loop
120  }
121 
122  //-------------------------------------------------------------------------
123  // STEP 3: Join the network
124  //-------------------------------------------------------------------------
125 
126  printf("Joining WiFi network: %s\r\n", NETWORK_SSID);
127 
128  // Initialize communication with WiFi module
129  if(WICONNECT_FAILED(result, wiconnectIfc.join(NETWORK_SSID, NETWORK_PASSWORD)))
130  {
131  printf("Failed to join network: %s\r\n", Wiconnect::getWiconnectResultStr(result));
132  for(;;); // infinite loop
133  }
134 
135 
136  //-------------------------------------------------------------------------
137  // STEP 4: Start the TCP server
138  //-------------------------------------------------------------------------
139 
140  printf("Starting TCP server, listening on: %s:%d\r\n", wiconnectIfc.getIpAddress(), TCP_SERVER_PORT);
141 
142  if(WICONNECT_FAILED(result, wiconnectIfc.tcpListen(TCP_SERVER_PORT, TCP_SERVER_MAX_CLIENTS)))
143  {
144  printf("Failed to start TCP server: %s\r\n", Wiconnect::getWiconnectResultStr(result));
145  for(;;); // infinite loop
146  }
147 
148 
149  for(;;)
150  {
151  //-------------------------------------------------------------------------
152  // STEP 5: Wait for clients to connect
153  //-------------------------------------------------------------------------
154 
155  printf("Waiting for a client to connect...\r\n");
156 
157  if(WICONNECT_FAILED(result, wiconnectIfc.tcpAccept(clientSocket)))
158  {
159  printf("Failed to accept client: %s\r\n", Wiconnect::getWiconnectResultStr(result));
160  continue;
161  }
162  printf("Client connected: %s:%d\r\n", clientSocket.getHost(), clientSocket.getRemotePort());
163 
164  //-------------------------------------------------------------------------
165  // STEP 6: Receive data from client
166  //-------------------------------------------------------------------------
167 
168  uint8_t *dataPtr; // pointer to client socket's internal RX buffer
169  uint16_t readSize; // will contain number of bytes available in RX buffer
170  if(WICONNECT_FAILED(result, clientSocket.read(&dataPtr, &readSize)))
171  {
172  printf("Failed to read data from client: %s\r\n", Wiconnect::getWiconnectResultStr(result));
173  clientSocket.close();
174  continue;
175  }
176 
177  printf("From client: %s\r\n", dataPtr);
178 
179 
180  //-------------------------------------------------------------------------
181  // STEP 7: Send data to client
182  //-------------------------------------------------------------------------
183 
184  if(WICONNECT_FAILED(result, clientSocket.puts("Hello client!!\r\n")))
185  {
186  printf("Failed to send data to client: %s\r\n", Wiconnect::getWiconnectResultStr(result));
187  clientSocket.close();
188  continue;
189  }
190 
191  //-------------------------------------------------------------------------
192  // STEP 8: Close client connection
193  //-------------------------------------------------------------------------
194 
195  clientSocket.close();
196  }
197 
198 }
199 
WiconnectResult
API Result code.
Host<->Wiconnect Module serial configuration.
Definition: sdk.h:129
const char * getIpAddress(char *buffer=NULL)
Return the current IP address of the module if possible, else return 0.0.0.0.
WiconnectResult init(bool bringNetworkUp=false)
Initialize library and communication link with WiConnect WiFi module.
WiconnectResult tcpAccept(WiconnectSocket &socket, int timeoutMs=WICONNECT_WAIT_FOREVER)
Wait for next client to connect to TCP server.
Connection object to remote server.
The root WiConnect library class. This class inheriets all WiConnect functionality.
WiconnectResult tcpListen(uint16_t listeningPort, int maxClients=0, Pin irqPin=PIN_NC)
Start internal TCP server and listen on specified port.
#define WICONNECT_FAILED(result, func)
Populates result with return value from func, returns TRUE if return value contains error...
WiconnectResult join(const char *ssid=NULL, const char *password=NULL, const Callback &completeHandler=Callback())
Join a WiFi network.