NNN40 run a HTTP server with RPC using Soft AP mode

Dependencies:   WIFI_API_32kRAM mbed

Fork of HTTP-Server by Francois Berder

The sample code will run as a WiFi Soft AP mode with given AP configuration setting including SSID name and password. IP address (fixed to 192.168.2.1 for the current version of WIFI_API)of AP router will be print out once Soft AP mode is operating.

User can open their web browser and go to http://192.168.2.1/. and have a try on switch on a led. Firstly, we need to create an object to control a led

/media/uploads/wgd8700/http_server2.png

Then, led can be switch on using RPC command /media/uploads/wgd8700/http_server3.png

More information cab be found from the links below

https://developer.mbed.org/users/feb11/code/HTTP-Server/

https://developer.mbed.org/cookbook/Interfacing-Using-RPC

Files at this revision

API Documentation at this revision

Comitter:
feb11
Date:
Wed Jul 17 16:23:35 2013 +0000
Parent:
6:d03e189ebbfe
Child:
8:464abd184b7b
Commit message:
Improved error handling and added log

Changed in this revision

HTTPServer.cpp Show annotated file Show diff for this revision Revisions of this file
RequestHandler.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/HTTPServer.cpp	Wed Jul 17 15:18:41 2013 +0000
+++ b/HTTPServer.cpp	Wed Jul 17 16:23:35 2013 +0000
@@ -51,20 +51,25 @@
             }
             else if(n != -1)
             {
+                printf("Received data\n");
                 buffer[n] = '\0';
                 handle_request(buffer);
                 if(formatter != NULL)
                 {
+                    printf("Sending data...");
                     char *page = formatter->get_page(reply);
                     do
                     {
                         c.send(page, strlen(page)+1);
                         page = formatter->get_page(reply);
                     }while(strlen(page)>0);
+                    printf("done\n");
                 }
                 else
                     c.send(INVALID_FORMATTER, strlen(INVALID_FORMATTER)+1);
             }
+            else
+                printf("Error while receiving data\n");
         }
     }
 }
--- a/RequestHandler.cpp	Wed Jul 17 15:18:41 2013 +0000
+++ b/RequestHandler.cpp	Wed Jul 17 16:23:35 2013 +0000
@@ -3,21 +3,29 @@
 #include "RPCObjectManager.h"
 #include "RPCCommand.h"
 
+const char* INVALID_CMD = "Invalid RPC command";
+const char* DELETE_ERROR = "You must send a DELETE request to remove an object ";
+const char* CREATE_ERROR = "You must send a PUT request to create an object";
+const char* FUNC_CALL_ERROR = "You must send a GET request to call a function";
+
 void GetRequestHandler::handle(const RPCCommand& cmd, char *reply)
 {
     switch(cmd.get_type())
     {
         case DELETE:
-            strcat(reply, "You must send a DELETE request to remove an object ");
+            printf("Error: %s\n", DELETE_ERROR);
+            strcat(reply, DELETE_ERROR);
             break;
         case FUNCTION_CALL:
             RPC::call(cmd.get_cmd(), reply);
             break;
         case CREATE:
-            strcat(reply, "You must send a PUT request to call a function");
+            printf("Error: %s\n", CREATE_ERROR);
+            strcat(reply, CREATE_ERROR);
             break;
         default:
-            strcat(reply, "Invalid RPC command");
+            printf("Error: %s\n", INVALID_CMD);
+            strcat(reply, INVALID_CMD);
             break;
     }
 }
@@ -27,10 +35,12 @@
     switch(cmd.get_type())
     {
         case DELETE:
-            strcat(reply, "You must send a DELETE request to remove an object ");
+            printf("Error: %s\n", DELETE_ERROR);
+            strcat(reply, DELETE_ERROR);
             break;
         case FUNCTION_CALL:
-            strcat(reply, "You must send a GET request to call a function");
+            printf("Error: %s\n", FUNC_CALL_ERROR);
+            strcat(reply, FUNC_CALL_ERROR);
             break;
         case CREATE:
             RPC::call(cmd.get_cmd(), reply);
@@ -40,10 +50,14 @@
                 strcat(reply, " has been created");
             }
             else
+            {
+                printf("Error while creating object\n");
                 strcat(reply, "Error while creating object.");
+            }
             break;
         default:
-            strcat(reply, "Invalid RPC command");
+            printf("Error: %s\n", INVALID_CMD);
+            strcat(reply, INVALID_CMD);
             break;
     }
 }
@@ -53,10 +67,12 @@
     switch(cmd.get_type())
     {
         case CREATE:
-            strcat(reply, "You must send a PUT request to remove an object ");
+            printf("Error: %s\n", CREATE_ERROR);
+            strcat(reply, CREATE_ERROR);
             break;
         case FUNCTION_CALL:
-            strcat(reply, "You must send a GET request to call a function");
+            printf("Error: %s\n", FUNC_CALL_ERROR);
+            strcat(reply, FUNC_CALL_ERROR);
             break;
         case DELETE:
             RPC::call(cmd.get_cmd(), reply);
@@ -65,7 +81,8 @@
             strcat(reply, cmd.get_obj_name());
             break;
         default:
-            strcat(reply, "Invalid RPC command");
+            printf("Error: %s\n", INVALID_CMD);
+            strcat(reply, INVALID_CMD);
             break;
     }
 }
@@ -84,7 +101,8 @@
             getHandler.handle(cmd, reply);
             break;
         default :
-            strcat(reply, "Invalid RPC command");
+            printf("Error: %s\n", INVALID_CMD);
+            strcat(reply, INVALID_CMD);
             break;
     }
 }