ACKme
/
wiconnect-ota_example
This demonstrates how to update the WiFi modules firmware using the WiConnect host library.
example.cpp@0:3c12b186f186, 2014-11-27 (annotated)
- Committer:
- dan_ackme
- Date:
- Thu Nov 27 09:14:05 2014 +0000
- Revision:
- 0:3c12b186f186
Initial check-in
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dan_ackme | 0:3c12b186f186 | 1 | /** |
dan_ackme | 0:3c12b186f186 | 2 | * @example ota/example.cpp |
dan_ackme | 0:3c12b186f186 | 3 | * |
dan_ackme | 0:3c12b186f186 | 4 | * This is an example of using the wiconnect API to update |
dan_ackme | 0:3c12b186f186 | 5 | * the wifi module's internal firmware to the latest version. |
dan_ackme | 0:3c12b186f186 | 6 | * |
dan_ackme | 0:3c12b186f186 | 7 | * It works as follows: |
dan_ackme | 0:3c12b186f186 | 8 | * 1. Instantiate the WiConnect Library |
dan_ackme | 0:3c12b186f186 | 9 | * 2. Initiate Communication with WiFi Module |
dan_ackme | 0:3c12b186f186 | 10 | * 3. Set the network credentials |
dan_ackme | 0:3c12b186f186 | 11 | * 4. Call the 'updateFirmware' wiconnect API and wait for the update to complete (takes ~60s) |
dan_ackme | 0:3c12b186f186 | 12 | * 5. That's it! |
dan_ackme | 0:3c12b186f186 | 13 | * |
dan_ackme | 0:3c12b186f186 | 14 | * |
dan_ackme | 0:3c12b186f186 | 15 | */ |
dan_ackme | 0:3c12b186f186 | 16 | |
dan_ackme | 0:3c12b186f186 | 17 | |
dan_ackme | 0:3c12b186f186 | 18 | /****************************************************************************** |
dan_ackme | 0:3c12b186f186 | 19 | * Example Variables |
dan_ackme | 0:3c12b186f186 | 20 | */ |
dan_ackme | 0:3c12b186f186 | 21 | |
dan_ackme | 0:3c12b186f186 | 22 | // This is the name of your WiFi network |
dan_ackme | 0:3c12b186f186 | 23 | // Look for this name in your WiFi settings |
dan_ackme | 0:3c12b186f186 | 24 | // (e.g. your phone's list of WiFi networks in the WiFi settings menu) |
dan_ackme | 0:3c12b186f186 | 25 | // tip: add double-quotes around SSID to add spaces to name |
dan_ackme | 0:3c12b186f186 | 26 | #define NETWORK_SSID "\"<YOUR NETWORK NAME HERE>\"" |
dan_ackme | 0:3c12b186f186 | 27 | |
dan_ackme | 0:3c12b186f186 | 28 | |
dan_ackme | 0:3c12b186f186 | 29 | // This is the password of your WiFi network |
dan_ackme | 0:3c12b186f186 | 30 | // Leave as empty string (e.g "") to connect to OPEN network |
dan_ackme | 0:3c12b186f186 | 31 | #define NETWORK_PASSWORD "\"<YOUR NETWORK PASSWORD HERE>\"" |
dan_ackme | 0:3c12b186f186 | 32 | |
dan_ackme | 0:3c12b186f186 | 33 | /****************************************************************************** |
dan_ackme | 0:3c12b186f186 | 34 | * Includes |
dan_ackme | 0:3c12b186f186 | 35 | */ |
dan_ackme | 0:3c12b186f186 | 36 | |
dan_ackme | 0:3c12b186f186 | 37 | // include C library headers |
dan_ackme | 0:3c12b186f186 | 38 | #include <stdio.h> // needed for printf |
dan_ackme | 0:3c12b186f186 | 39 | |
dan_ackme | 0:3c12b186f186 | 40 | // include target specific defines |
dan_ackme | 0:3c12b186f186 | 41 | #include "target_config.h" |
dan_ackme | 0:3c12b186f186 | 42 | // include the Wiconnect Host Library API header |
dan_ackme | 0:3c12b186f186 | 43 | #include "Wiconnect.h" |
dan_ackme | 0:3c12b186f186 | 44 | |
dan_ackme | 0:3c12b186f186 | 45 | |
dan_ackme | 0:3c12b186f186 | 46 | |
dan_ackme | 0:3c12b186f186 | 47 | /****************************************************************************** |
dan_ackme | 0:3c12b186f186 | 48 | * Global Defines |
dan_ackme | 0:3c12b186f186 | 49 | */ |
dan_ackme | 0:3c12b186f186 | 50 | |
dan_ackme | 0:3c12b186f186 | 51 | |
dan_ackme | 0:3c12b186f186 | 52 | // Serial used for printfs to terminal (i.e. NOT used for WiConnect) |
dan_ackme | 0:3c12b186f186 | 53 | static Serial consoleSerial(STDIO_UART_TX, STDIO_UART_RX); |
dan_ackme | 0:3c12b186f186 | 54 | |
dan_ackme | 0:3c12b186f186 | 55 | |
dan_ackme | 0:3c12b186f186 | 56 | /****************************************************************************** |
dan_ackme | 0:3c12b186f186 | 57 | * Starting point of application |
dan_ackme | 0:3c12b186f186 | 58 | */ |
dan_ackme | 0:3c12b186f186 | 59 | int main(int argc, char **argv) |
dan_ackme | 0:3c12b186f186 | 60 | { |
dan_ackme | 0:3c12b186f186 | 61 | WiconnectResult result; |
dan_ackme | 0:3c12b186f186 | 62 | |
dan_ackme | 0:3c12b186f186 | 63 | consoleSerial.baud(115200); // console terminal to 115200 baud |
dan_ackme | 0:3c12b186f186 | 64 | |
dan_ackme | 0:3c12b186f186 | 65 | //------------------------------------------------------------------------- |
dan_ackme | 0:3c12b186f186 | 66 | // STEP 1: Instantiate WiConnect Library |
dan_ackme | 0:3c12b186f186 | 67 | //------------------------------------------------------------------------- |
dan_ackme | 0:3c12b186f186 | 68 | |
dan_ackme | 0:3c12b186f186 | 69 | // Setup wiconnect serial interface configuration |
dan_ackme | 0:3c12b186f186 | 70 | // Here we only specify the rx buffer size and not rx buffer pointer, this means |
dan_ackme | 0:3c12b186f186 | 71 | // The serial RX buffer will be dynamically allocated |
dan_ackme | 0:3c12b186f186 | 72 | SerialConfig serialConfig(WICONNECT_RX_PIN, WICONNECT_TX_PIN, 256, NULL); |
dan_ackme | 0:3c12b186f186 | 73 | |
dan_ackme | 0:3c12b186f186 | 74 | // Instantiate the Wiconnect library |
dan_ackme | 0:3c12b186f186 | 75 | // Here we only specify the buffer size and not buffer pointer, this means |
dan_ackme | 0:3c12b186f186 | 76 | // The internal buffer will be dynamically allocated |
dan_ackme | 0:3c12b186f186 | 77 | Wiconnect wiconnect(serialConfig, 256, NULL, WICONNECT_RESET_PIN); |
dan_ackme | 0:3c12b186f186 | 78 | |
dan_ackme | 0:3c12b186f186 | 79 | //------------------------------------------------------------------------- |
dan_ackme | 0:3c12b186f186 | 80 | // STEP 2: Initiate Communication with WiFi Module |
dan_ackme | 0:3c12b186f186 | 81 | //------------------------------------------------------------------------- |
dan_ackme | 0:3c12b186f186 | 82 | |
dan_ackme | 0:3c12b186f186 | 83 | printf("Initializing WiConnect Library...\r\n"); |
dan_ackme | 0:3c12b186f186 | 84 | |
dan_ackme | 0:3c12b186f186 | 85 | // Initialize communication with WiFi module |
dan_ackme | 0:3c12b186f186 | 86 | if(WICONNECT_FAILED(result, wiconnect.init(true))) |
dan_ackme | 0:3c12b186f186 | 87 | { |
dan_ackme | 0:3c12b186f186 | 88 | // if the error is that the firmware is out-dated, then continue, else stop |
dan_ackme | 0:3c12b186f186 | 89 | if(result != WICONNECT_FIRMWARE_OUTDATED) |
dan_ackme | 0:3c12b186f186 | 90 | { |
dan_ackme | 0:3c12b186f186 | 91 | printf("Failed to initialize communication with WiFi module!\r\n" |
dan_ackme | 0:3c12b186f186 | 92 | "Make sure the wires are connected correctly\r\n"); |
dan_ackme | 0:3c12b186f186 | 93 | for(;;); // infinite loop |
dan_ackme | 0:3c12b186f186 | 94 | } |
dan_ackme | 0:3c12b186f186 | 95 | } |
dan_ackme | 0:3c12b186f186 | 96 | |
dan_ackme | 0:3c12b186f186 | 97 | |
dan_ackme | 0:3c12b186f186 | 98 | //------------------------------------------------------------------------- |
dan_ackme | 0:3c12b186f186 | 99 | // STEP 3: Set the network credentials |
dan_ackme | 0:3c12b186f186 | 100 | // Note: We manually set the network parameters to ensure SDK backward compatibility |
dan_ackme | 0:3c12b186f186 | 101 | //------------------------------------------------------------------------- |
dan_ackme | 0:3c12b186f186 | 102 | |
dan_ackme | 0:3c12b186f186 | 103 | printf("Setting network SSID: %s\r\n", NETWORK_SSID); |
dan_ackme | 0:3c12b186f186 | 104 | if(WICONNECT_FAILED(result, wiconnect.setSetting("wlan.ssid", NETWORK_SSID))) |
dan_ackme | 0:3c12b186f186 | 105 | { |
dan_ackme | 0:3c12b186f186 | 106 | printf("Failed to set wlan.ssid setting\r\n"); |
dan_ackme | 0:3c12b186f186 | 107 | for(;;); // infinite loop |
dan_ackme | 0:3c12b186f186 | 108 | } |
dan_ackme | 0:3c12b186f186 | 109 | |
dan_ackme | 0:3c12b186f186 | 110 | printf("Setting network password\r\n"); |
dan_ackme | 0:3c12b186f186 | 111 | if(WICONNECT_FAILED(result, wiconnect.setSetting("wlan.passkey", NETWORK_PASSWORD))) |
dan_ackme | 0:3c12b186f186 | 112 | { |
dan_ackme | 0:3c12b186f186 | 113 | printf("Failed to set wlan.passkey setting\r\n"); |
dan_ackme | 0:3c12b186f186 | 114 | for(;;); // infinite loop |
dan_ackme | 0:3c12b186f186 | 115 | } |
dan_ackme | 0:3c12b186f186 | 116 | |
dan_ackme | 0:3c12b186f186 | 117 | printf("Saving settings to Non-volatile Memory\r\n"); |
dan_ackme | 0:3c12b186f186 | 118 | if(WICONNECT_FAILED(result, wiconnect.saveSettings())) |
dan_ackme | 0:3c12b186f186 | 119 | { |
dan_ackme | 0:3c12b186f186 | 120 | printf("Failed save settings\r\n"); |
dan_ackme | 0:3c12b186f186 | 121 | for(;;); // infinite loop |
dan_ackme | 0:3c12b186f186 | 122 | } |
dan_ackme | 0:3c12b186f186 | 123 | |
dan_ackme | 0:3c12b186f186 | 124 | //------------------------------------------------------------------------- |
dan_ackme | 0:3c12b186f186 | 125 | // STEP 4: Update the module's firmware, this takes about 60s |
dan_ackme | 0:3c12b186f186 | 126 | //------------------------------------------------------------------------- |
dan_ackme | 0:3c12b186f186 | 127 | |
dan_ackme | 0:3c12b186f186 | 128 | printf("Updating WiFi module's internal firmware to latest (note this can take up to 60s)\r\n"); |
dan_ackme | 0:3c12b186f186 | 129 | |
dan_ackme | 0:3c12b186f186 | 130 | if(wiconnect.updateFirmware() != WICONNECT_SUCCESS) |
dan_ackme | 0:3c12b186f186 | 131 | { |
dan_ackme | 0:3c12b186f186 | 132 | printf("Failed to update the module's firmware\r\n"); |
dan_ackme | 0:3c12b186f186 | 133 | for(;;); // infinite loop |
dan_ackme | 0:3c12b186f186 | 134 | } |
dan_ackme | 0:3c12b186f186 | 135 | |
dan_ackme | 0:3c12b186f186 | 136 | //------------------------------------------------------------------------- |
dan_ackme | 0:3c12b186f186 | 137 | // STEP 5: Done! |
dan_ackme | 0:3c12b186f186 | 138 | //------------------------------------------------------------------------- |
dan_ackme | 0:3c12b186f186 | 139 | |
dan_ackme | 0:3c12b186f186 | 140 | wiconnect.getVersion(); |
dan_ackme | 0:3c12b186f186 | 141 | printf("Version: %s\r\n", wiconnect.getResponseBuffer()); |
dan_ackme | 0:3c12b186f186 | 142 | |
dan_ackme | 0:3c12b186f186 | 143 | while(true){} // infinite loop |
dan_ackme | 0:3c12b186f186 | 144 | } |
dan_ackme | 0:3c12b186f186 | 145 |