Example which shows HTTP client use with the USB modem.

Dependencies:   HTTPClient VodafoneUSBModem mbed-rtos mbed

Fork of VodafoneUSBModemHTTPClientTest by Donatien Garnier

Files at this revision

API Documentation at this revision

Comitter:
ashleymills
Date:
Wed Nov 28 09:48:37 2012 +0000
Parent:
5:13f0fa1662ab
Commit message:
Basic HTTP test

Changed in this revision

VodafoneUSBModem.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-rtos.lib Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 13f0fa1662ab -r 3def8cb2aa85 VodafoneUSBModem.lib
--- a/VodafoneUSBModem.lib	Tue Oct 30 10:18:34 2012 +0000
+++ b/VodafoneUSBModem.lib	Wed Nov 28 09:48:37 2012 +0000
@@ -1,1 +1,1 @@
-https://mbed.org/users/mbed_official/code/VodafoneUSBModem/#bf3cf216235c
+https://mbed.org/users/mbed_official/code/VodafoneUSBModem/#66db9afdc3c1
diff -r 13f0fa1662ab -r 3def8cb2aa85 main.cpp
--- a/main.cpp	Tue Oct 30 10:18:34 2012 +0000
+++ b/main.cpp	Wed Nov 28 09:48:37 2012 +0000
@@ -1,66 +1,102 @@
+// stuff to use DBG debug output
+#define __DEBUG__ 4 //Maximum verbosity
+#ifndef __MODULE__
+#define __MODULE__ "main.cpp"
+#endif
+
 #include "mbed.h"
 #include "VodafoneUSBModem.h"
 #include "HTTPClient.h"
+#include "socket.h"
 
-void test(void const*) 
-{
-    VodafoneUSBModem modem;
-    HTTPClient http;
-    char str[512];
-    
-    int ret = modem.connect("pp.vodafone.co.uk");
-    if(ret)
-    {
-      printf("Could not connect\n");
-      return;
-    }
-    
-    //GET data
-    printf("Trying to fetch page...\n");
-    ret = http.get("http://mbed.org/media/uploads/donatien/hello.txt", str, 128);
-    if (!ret)
-    {
-      printf("Page fetched successfully - read %d characters\n", strlen(str));
-      printf("Result: %s\n", str);
-    }
-    else
-    {
-      printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
-    }
-    
-    //POST data
-    HTTPMap map;
-    HTTPText text(str, 512);
-    map.put("Hello", "World");
-    map.put("test", "1234");
-    printf("Trying to post data...\n");
-    ret = http.post("http://httpbin.org/post", map, &text);
-    if (!ret)
-    {
-      printf("Executed POST successfully - read %d characters\n", strlen(str));
-      printf("Result: %s\n", str);
-    }
-    else
-    {
-      printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
-    }
-    
-    modem.disconnect();  
+// connect to socket at ipAddress and port
+bool connectToSocket(char *ipAddress, int port, int *sockfd) {
+  *sockfd = -1;
+  
+  // create the socket
+  if((*sockfd=socket(AF_INET,SOCK_STREAM,0))<0) {
+     INFO("Error opening socket");
+     return false;
+  }
+         
+  // create the socket address
+  sockaddr_in serverAddress;
+  std::memset(&serverAddress, 0, sizeof(struct sockaddr_in));
+  serverAddress.sin_addr.s_addr = inet_addr(ipAddress);
+  serverAddress.sin_family = AF_INET;
+  serverAddress.sin_port = htons(port);
 
-    while(1) {
-    }
+  // do socket connect
+  if(connect(*sockfd, (const struct sockaddr *)&serverAddress, sizeof(serverAddress))<0) {
+     close(*sockfd);
+     INFO("Could not connect");
+     return false;
+  }
+  return true;
 }
 
+int main() {
+   // setup debug macro
+   DBG_INIT();
+   DBG_SET_SPEED(115200);
+   DBG_SET_NEWLINE("\r\n");
+   INFO("Begin");
+   
+   // construct modem object
+   VodafoneUSBModem modem;
+   
+   // construc http client object
+   HTTPClient http;
+   
+   // locals
+   char str[512];
+   str[0] = 0x00;
+   int ret = 1;
 
-int main()
-{
-  Thread testTask(test, NULL, osPriorityNormal, 1024 * 4);
-  DigitalOut led(LED1);
-  while(1)
-  {
-    led=!led;
-    Thread::wait(1000);  
-  }
+   // connect to the mobile network (get an IP)
+   ret = modem.connect("internet","web","web");
+   if(ret!=0) {
+      DBG("Error connecting to the Vodafone network.");
+      return;
+   }
+   
+   int sockfd = 0;
+   if(connectToSocket("109.74.199.96",80,&sockfd)) {
+      INFO("Connected to socket OK");
+      close(sockfd);
+   }
+    
+   // do an HTTP GET
+   INFO("Trying to fetch page...");
+   ret = http.get("http://m2mthings.com/test.txt", str, 128,3000);
+   if(ret==0) {
+      INFO("Page fetched successfully - read %d characters", strlen(str));
+      INFO("Result: \"%s\"", str);
+   } else {
+      INFO("Error - ret = %d - HTTP return code = %d", ret, http.getHTTPResponseCode());
+   }
+    
+   // do an HTTP POST
+   HTTPMap map;
+   HTTPText text(str, 512);
+   map.put("Hello", "World");
+   map.put("test", "1234");
+   INFO("Trying to post data...");
+   ret = http.post("http://httpbin.org/post", map, &text);
+   if(ret==0) {
+      INFO("Executed POST successfully - read %d characters", strlen(str));
+      INFO("Result: %s\n", str);
+   } else {
+      INFO("Error - ret = %d - HTTP return code = %d", ret, http.getHTTPResponseCode());
+   }
+    
+   // disconnect from the mobile network
+   modem.disconnect();  
 
-  return 0;
+   // flash led 1 forever
+   DigitalOut l1(LED1);
+   while(1) { 
+      l1 = !l1;
+      Thread::wait(1000);
+   } 
 }
diff -r 13f0fa1662ab -r 3def8cb2aa85 mbed-rtos.lib
--- a/mbed-rtos.lib	Tue Oct 30 10:18:34 2012 +0000
+++ b/mbed-rtos.lib	Wed Nov 28 09:48:37 2012 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed-rtos/#9654a71f5a90
+http://mbed.org/users/mbed_official/code/mbed-rtos/#88a1a9c26ae3
diff -r 13f0fa1662ab -r 3def8cb2aa85 mbed.bld
--- a/mbed.bld	Tue Oct 30 10:18:34 2012 +0000
+++ b/mbed.bld	Wed Nov 28 09:48:37 2012 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/e2ed12d17f06
\ No newline at end of file
+http://mbed.org/users/mbed_official/code/mbed/builds/134def52cfa0
\ No newline at end of file