this is a demo code for HTTPClient_GPRS library

Dependencies:   GPRSInterface HTTPClient_GPRS mbed USBDevice

Fork of Seeed_HTTPClient_GPRSInterface_HelloWorld by wei zou

Committer:
yihui
Date:
Fri Mar 06 09:22:41 2015 +0000
Revision:
2:ecc68e79d692
Parent:
1:16498811e319
update url and increase url

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lawliet 0:cc0bec77f305 1 #include "mbed.h"
lawliet 0:cc0bec77f305 2 #include "GPRSInterface.h"
lawliet 0:cc0bec77f305 3 #include "HTTPClient.h"
lawliet 0:cc0bec77f305 4
yihui 1:16498811e319 5 #ifdef TARGET_ARCH_GPRS
yihui 1:16498811e319 6 #include "USBSerial.h"
yihui 1:16498811e319 7 #define LOG(args...) pc.printf(args)
yihui 1:16498811e319 8 USBSerial pc;
yihui 1:16498811e319 9 #else
yihui 1:16498811e319 10 #define LOG(args...) pc.printf(args)
yihui 1:16498811e319 11 Serial pc(USBTX, USBRX);
yihui 1:16498811e319 12 #endif
yihui 1:16498811e319 13
lawliet 0:cc0bec77f305 14 #define TEST_HTTP_GET 1
lawliet 0:cc0bec77f305 15 #define TEST_HTTP_POST 1
lawliet 0:cc0bec77f305 16 #define TEST_HTTP_PUT 1
lawliet 0:cc0bec77f305 17 #define TEST_HTTP_DELETE 1
lawliet 0:cc0bec77f305 18
yihui 1:16498811e319 19 #define PIN_TX P1_27
yihui 1:16498811e319 20 #define PIN_RX P1_26
lawliet 0:cc0bec77f305 21
yihui 1:16498811e319 22
yihui 1:16498811e319 23 GPRSInterface gprs(PIN_TX, PIN_RX, 115200, "uninet", NULL, NULL);
lawliet 0:cc0bec77f305 24 HTTPClient http;
yihui 2:ecc68e79d692 25 char str[1024];
lawliet 0:cc0bec77f305 26
yihui 1:16498811e319 27 #ifdef TARGET_ARCH_GPRS
yihui 1:16498811e319 28 DigitalOut power(P1_2);
yihui 1:16498811e319 29 DigitalOut powerKey(P1_7);
yihui 1:16498811e319 30
yihui 1:16498811e319 31 /* power pin: low enable
yihui 1:16498811e319 32 ___
yihui 1:16498811e319 33 |___
yihui 1:16498811e319 34
yihui 1:16498811e319 35 powerKey pin: you can also power up by long press the powerKey.
yihui 1:16498811e319 36
yihui 1:16498811e319 37 ___
yihui 1:16498811e319 38 ___| |___
yihui 1:16498811e319 39
yihui 1:16498811e319 40 */
yihui 1:16498811e319 41 void gprsPowerUp(void)
yihui 1:16498811e319 42 {
yihui 1:16498811e319 43 power = 1;
yihui 1:16498811e319 44 wait(2);
yihui 1:16498811e319 45 power = 0;
yihui 1:16498811e319 46 wait(2);
yihui 1:16498811e319 47
yihui 1:16498811e319 48 powerKey = 0;
yihui 1:16498811e319 49 wait(1);
yihui 1:16498811e319 50 powerKey = 1;
yihui 1:16498811e319 51 wait(2);
yihui 1:16498811e319 52 powerKey = 0;
yihui 1:16498811e319 53 wait(3);
yihui 1:16498811e319 54 }
yihui 1:16498811e319 55 #endif
yihui 1:16498811e319 56
lawliet 0:cc0bec77f305 57 int main()
lawliet 0:cc0bec77f305 58 {
yihui 1:16498811e319 59 #ifdef TARGET_ARCH_GPRS
yihui 1:16498811e319 60 gprsPowerUp();
yihui 1:16498811e319 61 #endif
lawliet 0:cc0bec77f305 62 gprs.init();
lawliet 0:cc0bec77f305 63
lawliet 0:cc0bec77f305 64 while(false == gprs.connect()) {
yihui 1:16498811e319 65 LOG("gprs connect error\n");
lawliet 0:cc0bec77f305 66 wait(2);
lawliet 0:cc0bec77f305 67 }
lawliet 0:cc0bec77f305 68
lawliet 0:cc0bec77f305 69 // successful DHCP
yihui 1:16498811e319 70 LOG("IP Address is %s\n", gprs.getIPAddress());
lawliet 0:cc0bec77f305 71
lawliet 0:cc0bec77f305 72 int ret;
lawliet 0:cc0bec77f305 73 HTTPMap map;
yihui 2:ecc68e79d692 74 HTTPText inText(str, 1024);
lawliet 0:cc0bec77f305 75 HTTPText outText(str);
lawliet 0:cc0bec77f305 76
lawliet 0:cc0bec77f305 77 #if TEST_HTTP_GET
lawliet 0:cc0bec77f305 78 //GET data
yihui 1:16498811e319 79 LOG("\nTrying to fetch page...\n");
yihui 2:ecc68e79d692 80 ret = http.get("http://developer.mbed.org/media/uploads/mbed_official/hello.txt", str, 1024);
lawliet 0:cc0bec77f305 81 if (!ret) {
yihui 1:16498811e319 82 LOG("Page fetched successfully - read %d characters\n", strlen(str));
yihui 1:16498811e319 83 LOG("Result: %s\n", str);
lawliet 0:cc0bec77f305 84 } else {
yihui 1:16498811e319 85 LOG("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
lawliet 0:cc0bec77f305 86 }
lawliet 0:cc0bec77f305 87 #endif
lawliet 0:cc0bec77f305 88
lawliet 0:cc0bec77f305 89 #if TEST_HTTP_POST
lawliet 0:cc0bec77f305 90 //POST data
lawliet 0:cc0bec77f305 91 map.put("Hello", "World");
lawliet 0:cc0bec77f305 92 map.put("test", "1234");
yihui 1:16498811e319 93 LOG("\nTrying to post data...\n");
lawliet 0:cc0bec77f305 94 ret = http.post("http://httpbin.org/post", map, &inText);
lawliet 0:cc0bec77f305 95 if (!ret) {
yihui 1:16498811e319 96 LOG("Executed POST successfully\n");
lawliet 0:cc0bec77f305 97 } else {
yihui 1:16498811e319 98 LOG("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
lawliet 0:cc0bec77f305 99 }
lawliet 0:cc0bec77f305 100 #endif
lawliet 0:cc0bec77f305 101
lawliet 0:cc0bec77f305 102 #if TEST_HTTP_PUT
lawliet 0:cc0bec77f305 103 //PUT data
lawliet 0:cc0bec77f305 104 strcpy(str, "This is a PUT test!");
yihui 1:16498811e319 105 LOG("\nTrying to put resource...\n");
lawliet 0:cc0bec77f305 106 ret = http.put("http://httpbin.org/put", outText, &inText);
lawliet 0:cc0bec77f305 107 if (!ret) {
yihui 1:16498811e319 108 LOG("Executed PUT successfully - read %d characters\n", strlen(str));
yihui 1:16498811e319 109 LOG("Result: %s\n", str);
lawliet 0:cc0bec77f305 110 } else {
yihui 1:16498811e319 111 LOG("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
lawliet 0:cc0bec77f305 112 }
lawliet 0:cc0bec77f305 113 #endif
lawliet 0:cc0bec77f305 114
lawliet 0:cc0bec77f305 115 #if TEST_HTTP_DELETE
lawliet 0:cc0bec77f305 116 //DELETE data
yihui 1:16498811e319 117 LOG("\nTrying to delete resource...\n");
lawliet 0:cc0bec77f305 118 ret = http.del("http://httpbin.org/delete", &inText);
lawliet 0:cc0bec77f305 119 if (!ret) {
yihui 1:16498811e319 120 LOG("Executed DELETE successfully\n");
lawliet 0:cc0bec77f305 121 } else {
yihui 1:16498811e319 122 LOG("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
lawliet 0:cc0bec77f305 123 }
lawliet 0:cc0bec77f305 124 #endif
lawliet 0:cc0bec77f305 125
lawliet 0:cc0bec77f305 126 gprs.disconnect();
lawliet 0:cc0bec77f305 127
lawliet 0:cc0bec77f305 128 return 0;
lawliet 0:cc0bec77f305 129 }