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.

Committer:
Bobty
Date:
Thu Sep 19 12:06:18 2013 +0000
Revision:
1:75bb184de749
Parent:
0:b5b4d07f7827
Child:
2:9d8793c23b46
Added code to support commands - still a lot of test code in place though

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Bobty 0:b5b4d07f7827 1 #ifndef RD_WEB_SERVER
Bobty 0:b5b4d07f7827 2 #define RD_WEB_SERVER
Bobty 0:b5b4d07f7827 3
Bobty 1:75bb184de749 4 #include <vector>
Bobty 0:b5b4d07f7827 5
Bobty 0:b5b4d07f7827 6 #include "mbed.h"
Bobty 0:b5b4d07f7827 7 #include "EthernetInterface.h"
Bobty 0:b5b4d07f7827 8
Bobty 1:75bb184de749 9 typedef void (*CmdCallbackType)(char* argStr);
Bobty 1:75bb184de749 10
Bobty 1:75bb184de749 11 class RdWebServerCmdDef
Bobty 1:75bb184de749 12 {
Bobty 1:75bb184de749 13 public:
Bobty 1:75bb184de749 14 RdWebServerCmdDef(char* pStr, CmdCallbackType callback)
Bobty 1:75bb184de749 15 {
Bobty 1:75bb184de749 16 _pCmdStr = pStr;
Bobty 1:75bb184de749 17 _callback = callback;
Bobty 1:75bb184de749 18 };
Bobty 1:75bb184de749 19 char* _pCmdStr;
Bobty 1:75bb184de749 20 CmdCallbackType _callback;
Bobty 1:75bb184de749 21 };
Bobty 1:75bb184de749 22
Bobty 0:b5b4d07f7827 23 class RdWebServer
Bobty 0:b5b4d07f7827 24 {
Bobty 0:b5b4d07f7827 25 public :
Bobty 0:b5b4d07f7827 26
Bobty 0:b5b4d07f7827 27 RdWebServer();
Bobty 0:b5b4d07f7827 28 virtual ~RdWebServer();
Bobty 0:b5b4d07f7827 29
Bobty 0:b5b4d07f7827 30 bool init(int port, DigitalOut* pStatusLed);
Bobty 0:b5b4d07f7827 31 void run();
Bobty 0:b5b4d07f7827 32 bool isListening()
Bobty 0:b5b4d07f7827 33 {
Bobty 0:b5b4d07f7827 34 return _serverIsListening;
Bobty 0:b5b4d07f7827 35 };
Bobty 0:b5b4d07f7827 36
Bobty 1:75bb184de749 37 void addCommand(char* pCmdStr, CmdCallbackType callback);
Bobty 1:75bb184de749 38
Bobty 0:b5b4d07f7827 39 private :
Bobty 0:b5b4d07f7827 40 int _port;
Bobty 0:b5b4d07f7827 41 DigitalOut* _pStatusLed;
Bobty 0:b5b4d07f7827 42 TCPSocketServer _socketSrv;
Bobty 0:b5b4d07f7827 43 bool _serverIsListening;
Bobty 1:75bb184de749 44 std::vector<RdWebServerCmdDef*> _commands;
Bobty 1:75bb184de749 45 bool extractCmdArgs(char* buf, char* pCmdStr, int maxCmdStrLen, char* pArgStr, int maxArgStrLen);
Bobty 1:75bb184de749 46
Bobty 1:75bb184de749 47
Bobty 0:b5b4d07f7827 48 };
Bobty 0:b5b4d07f7827 49
Bobty 0:b5b4d07f7827 50 #endif