![](/media/cache/group/ackmelogo_1.png.50x50_q85.png)
Simple example demonstrating WiConnect host library web setup feature.
example.cpp@6:1e3eb95956b9, 2014-08-26 (annotated)
- Committer:
- dan_ackme
- Date:
- Tue Aug 26 16:35:16 2014 -0700
- Revision:
- 6:1e3eb95956b9
- Parent:
- 1:44eae7ea80c7
- Child:
- 7:723f12b7b0de
fixed warning
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dan_ackme | 1:44eae7ea80c7 | 1 | /** |
dan_ackme | 1:44eae7ea80c7 | 2 | * @example web_setup/example.cpp |
dan_ackme | 1:44eae7ea80c7 | 3 | * |
dan_ackme | 1:44eae7ea80c7 | 4 | * This is an example of using the web setup network API to |
dan_ackme | 1:44eae7ea80c7 | 5 | * configure the module network credentials remotely. |
dan_ackme | 1:44eae7ea80c7 | 6 | * |
dan_ackme | 1:44eae7ea80c7 | 7 | * It works as follows: |
dan_ackme | 1:44eae7ea80c7 | 8 | * 1. Instantiate the WiConnect Library |
dan_ackme | 1:44eae7ea80c7 | 9 | * 2. Initiate Communication with WiFi Module |
dan_ackme | 1:44eae7ea80c7 | 10 | * 3. Start WebSetup with specified parameters |
dan_ackme | 1:44eae7ea80c7 | 11 | * 4. Wait for web setup webpage to complete |
dan_ackme | 1:44eae7ea80c7 | 12 | * 5. That's it! |
dan_ackme | 1:44eae7ea80c7 | 13 | * |
dan_ackme | 1:44eae7ea80c7 | 14 | * |
dan_ackme | 1:44eae7ea80c7 | 15 | */ |
dan_ackme | 1:44eae7ea80c7 | 16 | |
dan_ackme | 1:44eae7ea80c7 | 17 | |
dan_ackme | 1:44eae7ea80c7 | 18 | /****************************************************************************** |
dan_ackme | 1:44eae7ea80c7 | 19 | * Example Variables |
dan_ackme | 1:44eae7ea80c7 | 20 | */ |
dan_ackme | 1:44eae7ea80c7 | 21 | |
dan_ackme | 1:44eae7ea80c7 | 22 | // This is the name of the WebSetup network |
dan_ackme | 1:44eae7ea80c7 | 23 | // Look for this name in your WiFi settings |
dan_ackme | 1:44eae7ea80c7 | 24 | // (e.g. your phone's list of WiFi networks in the WiFi settings menu) |
dan_ackme | 1:44eae7ea80c7 | 25 | // tip: add double-quotes around SSID to add spaces to name |
dan_ackme | 1:44eae7ea80c7 | 26 | #define WEB_SETUP_SSID "\"WiConnect WebSetup Example\"" |
dan_ackme | 1:44eae7ea80c7 | 27 | |
dan_ackme | 1:44eae7ea80c7 | 28 | // This is the password for the WebSetup network |
dan_ackme | 1:44eae7ea80c7 | 29 | // Leave as empty string (e.g "") to create OPEN network |
dan_ackme | 1:44eae7ea80c7 | 30 | #define WEB_SETUP_PASSWORD "password" |
dan_ackme | 1:44eae7ea80c7 | 31 | |
dan_ackme | 1:44eae7ea80c7 | 32 | |
dan_ackme | 1:44eae7ea80c7 | 33 | |
dan_ackme | 1:44eae7ea80c7 | 34 | |
dan_ackme | 1:44eae7ea80c7 | 35 | /****************************************************************************** |
dan_ackme | 1:44eae7ea80c7 | 36 | * Includes |
dan_ackme | 1:44eae7ea80c7 | 37 | */ |
dan_ackme | 1:44eae7ea80c7 | 38 | |
dan_ackme | 1:44eae7ea80c7 | 39 | // include C library headers |
dan_ackme | 1:44eae7ea80c7 | 40 | #include <stdio.h> // needed for printf |
dan_ackme | 1:44eae7ea80c7 | 41 | |
dan_ackme | 1:44eae7ea80c7 | 42 | // include target specific defines |
dan_ackme | 1:44eae7ea80c7 | 43 | #include "target_config.h" |
dan_ackme | 1:44eae7ea80c7 | 44 | // include the Wiconnect Host Library API header |
dan_ackme | 1:44eae7ea80c7 | 45 | #include "Wiconnect.h" |
dan_ackme | 1:44eae7ea80c7 | 46 | |
dan_ackme | 1:44eae7ea80c7 | 47 | |
dan_ackme | 1:44eae7ea80c7 | 48 | |
dan_ackme | 1:44eae7ea80c7 | 49 | /****************************************************************************** |
dan_ackme | 1:44eae7ea80c7 | 50 | * Local Functions |
dan_ackme | 1:44eae7ea80c7 | 51 | */ |
dan_ackme | 1:44eae7ea80c7 | 52 | static void webSetupCompleteCallback(WiconnectResult result, void *arg1, void *arg2); |
dan_ackme | 1:44eae7ea80c7 | 53 | |
dan_ackme | 1:44eae7ea80c7 | 54 | |
dan_ackme | 1:44eae7ea80c7 | 55 | /****************************************************************************** |
dan_ackme | 1:44eae7ea80c7 | 56 | * Global Defines |
dan_ackme | 1:44eae7ea80c7 | 57 | */ |
dan_ackme | 1:44eae7ea80c7 | 58 | |
dan_ackme | 1:44eae7ea80c7 | 59 | |
dan_ackme | 1:44eae7ea80c7 | 60 | // Serial used for printfs to terminal (i.e. NOT used for WiConnect) |
dan_ackme | 1:44eae7ea80c7 | 61 | static Serial consoleSerial(STDIO_UART_TX, STDIO_UART_RX); |
dan_ackme | 1:44eae7ea80c7 | 62 | |
dan_ackme | 1:44eae7ea80c7 | 63 | // Buffer used internally by WiConnect library, note that this is optional |
dan_ackme | 1:44eae7ea80c7 | 64 | static uint8_t wiconnectInternalBuffer[256]; |
dan_ackme | 1:44eae7ea80c7 | 65 | |
dan_ackme | 1:44eae7ea80c7 | 66 | // Flag that indicate websetup has completed |
dan_ackme | 1:44eae7ea80c7 | 67 | static volatile bool webSetupCompleteFlag = false; |
dan_ackme | 1:44eae7ea80c7 | 68 | |
dan_ackme | 1:44eae7ea80c7 | 69 | |
dan_ackme | 1:44eae7ea80c7 | 70 | /****************************************************************************** |
dan_ackme | 1:44eae7ea80c7 | 71 | * Starting point of application |
dan_ackme | 1:44eae7ea80c7 | 72 | */ |
dan_ackme | 1:44eae7ea80c7 | 73 | int main(int argc, char **argv) |
dan_ackme | 1:44eae7ea80c7 | 74 | { |
dan_ackme | 1:44eae7ea80c7 | 75 | consoleSerial.baud(115200); // console terminal to 115200 baud |
dan_ackme | 1:44eae7ea80c7 | 76 | |
dan_ackme | 1:44eae7ea80c7 | 77 | //------------------------------------------------------------------------- |
dan_ackme | 1:44eae7ea80c7 | 78 | // STEP 1: Instantiate WiConnect Library |
dan_ackme | 1:44eae7ea80c7 | 79 | //------------------------------------------------------------------------- |
dan_ackme | 1:44eae7ea80c7 | 80 | |
dan_ackme | 1:44eae7ea80c7 | 81 | // Setup wiconnect serial interface configuration |
dan_ackme | 1:44eae7ea80c7 | 82 | // Here we only specify the rx buffer size and not rx buffer pointer, this means |
dan_ackme | 1:44eae7ea80c7 | 83 | // The serial RX buffer will be dynamically allocated |
dan_ackme | 1:44eae7ea80c7 | 84 | SerialConfig serialConfig(WICONNECT_RX_PIN, WICONNECT_TX_PIN, 256, NULL); |
dan_ackme | 1:44eae7ea80c7 | 85 | |
dan_ackme | 1:44eae7ea80c7 | 86 | // Instantiate the Wiconnect library |
dan_ackme | 1:44eae7ea80c7 | 87 | // Here we specify the buffer size AND buffer pointer, this means we're using static allocation |
dan_ackme | 1:44eae7ea80c7 | 88 | Wiconnect wiconnect(serialConfig, sizeof(wiconnectInternalBuffer), wiconnectInternalBuffer, WICONNECT_RESET_PIN); |
dan_ackme | 1:44eae7ea80c7 | 89 | |
dan_ackme | 1:44eae7ea80c7 | 90 | |
dan_ackme | 1:44eae7ea80c7 | 91 | //------------------------------------------------------------------------- |
dan_ackme | 1:44eae7ea80c7 | 92 | // STEP 2: Initiate Communication with WiFi Module |
dan_ackme | 1:44eae7ea80c7 | 93 | //------------------------------------------------------------------------- |
dan_ackme | 1:44eae7ea80c7 | 94 | |
dan_ackme | 1:44eae7ea80c7 | 95 | printf("Initializing WiConnect Library...\r\n"); |
dan_ackme | 1:44eae7ea80c7 | 96 | |
dan_ackme | 1:44eae7ea80c7 | 97 | // Initialize communication with WiFi module |
dan_ackme | 1:44eae7ea80c7 | 98 | if(wiconnect.init(true) != WICONNECT_SUCCESS) |
dan_ackme | 1:44eae7ea80c7 | 99 | { |
dan_ackme | 1:44eae7ea80c7 | 100 | printf("Failed to initialize communication with WiFi module!\r\n" |
dan_ackme | 1:44eae7ea80c7 | 101 | "Make sure the wires are connected correctly\r\n"); |
dan_ackme | 1:44eae7ea80c7 | 102 | for(;;); // infinite loop |
dan_ackme | 1:44eae7ea80c7 | 103 | } |
dan_ackme | 1:44eae7ea80c7 | 104 | |
dan_ackme | 1:44eae7ea80c7 | 105 | |
dan_ackme | 1:44eae7ea80c7 | 106 | //------------------------------------------------------------------------- |
dan_ackme | 1:44eae7ea80c7 | 107 | // STEP 3: Start WebSetup with specified parameters |
dan_ackme | 1:44eae7ea80c7 | 108 | //------------------------------------------------------------------------- |
dan_ackme | 1:44eae7ea80c7 | 109 | |
dan_ackme | 1:44eae7ea80c7 | 110 | printf("Starting Websetup...\r\n"); |
dan_ackme | 1:44eae7ea80c7 | 111 | |
dan_ackme | 1:44eae7ea80c7 | 112 | // Start web setup |
dan_ackme | 1:44eae7ea80c7 | 113 | if(wiconnect.startWebSetup(WEB_SETUP_SSID, WEB_SETUP_PASSWORD, Callback(webSetupCompleteCallback)) != WICONNECT_SUCCESS) |
dan_ackme | 1:44eae7ea80c7 | 114 | { |
dan_ackme | 1:44eae7ea80c7 | 115 | printf("Failed to start web setup\r\n"); |
dan_ackme | 1:44eae7ea80c7 | 116 | for(;;); // infinite loop |
dan_ackme | 1:44eae7ea80c7 | 117 | } |
dan_ackme | 1:44eae7ea80c7 | 118 | |
dan_ackme | 1:44eae7ea80c7 | 119 | //------------------------------------------------------------------------- |
dan_ackme | 1:44eae7ea80c7 | 120 | // STEP 4: Wait for web setup webpage to complete |
dan_ackme | 1:44eae7ea80c7 | 121 | //------------------------------------------------------------------------- |
dan_ackme | 1:44eae7ea80c7 | 122 | |
dan_ackme | 1:44eae7ea80c7 | 123 | printf("Web setup has started.\r\n\r\n"); |
dan_ackme | 1:44eae7ea80c7 | 124 | printf("1. Using your phone (or PC, Mac, Linux, etc.)\r\n connect to the WiFi network: %s\r\n", WEB_SETUP_SSID); |
dan_ackme | 1:44eae7ea80c7 | 125 | printf("2. The password is: %s\r\n", WEB_SETUP_PASSWORD); |
dan_ackme | 1:44eae7ea80c7 | 126 | printf("3. Once connected, open your browser and enter the URL: http://setup.com\r\n"); |
dan_ackme | 1:44eae7ea80c7 | 127 | printf("4. This will bringup a setup page, enter your router's credentials.\r\n"); |
dan_ackme | 1:44eae7ea80c7 | 128 | printf("5. Click the 'Save & Exit' button at the bottom of the webpage\r\n\r\n"); |
dan_ackme | 1:44eae7ea80c7 | 129 | |
dan_ackme | 1:44eae7ea80c7 | 130 | while(!webSetupCompleteFlag) |
dan_ackme | 1:44eae7ea80c7 | 131 | { |
dan_ackme | 1:44eae7ea80c7 | 132 | // do nothing while we wait |
dan_ackme | 1:44eae7ea80c7 | 133 | // When the user exits web setup from the webpage |
dan_ackme | 1:44eae7ea80c7 | 134 | // webSetupCompleteCallback() will execute and set webSetupCompleteFlag TRUE |
dan_ackme | 1:44eae7ea80c7 | 135 | } |
dan_ackme | 1:44eae7ea80c7 | 136 | |
dan_ackme | 1:44eae7ea80c7 | 137 | //------------------------------------------------------------------------- |
dan_ackme | 1:44eae7ea80c7 | 138 | // STEP 5: Done! |
dan_ackme | 1:44eae7ea80c7 | 139 | //------------------------------------------------------------------------- |
dan_ackme | 1:44eae7ea80c7 | 140 | |
dan_ackme | 6:1e3eb95956b9 | 141 | printf("IP Address: %s\r\n", wiconnect.getIpAddress()); |
dan_ackme | 1:44eae7ea80c7 | 142 | printf("Web setup example has completed!\r\n"); |
dan_ackme | 1:44eae7ea80c7 | 143 | |
dan_ackme | 6:1e3eb95956b9 | 144 | while(true){} // infinite loop |
dan_ackme | 1:44eae7ea80c7 | 145 | } |
dan_ackme | 1:44eae7ea80c7 | 146 | |
dan_ackme | 1:44eae7ea80c7 | 147 | |
dan_ackme | 1:44eae7ea80c7 | 148 | // this function is called when web setup completes |
dan_ackme | 1:44eae7ea80c7 | 149 | // it is called in the background |
dan_ackme | 1:44eae7ea80c7 | 150 | static void webSetupCompleteCallback(WiconnectResult result, void *arg1, void *arg2) |
dan_ackme | 1:44eae7ea80c7 | 151 | { |
dan_ackme | 1:44eae7ea80c7 | 152 | webSetupCompleteFlag = true; |
dan_ackme | 1:44eae7ea80c7 | 153 | } |