ACKme Logo WiConnect Host Library- API Reference Guide
 
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Groups Pages
main.cpp
1 
2 #include "Wiconnect.h"
3 #include "target_config.h"
4 
5 #define NETWORK_SSID "<YOUR NETWORK SSID HERE>"
6 #define NETWORK_PASSWORD "<YOUR NETWORK PASSWORD HERE>"
7 
8 #define ECHO_SERVER_ADDRESS "<YOUR LOCAL IP ADDRESS HERE>"
9 #define ECHO_SERVER_PORT 7
10 
11 
12 static Serial consoleSerial(STDIO_UART_TX, STDIO_UART_RX);
13 static char buf[256];
14 
15 
16 int main()
17 {
18  WiconnectResult result;
19  SerialConfig serialConfig(WICONNECT_RX_PIN, WICONNECT_TX_PIN, 256);
20  Wiconnect wiconnect(serialConfig, 256, NULL, WICONNECT_RESET_PIN);
21 
22  consoleSerial.baud(115200);
23 
24  if(WICONNECT_FAILED(result, wiconnect.init(true)))
25  {
26  printf("Failed to initialize Wiconnect: %s\r\n", Wiconnect::getWiconnectResultStr(result));
27  if(result == WICONNECT_FIRMWARE_OUTDATED)
28  {
29  printf("The WiFi firmware is not supported. Run the ota example to update the firmware:\r\n");
30  printf("https://developer.mbed.org/teams/ACKme/code/wiconnect-ota_example");
31  }
32  for(;;);
33  }
34  else if(WICONNECT_FAILED(result, wiconnect.join(NETWORK_SSID, NETWORK_PASSWORD)))
35  {
36  printf("Failed to join network: %s\r\n", Wiconnect::getWiconnectResultStr(result));
37  for(;;);
38  }
39 
40  printf("IP Address is %s\n", wiconnect.getIpAddress());
41 
42  TCPSocketConnection socket;
43  while (socket.connect(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT) < 0)
44  {
45  printf("Unable to connect to (%s) on port (%d)\r\n", ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT);
46  wait(1);
47  }
48 
49  char hello[] = "Hello World\n";
50  if(socket.send_all(hello, sizeof(hello) - 1) == -1)
51  {
52  printf("Failed to send data\r\n");
53  for(;;);
54  }
55 
56  int n = socket.receive(buf, 256);
57  if(n == -1)
58  {
59  printf("Failed to receive data\r\n");
60  for(;;);
61  }
62  buf[n] = '\0';
63  printf("%s", buf);
64 
65  socket.close();
66  wiconnect.deinit();
67 
68  printf("Finished!");
69  while(true) {}
70 }
WiconnectResult
API Result code.
Host<->Wiconnect Module serial configuration.
Definition: sdk.h:148
The WiFi module's firmware is out-dated. See updateFirmware() to update the firmware.
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...