Simple app demonstrating the HTTP GET feature of the WiConnect Host Library.

Dependencies:   WiConnect mbed

Committer:
dan_ackme
Date:
Thu Nov 27 10:29:35 2014 +0000
Revision:
13:32538c7842e6
Parent:
12:106e10b527f9
updated for latest wiconnect

Who changed what in which revision?

UserRevisionLine numberNew 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 13:32538c7842e6 22 #define HTTP_URL "http://mbed.org"
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 13:32538c7842e6 58 WiconnectResult result;
dan_ackme 13:32538c7842e6 59
dan_ackme 1:40ed62996887 60 consoleSerial.baud(115200); // console terminal to 115200 baud
dan_ackme 1:40ed62996887 61
dan_ackme 1:40ed62996887 62 //-------------------------------------------------------------------------
dan_ackme 1:40ed62996887 63 // STEP 1: Instantiate WiConnect Library
dan_ackme 1:40ed62996887 64 //-------------------------------------------------------------------------
dan_ackme 1:40ed62996887 65
dan_ackme 1:40ed62996887 66 // Setup wiconnect serial interface configuration
dan_ackme 1:40ed62996887 67 // Here we only specify the rx buffer size and not rx buffer pointer, this means
dan_ackme 1:40ed62996887 68 // The serial RX buffer will be dynamically allocated
dan_ackme 1:40ed62996887 69 SerialConfig serialConfig(WICONNECT_RX_PIN, WICONNECT_TX_PIN, 256, NULL);
dan_ackme 1:40ed62996887 70
dan_ackme 1:40ed62996887 71 // Instantiate and initialize the Wiconnect library
dan_ackme 1:40ed62996887 72 // Here we only specify the buffer size and not buffer pointer, this means
dan_ackme 1:40ed62996887 73 // The internal buffer will be dynamically allocated
dan_ackme 1:40ed62996887 74 Wiconnect wiconnect(serialConfig, 256, NULL, WICONNECT_RESET_PIN);
dan_ackme 1:40ed62996887 75
dan_ackme 1:40ed62996887 76 // set the default timeout to 15s as some websites take awhile to respond
dan_ackme 1:40ed62996887 77 wiconnect.setCommandDefaultTimeout(15000);
dan_ackme 1:40ed62996887 78
dan_ackme 1:40ed62996887 79 //-------------------------------------------------------------------------
dan_ackme 1:40ed62996887 80 // STEP 2: Initiate Communication with WiFi Module
dan_ackme 1:40ed62996887 81 //-------------------------------------------------------------------------
dan_ackme 1:40ed62996887 82
dan_ackme 1:40ed62996887 83 printf("Initializing WiConnect Library...\r\n");
dan_ackme 1:40ed62996887 84
dan_ackme 1:40ed62996887 85 // Initialize communication with WiFi module
dan_ackme 13:32538c7842e6 86 if(WICONNECT_FAILED(result, wiconnect.init(true)))
dan_ackme 1:40ed62996887 87 {
dan_ackme 12:106e10b527f9 88 if(result == WICONNECT_FIRMWARE_OUTDATED)
dan_ackme 12:106e10b527f9 89 {
dan_ackme 13:32538c7842e6 90 printf("** The WiFi firmware is not supported. Run the ota example to update the firmware:\r\n");
dan_ackme 13:32538c7842e6 91 printf("https://developer.mbed.org/teams/ACKme/code/wiconnect-ota_example\r\n\r\n");
dan_ackme 12:106e10b527f9 92 }
dan_ackme 12:106e10b527f9 93 else
dan_ackme 12:106e10b527f9 94 {
dan_ackme 12:106e10b527f9 95 printf("Failed to initialize communication with WiFi module!\r\n"
dan_ackme 12:106e10b527f9 96 "Make sure the wires are connected correctly\r\n");
dan_ackme 12:106e10b527f9 97 }
dan_ackme 1:40ed62996887 98 for(;;); // infinite loop
dan_ackme 1:40ed62996887 99 }
dan_ackme 1:40ed62996887 100
dan_ackme 1:40ed62996887 101
dan_ackme 1:40ed62996887 102 //-------------------------------------------------------------------------
dan_ackme 1:40ed62996887 103 // STEP 3: Issue a HTTP GET request
dan_ackme 1:40ed62996887 104 //-------------------------------------------------------------------------
dan_ackme 1:40ed62996887 105
dan_ackme 1:40ed62996887 106 // Initiate a socket with an RX buffer of 256 bytes
dan_ackme 1:40ed62996887 107 // We're not specifying the rx buffer pointer so that means it's dynamically allocated
dan_ackme 3:2bfba274568c 108 WiconnectSocket socket(256);
dan_ackme 1:40ed62996887 109
dan_ackme 1:40ed62996887 110
dan_ackme 1:40ed62996887 111 printf("Issuing HTTP Request: %s\r\n", HTTP_URL);
dan_ackme 1:40ed62996887 112
dan_ackme 1:40ed62996887 113 // Issue HTTP request
dan_ackme 1:40ed62996887 114 // NOTE: the module must have valid network credentials
dan_ackme 1:40ed62996887 115 if(wiconnect.httpGet(socket, HTTP_URL) != WICONNECT_SUCCESS)
dan_ackme 1:40ed62996887 116 {
dan_ackme 1:40ed62996887 117 printf("Failed to issue HTTP request. Does the module have valid network credentials?\r\n");
dan_ackme 13:32538c7842e6 118 printf("Use the network join example to configure the module's network credentials:\r\n");
dan_ackme 13:32538c7842e6 119 printf("https://developer.mbed.org/teams/ACKme/code/wiconnect-join_example\r\n");
dan_ackme 1:40ed62996887 120 for(;;); // infinite loop
dan_ackme 1:40ed62996887 121 }
dan_ackme 1:40ed62996887 122
dan_ackme 1:40ed62996887 123 //-------------------------------------------------------------------------
dan_ackme 1:40ed62996887 124 // STEP 4: Read the HTTP page and print
dan_ackme 1:40ed62996887 125 //-------------------------------------------------------------------------
dan_ackme 1:40ed62996887 126
dan_ackme 1:40ed62996887 127 uint8_t *dataPtr;
dan_ackme 1:40ed62996887 128 uint16_t dataLength;
dan_ackme 1:40ed62996887 129
dan_ackme 1:40ed62996887 130 // while there's data to read, retreive and print it
dan_ackme 1:40ed62996887 131 while(socket.read(&dataPtr, &dataLength) == WICONNECT_SUCCESS)
dan_ackme 1:40ed62996887 132 {
dan_ackme 1:40ed62996887 133 fwrite(dataPtr, 1, dataLength, stdout);
dan_ackme 1:40ed62996887 134 }
dan_ackme 1:40ed62996887 135
dan_ackme 1:40ed62996887 136 //-------------------------------------------------------------------------
dan_ackme 1:40ed62996887 137 // STEP 5: Done!
dan_ackme 1:40ed62996887 138 //-------------------------------------------------------------------------
dan_ackme 1:40ed62996887 139
dan_ackme 1:40ed62996887 140 printf("HTTP GET example has completed!\r\n");
dan_ackme 1:40ed62996887 141
dan_ackme 6:f6cf38fba1d4 142 while(true){} // infinite loop
dan_ackme 1:40ed62996887 143 }
dan_ackme 1:40ed62996887 144
dan_ackme 1:40ed62996887 145