test

Dependencies:   C027 HTTPClient UbloxUSBModem mbed

Fork of C027_HTTPClientTest by u-blox

main.cpp

Committer:
mazgch
Date:
2014-03-03
Revision:
16:b2b75c8a01fa
Parent:
15:9350ec2ec1cd
Parent:
12:393f2a870b42

File content as of revision 16:b2b75c8a01fa:

#include "mbed.h"
#include "C027.h"
#include "UbloxModem.h"

#include "HTTPClient.h"

C027 c027;

#ifndef MODEM_APN
#warning APN not specified, using "internet"
#define MODEM_APN "internet"
#endif

#ifndef MODEM_USERNAME
#warning username not specified
#define MODEM_USERNAME NULL
#endif

#ifndef MODEM_PASSWORD
#warning password not specified
#define MODEM_PASSWORD NULL
#endif

void test(void const*)
{
#if 1 // Switch this to select between the Serial and USB interface
    // Serial is supported by LISA-C, LISA-U and SARA-G
    c027.mdmPower(true);
    wait_ms(5000);
    UbloxSerModem modem;
#else
    // USB is supported by LISA-C or LISA-U
    c027.mdmUsbEnable(true);
    c027.mdmPower(true);
    wait_ms(5000);
    UbloxUSBModem modem;
#endif

    HTTPClient http;
    char str[512];

    int ret;
    for (;;) 
    {
        ret = modem.connect(MODEM_APN, MODEM_USERNAME, MODEM_PASSWORD); // eventaully set another apn here
        if (!ret)
            break;
        wait_ms(1000);
    }
    
    //GET data
    printf("Trying to fetch page...\n");
    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 text(str, 512);
    map.put("Hello", "World");
    map.put("test", "1234");
    printf("Trying to post data...\n");
    ret = http.post("http://httpbin.org/post", map, &text);
    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());
    }

    modem.disconnect();
    c027.mdmPower(false);
    
    while(1) {
    }
}


int main()
{
    Thread testTask(test, NULL, osPriorityNormal, 1024 * 4);
    DigitalOut led(LED); // on rev A you should reasign the signal to A0
    while(1) {
        led=!led;
        Thread::wait(1000);
    }

    return 0;
}