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:
Tue Oct 13 18:33:57 2015 +0000
Parent:
25:ffa1dddd3da4
Child:
27:0c2d6f598ae5
Commit message:
Added the capability to support a Mutex to control access to the SD card and avoid clashes

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	Mon Sep 28 10:29:45 2015 +0000
+++ b/RdWebServer.cpp	Tue Oct 13 18:33:57 2015 +0000
@@ -20,7 +20,7 @@
 const int MAX_FILENAME_LEN = (MAX_ARGSTR_LEN + 20);
 
 // Constructor
-RdWebServer::RdWebServer()
+RdWebServer::RdWebServer(Mutex* pSdCardMutex)
 {
     _initOk = false;
     _pStatusLed = NULL;
@@ -31,6 +31,7 @@
     _curContentLen = -1;
     _curHttpMethod = METHOD_OTHER;
     _curWebServerCmdDef = NULL;
+    _pSdCardMutex = pSdCardMutex;
 }
 
 // Destructor
@@ -307,6 +308,11 @@
     {
         RD_INFO("Request directory %s%s", _pBaseWebFolder, inFileName);
         sprintf(filename, "%s%s", _pBaseWebFolder, inFileName);
+        
+        // Obtain lock on SD card (if reqd)
+        if (_pSdCardMutex)
+            _pSdCardMutex->lock();
+            
         DIR *d = opendir(filename);
         if (d != NULL) 
         {
@@ -323,6 +329,11 @@
             }
         }
         closedir(d);
+        
+        // Release lock on SD card (if reqd)
+        if (_pSdCardMutex)
+            _pSdCardMutex->unlock();
+
         RD_DBG("Directory closed\n");
         sprintf(_httpHeader,"</ul></body></html>");
         clientSocketConn.send_all(_httpHeader,strlen(_httpHeader));
@@ -334,6 +345,10 @@
         sprintf(filename, "%s%s", _pBaseWebFolder, inFileName);
         RD_INFO("Filename %s", filename);
             
+        // Obtain lock on SD card (if reqd)
+        if (_pSdCardMutex)
+            _pSdCardMutex->lock();
+
         FILE* fp = fopen(filename, "r");
         if (fp == NULL)
         {
@@ -366,6 +381,11 @@
             fclose(fp);
             handledOk = true;
         }
+        
+        // Obtain lock on SD card (if reqd)
+        if (_pSdCardMutex)
+            _pSdCardMutex->unlock();
+
     }
     return handledOk;
 }
--- a/RdWebServer.h	Mon Sep 28 10:29:45 2015 +0000
+++ b/RdWebServer.h	Tue Oct 13 18:33:57 2015 +0000
@@ -128,7 +128,7 @@
         static const int METHOD_GET = 1;
         static const int METHOD_POST = 2;
         static const int METHOD_OPTIONS = 3;
-        RdWebServer();
+        RdWebServer(Mutex* pSdCardMutex = NULL);
         virtual ~RdWebServer();
         
         bool init(int port, DigitalOut* pStatusLed, char* pBaseWebFolder);
@@ -172,7 +172,9 @@
         int _curContentLen;
         int _curHttpMethod;
         RdWebServerCmdDef* _curWebServerCmdDef;
-
+        
+        // Mutex for SD card access (if supplied in constructor)
+        Mutex* _pSdCardMutex;
 };
 
 #endif