u-blox modem HTTP client test

Dependencies:   UbloxUSBModem mbed

Committer:
mbed_official
Date:
Tue Jun 03 11:30:51 2014 +0100
Revision:
6:d17e425e9425
Parent:
3:03b3ef627772
Synchronized with git revision bcacbb9fbf3432829227430830cca4315b57c1b9

Full URL: https://github.com/mbedmicro/mbed/commit/bcacbb9fbf3432829227430830cca4315b57c1b9/

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bogdanm 1:0112fc45285a 1 #include "mbed.h"
bogdanm 1:0112fc45285a 2 #include "CellularModem.h"
bogdanm 1:0112fc45285a 3 #include "HTTPClient.h"
bogdanm 1:0112fc45285a 4 #include "httptest.h"
bogdanm 1:0112fc45285a 5
bogdanm 1:0112fc45285a 6 int httptest(CellularModem& modem, const char* apn, const char* username, const char* password)
mbed_official 6:d17e425e9425 7 {
bogdanm 1:0112fc45285a 8 printf("Connecting...\n");
mbed_official 6:d17e425e9425 9
bogdanm 1:0112fc45285a 10 HTTPClient http;
bogdanm 1:0112fc45285a 11 char str[512];
bogdanm 1:0112fc45285a 12
mbed_official 3:03b3ef627772 13 modem.power(true);
mbed_official 3:03b3ef627772 14 Thread::wait(1000);
bogdanm 1:0112fc45285a 15 int ret = modem.connect(apn, username, password);
bogdanm 1:0112fc45285a 16 if(ret)
bogdanm 1:0112fc45285a 17 {
bogdanm 1:0112fc45285a 18 printf("Could not connect\n");
bogdanm 1:0112fc45285a 19 return false;
bogdanm 1:0112fc45285a 20 }
mbed_official 6:d17e425e9425 21
bogdanm 1:0112fc45285a 22 //GET data
bogdanm 1:0112fc45285a 23 printf("Trying to fetch page...\n");
bogdanm 1:0112fc45285a 24 ret = http.get("http://mbed.org/media/uploads/donatien/hello.txt", str, 128);
bogdanm 1:0112fc45285a 25 if (!ret)
bogdanm 1:0112fc45285a 26 {
bogdanm 1:0112fc45285a 27 printf("Page fetched successfully - read %d characters\n", strlen(str));
bogdanm 1:0112fc45285a 28 printf("Result: %s\n", str);
bogdanm 1:0112fc45285a 29 }
bogdanm 1:0112fc45285a 30 else
bogdanm 1:0112fc45285a 31 {
bogdanm 1:0112fc45285a 32 printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
bogdanm 1:0112fc45285a 33 modem.disconnect();
bogdanm 1:0112fc45285a 34 return false;
bogdanm 1:0112fc45285a 35 }
mbed_official 6:d17e425e9425 36
bogdanm 1:0112fc45285a 37 //POST data
bogdanm 1:0112fc45285a 38 HTTPMap map;
bogdanm 1:0112fc45285a 39 HTTPText text(str, 512);
bogdanm 1:0112fc45285a 40 map.put("Hello", "World");
bogdanm 1:0112fc45285a 41 map.put("test", "1234");
bogdanm 1:0112fc45285a 42 printf("Trying to post data...\n");
bogdanm 1:0112fc45285a 43 ret = http.post("http://httpbin.org/post", map, &text);
bogdanm 1:0112fc45285a 44 if (!ret)
bogdanm 1:0112fc45285a 45 {
bogdanm 1:0112fc45285a 46 printf("Executed POST successfully - read %d characters\n", strlen(str));
bogdanm 1:0112fc45285a 47 printf("Result: %s\n", str);
bogdanm 1:0112fc45285a 48 }
bogdanm 1:0112fc45285a 49 else
bogdanm 1:0112fc45285a 50 {
bogdanm 1:0112fc45285a 51 printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
bogdanm 1:0112fc45285a 52 modem.disconnect();
bogdanm 1:0112fc45285a 53 return false;
bogdanm 1:0112fc45285a 54 }
mbed_official 6:d17e425e9425 55
bogdanm 1:0112fc45285a 56 modem.disconnect();
bogdanm 1:0112fc45285a 57 return true;
bogdanm 1:0112fc45285a 58 }