ACKme Logo WiConnect Host Library- API Reference Guide
 
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Groups Pages
example.cpp
1 
18 /******************************************************************************
19  * Example Variables
20  */
21 
22 // This is the name of the WebSetup network
23 // Look for this name in your WiFi settings
24 // (e.g. your phone's list of WiFi networks in the WiFi settings menu)
25 // tip: add double-quotes around SSID to add spaces to name
26 #define WEB_SETUP_SSID "\"WiConnect WebSetup Example\""
27 
28 // This is the password for the WebSetup network
29 // Leave as empty string (e.g "") to create OPEN network
30 #define WEB_SETUP_PASSWORD "password"
31 
32 
33 
34 
35 /******************************************************************************
36  * Includes
37  */
38 
39 // include C library headers
40 #include <stdio.h> // needed for printf
41 
42 // include target specific defines
43 #include "target_config.h"
44 // include the Wiconnect Host Library API header
45 #include "Wiconnect.h"
46 
47 
48 
49 /******************************************************************************
50  * Local Functions
51  */
52 static void webSetupCompleteCallback(WiconnectResult result, void *arg1, void *arg2);
53 
54 
55 /******************************************************************************
56  * Global Defines
57  */
58 
59 
60 // Serial used for printfs to terminal (i.e. NOT used for WiConnect)
61 static Serial consoleSerial(STDIO_UART_TX, STDIO_UART_RX);
62 
63 // Buffer used internally by WiConnect library, note that this is optional
64 static uint8_t wiconnectInternalBuffer[256];
65 
66 // Flag that indicate websetup has completed
67 static volatile bool webSetupCompleteFlag = false;
68 
69 
70 /******************************************************************************
71  * Starting point of application
72  */
73 int main(int argc, char **argv)
74 {
75  consoleSerial.baud(115200); // console terminal to 115200 baud
76 
77  //-------------------------------------------------------------------------
78  // STEP 1: Instantiate WiConnect Library
79  //-------------------------------------------------------------------------
80 
81  // Setup wiconnect serial interface configuration
82  // Here we only specify the rx buffer size and not rx buffer pointer, this means
83  // The serial RX buffer will be dynamically allocated
84  SerialConfig serialConfig(WICONNECT_RX_PIN, WICONNECT_TX_PIN, 256, NULL);
85 
86  // Instantiate the Wiconnect library
87  // Here we specify the buffer size AND buffer pointer, this means we're using static allocation
88  Wiconnect wiconnect(serialConfig, sizeof(wiconnectInternalBuffer), wiconnectInternalBuffer, WICONNECT_RESET_PIN);
89 
90 
91  //-------------------------------------------------------------------------
92  // STEP 2: Initiate Communication with WiFi Module
93  //-------------------------------------------------------------------------
94 
95  printf("Initializing WiConnect Library...\r\n");
96 
97  // Initialize communication with WiFi module
98  if(wiconnect.init(true) != WICONNECT_SUCCESS)
99  {
100  if(result == WICONNECT_FIRMWARE_OUTDATED)
101  {
102  printf("The WiFi firmware is not supported. Run the ota example to update the firmware:\r\n");
103  printf("https://developer.mbed.org/teams/ACKme/code/wiconnect-ota_example");
104  }
105  else
106  {
107  printf("Failed to initialize communication with WiFi module!\r\n"
108  "Make sure the wires are connected correctly\r\n");
109  }
110  for(;;); // infinite loop
111  }
112 
113 
114  //-------------------------------------------------------------------------
115  // STEP 3: Start WebSetup with specified parameters
116  //-------------------------------------------------------------------------
117 
118  printf("Starting Websetup...\r\n");
119 
120  // Start web setup
121  if(wiconnect.startWebSetup(WEB_SETUP_SSID, WEB_SETUP_PASSWORD, Callback(webSetupCompleteCallback)) != WICONNECT_SUCCESS)
122  {
123  printf("Failed to start web setup\r\n");
124  for(;;); // infinite loop
125  }
126 
127  //-------------------------------------------------------------------------
128  // STEP 4: Wait for web setup webpage to complete
129  //-------------------------------------------------------------------------
130 
131  printf("Web setup has started.\r\n\r\n");
132  printf("1. Using your phone (or PC, Mac, Linux, etc.)\r\n connect to the WiFi network: %s\r\n", WEB_SETUP_SSID);
133  printf("2. The password is: %s\r\n", WEB_SETUP_PASSWORD);
134  printf("3. Once connected, open your browser and enter the URL: http://setup.com\r\n");
135  printf("4. This will bringup a setup page, enter your router's credentials.\r\n");
136  printf("5. Click the 'Save & Exit' button at the bottom of the webpage\r\n\r\n");
137 
138  while(!webSetupCompleteFlag)
139  {
140  // do nothing while we wait
141  // When the user exits web setup from the webpage
142  // webSetupCompleteCallback() will execute and set webSetupCompleteFlag TRUE
143  }
144 
145  //-------------------------------------------------------------------------
146  // STEP 5: Done!
147  //-------------------------------------------------------------------------
148 
149  printf("IP Address: %s\r\n", wiconnect.getIpAddress());
150  printf("Web setup example has completed!\r\n");
151 
152  while(true){} // infinite loop
153 }
154 
155 
156 // this function is called when web setup completes
157 // it is called in the background
158 static void webSetupCompleteCallback(WiconnectResult result, void *arg1, void *arg2)
159 {
160  webSetupCompleteFlag = true;
161 }
WiconnectResult
API Result code.
Generic callback function.
Definition: Callback.h:49
Host<->Wiconnect Module serial configuration.
Definition: sdk.h:148
The WiFi module's firmware is out-dated. See updateFirmware() to update the firmware.
Command successfully completed.
The root WiConnect library class. This class inheriets all WiConnect functionality.