http client example

Dependencies:   C027_Support HTTPClient mbed

Fork of HTTPClient_HelloWorld by Donatien Garnier

Files at this revision

API Documentation at this revision

Comitter:
mazgch
Date:
Mon May 12 13:58:58 2014 +0000
Parent:
3:412a526d7054
Child:
5:a18ddbfd70c9
Commit message:
use latest lib

Changed in this revision

C027_Support.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/C027_Support.lib	Mon May 12 09:17:35 2014 +0000
+++ b/C027_Support.lib	Mon May 12 13:58:58 2014 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/teams/ublox/code/C027_Support/#8071747a7cb3
+http://mbed.org/teams/ublox/code/C027_Support/#869bd35f44cc
--- a/main.cpp	Mon May 12 09:17:35 2014 +0000
+++ b/main.cpp	Mon May 12 13:58:58 2014 +0000
@@ -31,95 +31,77 @@
 {
     // turn on the supplies of the Modem and the GPS
     c027.mdmPower(true);
-    wait(2);
-    // Create the modem object
+    printf("Modem Initialize\r\n");
     MDMSerial mdm;
+    if (!mdm.connect(SIMPIN, APN,USERNAME,PASSWORD, true))
+        return -1;
     
-    // initialize the modem 
-    printf("Modem Initialize\r\n");
-    MDMParser::DevStatus devStatus;
-    bool mdmOk = mdm.init(SIMPIN, &devStatus);
-    if (mdmOk)
+    HTTPClient http;
+    
+    //GET data
+    printf("\nTrying to fetch page...\n");
+    int ret = http.get("http://mbed.org/media/uploads/donatien/hello.txt", str, 128);
+    if (!ret)
     {
-        // wait until we are connected
-        printf("Network Check\r\n");
-        MDMParser::NetStatus netStatus;
-        while (!mdm.checkNetStatus(&netStatus))
-            wait_ms(1000);
-    
-        printf("Network Join\r\n");
-        // join the internet connection 
-        MDMParser::IP ip = mdm.join(APN,USERNAME,PASSWORD);
-        if (ip != NOIP)
-        {
-            printf("  IP Address: " IPSTR "\r\n", IPNUM(ip));
-            HTTPClient http;
+      printf("Page fetched successfully - read %d characters\n", strlen(str));
+      printf("Result: %s\n", str);
+    }
+    else
+    {
+      printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
+    }
     
-            //GET data
-            printf("\nTrying to fetch page...\n");
-            int ret = http.get("http://mbed.org/media/uploads/donatien/hello.txt", str, 128);
-            if (!ret)
-            {
-              printf("Page fetched successfully - read %d characters\n", strlen(str));
-              printf("Result: %s\n", str);
-            }
-            else
-            {
-              printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
-            }
-            
-            //POST data
-            HTTPMap map;
-            HTTPText inText(str, 512);
-            map.put("Hello", "World");
-            map.put("test", "1234");
-            printf("\nTrying to post data...\n");
-            ret = http.post("http://httpbin.org/post", map, &inText);
-            if (!ret)
-            {
-              printf("Executed POST successfully - read %d characters\n", strlen(str));
-              printf("Result: %s\n", str);
-            }
-            else
-            {
-              printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
-            }
-            
-            //PUT data
-            strcpy(str, "This is a PUT test!");
-            HTTPText outText(str);
-            //HTTPText inText(str, 512);
-            printf("\nTrying to put resource...\n");
-            ret = http.put("http://httpbin.org/put", outText, &inText);
-            if (!ret)
-            {
-              printf("Executed PUT successfully - read %d characters\n", strlen(str));
-              printf("Result: %s\n", str);
-            }
-            else
-            {
-              printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
-            }
-            
-            //DELETE data
-            //HTTPText inText(str, 512);
-            printf("\nTrying to delete resource...\n");
-            ret = http.del("http://httpbin.org/delete", &inText);
-            if (!ret)
-            {
-              printf("Executed DELETE successfully - read %d characters\n", strlen(str));
-              printf("Result: %s\n", str);
-            }
-            else
-            {
-              printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
-            }
+    //POST data
+    HTTPMap map;
+    HTTPText inText(str, 512);
+    map.put("Hello", "World");
+    map.put("test", "1234");
+    printf("\nTrying to post data...\n");
+    ret = http.post("http://httpbin.org/post", map, &inText);
+    if (!ret)
+    {
+      printf("Executed POST successfully - read %d characters\n", strlen(str));
+      printf("Result: %s\n", str);
+    }
+    else
+    {
+      printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
+    }
     
-            mdm.disconnect();
-        }
+    //PUT data
+    strcpy(str, "This is a PUT test!");
+    HTTPText outText(str);
+    //HTTPText inText(str, 512);
+    printf("\nTrying to put resource...\n");
+    ret = http.put("http://httpbin.org/put", outText, &inText);
+    if (!ret)
+    {
+      printf("Executed PUT successfully - read %d characters\n", strlen(str));
+      printf("Result: %s\n", str);
+    }
+    else
+    {
+      printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
     }
+    
+    //DELETE data
+    //HTTPText inText(str, 512);
+    printf("\nTrying to delete resource...\n");
+    ret = http.del("http://httpbin.org/delete", &inText);
+    if (!ret)
+    {
+      printf("Executed DELETE successfully - read %d characters\n", strlen(str));
+      printf("Result: %s\n", str);
+    }
+    else
+    {
+      printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
+    }
+
+    mdm.disconnect();
     mdm.powerOff();
     c027.mdmPower(false);
+    printf("Done\n");
     
     while(true);
 }