http client example

Dependencies:   C027_Support HTTPClient mbed

Fork of HTTPClient_HelloWorld by Donatien Garnier

Committer:
mazgch
Date:
Mon May 12 09:17:35 2014 +0000
Revision:
3:412a526d7054
Parent:
2:270e2d0bb85a
Child:
4:7fd97087e573
cellular port of httpclient example

Who changed what in which revision?

UserRevisionLine numberNew contents of line
donatien 0:0e0debc29569 1 #include "mbed.h"
donatien 0:0e0debc29569 2 #include "HTTPClient.h"
mazgch 3:412a526d7054 3 #include "C027.h"
mazgch 3:412a526d7054 4 #include "MDM.h"
donatien 0:0e0debc29569 5
mazgch 3:412a526d7054 6 //----------------------------------------------------------------------
mazgch 3:412a526d7054 7 // You may need to configure these parameters
mazgch 3:412a526d7054 8
mazgch 3:412a526d7054 9 /** Set your secret SIM pin here "1234"
mazgch 3:412a526d7054 10 */
mazgch 3:412a526d7054 11 #define SIMPIN NULL
mazgch 3:412a526d7054 12
mazgch 3:412a526d7054 13 /** The APN of your network operator, sometimes it is "internet"
mazgch 3:412a526d7054 14 check your contract with the network operator
mazgch 3:412a526d7054 15 */
mazgch 3:412a526d7054 16 #define APN "gprs.swisscom.ch"
mazgch 3:412a526d7054 17
mazgch 3:412a526d7054 18 /** Set the user name for your APN, or NULL if not needed
mazgch 3:412a526d7054 19 */
mazgch 3:412a526d7054 20 #define USERNAME NULL
mazgch 3:412a526d7054 21
mazgch 3:412a526d7054 22 /** Set the password for your APN, or NULL if not needed
mazgch 3:412a526d7054 23 */
mazgch 3:412a526d7054 24 #define PASSWORD NULL
mazgch 3:412a526d7054 25
mazgch 3:412a526d7054 26 C027 c027;
mazgch 3:412a526d7054 27
donatien 1:d263603373ac 28 char str[512];
donatien 1:d263603373ac 29
donatien 0:0e0debc29569 30 int main()
donatien 0:0e0debc29569 31 {
mazgch 3:412a526d7054 32 // turn on the supplies of the Modem and the GPS
mazgch 3:412a526d7054 33 c027.mdmPower(true);
mazgch 3:412a526d7054 34 wait(2);
mazgch 3:412a526d7054 35 // Create the modem object
mazgch 3:412a526d7054 36 MDMSerial mdm;
donatien 1:d263603373ac 37
mazgch 3:412a526d7054 38 // initialize the modem
mazgch 3:412a526d7054 39 printf("Modem Initialize\r\n");
mazgch 3:412a526d7054 40 MDMParser::DevStatus devStatus;
mazgch 3:412a526d7054 41 bool mdmOk = mdm.init(SIMPIN, &devStatus);
mazgch 3:412a526d7054 42 if (mdmOk)
donatien 0:0e0debc29569 43 {
mazgch 3:412a526d7054 44 // wait until we are connected
mazgch 3:412a526d7054 45 printf("Network Check\r\n");
mazgch 3:412a526d7054 46 MDMParser::NetStatus netStatus;
mazgch 3:412a526d7054 47 while (!mdm.checkNetStatus(&netStatus))
mazgch 3:412a526d7054 48 wait_ms(1000);
mazgch 3:412a526d7054 49
mazgch 3:412a526d7054 50 printf("Network Join\r\n");
mazgch 3:412a526d7054 51 // join the internet connection
mazgch 3:412a526d7054 52 MDMParser::IP ip = mdm.join(APN,USERNAME,PASSWORD);
mazgch 3:412a526d7054 53 if (ip != NOIP)
mazgch 3:412a526d7054 54 {
mazgch 3:412a526d7054 55 printf(" IP Address: " IPSTR "\r\n", IPNUM(ip));
mazgch 3:412a526d7054 56 HTTPClient http;
donatien 0:0e0debc29569 57
mazgch 3:412a526d7054 58 //GET data
mazgch 3:412a526d7054 59 printf("\nTrying to fetch page...\n");
mazgch 3:412a526d7054 60 int ret = http.get("http://mbed.org/media/uploads/donatien/hello.txt", str, 128);
mazgch 3:412a526d7054 61 if (!ret)
mazgch 3:412a526d7054 62 {
mazgch 3:412a526d7054 63 printf("Page fetched successfully - read %d characters\n", strlen(str));
mazgch 3:412a526d7054 64 printf("Result: %s\n", str);
mazgch 3:412a526d7054 65 }
mazgch 3:412a526d7054 66 else
mazgch 3:412a526d7054 67 {
mazgch 3:412a526d7054 68 printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
mazgch 3:412a526d7054 69 }
mazgch 3:412a526d7054 70
mazgch 3:412a526d7054 71 //POST data
mazgch 3:412a526d7054 72 HTTPMap map;
mazgch 3:412a526d7054 73 HTTPText inText(str, 512);
mazgch 3:412a526d7054 74 map.put("Hello", "World");
mazgch 3:412a526d7054 75 map.put("test", "1234");
mazgch 3:412a526d7054 76 printf("\nTrying to post data...\n");
mazgch 3:412a526d7054 77 ret = http.post("http://httpbin.org/post", map, &inText);
mazgch 3:412a526d7054 78 if (!ret)
mazgch 3:412a526d7054 79 {
mazgch 3:412a526d7054 80 printf("Executed POST successfully - read %d characters\n", strlen(str));
mazgch 3:412a526d7054 81 printf("Result: %s\n", str);
mazgch 3:412a526d7054 82 }
mazgch 3:412a526d7054 83 else
mazgch 3:412a526d7054 84 {
mazgch 3:412a526d7054 85 printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
mazgch 3:412a526d7054 86 }
mazgch 3:412a526d7054 87
mazgch 3:412a526d7054 88 //PUT data
mazgch 3:412a526d7054 89 strcpy(str, "This is a PUT test!");
mazgch 3:412a526d7054 90 HTTPText outText(str);
mazgch 3:412a526d7054 91 //HTTPText inText(str, 512);
mazgch 3:412a526d7054 92 printf("\nTrying to put resource...\n");
mazgch 3:412a526d7054 93 ret = http.put("http://httpbin.org/put", outText, &inText);
mazgch 3:412a526d7054 94 if (!ret)
mazgch 3:412a526d7054 95 {
mazgch 3:412a526d7054 96 printf("Executed PUT successfully - read %d characters\n", strlen(str));
mazgch 3:412a526d7054 97 printf("Result: %s\n", str);
mazgch 3:412a526d7054 98 }
mazgch 3:412a526d7054 99 else
mazgch 3:412a526d7054 100 {
mazgch 3:412a526d7054 101 printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
mazgch 3:412a526d7054 102 }
mazgch 3:412a526d7054 103
mazgch 3:412a526d7054 104 //DELETE data
mazgch 3:412a526d7054 105 //HTTPText inText(str, 512);
mazgch 3:412a526d7054 106 printf("\nTrying to delete resource...\n");
mazgch 3:412a526d7054 107 ret = http.del("http://httpbin.org/delete", &inText);
mazgch 3:412a526d7054 108 if (!ret)
mazgch 3:412a526d7054 109 {
mazgch 3:412a526d7054 110 printf("Executed DELETE successfully - read %d characters\n", strlen(str));
mazgch 3:412a526d7054 111 printf("Result: %s\n", str);
mazgch 3:412a526d7054 112 }
mazgch 3:412a526d7054 113 else
mazgch 3:412a526d7054 114 {
mazgch 3:412a526d7054 115 printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
mazgch 3:412a526d7054 116 }
donatien 2:270e2d0bb85a 117
mazgch 3:412a526d7054 118 mdm.disconnect();
mazgch 3:412a526d7054 119 }
donatien 2:270e2d0bb85a 120 }
mazgch 3:412a526d7054 121 mdm.powerOff();
mazgch 3:412a526d7054 122 c027.mdmPower(false);
donatien 2:270e2d0bb85a 123
mazgch 3:412a526d7054 124 while(true);
donatien 0:0e0debc29569 125 }