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

Revision:
2:e20a75d47720
Parent:
1:7a2c5889a55d
Child:
3:85181c1324f2
--- a/main.cpp	Thu Jul 24 20:03:05 2014 +0000
+++ b/main.cpp	Tue Aug 05 14:43:29 2014 +0000
@@ -3,13 +3,16 @@
 
 int main(){
     //Modify to match your apn if you are using an HSPA radio with a SIM card
-    const char APN[] = "internet";
+    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);
     
-    //HTTP Test server is httpbin.org, the RequestLine is the HTTP request string
-    const char RequestLine[] = "GET /get HTTP/1.1\r\nHost: httpbin.org\r\nContent-type: application/MTSAS-DEV\r\nContent-length: 0\r\n\r\n";
+    //Receive buffer
+    char rbuf[4000];
     
-    //Response character array to read in the HTTP response
-    char read_response[4096] = {0};
+    //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
@@ -40,26 +43,43 @@
     //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();
     
-    //Open TCP socket to httpbin.org
-    radio->open("httpbin.org", 80, IPStack::TCP);
+    //Format HTTP Get request
+    url = base_url + url_get;
     
-    //Send HTTP request
-    radio->write(RequestLine, sizeof(RequestLine), 2000);
-    printf("HTTP Request sent:\n\n%s\n\n",RequestLine);
-    
-    //Read HTTP response from server
-    radio->read(read_response, 4097, 2000);
-    printf("HTTP Response read:\n\n%s\n\n", read_response);
+    //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();
     
-    printf("End of example code\n");
+    logInfo("End of example code\n");
     return 0;
 }
\ No newline at end of file