Example program demonstrating use of HTTP or HTTPS protocol over a cellular connection with httpbin.org using the MTSAS library. (Demonstrates GET,POST,basic-auth)

Dependencies:   mbed mtsas

main.cpp

Committer:
Vanger
Date:
2014-08-05
Revision:
2:e20a75d47720
Parent:
1:7a2c5889a55d
Child:
3:85181c1324f2

File content as of revision 2:e20a75d47720:

#include "mbed.h"
#include "mtsas.h"

int main(){
    //Modify to match your apn if you are using an HSPA radio with a SIM card
    const char APN[] = "";
    
    /* Sets the log level for INFO level. INFO is a midway log level, available levels are: TRACE, DEBUG, INFO, WARNING, ERROR, FATAL, NONE */
    MTSLog::setLogLevel(MTSLog::INFO_LEVEL);
    
    //Receive buffer
    char rbuf[4000];
    
    //url strings for HTTP requests, httpbin.org is a public website for fielding test HTTP requests
    string url, base_url = "http://httpbin.org:80", url_get = "/get";
    
    /** STMicro Nucelo F401RE
    * The supported jumper configurations of the MTSAS do not line up with
    * the pin mapping of the Nucleo F401RE. Therefore, the MTSAS serial TX
    * pin (JP8 Pin 2) must be manually jumped to Serial1 RX (Shield pin D2)
    * and the MTSAS serial RX pin (JP9 Pin 2) pin must be manually jumped to
    * Serial1 TX (Shield pin D8).
    * Uncomment the following line to use the STMicro Nuceleo F401RE
    */
    MTSSerialFlowControl* io = new MTSSerialFlowControl(D8, D2, D3, D6);
    
    /** Freescale KL46Z
    * To configure the pins for the Freescale KL46Z board, use configuration B
    * for the SocketModem. The TX pin should be jumped to pin D2 (JP8), and the
    * RX pin should be jumped to pin D9 (JP9). 
    * Uncomment te following line to use the Freescale KL46Z board
    */
    //MTSSerialFlowControl* io = new MTSSerialFlowControl(D2, D9, D3, D6);
    
    /** Freescale KL64F
    * To configure the pins for the Freescale KL46Z board, use configuration A
    * for the SocketModem. The TX pin should be jumped to pin D1 (JP8), and the
    * RX pin should be jumped to pin D0 (JP9). 
    * Uncomment te following line to use the Freescale KL46F board
    */
    //MTSSerialFlowControl* io = new MTSSerialFlowControl(D1, D0, D3, D6);
    
    //Sets the baudrate for communicating with the radio
    io->baud(115200); 
    
    //Initialize radio configurations
    Cellular* radio = CellularFactory::create(io);
    if( ! radio) {
        logFatal("Radio initialization failed");
    }
    radio->configureSignals(D4,D7,RESET);
    Transport::setTransport(radio);
    
    //Set up HTTP interface
    HTTPClient* http = new HTTPClient();
    HTTPText* receive = new HTTPText(rbuf);
    
    //Set APN value
    radio->setApn(APN);
    
    //Establish PPP link
    radio->connect();
    
    //Format HTTP Get request
    url = base_url + url_get;
    
    //Send HTTP GET request
    for (int i = 0; i < 10; i++) {
        if(http->get(url.c_str(), receive, 5000) == HTTP_OK) {
            http->get(url.c_str(), receive, 5000);
            logInfo("HTTP get succeeded");
            break;
        }
        if (i >= 10) {
            logError("HTTP GET failed");
        }
        wait(1);
    }
    
    //Disconnect PPP link
    radio->disconnect();
    
    logInfo("End of example code\n");
    return 0;
}