ACKme Logo WiConnect Host Library- API Reference Guide
 
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Groups Pages
example.cpp
1 
17 /******************************************************************************
18  * Example Variables
19  */
20 
21 // the URL of the webpage to GET
22 #define HTTP_URL "http://ack.me"
23 
24 
25 
26 
27 
28 /******************************************************************************
29  * Includes
30  */
31 
32 // include C library headers
33 #include <stdio.h> // needed for printf
34 
35 // include target specific defines
36 #include "target_config.h"
37 // include the Wiconnect Host Library API header
38 #include "Wiconnect.h"
39 
40 
41 
42 
43 /******************************************************************************
44  * Global Defines
45  */
46 
47 
48 // Serial used for printfs to terminal (i.e. NOT used for WiConnect)
49 static Serial consoleSerial(STDIO_UART_TX, STDIO_UART_RX);
50 
51 
52 
53 /******************************************************************************
54  * Starting point of application
55  */
56 int main(int argc, char **argv)
57 {
58  consoleSerial.baud(115200); // console terminal to 115200 baud
59 
60  //-------------------------------------------------------------------------
61  // STEP 1: Instantiate WiConnect Library
62  //-------------------------------------------------------------------------
63 
64  // Setup wiconnect serial interface configuration
65  // Here we only specify the rx buffer size and not rx buffer pointer, this means
66  // The serial RX buffer will be dynamically allocated
67  SerialConfig serialConfig(WICONNECT_RX_PIN, WICONNECT_TX_PIN, 256, NULL);
68 
69  // Instantiate and initialize the Wiconnect library
70  // Here we only specify the buffer size and not buffer pointer, this means
71  // The internal buffer will be dynamically allocated
72  Wiconnect wiconnect(serialConfig, 256, NULL, WICONNECT_RESET_PIN);
73 
74  // set the default timeout to 15s as some websites take awhile to respond
75  wiconnect.setCommandDefaultTimeout(15000);
76 
77  //-------------------------------------------------------------------------
78  // STEP 2: Initiate Communication with WiFi Module
79  //-------------------------------------------------------------------------
80 
81  printf("Initializing WiConnect Library...\r\n");
82 
83  // Initialize communication with WiFi module
84  if(wiconnect.init(true) != WICONNECT_SUCCESS)
85  {
86  if(result == WICONNECT_FIRMWARE_OUTDATED)
87  {
88  printf("The WiFi firmware is not supported. Run the ota example to update the firmware:\r\n");
89  printf("https://developer.mbed.org/teams/ACKme/code/wiconnect-ota_example");
90  }
91  else
92  {
93  printf("Failed to initialize communication with WiFi module!\r\n"
94  "Make sure the wires are connected correctly\r\n");
95  }
96  for(;;); // infinite loop
97  }
98 
99 
100  //-------------------------------------------------------------------------
101  // STEP 3: Issue a HTTP GET request
102  //-------------------------------------------------------------------------
103 
104  // Initiate a socket with an RX buffer of 256 bytes
105  // We're not specifying the rx buffer pointer so that means it's dynamically allocated
106  WiconnectSocket socket(256);
107 
108 
109  printf("Issuing HTTP Request: %s\r\n", HTTP_URL);
110 
111  // Issue HTTP request
112  // NOTE: the module must have valid network credentials
113  if(wiconnect.httpGet(socket, HTTP_URL) != WICONNECT_SUCCESS)
114  {
115  printf("Failed to issue HTTP request. Does the module have valid network credentials?\r\n");
116  for(;;); // infinite loop
117  }
118 
119  //-------------------------------------------------------------------------
120  // STEP 4: Read the HTTP page and print
121  //-------------------------------------------------------------------------
122 
123  uint8_t *dataPtr;
124  uint16_t dataLength;
125 
126  // while there's data to read, retreive and print it
127  while(socket.read(&dataPtr, &dataLength) == WICONNECT_SUCCESS)
128  {
129  fwrite(dataPtr, 1, dataLength, stdout);
130  }
131 
132  //-------------------------------------------------------------------------
133  // STEP 5: Done!
134  //-------------------------------------------------------------------------
135 
136  printf("HTTP GET example has completed!\r\n");
137 
138  while(true){} // infinite loop
139 }
140 
141 
Host<->Wiconnect Module serial configuration.
Definition: sdk.h:148
The WiFi module's firmware is out-dated. See updateFirmware() to update the firmware.
Connection object to remote server.
Command successfully completed.
The root WiConnect library class. This class inheriets all WiConnect functionality.