A simple web server mainly based on ideas from Jasper Schuurmans Netduino web server

Dependents:   RdBlindsServer SpideyWallWeb RdGasUseMonitor

A fast and reliable web server for MBED! http://robdobson.com/2015/08/a-reliable-mbed-webserver/

It has a very neat way to implement REST commands and can serve files from local storage (on LPC1768 for instance) and from SD cards. It also has a caching facility which is particularly useful for serving files from local storage.

The server can be run in the main() thread (and has a sub-2ms response time if this is done) or in a mbed-rtos thread which increases the response time to (a still respectable) 30ms or so.

The latest project that uses this is here - https://developer.mbed.org/users/Bobty/code/SpideyWallWeb/

int main (void)
{
    // Ethernet interface
    EthernetInterface::init();

    // Connect ethernet
    EthernetInterface::connect();

    // Init the web server
    pc.printf("Starting web server\r\n");
    char* baseWebFolder = "/sd/";  // should be /sd/ for SDcard files - not used for local file system
    RdWebServer webServer;
    
    // Add commands to handle the home page and favicon
    webServer.addCommand("", RdWebServerCmdDef::CMD_LOCALFILE, NULL, "index.htm", true);
    webServer.addCommand("favicon.ico", RdWebServerCmdDef::CMD_LOCALFILE, NULL, NULL, true);
    
    // Add the lightwall control commands
    webServer.addCommand("name", RdWebServerCmdDef::CMD_CALLBACK, &lightwallGetSystemName);
    webServer.addCommand("clear", RdWebServerCmdDef::CMD_CALLBACK, &lightwallClear);
    webServer.addCommand("rawfill", RdWebServerCmdDef::CMD_CALLBACK, &lightwallRawFill);
    webServer.addCommand("fill", RdWebServerCmdDef::CMD_CALLBACK, &lightwallFill);
    webServer.addCommand("showleds", RdWebServerCmdDef::CMD_CALLBACK, &lightwallShowLeds);
    
    // Start the server
    webServer.init(WEBPORT, &led4, baseWebFolder);
    webServer.run();

}

// Get system name - No arguments required
char* lightwallGetSystemName(int method, char*cmdStr, char* argStr, char* msgBuffer, int msgLen, 
                int contentLen, unsigned char* pPayload, int payloadLen, int splitPayloadPos)
{
    // Perform any required actions here ....

    // ...

    // Return the system name
    return systemName;
}

This server was originally based on a Netduino web server from Jasper Schuurmans but has been optimised for speed.

Files at this revision

API Documentation at this revision

Comitter:
Bobty
Date:
Fri Oct 04 10:36:57 2013 +0000
Parent:
2:9d8793c23b46
Child:
4:6afb3bbf20a4
Commit message:
Tidied up a little - no functional changes

Changed in this revision

RdWebServer.cpp Show annotated file Show diff for this revision Revisions of this file
RdWebServer.h Show annotated file Show diff for this revision Revisions of this file
--- a/RdWebServer.cpp	Fri Oct 04 07:41:28 2013 +0000
+++ b/RdWebServer.cpp	Fri Oct 04 10:36:57 2013 +0000
@@ -1,3 +1,9 @@
+/* RdWebServer.cpp
+   Rob Dobson 2013
+   Inspired by Jasper Schuurmans multi-threaded web server for Netduino http://www.schuurmans.cc/multi-threaded-web-server-for-netduino-plus
+   More details at http://robdobson.com/2013/10/moving-my-window-shades-control-to-mbed/
+*/
+
 #include "RdWebServer.h"
 
 #define MAX_CMDSTR_LEN 100
@@ -322,7 +328,7 @@
 //                printf("Received Data: %d\n\r\n\r%.*s\n\r",strlen(buffer),strlen(buffer),buffer);
                 if (strncmp(buffer, "GET ", 4) == 0)
                 {
-//                    printf("GET request incomming.\n\r");
+//                    printf("GET request\n\r");
                     char cmdStr[MAX_CMDSTR_LEN];
                     char argStr[MAX_ARGSTR_LEN];
                     if (extractCmdArgs(buffer+3, cmdStr, MAX_CMDSTR_LEN, argStr, MAX_ARGSTR_LEN))
@@ -362,14 +368,7 @@
                         if (!cmdFound)
                             handleLocalFileRequest(cmdStr, argStr, &client, httpHeader, false);
                     }
-                    
-                    //setup http response header & data
-/*                    char httpHeader[256] = {};
-                    sprintf(httpHeader,"HTTP/1.1 200 OK\n\rContent-Length: %d\n\rContent-Type: text\n\rConnection: Close\n\r\n\r",strlen(buffer));
-                    client.send(httpHeader,strlen(httpHeader));
-                    client.send(cmdStr,strlen(cmdStr)); */
-                    //clientIsConnected = false;
-//                    printf("Server done.\n\r");
+
                     break;
                 }
                 
--- a/RdWebServer.h	Fri Oct 04 07:41:28 2013 +0000
+++ b/RdWebServer.h	Fri Oct 04 10:36:57 2013 +0000
@@ -1,3 +1,7 @@
+/* RdWebServer.h
+   Rob Dobson 2013
+   Inspired by Jasper Schuurmans multi-threaded web server for Netduino http://www.schuurmans.cc/multi-threaded-web-server-for-netduino-plus
+*/
 #ifndef RD_WEB_SERVER
 #define RD_WEB_SERVER