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

Dependencies:   WiConnect mbed

Committer:
dan_ackme
Date:
Wed Aug 13 04:41:33 2014 -0700
Revision:
1:40ed62996887
Child:
3:2bfba274568c
renamed file

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 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 1:40ed62996887 86 printf("Failed to initialize communication with WiFi module!\r\n"
dan_ackme 1:40ed62996887 87 "Make sure the wires are connected correctly\r\n");
dan_ackme 1:40ed62996887 88 for(;;); // infinite loop
dan_ackme 1:40ed62996887 89 }
dan_ackme 1:40ed62996887 90
dan_ackme 1:40ed62996887 91
dan_ackme 1:40ed62996887 92 //-------------------------------------------------------------------------
dan_ackme 1:40ed62996887 93 // STEP 3: Issue a HTTP GET request
dan_ackme 1:40ed62996887 94 //-------------------------------------------------------------------------
dan_ackme 1:40ed62996887 95
dan_ackme 1:40ed62996887 96 // Initiate a socket with an RX buffer of 256 bytes
dan_ackme 1:40ed62996887 97 // We're not specifying the rx buffer pointer so that means it's dynamically allocated
dan_ackme 1:40ed62996887 98 Socket socket(256);
dan_ackme 1:40ed62996887 99
dan_ackme 1:40ed62996887 100
dan_ackme 1:40ed62996887 101 printf("Issuing HTTP Request: %s\r\n", HTTP_URL);
dan_ackme 1:40ed62996887 102
dan_ackme 1:40ed62996887 103 // Issue HTTP request
dan_ackme 1:40ed62996887 104 // NOTE: the module must have valid network credentials
dan_ackme 1:40ed62996887 105 if(wiconnect.httpGet(socket, HTTP_URL) != WICONNECT_SUCCESS)
dan_ackme 1:40ed62996887 106 {
dan_ackme 1:40ed62996887 107 printf("Failed to issue HTTP request. Does the module have valid network credentials?\r\n");
dan_ackme 1:40ed62996887 108 for(;;); // infinite loop
dan_ackme 1:40ed62996887 109 }
dan_ackme 1:40ed62996887 110
dan_ackme 1:40ed62996887 111 //-------------------------------------------------------------------------
dan_ackme 1:40ed62996887 112 // STEP 4: Read the HTTP page and print
dan_ackme 1:40ed62996887 113 //-------------------------------------------------------------------------
dan_ackme 1:40ed62996887 114
dan_ackme 1:40ed62996887 115 uint8_t *dataPtr;
dan_ackme 1:40ed62996887 116 uint16_t dataLength;
dan_ackme 1:40ed62996887 117
dan_ackme 1:40ed62996887 118 // while there's data to read, retreive and print it
dan_ackme 1:40ed62996887 119 while(socket.read(&dataPtr, &dataLength) == WICONNECT_SUCCESS)
dan_ackme 1:40ed62996887 120 {
dan_ackme 1:40ed62996887 121 fwrite(dataPtr, 1, dataLength, stdout);
dan_ackme 1:40ed62996887 122 }
dan_ackme 1:40ed62996887 123
dan_ackme 1:40ed62996887 124 //-------------------------------------------------------------------------
dan_ackme 1:40ed62996887 125 // STEP 5: Done!
dan_ackme 1:40ed62996887 126 //-------------------------------------------------------------------------
dan_ackme 1:40ed62996887 127
dan_ackme 1:40ed62996887 128 printf("HTTP GET example has completed!\r\n");
dan_ackme 1:40ed62996887 129
dan_ackme 1:40ed62996887 130 for(;;); // infinite loop
dan_ackme 1:40ed62996887 131
dan_ackme 1:40ed62996887 132 return 0;
dan_ackme 1:40ed62996887 133 }
dan_ackme 1:40ed62996887 134
dan_ackme 1:40ed62996887 135