ACKme / Mbed 2 deprecated wiconnect-web_setup_example

Dependencies:   WiConnect mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers example.cpp Source File

example.cpp

00001 /**
00002  * @example web_setup/example.cpp
00003  *
00004  * This is an example of using the web setup network API to
00005  * configure the module network credentials remotely.
00006  *
00007  * It works as follows:
00008  * 1. Instantiate the WiConnect Library
00009  * 2. Initiate Communication with WiFi Module
00010  * 3. Start WebSetup with specified parameters
00011  * 4. Wait for web setup webpage to complete
00012  * 5. That's it!
00013  *
00014  *
00015  */
00016 
00017 
00018 /******************************************************************************
00019  * Example Variables
00020  */
00021 
00022 // This is the name of the WebSetup network
00023 // Look for this name in your WiFi settings
00024 // (e.g. your phone's list of WiFi networks in the WiFi settings menu)
00025 // tip: add double-quotes around SSID to add spaces to name
00026 #define WEB_SETUP_SSID "\"WiConnect WebSetup Example\""
00027 
00028 // This is the password for the WebSetup network
00029 // Leave as empty string (e.g "") to create OPEN network
00030 #define WEB_SETUP_PASSWORD "password"
00031 
00032 
00033 
00034 
00035 /******************************************************************************
00036  * Includes
00037  */
00038 
00039 // include C library headers
00040 #include <stdio.h> // needed for printf
00041 
00042 // include target specific defines
00043 #include "target_config.h"
00044 // include the Wiconnect Host Library API header
00045 #include "Wiconnect.h"
00046 
00047 
00048 
00049 
00050 /******************************************************************************
00051  * Global Defines
00052  */
00053 
00054 
00055 // Serial used for printfs to terminal (i.e. NOT used for WiConnect)
00056 static Serial consoleSerial(STDIO_UART_TX, STDIO_UART_RX);
00057 
00058 // Buffer used internally by WiConnect library, note that this is optional
00059 static uint8_t wiconnectInternalBuffer[256];
00060 
00061 
00062 
00063 /******************************************************************************
00064  * Starting point of application
00065  */
00066 int main(int argc, char **argv)
00067 {
00068     WiconnectResult result;
00069     consoleSerial.baud(115200); // console terminal to 115200 baud
00070 
00071     //-------------------------------------------------------------------------
00072     // STEP 1: Instantiate WiConnect Library
00073     //-------------------------------------------------------------------------
00074 
00075     // Setup  wiconnect serial interface configuration
00076     // Here we only specify the rx buffer size and not rx buffer pointer, this means
00077     // The serial RX buffer will be dynamically allocated
00078     SerialConfig serialConfig(WICONNECT_RX_PIN, WICONNECT_TX_PIN, 256, NULL);
00079 
00080     // Instantiate the Wiconnect library
00081     // Here we specify the buffer size AND buffer pointer, this means we're using static allocation
00082     Wiconnect wiconnect(serialConfig, sizeof(wiconnectInternalBuffer), wiconnectInternalBuffer, WICONNECT_RESET_PIN);
00083 
00084 
00085     //-------------------------------------------------------------------------
00086     // STEP 2: Initiate Communication with WiFi Module
00087     //-------------------------------------------------------------------------
00088 
00089     printf("Initializing WiConnect Library...\r\n");
00090 
00091     // Initialize communication with WiFi module
00092     if(WICONNECT_FAILED(result, wiconnect.init()))
00093     {
00094         if(result == WICONNECT_FIRMWARE_OUTDATED)
00095         {
00096             printf("*** The WiFi firmware is not supported. Run the ota example to update the firmware:\r\n");
00097             printf("https://developer.mbed.org/teams/ACKme/code/wiconnect-ota_example\r\n\r\n");
00098         }
00099         else
00100         {
00101             printf("Failed to initialize communication with WiFi module!\r\n"
00102                    "Make sure the wires are connected correctly\r\n");
00103         }
00104         for(;;); // infinite loop
00105     }
00106 
00107 
00108     //-------------------------------------------------------------------------
00109     // STEP 3: Start WebSetup with specified parameters
00110     //-------------------------------------------------------------------------
00111 
00112     printf("Starting Websetup...\r\n");
00113 
00114     // Start web setup
00115     if(wiconnect.startWebSetup(WEB_SETUP_SSID, WEB_SETUP_PASSWORD) != WICONNECT_SUCCESS)
00116     {
00117         printf("Failed to start web setup\r\n");
00118         for(;;); // infinite loop
00119     }
00120 
00121     //-------------------------------------------------------------------------
00122     // STEP 4: Wait for web setup webpage to complete
00123     //-------------------------------------------------------------------------
00124 
00125     printf("Web setup has started.\r\n\r\n");
00126     printf("1. Using your phone (or PC, Mac, Linux, etc.)\r\n   connect to the WiFi network: %s\r\n", WEB_SETUP_SSID);
00127     printf("2. The password is: %s\r\n", WEB_SETUP_PASSWORD);
00128     printf("3. Once connected, open your browser and enter the URL: http://setup.com\r\n");
00129     printf("4. This will bringup a setup page, enter your router's credentials.\r\n");
00130     printf("5. Click the 'Save & Exit' button at the bottom of the webpage\r\n\r\n");
00131 
00132     for(;;)
00133     {
00134         bool isRunning;
00135         
00136         if(wiconnect.isWebSetupRunning(&isRunning) != WICONNECT_SUCCESS)
00137         {
00138             printf("Failed to get websetup status\r\n");
00139             for(;;);
00140         }
00141         delayMs(100);
00142         if(!isRunning)
00143             break;
00144     }
00145 
00146     //-------------------------------------------------------------------------
00147     // STEP 5: Done!
00148     //-------------------------------------------------------------------------
00149 
00150     printf("IP Address: %s\r\n", wiconnect.getIpAddress());
00151     printf("Web setup example has completed!\r\n");
00152 
00153     while(true){} // infinite loop
00154 }
00155 
00156