ACKme
/
wiconnect-http_get_example
Simple app demonstrating the HTTP GET feature of the WiConnect Host Library.
example.cpp@11:b7a7a7d3b805, 2014-11-03 (annotated)
- Committer:
- dan_ackme
- Date:
- Mon Nov 03 23:58:14 2014 +0000
- Revision:
- 11:b7a7a7d3b805
- Parent:
- 6:f6cf38fba1d4
Specify network credentials in example.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dan_ackme | 1:40ed62996887 | 1 | /** |
dan_ackme | 1:40ed62996887 | 2 | * @example http_get/example.cpp |
dan_ackme | 1:40ed62996887 | 3 | * |
dan_ackme | 1:40ed62996887 | 4 | * This is an example of using the http socket API to |
dan_ackme | 1:40ed62996887 | 5 | * download a webpage. |
dan_ackme | 1:40ed62996887 | 6 | * |
dan_ackme | 1:40ed62996887 | 7 | * It works as follows: |
dan_ackme | 1:40ed62996887 | 8 | * 1. Instantiate the WiConnect Library |
dan_ackme | 1:40ed62996887 | 9 | * 2. Initiate Communication with WiFi Module |
dan_ackme | 1:40ed62996887 | 10 | * 3. Issue a HTTP GET request (Note: The module must have valid network credentials See @ref web_setup/example.cpp) |
dan_ackme | 1:40ed62996887 | 11 | * 4. Read the HTTP page and print |
dan_ackme | 1:40ed62996887 | 12 | * 5. That's it! |
dan_ackme | 1:40ed62996887 | 13 | * |
dan_ackme | 1:40ed62996887 | 14 | */ |
dan_ackme | 1:40ed62996887 | 15 | |
dan_ackme | 1:40ed62996887 | 16 | |
dan_ackme | 1:40ed62996887 | 17 | /****************************************************************************** |
dan_ackme | 1:40ed62996887 | 18 | * Example Variables |
dan_ackme | 1:40ed62996887 | 19 | */ |
dan_ackme | 1:40ed62996887 | 20 | |
dan_ackme | 1:40ed62996887 | 21 | // the URL of the webpage to GET |
dan_ackme | 11:b7a7a7d3b805 | 22 | #define HTTP_URL "http://mbed.org/" |
dan_ackme | 1:40ed62996887 | 23 | |
dan_ackme | 1:40ed62996887 | 24 | |
dan_ackme | 11:b7a7a7d3b805 | 25 | // This is the name of your WiFi network |
dan_ackme | 11:b7a7a7d3b805 | 26 | // Look for this name in your WiFi settings |
dan_ackme | 11:b7a7a7d3b805 | 27 | // (e.g. your phone's list of WiFi networks in the WiFi settings menu) |
dan_ackme | 11:b7a7a7d3b805 | 28 | // tip: add double-quotes around SSID to add spaces to name |
dan_ackme | 11:b7a7a7d3b805 | 29 | #define NETWORK_SSID "<YOUR SSID>" |
dan_ackme | 1:40ed62996887 | 30 | |
dan_ackme | 11:b7a7a7d3b805 | 31 | // This is the password of your WiFi network |
dan_ackme | 11:b7a7a7d3b805 | 32 | // Leave as empty string (e.g "") to connect to OPEN network |
dan_ackme | 11:b7a7a7d3b805 | 33 | #define NETWORK_PASSWORD "<YOUR PASSWORD>" |
dan_ackme | 1:40ed62996887 | 34 | |
dan_ackme | 1:40ed62996887 | 35 | |
dan_ackme | 1:40ed62996887 | 36 | /****************************************************************************** |
dan_ackme | 1:40ed62996887 | 37 | * Includes |
dan_ackme | 1:40ed62996887 | 38 | */ |
dan_ackme | 1:40ed62996887 | 39 | |
dan_ackme | 1:40ed62996887 | 40 | // include C library headers |
dan_ackme | 1:40ed62996887 | 41 | #include <stdio.h> // needed for printf |
dan_ackme | 1:40ed62996887 | 42 | |
dan_ackme | 1:40ed62996887 | 43 | // include target specific defines |
dan_ackme | 1:40ed62996887 | 44 | #include "target_config.h" |
dan_ackme | 1:40ed62996887 | 45 | // include the Wiconnect Host Library API header |
dan_ackme | 1:40ed62996887 | 46 | #include "Wiconnect.h" |
dan_ackme | 1:40ed62996887 | 47 | |
dan_ackme | 1:40ed62996887 | 48 | |
dan_ackme | 1:40ed62996887 | 49 | |
dan_ackme | 1:40ed62996887 | 50 | |
dan_ackme | 1:40ed62996887 | 51 | /****************************************************************************** |
dan_ackme | 1:40ed62996887 | 52 | * Global Defines |
dan_ackme | 1:40ed62996887 | 53 | */ |
dan_ackme | 1:40ed62996887 | 54 | |
dan_ackme | 1:40ed62996887 | 55 | |
dan_ackme | 1:40ed62996887 | 56 | // Serial used for printfs to terminal (i.e. NOT used for WiConnect) |
dan_ackme | 1:40ed62996887 | 57 | static Serial consoleSerial(STDIO_UART_TX, STDIO_UART_RX); |
dan_ackme | 1:40ed62996887 | 58 | |
dan_ackme | 1:40ed62996887 | 59 | |
dan_ackme | 1:40ed62996887 | 60 | |
dan_ackme | 1:40ed62996887 | 61 | /****************************************************************************** |
dan_ackme | 1:40ed62996887 | 62 | * Starting point of application |
dan_ackme | 1:40ed62996887 | 63 | */ |
dan_ackme | 1:40ed62996887 | 64 | int main(int argc, char **argv) |
dan_ackme | 1:40ed62996887 | 65 | { |
dan_ackme | 1:40ed62996887 | 66 | consoleSerial.baud(115200); // console terminal to 115200 baud |
dan_ackme | 1:40ed62996887 | 67 | |
dan_ackme | 1:40ed62996887 | 68 | //------------------------------------------------------------------------- |
dan_ackme | 1:40ed62996887 | 69 | // STEP 1: Instantiate WiConnect Library |
dan_ackme | 1:40ed62996887 | 70 | //------------------------------------------------------------------------- |
dan_ackme | 1:40ed62996887 | 71 | |
dan_ackme | 1:40ed62996887 | 72 | // Setup wiconnect serial interface configuration |
dan_ackme | 1:40ed62996887 | 73 | // Here we only specify the rx buffer size and not rx buffer pointer, this means |
dan_ackme | 1:40ed62996887 | 74 | // The serial RX buffer will be dynamically allocated |
dan_ackme | 1:40ed62996887 | 75 | SerialConfig serialConfig(WICONNECT_RX_PIN, WICONNECT_TX_PIN, 256, NULL); |
dan_ackme | 1:40ed62996887 | 76 | |
dan_ackme | 1:40ed62996887 | 77 | // Instantiate and initialize the Wiconnect library |
dan_ackme | 1:40ed62996887 | 78 | // Here we only specify the buffer size and not buffer pointer, this means |
dan_ackme | 1:40ed62996887 | 79 | // The internal buffer will be dynamically allocated |
dan_ackme | 1:40ed62996887 | 80 | Wiconnect wiconnect(serialConfig, 256, NULL, WICONNECT_RESET_PIN); |
dan_ackme | 1:40ed62996887 | 81 | |
dan_ackme | 1:40ed62996887 | 82 | // set the default timeout to 15s as some websites take awhile to respond |
dan_ackme | 1:40ed62996887 | 83 | wiconnect.setCommandDefaultTimeout(15000); |
dan_ackme | 1:40ed62996887 | 84 | |
dan_ackme | 1:40ed62996887 | 85 | //------------------------------------------------------------------------- |
dan_ackme | 1:40ed62996887 | 86 | // STEP 2: Initiate Communication with WiFi Module |
dan_ackme | 1:40ed62996887 | 87 | //------------------------------------------------------------------------- |
dan_ackme | 1:40ed62996887 | 88 | |
dan_ackme | 1:40ed62996887 | 89 | printf("Initializing WiConnect Library...\r\n"); |
dan_ackme | 1:40ed62996887 | 90 | |
dan_ackme | 1:40ed62996887 | 91 | // Initialize communication with WiFi module |
dan_ackme | 1:40ed62996887 | 92 | if(wiconnect.init(true) != WICONNECT_SUCCESS) |
dan_ackme | 1:40ed62996887 | 93 | { |
dan_ackme | 1:40ed62996887 | 94 | printf("Failed to initialize communication with WiFi module!\r\n" |
dan_ackme | 1:40ed62996887 | 95 | "Make sure the wires are connected correctly\r\n"); |
dan_ackme | 1:40ed62996887 | 96 | for(;;); // infinite loop |
dan_ackme | 1:40ed62996887 | 97 | } |
dan_ackme | 11:b7a7a7d3b805 | 98 | |
dan_ackme | 11:b7a7a7d3b805 | 99 | printf("Joining Wi-Fi network: %s\r\n", NETWORK_SSID); |
dan_ackme | 11:b7a7a7d3b805 | 100 | if(wiconnect.join(NETWORK_SSID, NETWORK_PASSWORD) != WICONNECT_SUCCESS) |
dan_ackme | 11:b7a7a7d3b805 | 101 | { |
dan_ackme | 11:b7a7a7d3b805 | 102 | NetworkJoinResult joinResult; |
dan_ackme | 11:b7a7a7d3b805 | 103 | wiconnect.getNetworkJoinResult(&joinResult); |
dan_ackme | 11:b7a7a7d3b805 | 104 | printf("Failed to send join command. Join result: %s\r\n", Wiconnect::networkJoinResultToStr(joinResult)); |
dan_ackme | 11:b7a7a7d3b805 | 105 | for(;;); // infinite loop |
dan_ackme | 11:b7a7a7d3b805 | 106 | } |
dan_ackme | 1:40ed62996887 | 107 | |
dan_ackme | 1:40ed62996887 | 108 | |
dan_ackme | 1:40ed62996887 | 109 | //------------------------------------------------------------------------- |
dan_ackme | 1:40ed62996887 | 110 | // STEP 3: Issue a HTTP GET request |
dan_ackme | 1:40ed62996887 | 111 | //------------------------------------------------------------------------- |
dan_ackme | 1:40ed62996887 | 112 | |
dan_ackme | 1:40ed62996887 | 113 | // Initiate a socket with an RX buffer of 256 bytes |
dan_ackme | 1:40ed62996887 | 114 | // We're not specifying the rx buffer pointer so that means it's dynamically allocated |
dan_ackme | 3:2bfba274568c | 115 | WiconnectSocket socket(256); |
dan_ackme | 1:40ed62996887 | 116 | |
dan_ackme | 1:40ed62996887 | 117 | |
dan_ackme | 1:40ed62996887 | 118 | printf("Issuing HTTP Request: %s\r\n", HTTP_URL); |
dan_ackme | 1:40ed62996887 | 119 | |
dan_ackme | 1:40ed62996887 | 120 | // Issue HTTP request |
dan_ackme | 1:40ed62996887 | 121 | // NOTE: the module must have valid network credentials |
dan_ackme | 1:40ed62996887 | 122 | if(wiconnect.httpGet(socket, HTTP_URL) != WICONNECT_SUCCESS) |
dan_ackme | 1:40ed62996887 | 123 | { |
dan_ackme | 11:b7a7a7d3b805 | 124 | printf("Failed to issue HTTP request\r\n"); |
dan_ackme | 1:40ed62996887 | 125 | for(;;); // infinite loop |
dan_ackme | 1:40ed62996887 | 126 | } |
dan_ackme | 1:40ed62996887 | 127 | |
dan_ackme | 1:40ed62996887 | 128 | //------------------------------------------------------------------------- |
dan_ackme | 1:40ed62996887 | 129 | // STEP 4: Read the HTTP page and print |
dan_ackme | 1:40ed62996887 | 130 | //------------------------------------------------------------------------- |
dan_ackme | 1:40ed62996887 | 131 | |
dan_ackme | 1:40ed62996887 | 132 | uint8_t *dataPtr; |
dan_ackme | 1:40ed62996887 | 133 | uint16_t dataLength; |
dan_ackme | 1:40ed62996887 | 134 | |
dan_ackme | 1:40ed62996887 | 135 | // while there's data to read, retreive and print it |
dan_ackme | 1:40ed62996887 | 136 | while(socket.read(&dataPtr, &dataLength) == WICONNECT_SUCCESS) |
dan_ackme | 1:40ed62996887 | 137 | { |
dan_ackme | 1:40ed62996887 | 138 | fwrite(dataPtr, 1, dataLength, stdout); |
dan_ackme | 1:40ed62996887 | 139 | } |
dan_ackme | 1:40ed62996887 | 140 | |
dan_ackme | 1:40ed62996887 | 141 | //------------------------------------------------------------------------- |
dan_ackme | 1:40ed62996887 | 142 | // STEP 5: Done! |
dan_ackme | 1:40ed62996887 | 143 | //------------------------------------------------------------------------- |
dan_ackme | 1:40ed62996887 | 144 | |
dan_ackme | 1:40ed62996887 | 145 | printf("HTTP GET example has completed!\r\n"); |
dan_ackme | 1:40ed62996887 | 146 | |
dan_ackme | 6:f6cf38fba1d4 | 147 | while(true){} // infinite loop |
dan_ackme | 1:40ed62996887 | 148 | } |
dan_ackme | 1:40ed62996887 | 149 | |
dan_ackme | 1:40ed62996887 | 150 |