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  if(result == WICONNECT_FIRMWARE_OUTDATED)
120  {
121  printf("The WiFi firmware is not supported. Run the ota example to update the firmware:\r\n");
122  printf("https://developer.mbed.org/teams/ACKme/code/wiconnect-ota_example");
123  }
124  for(;;); // infinite loop
125  }
126 
127  //-------------------------------------------------------------------------
128  // STEP 3: Join the network
129  //-------------------------------------------------------------------------
130 
131  printf("Joining WiFi network: %s\r\n", NETWORK_SSID);
132 
133  // Initialize communication with WiFi module
134  if(WICONNECT_FAILED(result, wiconnectIfc.join(NETWORK_SSID, NETWORK_PASSWORD)))
135  {
136  printf("Failed to join network: %s\r\n", Wiconnect::getWiconnectResultStr(result));
137  for(;;); // infinite loop
138  }
139 
140 
141  //-------------------------------------------------------------------------
142  // STEP 4: Start the TCP server
143  //-------------------------------------------------------------------------
144 
145  printf("Starting TCP server, listening on: %s:%d\r\n", wiconnectIfc.getIpAddress(), TCP_SERVER_PORT);
146 
147  if(WICONNECT_FAILED(result, wiconnectIfc.tcpListen(TCP_SERVER_PORT, TCP_SERVER_MAX_CLIENTS)))
148  {
149  printf("Failed to start TCP server: %s\r\n", Wiconnect::getWiconnectResultStr(result));
150  for(;;); // infinite loop
151  }
152 
153 
154  for(;;)
155  {
156  //-------------------------------------------------------------------------
157  // STEP 5: Wait for clients to connect
158  //-------------------------------------------------------------------------
159 
160  printf("Waiting for a client to connect...\r\n");
161 
162  if(WICONNECT_FAILED(result, wiconnectIfc.tcpAccept(clientSocket)))
163  {
164  printf("Failed to accept client: %s\r\n", Wiconnect::getWiconnectResultStr(result));
165  continue;
166  }
167  printf("Client connected: %s:%d\r\n", clientSocket.getHost(), clientSocket.getRemotePort());
168 
169  //-------------------------------------------------------------------------
170  // STEP 6: Receive data from client
171  //-------------------------------------------------------------------------
172 
173  uint8_t *dataPtr; // pointer to client socket's internal RX buffer
174  uint16_t readSize; // will contain number of bytes available in RX buffer
175  if(WICONNECT_FAILED(result, clientSocket.read(&dataPtr, &readSize)))
176  {
177  printf("Failed to read data from client: %s\r\n", Wiconnect::getWiconnectResultStr(result));
178  clientSocket.close();
179  continue;
180  }
181 
182  printf("From client: %s\r\n", dataPtr);
183 
184 
185  //-------------------------------------------------------------------------
186  // STEP 7: Send data to client
187  //-------------------------------------------------------------------------
188 
189  if(WICONNECT_FAILED(result, clientSocket.puts("Hello client!!\r\n")))
190  {
191  printf("Failed to send data to client: %s\r\n", Wiconnect::getWiconnectResultStr(result));
192  clientSocket.close();
193  continue;
194  }
195 
196  //-------------------------------------------------------------------------
197  // STEP 8: Close client connection
198  //-------------------------------------------------------------------------
199 
200  clientSocket.close();
201  }
202 
203 }
204 
WiconnectResult
API Result code.
Host<->Wiconnect Module serial configuration.
Definition: sdk.h:148
WiconnectResult tcpListen(uint16_t listeningPort, int maxClients=0)
Start internal TCP server and listen on specified port.
The WiFi module's firmware is out-dated. See updateFirmware() to update the firmware.
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, uint32_t 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.
#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.