http client example

Dependencies:   C027_Support HTTPClient mbed

Fork of HTTPClient_HelloWorld by Donatien Garnier

Committer:
mazgch
Date:
Fri Jun 06 15:18:36 2014 +0000
Revision:
7:b14d0f112a73
Parent:
6:6ff6061a0f76
Child:
8:eea979594a37
use latest version of lib

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 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 7:b14d0f112a73 31 {
mazgch 7:b14d0f112a73 32 char urlString[100] = "http://192.38.36.13/hanshenrik/GPRS/gprs.php?mode=";
mazgch 7:b14d0f112a73 33 char url[200];
mazgch 7:b14d0f112a73 34 char databuffer[100];
mazgch 7:b14d0f112a73 35 strcpy (url,urlString);
mazgch 7:b14d0f112a73 36 int id = 10;
mazgch 7:b14d0f112a73 37 float la = 10.0, lo = 20.0;
mazgch 7:b14d0f112a73 38 float pres = 30.0, temp = 40.0, o3 = 50.0;
mazgch 7:b14d0f112a73 39 int co2 = 200;
mazgch 7:b14d0f112a73 40 //sprintf(databuffer, "latitude&id=%d&data=%.5f", id, la); //This is a short URL that works!
mazgch 7:b14d0f112a73 41 sprintf(databuffer, "update&id=%d&lat=%.1f&lng=%.1f&pres=%.1f&temp=%.1f&co2=%d&o3=%.1f",id,la,lo,pres,temp,co2,o3);
mazgch 7:b14d0f112a73 42 printf("URL string: \"%s\"\r\n", url);
mazgch 7:b14d0f112a73 43 wait_ms(1000);
mazgch 7:b14d0f112a73 44 strcat(url, databuffer);
mazgch 7:b14d0f112a73 45 wait_ms(1000);
mazgch 7:b14d0f112a73 46 printf("URL string: \"%s\"\r\n", url);
mazgch 7:b14d0f112a73 47 wait_ms(1000);
mazgch 7:b14d0f112a73 48 int ret = http.get(url, str, 128);
mazgch 7:b14d0f112a73 49 wait_ms(1000);
mazgch 7:b14d0f112a73 50 printf("%d \"%s\"\r\n", ret, str);
mazgch 7:b14d0f112a73 51 wait_ms(1000);
mazgch 7:b14d0f112a73 52 while (1);
mazgch 7:b14d0f112a73 53 }
mazgch 4:7fd97087e573 54
mazgch 4:7fd97087e573 55 //GET data
mazgch 4:7fd97087e573 56 printf("\nTrying to fetch page...\n");
mazgch 4:7fd97087e573 57 int ret = http.get("http://mbed.org/media/uploads/donatien/hello.txt", str, 128);
mazgch 4:7fd97087e573 58 if (!ret)
donatien 0:0e0debc29569 59 {
mazgch 4:7fd97087e573 60 printf("Page fetched successfully - read %d characters\n", strlen(str));
mazgch 4:7fd97087e573 61 printf("Result: %s\n", str);
mazgch 4:7fd97087e573 62 }
mazgch 4:7fd97087e573 63 else
mazgch 4:7fd97087e573 64 {
mazgch 4:7fd97087e573 65 printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
mazgch 4:7fd97087e573 66 }
donatien 0:0e0debc29569 67
mazgch 4:7fd97087e573 68 //POST data
mazgch 4:7fd97087e573 69 HTTPMap map;
mazgch 4:7fd97087e573 70 HTTPText inText(str, 512);
mazgch 4:7fd97087e573 71 map.put("Hello", "World");
mazgch 4:7fd97087e573 72 map.put("test", "1234");
mazgch 4:7fd97087e573 73 printf("\nTrying to post data...\n");
mazgch 4:7fd97087e573 74 ret = http.post("http://httpbin.org/post", map, &inText);
mazgch 4:7fd97087e573 75 if (!ret)
mazgch 4:7fd97087e573 76 {
mazgch 4:7fd97087e573 77 printf("Executed POST successfully - read %d characters\n", strlen(str));
mazgch 4:7fd97087e573 78 printf("Result: %s\n", str);
mazgch 4:7fd97087e573 79 }
mazgch 4:7fd97087e573 80 else
mazgch 4:7fd97087e573 81 {
mazgch 4:7fd97087e573 82 printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
mazgch 4:7fd97087e573 83 }
donatien 2:270e2d0bb85a 84
mazgch 4:7fd97087e573 85 //PUT data
mazgch 4:7fd97087e573 86 strcpy(str, "This is a PUT test!");
mazgch 4:7fd97087e573 87 HTTPText outText(str);
mazgch 4:7fd97087e573 88 //HTTPText inText(str, 512);
mazgch 4:7fd97087e573 89 printf("\nTrying to put resource...\n");
mazgch 4:7fd97087e573 90 ret = http.put("http://httpbin.org/put", outText, &inText);
mazgch 4:7fd97087e573 91 if (!ret)
mazgch 4:7fd97087e573 92 {
mazgch 4:7fd97087e573 93 printf("Executed PUT successfully - read %d characters\n", strlen(str));
mazgch 4:7fd97087e573 94 printf("Result: %s\n", str);
mazgch 4:7fd97087e573 95 }
mazgch 4:7fd97087e573 96 else
mazgch 4:7fd97087e573 97 {
mazgch 4:7fd97087e573 98 printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
donatien 2:270e2d0bb85a 99 }
mazgch 4:7fd97087e573 100
mazgch 4:7fd97087e573 101 //DELETE data
mazgch 4:7fd97087e573 102 //HTTPText inText(str, 512);
mazgch 4:7fd97087e573 103 printf("\nTrying to delete resource...\n");
mazgch 4:7fd97087e573 104 ret = http.del("http://httpbin.org/delete", &inText);
mazgch 4:7fd97087e573 105 if (!ret)
mazgch 4:7fd97087e573 106 {
mazgch 4:7fd97087e573 107 printf("Executed DELETE successfully - read %d characters\n", strlen(str));
mazgch 4:7fd97087e573 108 printf("Result: %s\n", str);
mazgch 4:7fd97087e573 109 }
mazgch 4:7fd97087e573 110 else
mazgch 4:7fd97087e573 111 {
mazgch 4:7fd97087e573 112 printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
mazgch 4:7fd97087e573 113 }
mazgch 4:7fd97087e573 114
mazgch 4:7fd97087e573 115 mdm.disconnect();
mazgch 3:412a526d7054 116 mdm.powerOff();
mazgch 6:6ff6061a0f76 117
mazgch 3:412a526d7054 118 while(true);
donatien 0:0e0debc29569 119 }