ACKme
/
wiconnect-http_get_example
Simple app demonstrating the HTTP GET feature of the WiConnect Host Library.
example.cpp@12:106e10b527f9, 2014-11-27 (annotated)
- Committer:
- dan_ackme
- Date:
- Thu Nov 27 00:00:08 2014 -0800
- Revision:
- 12:106e10b527f9
- Parent:
- 6:f6cf38fba1d4
- Child:
- 13:32538c7842e6
added warning about outdated fw
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 | 1:40ed62996887 | 22 | #define HTTP_URL "http://ack.me" |
dan_ackme | 1:40ed62996887 | 23 | |
dan_ackme | 1:40ed62996887 | 24 | |
dan_ackme | 1:40ed62996887 | 25 | |
dan_ackme | 1:40ed62996887 | 26 | |
dan_ackme | 1:40ed62996887 | 27 | |
dan_ackme | 1:40ed62996887 | 28 | /****************************************************************************** |
dan_ackme | 1:40ed62996887 | 29 | * Includes |
dan_ackme | 1:40ed62996887 | 30 | */ |
dan_ackme | 1:40ed62996887 | 31 | |
dan_ackme | 1:40ed62996887 | 32 | // include C library headers |
dan_ackme | 1:40ed62996887 | 33 | #include <stdio.h> // needed for printf |
dan_ackme | 1:40ed62996887 | 34 | |
dan_ackme | 1:40ed62996887 | 35 | // include target specific defines |
dan_ackme | 1:40ed62996887 | 36 | #include "target_config.h" |
dan_ackme | 1:40ed62996887 | 37 | // include the Wiconnect Host Library API header |
dan_ackme | 1:40ed62996887 | 38 | #include "Wiconnect.h" |
dan_ackme | 1:40ed62996887 | 39 | |
dan_ackme | 1:40ed62996887 | 40 | |
dan_ackme | 1:40ed62996887 | 41 | |
dan_ackme | 1:40ed62996887 | 42 | |
dan_ackme | 1:40ed62996887 | 43 | /****************************************************************************** |
dan_ackme | 1:40ed62996887 | 44 | * Global Defines |
dan_ackme | 1:40ed62996887 | 45 | */ |
dan_ackme | 1:40ed62996887 | 46 | |
dan_ackme | 1:40ed62996887 | 47 | |
dan_ackme | 1:40ed62996887 | 48 | // Serial used for printfs to terminal (i.e. NOT used for WiConnect) |
dan_ackme | 1:40ed62996887 | 49 | static Serial consoleSerial(STDIO_UART_TX, STDIO_UART_RX); |
dan_ackme | 1:40ed62996887 | 50 | |
dan_ackme | 1:40ed62996887 | 51 | |
dan_ackme | 1:40ed62996887 | 52 | |
dan_ackme | 1:40ed62996887 | 53 | /****************************************************************************** |
dan_ackme | 1:40ed62996887 | 54 | * Starting point of application |
dan_ackme | 1:40ed62996887 | 55 | */ |
dan_ackme | 1:40ed62996887 | 56 | int main(int argc, char **argv) |
dan_ackme | 1:40ed62996887 | 57 | { |
dan_ackme | 1:40ed62996887 | 58 | consoleSerial.baud(115200); // console terminal to 115200 baud |
dan_ackme | 1:40ed62996887 | 59 | |
dan_ackme | 1:40ed62996887 | 60 | //------------------------------------------------------------------------- |
dan_ackme | 1:40ed62996887 | 61 | // STEP 1: Instantiate WiConnect Library |
dan_ackme | 1:40ed62996887 | 62 | //------------------------------------------------------------------------- |
dan_ackme | 1:40ed62996887 | 63 | |
dan_ackme | 1:40ed62996887 | 64 | // Setup wiconnect serial interface configuration |
dan_ackme | 1:40ed62996887 | 65 | // Here we only specify the rx buffer size and not rx buffer pointer, this means |
dan_ackme | 1:40ed62996887 | 66 | // The serial RX buffer will be dynamically allocated |
dan_ackme | 1:40ed62996887 | 67 | SerialConfig serialConfig(WICONNECT_RX_PIN, WICONNECT_TX_PIN, 256, NULL); |
dan_ackme | 1:40ed62996887 | 68 | |
dan_ackme | 1:40ed62996887 | 69 | // Instantiate and initialize the Wiconnect library |
dan_ackme | 1:40ed62996887 | 70 | // Here we only specify the buffer size and not buffer pointer, this means |
dan_ackme | 1:40ed62996887 | 71 | // The internal buffer will be dynamically allocated |
dan_ackme | 1:40ed62996887 | 72 | Wiconnect wiconnect(serialConfig, 256, NULL, WICONNECT_RESET_PIN); |
dan_ackme | 1:40ed62996887 | 73 | |
dan_ackme | 1:40ed62996887 | 74 | // set the default timeout to 15s as some websites take awhile to respond |
dan_ackme | 1:40ed62996887 | 75 | wiconnect.setCommandDefaultTimeout(15000); |
dan_ackme | 1:40ed62996887 | 76 | |
dan_ackme | 1:40ed62996887 | 77 | //------------------------------------------------------------------------- |
dan_ackme | 1:40ed62996887 | 78 | // STEP 2: Initiate Communication with WiFi Module |
dan_ackme | 1:40ed62996887 | 79 | //------------------------------------------------------------------------- |
dan_ackme | 1:40ed62996887 | 80 | |
dan_ackme | 1:40ed62996887 | 81 | printf("Initializing WiConnect Library...\r\n"); |
dan_ackme | 1:40ed62996887 | 82 | |
dan_ackme | 1:40ed62996887 | 83 | // Initialize communication with WiFi module |
dan_ackme | 1:40ed62996887 | 84 | if(wiconnect.init(true) != WICONNECT_SUCCESS) |
dan_ackme | 1:40ed62996887 | 85 | { |
dan_ackme | 12:106e10b527f9 | 86 | if(result == WICONNECT_FIRMWARE_OUTDATED) |
dan_ackme | 12:106e10b527f9 | 87 | { |
dan_ackme | 12:106e10b527f9 | 88 | printf("The WiFi firmware is not supported. Run the ota example to update the firmware:\r\n"); |
dan_ackme | 12:106e10b527f9 | 89 | printf("https://developer.mbed.org/teams/ACKme/code/wiconnect-ota_example"); |
dan_ackme | 12:106e10b527f9 | 90 | } |
dan_ackme | 12:106e10b527f9 | 91 | else |
dan_ackme | 12:106e10b527f9 | 92 | { |
dan_ackme | 12:106e10b527f9 | 93 | printf("Failed to initialize communication with WiFi module!\r\n" |
dan_ackme | 12:106e10b527f9 | 94 | "Make sure the wires are connected correctly\r\n"); |
dan_ackme | 12:106e10b527f9 | 95 | } |
dan_ackme | 1:40ed62996887 | 96 | for(;;); // infinite loop |
dan_ackme | 1:40ed62996887 | 97 | } |
dan_ackme | 1:40ed62996887 | 98 | |
dan_ackme | 1:40ed62996887 | 99 | |
dan_ackme | 1:40ed62996887 | 100 | //------------------------------------------------------------------------- |
dan_ackme | 1:40ed62996887 | 101 | // STEP 3: Issue a HTTP GET request |
dan_ackme | 1:40ed62996887 | 102 | //------------------------------------------------------------------------- |
dan_ackme | 1:40ed62996887 | 103 | |
dan_ackme | 1:40ed62996887 | 104 | // Initiate a socket with an RX buffer of 256 bytes |
dan_ackme | 1:40ed62996887 | 105 | // We're not specifying the rx buffer pointer so that means it's dynamically allocated |
dan_ackme | 3:2bfba274568c | 106 | WiconnectSocket socket(256); |
dan_ackme | 1:40ed62996887 | 107 | |
dan_ackme | 1:40ed62996887 | 108 | |
dan_ackme | 1:40ed62996887 | 109 | printf("Issuing HTTP Request: %s\r\n", HTTP_URL); |
dan_ackme | 1:40ed62996887 | 110 | |
dan_ackme | 1:40ed62996887 | 111 | // Issue HTTP request |
dan_ackme | 1:40ed62996887 | 112 | // NOTE: the module must have valid network credentials |
dan_ackme | 1:40ed62996887 | 113 | if(wiconnect.httpGet(socket, HTTP_URL) != WICONNECT_SUCCESS) |
dan_ackme | 1:40ed62996887 | 114 | { |
dan_ackme | 1:40ed62996887 | 115 | printf("Failed to issue HTTP request. Does the module have valid network credentials?\r\n"); |
dan_ackme | 1:40ed62996887 | 116 | for(;;); // infinite loop |
dan_ackme | 1:40ed62996887 | 117 | } |
dan_ackme | 1:40ed62996887 | 118 | |
dan_ackme | 1:40ed62996887 | 119 | //------------------------------------------------------------------------- |
dan_ackme | 1:40ed62996887 | 120 | // STEP 4: Read the HTTP page and print |
dan_ackme | 1:40ed62996887 | 121 | //------------------------------------------------------------------------- |
dan_ackme | 1:40ed62996887 | 122 | |
dan_ackme | 1:40ed62996887 | 123 | uint8_t *dataPtr; |
dan_ackme | 1:40ed62996887 | 124 | uint16_t dataLength; |
dan_ackme | 1:40ed62996887 | 125 | |
dan_ackme | 1:40ed62996887 | 126 | // while there's data to read, retreive and print it |
dan_ackme | 1:40ed62996887 | 127 | while(socket.read(&dataPtr, &dataLength) == WICONNECT_SUCCESS) |
dan_ackme | 1:40ed62996887 | 128 | { |
dan_ackme | 1:40ed62996887 | 129 | fwrite(dataPtr, 1, dataLength, stdout); |
dan_ackme | 1:40ed62996887 | 130 | } |
dan_ackme | 1:40ed62996887 | 131 | |
dan_ackme | 1:40ed62996887 | 132 | //------------------------------------------------------------------------- |
dan_ackme | 1:40ed62996887 | 133 | // STEP 5: Done! |
dan_ackme | 1:40ed62996887 | 134 | //------------------------------------------------------------------------- |
dan_ackme | 1:40ed62996887 | 135 | |
dan_ackme | 1:40ed62996887 | 136 | printf("HTTP GET example has completed!\r\n"); |
dan_ackme | 1:40ed62996887 | 137 | |
dan_ackme | 6:f6cf38fba1d4 | 138 | while(true){} // infinite loop |
dan_ackme | 1:40ed62996887 | 139 | } |
dan_ackme | 1:40ed62996887 | 140 | |
dan_ackme | 1:40ed62996887 | 141 |