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.
RdWebServer.h@0:b5b4d07f7827, 2013-09-18 (annotated)
- Committer:
- Bobty
- Date:
- Wed Sep 18 21:36:50 2013 +0000
- Revision:
- 0:b5b4d07f7827
- Child:
- 1:75bb184de749
Initial
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Bobty | 0:b5b4d07f7827 | 1 | #ifndef RD_WEB_SERVER |
Bobty | 0:b5b4d07f7827 | 2 | #define RD_WEB_SERVER |
Bobty | 0:b5b4d07f7827 | 3 | |
Bobty | 0:b5b4d07f7827 | 4 | #include <map> |
Bobty | 0:b5b4d07f7827 | 5 | |
Bobty | 0:b5b4d07f7827 | 6 | #include "mbed.h" |
Bobty | 0:b5b4d07f7827 | 7 | #include "EthernetInterface.h" |
Bobty | 0:b5b4d07f7827 | 8 | |
Bobty | 0:b5b4d07f7827 | 9 | class RdWebServer |
Bobty | 0:b5b4d07f7827 | 10 | { |
Bobty | 0:b5b4d07f7827 | 11 | public : |
Bobty | 0:b5b4d07f7827 | 12 | |
Bobty | 0:b5b4d07f7827 | 13 | RdWebServer(); |
Bobty | 0:b5b4d07f7827 | 14 | virtual ~RdWebServer(); |
Bobty | 0:b5b4d07f7827 | 15 | |
Bobty | 0:b5b4d07f7827 | 16 | bool init(int port, DigitalOut* pStatusLed); |
Bobty | 0:b5b4d07f7827 | 17 | void run(); |
Bobty | 0:b5b4d07f7827 | 18 | bool isListening() |
Bobty | 0:b5b4d07f7827 | 19 | { |
Bobty | 0:b5b4d07f7827 | 20 | return _serverIsListening; |
Bobty | 0:b5b4d07f7827 | 21 | }; |
Bobty | 0:b5b4d07f7827 | 22 | |
Bobty | 0:b5b4d07f7827 | 23 | private : |
Bobty | 0:b5b4d07f7827 | 24 | int _port; |
Bobty | 0:b5b4d07f7827 | 25 | DigitalOut* _pStatusLed; |
Bobty | 0:b5b4d07f7827 | 26 | TCPSocketServer _socketSrv; |
Bobty | 0:b5b4d07f7827 | 27 | bool _serverIsListening; |
Bobty | 0:b5b4d07f7827 | 28 | }; |
Bobty | 0:b5b4d07f7827 | 29 | |
Bobty | 0:b5b4d07f7827 | 30 | #endif |