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.
Revision 9:35668248199b, committed 2015-02-22
- Comitter:
- Bobty
- Date:
- Sun Feb 22 11:54:18 2015 +0000
- Parent:
- 8:de915bd70ec1
- Child:
- 10:b4b9d4d5e5be
- Commit message:
- Changed back to non-blocking - seems more stable
Changed in this revision
RdWebServer.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/RdWebServer.cpp Fri Feb 20 08:53:08 2015 +0000 +++ b/RdWebServer.cpp Sun Feb 22 11:54:18 2015 +0000 @@ -120,7 +120,7 @@ } else { - client.set_blocking(false, 1000); + client.set_blocking(false, 2000); pc.printf("Connection from IP: %s\n\r",client.get_address()); if (_pStatusLed != NULL) *_pStatusLed = true; @@ -230,7 +230,7 @@ char fileBuf[1024]; while ((rdCnt = fread(fileBuf, sizeof( char ), 1024, fp)) == 1024) { - client.send(fileBuf, rdCnt); + client.send_all(fileBuf, rdCnt); } client.send(fileBuf, rdCnt); fclose(fp);