http client example

Dependencies:   C027_Support HTTPClient mbed

Fork of HTTPClient_HelloWorld by Donatien Garnier

main.cpp

Committer:
mazgch
Date:
2014-05-12
Revision:
3:412a526d7054
Parent:
2:270e2d0bb85a
Child:
4:7fd97087e573

File content as of revision 3:412a526d7054:

#include "mbed.h"
#include "HTTPClient.h"
#include "C027.h"
#include "MDM.h"

//----------------------------------------------------------------------
// You may need to configure these parameters

/** Set your secret SIM pin here "1234"
*/
#define SIMPIN      NULL

/** The APN of your network operator, sometimes it is "internet" 
    check your contract with the network operator
*/
#define APN         "gprs.swisscom.ch"

/** Set the user name for your APN, or NULL if not needed
*/
#define USERNAME    NULL

/** Set the password for your APN, or NULL if not needed
*/
#define PASSWORD    NULL 

C027 c027;

char str[512];

int main() 
{
    // turn on the supplies of the Modem and the GPS
    c027.mdmPower(true);
    wait(2);
    // Create the modem object
    MDMSerial mdm;
    
    // initialize the modem 
    printf("Modem Initialize\r\n");
    MDMParser::DevStatus devStatus;
    bool mdmOk = mdm.init(SIMPIN, &devStatus);
    if (mdmOk)
    {
        // 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;
    
            //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());
            }
    
            mdm.disconnect();
        }
    }
    mdm.powerOff();
    c027.mdmPower(false);
    
    while(true);
}