NTPClientTest, working with SARA-G350 on C027
Dependencies: C027 NTPClient UbloxUSBModem mbed
Fork of C027_USSDTest_SARA350 by
main.cpp@12:88552ae75244, 2014-03-03 (annotated)
- Committer:
- mazgch
- Date:
- Mon Mar 03 07:49:48 2014 +0000
- Revision:
- 12:88552ae75244
- Parent:
- 10:e2315bcdd7be
- Child:
- 13:9350ec2ec1cd
Updated HttpClientTest; Need to use mbed libraries revision 74 (the new flow control handling breaks the serial port);
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
donatien | 0:f3f18ac13e0c | 1 | #include "mbed.h" |
mazgch | 6:77c131006c67 | 2 | #include "C027.h" |
mazgch | 12:88552ae75244 | 3 | #include "UbloxModem.h" |
mazgch | 6:77c131006c67 | 4 | |
donatien | 0:f3f18ac13e0c | 5 | #include "HTTPClient.h" |
donatien | 0:f3f18ac13e0c | 6 | |
mazgch | 10:e2315bcdd7be | 7 | C027 c027; |
mazgch | 10:e2315bcdd7be | 8 | |
mazgch | 12:88552ae75244 | 9 | #define MODEM_APN "gprs.swisscom.ch" |
mazgch | 12:88552ae75244 | 10 | |
mazgch | 12:88552ae75244 | 11 | #ifndef MODEM_APN |
mazgch | 12:88552ae75244 | 12 | #warning APN not specified, using "internet" |
mazgch | 12:88552ae75244 | 13 | #define MODEM_APN "internet" |
mazgch | 12:88552ae75244 | 14 | #endif |
mazgch | 12:88552ae75244 | 15 | |
mazgch | 12:88552ae75244 | 16 | #ifndef MODEM_USERNAME |
mazgch | 12:88552ae75244 | 17 | #warning username not specified |
mazgch | 12:88552ae75244 | 18 | #define MODEM_USERNAME NULL |
mazgch | 12:88552ae75244 | 19 | #endif |
mazgch | 12:88552ae75244 | 20 | |
mazgch | 12:88552ae75244 | 21 | #ifndef MODEM_PASSWORD |
mazgch | 12:88552ae75244 | 22 | #warning password not specified |
mazgch | 12:88552ae75244 | 23 | #define MODEM_PASSWORD NULL |
mazgch | 12:88552ae75244 | 24 | #endif |
mazgch | 12:88552ae75244 | 25 | |
mazgch | 7:c558c74ceb0f | 26 | void test(void const*) |
donatien | 0:f3f18ac13e0c | 27 | { |
mazgch | 12:88552ae75244 | 28 | #if 1 |
mazgch | 12:88552ae75244 | 29 | // Serial is supported by LISA-C, LISA-U and SARA-G |
mazgch | 10:e2315bcdd7be | 30 | c027.mdmPower(true); |
mazgch | 12:88552ae75244 | 31 | wait_ms(5000); |
mazgch | 12:88552ae75244 | 32 | UbloxSerModem modem; |
mazgch | 12:88552ae75244 | 33 | #else |
mazgch | 12:88552ae75244 | 34 | // USB is supported by LISA-C or LISA-U |
mazgch | 12:88552ae75244 | 35 | c027.mdmUsbEnable(true); |
mazgch | 12:88552ae75244 | 36 | c027.mdmPower(true); |
mazgch | 12:88552ae75244 | 37 | wait_ms(5000); |
mazgch | 12:88552ae75244 | 38 | UbloxUSBModem modem; |
mazgch | 12:88552ae75244 | 39 | #endif |
mazgch | 12:88552ae75244 | 40 | |
donatien | 1:6ea9ac27702c | 41 | HTTPClient http; |
donatien | 1:6ea9ac27702c | 42 | char str[512]; |
mazgch | 7:c558c74ceb0f | 43 | |
mazgch | 12:88552ae75244 | 44 | int ret; |
mazgch | 12:88552ae75244 | 45 | for (;;) |
mazgch | 12:88552ae75244 | 46 | { |
mazgch | 12:88552ae75244 | 47 | ret = modem.connect(MODEM_APN, MODEM_USERNAME, MODEM_PASSWORD); // eventaully set another apn here |
mazgch | 12:88552ae75244 | 48 | if (!ret) |
mazgch | 12:88552ae75244 | 49 | break; |
mazgch | 12:88552ae75244 | 50 | wait_ms(1000); |
mazgch | 12:88552ae75244 | 51 | } |
mazgch | 8:95a97898084a | 52 | if(ret) |
mazgch | 8:95a97898084a | 53 | { |
mazgch | 8:95a97898084a | 54 | printf("Could not connect %d\n", ret); |
mazgch | 7:c558c74ceb0f | 55 | return; |
donatien | 1:6ea9ac27702c | 56 | } |
mazgch | 8:95a97898084a | 57 | |
donatien | 1:6ea9ac27702c | 58 | //GET data |
donatien | 1:6ea9ac27702c | 59 | printf("Trying to fetch page...\n"); |
donatien | 0:f3f18ac13e0c | 60 | ret = http.get("http://mbed.org/media/uploads/donatien/hello.txt", str, 128); |
mazgch | 7:c558c74ceb0f | 61 | if (!ret) { |
mazgch | 7:c558c74ceb0f | 62 | printf("Page fetched successfully - read %d characters\n", strlen(str)); |
mazgch | 7:c558c74ceb0f | 63 | printf("Result: %s\n", str); |
mazgch | 7:c558c74ceb0f | 64 | } else { |
mazgch | 7:c558c74ceb0f | 65 | printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); |
donatien | 0:f3f18ac13e0c | 66 | } |
mazgch | 7:c558c74ceb0f | 67 | |
donatien | 1:6ea9ac27702c | 68 | //POST data |
donatien | 0:f3f18ac13e0c | 69 | HTTPMap map; |
donatien | 0:f3f18ac13e0c | 70 | HTTPText text(str, 512); |
donatien | 0:f3f18ac13e0c | 71 | map.put("Hello", "World"); |
donatien | 0:f3f18ac13e0c | 72 | map.put("test", "1234"); |
donatien | 1:6ea9ac27702c | 73 | printf("Trying to post data...\n"); |
donatien | 0:f3f18ac13e0c | 74 | ret = http.post("http://httpbin.org/post", map, &text); |
mazgch | 7:c558c74ceb0f | 75 | if (!ret) { |
mazgch | 7:c558c74ceb0f | 76 | printf("Executed POST successfully - read %d characters\n", strlen(str)); |
mazgch | 7:c558c74ceb0f | 77 | printf("Result: %s\n", str); |
mazgch | 7:c558c74ceb0f | 78 | } else { |
mazgch | 7:c558c74ceb0f | 79 | printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); |
donatien | 0:f3f18ac13e0c | 80 | } |
mazgch | 7:c558c74ceb0f | 81 | |
mazgch | 7:c558c74ceb0f | 82 | modem.disconnect(); |
mazgch | 10:e2315bcdd7be | 83 | c027.mdmPower(false); |
mazgch | 10:e2315bcdd7be | 84 | |
donatien | 1:6ea9ac27702c | 85 | while(1) { |
donatien | 1:6ea9ac27702c | 86 | } |
donatien | 0:f3f18ac13e0c | 87 | } |
donatien | 0:f3f18ac13e0c | 88 | |
donatien | 0:f3f18ac13e0c | 89 | |
donatien | 0:f3f18ac13e0c | 90 | int main() |
donatien | 0:f3f18ac13e0c | 91 | { |
mazgch | 7:c558c74ceb0f | 92 | Thread testTask(test, NULL, osPriorityNormal, 1024 * 4); |
mazgch | 10:e2315bcdd7be | 93 | DigitalOut led(LED); // on rev A you should reasign the signal to A0 |
mazgch | 7:c558c74ceb0f | 94 | while(1) { |
mazgch | 7:c558c74ceb0f | 95 | led=!led; |
mazgch | 7:c558c74ceb0f | 96 | Thread::wait(1000); |
mazgch | 7:c558c74ceb0f | 97 | } |
donatien | 0:f3f18ac13e0c | 98 | |
mazgch | 7:c558c74ceb0f | 99 | return 0; |
donatien | 0:f3f18ac13e0c | 100 | } |