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

Dependencies:   WiConnect mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers example.cpp Source File

example.cpp

00001 /**
00002  * @example http_get/example.cpp
00003  *
00004  * This is an example of using the http socket API to
00005  * download a webpage.
00006  *
00007  * It works as follows:
00008  * 1. Instantiate the WiConnect Library
00009  * 2. Initiate Communication with WiFi Module
00010  * 3. Issue a HTTP GET request (Note: The module must have valid network credentials See @ref web_setup/example.cpp)
00011  * 4. Read the HTTP page and print
00012  * 5. That's it!
00013  *
00014  */
00015 
00016 
00017 /******************************************************************************
00018  * Example Variables
00019  */
00020 
00021 // the URL of the webpage to GET
00022 #define HTTP_URL "http://mbed.org"
00023 
00024 
00025 
00026 
00027 
00028 /******************************************************************************
00029  * Includes
00030  */
00031 
00032 // include C library headers
00033 #include <stdio.h> // needed for printf
00034 
00035 // include target specific defines
00036 #include "target_config.h"
00037 // include the Wiconnect Host Library API header
00038 #include "Wiconnect.h"
00039 
00040 
00041 
00042 
00043 /******************************************************************************
00044  * Global Defines
00045  */
00046 
00047 
00048 // Serial used for printfs to terminal (i.e. NOT used for WiConnect)
00049 static Serial consoleSerial(STDIO_UART_TX, STDIO_UART_RX);
00050 
00051 
00052 
00053 /******************************************************************************
00054  * Starting point of application
00055  */
00056 int main(int argc, char **argv)
00057 {
00058     WiconnectResult result;
00059     
00060     consoleSerial.baud(115200); // console terminal to 115200 baud
00061 
00062     //-------------------------------------------------------------------------
00063     // STEP 1: Instantiate WiConnect Library
00064     //-------------------------------------------------------------------------
00065 
00066     // Setup  wiconnect serial interface configuration
00067     // Here we only specify the rx buffer size and not rx buffer pointer, this means
00068     // The serial RX buffer will be dynamically allocated
00069     SerialConfig serialConfig(WICONNECT_RX_PIN, WICONNECT_TX_PIN, 256, NULL);
00070 
00071     // Instantiate and initialize the Wiconnect library
00072     // Here we only specify the buffer size and not buffer pointer, this means
00073     // The internal buffer will be dynamically allocated
00074     Wiconnect wiconnect(serialConfig, 256, NULL, WICONNECT_RESET_PIN);
00075 
00076     // set the default timeout to 15s as some websites take awhile to respond
00077     wiconnect.setCommandDefaultTimeout(15000);
00078 
00079     //-------------------------------------------------------------------------
00080     // STEP 2: Initiate Communication with WiFi Module
00081     //-------------------------------------------------------------------------
00082 
00083     printf("Initializing WiConnect Library...\r\n");
00084 
00085     // Initialize communication with WiFi module
00086     if(WICONNECT_FAILED(result, wiconnect.init(true)))
00087     {
00088         if(result == WICONNECT_FIRMWARE_OUTDATED)
00089         {
00090             printf("** The WiFi firmware is not supported. Run the ota example to update the firmware:\r\n");
00091             printf("https://developer.mbed.org/teams/ACKme/code/wiconnect-ota_example\r\n\r\n");
00092         }
00093         else
00094         {
00095             printf("Failed to initialize communication with WiFi module!\r\n"
00096                    "Make sure the wires are connected correctly\r\n");
00097         }
00098         for(;;); // infinite loop
00099     }
00100 
00101 
00102     //-------------------------------------------------------------------------
00103     // STEP 3: Issue a HTTP GET request
00104     //-------------------------------------------------------------------------
00105 
00106     // Initiate a socket with an RX buffer of 256 bytes
00107     // We're not specifying the rx buffer pointer so that means it's dynamically allocated
00108     WiconnectSocket socket(256);
00109 
00110 
00111     printf("Issuing HTTP Request: %s\r\n", HTTP_URL);
00112 
00113     // Issue HTTP request
00114     // NOTE: the module must have valid network credentials
00115     if(wiconnect.httpGet(socket, HTTP_URL) != WICONNECT_SUCCESS)
00116     {
00117         printf("Failed to issue HTTP request. Does the module have valid network credentials?\r\n");
00118         printf("Use the network join example to configure the module's network credentials:\r\n");
00119         printf("https://developer.mbed.org/teams/ACKme/code/wiconnect-join_example\r\n");
00120         for(;;); // infinite loop
00121     }
00122 
00123     //-------------------------------------------------------------------------
00124     // STEP 4: Read the HTTP page and print
00125     //-------------------------------------------------------------------------
00126 
00127     uint8_t *dataPtr;
00128     uint16_t dataLength;
00129 
00130     // while there's data to read, retreive and print it
00131     while(socket.read(&dataPtr, &dataLength) == WICONNECT_SUCCESS)
00132     {
00133         fwrite(dataPtr, 1, dataLength, stdout);
00134     }
00135 
00136     //-------------------------------------------------------------------------
00137     // STEP 5: Done!
00138     //-------------------------------------------------------------------------
00139 
00140     printf("HTTP GET example has completed!\r\n");
00141 
00142     while(true){} // infinite loop
00143 }
00144 
00145