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  printf("Failed to initialize communication with WiFi module!\r\n"
87  "Make sure the wires are connected correctly\r\n");
88  for(;;); // infinite loop
89  }
90 
91 
92  //-------------------------------------------------------------------------
93  // STEP 3: Issue a HTTP GET request
94  //-------------------------------------------------------------------------
95 
96  // Initiate a socket with an RX buffer of 256 bytes
97  // We're not specifying the rx buffer pointer so that means it's dynamically allocated
98  WiconnectSocket socket(256);
99 
100 
101  printf("Issuing HTTP Request: %s\r\n", HTTP_URL);
102 
103  // Issue HTTP request
104  // NOTE: the module must have valid network credentials
105  if(wiconnect.httpGet(socket, HTTP_URL) != WICONNECT_SUCCESS)
106  {
107  printf("Failed to issue HTTP request. Does the module have valid network credentials?\r\n");
108  for(;;); // infinite loop
109  }
110 
111  //-------------------------------------------------------------------------
112  // STEP 4: Read the HTTP page and print
113  //-------------------------------------------------------------------------
114 
115  uint8_t *dataPtr;
116  uint16_t dataLength;
117 
118  // while there's data to read, retreive and print it
119  while(socket.read(&dataPtr, &dataLength) == WICONNECT_SUCCESS)
120  {
121  fwrite(dataPtr, 1, dataLength, stdout);
122  }
123 
124  //-------------------------------------------------------------------------
125  // STEP 5: Done!
126  //-------------------------------------------------------------------------
127 
128  printf("HTTP GET example has completed!\r\n");
129 
130  while(true){} // infinite loop
131 }
132 
133 
Host<->Wiconnect Module serial configuration.
Definition: sdk.h:129
Connection object to remote server.
Command successfully completed.
The root WiConnect library class. This class inheriets all WiConnect functionality.