Simple example demonstrating WiConnect host library web setup feature.

Dependencies:   WiConnect mbed

Committer:
dan_ackme
Date:
Wed Aug 13 10:18:30 2014 +0000
Revision:
0:1cdfcc4243a0
initial check-in

Who changed what in which revision?

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