Example which shows HTTP client use with the USB modem.

Dependencies:   HTTPClient VodafoneUSBModem mbed-rtos mbed

Fork of VodafoneUSBModemHTTPClientTest by Donatien Garnier

Committer:
ashleymills
Date:
Wed Nov 28 09:48:37 2012 +0000
Revision:
6:3def8cb2aa85
Parent:
4:47a1a2527e25
Basic HTTP test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ashleymills 6:3def8cb2aa85 1 // stuff to use DBG debug output
ashleymills 6:3def8cb2aa85 2 #define __DEBUG__ 4 //Maximum verbosity
ashleymills 6:3def8cb2aa85 3 #ifndef __MODULE__
ashleymills 6:3def8cb2aa85 4 #define __MODULE__ "main.cpp"
ashleymills 6:3def8cb2aa85 5 #endif
ashleymills 6:3def8cb2aa85 6
donatien 0:f3f18ac13e0c 7 #include "mbed.h"
donatien 1:6ea9ac27702c 8 #include "VodafoneUSBModem.h"
donatien 0:f3f18ac13e0c 9 #include "HTTPClient.h"
ashleymills 6:3def8cb2aa85 10 #include "socket.h"
donatien 0:f3f18ac13e0c 11
ashleymills 6:3def8cb2aa85 12 // connect to socket at ipAddress and port
ashleymills 6:3def8cb2aa85 13 bool connectToSocket(char *ipAddress, int port, int *sockfd) {
ashleymills 6:3def8cb2aa85 14 *sockfd = -1;
ashleymills 6:3def8cb2aa85 15
ashleymills 6:3def8cb2aa85 16 // create the socket
ashleymills 6:3def8cb2aa85 17 if((*sockfd=socket(AF_INET,SOCK_STREAM,0))<0) {
ashleymills 6:3def8cb2aa85 18 INFO("Error opening socket");
ashleymills 6:3def8cb2aa85 19 return false;
ashleymills 6:3def8cb2aa85 20 }
ashleymills 6:3def8cb2aa85 21
ashleymills 6:3def8cb2aa85 22 // create the socket address
ashleymills 6:3def8cb2aa85 23 sockaddr_in serverAddress;
ashleymills 6:3def8cb2aa85 24 std::memset(&serverAddress, 0, sizeof(struct sockaddr_in));
ashleymills 6:3def8cb2aa85 25 serverAddress.sin_addr.s_addr = inet_addr(ipAddress);
ashleymills 6:3def8cb2aa85 26 serverAddress.sin_family = AF_INET;
ashleymills 6:3def8cb2aa85 27 serverAddress.sin_port = htons(port);
donatien 0:f3f18ac13e0c 28
ashleymills 6:3def8cb2aa85 29 // do socket connect
ashleymills 6:3def8cb2aa85 30 if(connect(*sockfd, (const struct sockaddr *)&serverAddress, sizeof(serverAddress))<0) {
ashleymills 6:3def8cb2aa85 31 close(*sockfd);
ashleymills 6:3def8cb2aa85 32 INFO("Could not connect");
ashleymills 6:3def8cb2aa85 33 return false;
ashleymills 6:3def8cb2aa85 34 }
ashleymills 6:3def8cb2aa85 35 return true;
donatien 0:f3f18ac13e0c 36 }
donatien 0:f3f18ac13e0c 37
ashleymills 6:3def8cb2aa85 38 int main() {
ashleymills 6:3def8cb2aa85 39 // setup debug macro
ashleymills 6:3def8cb2aa85 40 DBG_INIT();
ashleymills 6:3def8cb2aa85 41 DBG_SET_SPEED(115200);
ashleymills 6:3def8cb2aa85 42 DBG_SET_NEWLINE("\r\n");
ashleymills 6:3def8cb2aa85 43 INFO("Begin");
ashleymills 6:3def8cb2aa85 44
ashleymills 6:3def8cb2aa85 45 // construct modem object
ashleymills 6:3def8cb2aa85 46 VodafoneUSBModem modem;
ashleymills 6:3def8cb2aa85 47
ashleymills 6:3def8cb2aa85 48 // construc http client object
ashleymills 6:3def8cb2aa85 49 HTTPClient http;
ashleymills 6:3def8cb2aa85 50
ashleymills 6:3def8cb2aa85 51 // locals
ashleymills 6:3def8cb2aa85 52 char str[512];
ashleymills 6:3def8cb2aa85 53 str[0] = 0x00;
ashleymills 6:3def8cb2aa85 54 int ret = 1;
donatien 0:f3f18ac13e0c 55
ashleymills 6:3def8cb2aa85 56 // connect to the mobile network (get an IP)
ashleymills 6:3def8cb2aa85 57 ret = modem.connect("internet","web","web");
ashleymills 6:3def8cb2aa85 58 if(ret!=0) {
ashleymills 6:3def8cb2aa85 59 DBG("Error connecting to the Vodafone network.");
ashleymills 6:3def8cb2aa85 60 return;
ashleymills 6:3def8cb2aa85 61 }
ashleymills 6:3def8cb2aa85 62
ashleymills 6:3def8cb2aa85 63 int sockfd = 0;
ashleymills 6:3def8cb2aa85 64 if(connectToSocket("109.74.199.96",80,&sockfd)) {
ashleymills 6:3def8cb2aa85 65 INFO("Connected to socket OK");
ashleymills 6:3def8cb2aa85 66 close(sockfd);
ashleymills 6:3def8cb2aa85 67 }
ashleymills 6:3def8cb2aa85 68
ashleymills 6:3def8cb2aa85 69 // do an HTTP GET
ashleymills 6:3def8cb2aa85 70 INFO("Trying to fetch page...");
ashleymills 6:3def8cb2aa85 71 ret = http.get("http://m2mthings.com/test.txt", str, 128,3000);
ashleymills 6:3def8cb2aa85 72 if(ret==0) {
ashleymills 6:3def8cb2aa85 73 INFO("Page fetched successfully - read %d characters", strlen(str));
ashleymills 6:3def8cb2aa85 74 INFO("Result: \"%s\"", str);
ashleymills 6:3def8cb2aa85 75 } else {
ashleymills 6:3def8cb2aa85 76 INFO("Error - ret = %d - HTTP return code = %d", ret, http.getHTTPResponseCode());
ashleymills 6:3def8cb2aa85 77 }
ashleymills 6:3def8cb2aa85 78
ashleymills 6:3def8cb2aa85 79 // do an HTTP POST
ashleymills 6:3def8cb2aa85 80 HTTPMap map;
ashleymills 6:3def8cb2aa85 81 HTTPText text(str, 512);
ashleymills 6:3def8cb2aa85 82 map.put("Hello", "World");
ashleymills 6:3def8cb2aa85 83 map.put("test", "1234");
ashleymills 6:3def8cb2aa85 84 INFO("Trying to post data...");
ashleymills 6:3def8cb2aa85 85 ret = http.post("http://httpbin.org/post", map, &text);
ashleymills 6:3def8cb2aa85 86 if(ret==0) {
ashleymills 6:3def8cb2aa85 87 INFO("Executed POST successfully - read %d characters", strlen(str));
ashleymills 6:3def8cb2aa85 88 INFO("Result: %s\n", str);
ashleymills 6:3def8cb2aa85 89 } else {
ashleymills 6:3def8cb2aa85 90 INFO("Error - ret = %d - HTTP return code = %d", ret, http.getHTTPResponseCode());
ashleymills 6:3def8cb2aa85 91 }
ashleymills 6:3def8cb2aa85 92
ashleymills 6:3def8cb2aa85 93 // disconnect from the mobile network
ashleymills 6:3def8cb2aa85 94 modem.disconnect();
donatien 0:f3f18ac13e0c 95
ashleymills 6:3def8cb2aa85 96 // flash led 1 forever
ashleymills 6:3def8cb2aa85 97 DigitalOut l1(LED1);
ashleymills 6:3def8cb2aa85 98 while(1) {
ashleymills 6:3def8cb2aa85 99 l1 = !l1;
ashleymills 6:3def8cb2aa85 100 Thread::wait(1000);
ashleymills 6:3def8cb2aa85 101 }
donatien 0:f3f18ac13e0c 102 }