Example showing the ublox Cellular GPS/GNSS module with the HTTPClient library to fetch and upload web pages.
Dependencies: C027_Support HTTPClient mbed
main.cpp@8:eea979594a37, 2014-06-13 (annotated)
- Committer:
- mazgch
- Date:
- Fri Jun 13 06:39:40 2014 +0000
- Revision:
- 8:eea979594a37
- Parent:
- 7:b14d0f112a73
- Child:
- 9:cd98ff458412
remove test code
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
donatien | 0:0e0debc29569 | 1 | #include "mbed.h" |
donatien | 0:0e0debc29569 | 2 | #include "HTTPClient.h" |
mazgch | 6:6ff6061a0f76 | 3 | |
mazgch | 6:6ff6061a0f76 | 4 | //------------------------------------------------------------------------------------ |
mazgch | 6:6ff6061a0f76 | 5 | // You need to configure these cellular modem / SIM parameters. |
mazgch | 6:6ff6061a0f76 | 6 | // These parameters are ignored for LISA-C200 variants and can be left NULL. |
mazgch | 6:6ff6061a0f76 | 7 | //------------------------------------------------------------------------------------ |
mazgch | 3:412a526d7054 | 8 | #include "MDM.h" |
mazgch | 6:6ff6061a0f76 | 9 | //! Set your secret SIM pin here (e.g. "1234"). Check your SIM manual. |
mazgch | 3:412a526d7054 | 10 | #define SIMPIN NULL |
mazgch | 6:6ff6061a0f76 | 11 | /*! The APN of your network operator SIM, sometimes it is "internet" check your |
mazgch | 6:6ff6061a0f76 | 12 | contract with the network operator. You can also try to look-up your settings in |
mazgch | 6:6ff6061a0f76 | 13 | google: https://www.google.de/search?q=APN+list */ |
mazgch | 7:b14d0f112a73 | 14 | #define APN NULL |
mazgch | 6:6ff6061a0f76 | 15 | //! Set the user name for your APN, or NULL if not needed |
mazgch | 3:412a526d7054 | 16 | #define USERNAME NULL |
mazgch | 6:6ff6061a0f76 | 17 | //! Set the password for your APN, or NULL if not needed |
mazgch | 3:412a526d7054 | 18 | #define PASSWORD NULL |
mazgch | 6:6ff6061a0f76 | 19 | //------------------------------------------------------------------------------------ |
mazgch | 3:412a526d7054 | 20 | |
donatien | 1:d263603373ac | 21 | char str[512]; |
donatien | 1:d263603373ac | 22 | |
donatien | 0:0e0debc29569 | 23 | int main() |
donatien | 0:0e0debc29569 | 24 | { |
mazgch | 5:a18ddbfd70c9 | 25 | // turn on the supplies of the Modem |
mazgch | 3:412a526d7054 | 26 | MDMSerial mdm; |
mazgch | 7:b14d0f112a73 | 27 | mdm.setDebug(4); // enable this for debugging issues |
mazgch | 6:6ff6061a0f76 | 28 | if (!mdm.connect(SIMPIN, APN,USERNAME,PASSWORD)) |
mazgch | 4:7fd97087e573 | 29 | return -1; |
mazgch | 4:7fd97087e573 | 30 | HTTPClient http; |
mazgch | 8:eea979594a37 | 31 | |
mazgch | 4:7fd97087e573 | 32 | //GET data |
mazgch | 4:7fd97087e573 | 33 | printf("\nTrying to fetch page...\n"); |
mazgch | 4:7fd97087e573 | 34 | int ret = http.get("http://mbed.org/media/uploads/donatien/hello.txt", str, 128); |
mazgch | 4:7fd97087e573 | 35 | if (!ret) |
donatien | 0:0e0debc29569 | 36 | { |
mazgch | 4:7fd97087e573 | 37 | printf("Page fetched successfully - read %d characters\n", strlen(str)); |
mazgch | 4:7fd97087e573 | 38 | printf("Result: %s\n", str); |
mazgch | 4:7fd97087e573 | 39 | } |
mazgch | 4:7fd97087e573 | 40 | else |
mazgch | 4:7fd97087e573 | 41 | { |
mazgch | 4:7fd97087e573 | 42 | printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); |
mazgch | 4:7fd97087e573 | 43 | } |
donatien | 0:0e0debc29569 | 44 | |
mazgch | 4:7fd97087e573 | 45 | //POST data |
mazgch | 4:7fd97087e573 | 46 | HTTPMap map; |
mazgch | 4:7fd97087e573 | 47 | HTTPText inText(str, 512); |
mazgch | 4:7fd97087e573 | 48 | map.put("Hello", "World"); |
mazgch | 4:7fd97087e573 | 49 | map.put("test", "1234"); |
mazgch | 4:7fd97087e573 | 50 | printf("\nTrying to post data...\n"); |
mazgch | 4:7fd97087e573 | 51 | ret = http.post("http://httpbin.org/post", map, &inText); |
mazgch | 4:7fd97087e573 | 52 | if (!ret) |
mazgch | 4:7fd97087e573 | 53 | { |
mazgch | 4:7fd97087e573 | 54 | printf("Executed POST successfully - read %d characters\n", strlen(str)); |
mazgch | 4:7fd97087e573 | 55 | printf("Result: %s\n", str); |
mazgch | 4:7fd97087e573 | 56 | } |
mazgch | 4:7fd97087e573 | 57 | else |
mazgch | 4:7fd97087e573 | 58 | { |
mazgch | 4:7fd97087e573 | 59 | printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); |
mazgch | 4:7fd97087e573 | 60 | } |
donatien | 2:270e2d0bb85a | 61 | |
mazgch | 4:7fd97087e573 | 62 | //PUT data |
mazgch | 4:7fd97087e573 | 63 | strcpy(str, "This is a PUT test!"); |
mazgch | 4:7fd97087e573 | 64 | HTTPText outText(str); |
mazgch | 4:7fd97087e573 | 65 | //HTTPText inText(str, 512); |
mazgch | 4:7fd97087e573 | 66 | printf("\nTrying to put resource...\n"); |
mazgch | 4:7fd97087e573 | 67 | ret = http.put("http://httpbin.org/put", outText, &inText); |
mazgch | 4:7fd97087e573 | 68 | if (!ret) |
mazgch | 4:7fd97087e573 | 69 | { |
mazgch | 4:7fd97087e573 | 70 | printf("Executed PUT successfully - read %d characters\n", strlen(str)); |
mazgch | 4:7fd97087e573 | 71 | printf("Result: %s\n", str); |
mazgch | 4:7fd97087e573 | 72 | } |
mazgch | 4:7fd97087e573 | 73 | else |
mazgch | 4:7fd97087e573 | 74 | { |
mazgch | 4:7fd97087e573 | 75 | printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); |
donatien | 2:270e2d0bb85a | 76 | } |
mazgch | 4:7fd97087e573 | 77 | |
mazgch | 4:7fd97087e573 | 78 | //DELETE data |
mazgch | 4:7fd97087e573 | 79 | //HTTPText inText(str, 512); |
mazgch | 4:7fd97087e573 | 80 | printf("\nTrying to delete resource...\n"); |
mazgch | 4:7fd97087e573 | 81 | ret = http.del("http://httpbin.org/delete", &inText); |
mazgch | 4:7fd97087e573 | 82 | if (!ret) |
mazgch | 4:7fd97087e573 | 83 | { |
mazgch | 4:7fd97087e573 | 84 | printf("Executed DELETE successfully - read %d characters\n", strlen(str)); |
mazgch | 4:7fd97087e573 | 85 | printf("Result: %s\n", str); |
mazgch | 4:7fd97087e573 | 86 | } |
mazgch | 4:7fd97087e573 | 87 | else |
mazgch | 4:7fd97087e573 | 88 | { |
mazgch | 4:7fd97087e573 | 89 | printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); |
mazgch | 4:7fd97087e573 | 90 | } |
mazgch | 4:7fd97087e573 | 91 | |
mazgch | 4:7fd97087e573 | 92 | mdm.disconnect(); |
mazgch | 3:412a526d7054 | 93 | mdm.powerOff(); |
mazgch | 6:6ff6061a0f76 | 94 | |
mazgch | 3:412a526d7054 | 95 | while(true); |
donatien | 0:0e0debc29569 | 96 | } |