A sample program demonstrating a small but powerful web server using the Wifly module. This uses several libraries from others, but has a custom version of the WiflyInterface library, with numerous improvement to the mbed standard library.

Dependencies:   SW_HTTPServer WiflyInterface mbed C12832 IniManager

Here's the code

But you also might want to check out the SmartBoard-WiFly project page.

Basic Web Server

  • Serves static files from the selected file system. This is a compile-time setting, and a typical configuration supports gif, jpg, jpeg, ico, png, zip, gz, tar, txt, pdf, htm, and html.
  • It is designed to be small, thereby better supporting the limited resources of an embedded environment.

Advanced Web Services

  • Serves dynamically generated pages, where your software registers for a path, and then everything to that path activates your handler. Your handler then defines the header and body response.
  • Dynamic handlers can process GET query parameters (e.g. /dyn1?sky=blue&grass=green).
  • Dynamic handlers can process POST query parameters, as delivered from submission of a form.
  • Dynamic handlers can protect a resource with user:password access.

Run-Time Configurations

  • File System Support - using either the "local" file system supported by the magic chip, or from either an SD-Card or a USB flash drive.
  • Configurable to the maximum number of dynamic handlers (minimize memory requirements).
  • Configurable to the maximum number of name=value pairs for dynamic handlers (minimize memory requirements).

Compile-Time Configurations

  • Default filename for URL ending in '/' - default is 'index.htm'.
  • Configurable buffer sizes permit minimizing RAM requirements.
  • Configurable header response information.
  • Configurable for which serial port is used to communicate to the WiFly module.
  • Improved security option - to disable telnet access.

Diagnostics

  • API to determine the largest header (to more efficiently size the buffers).
  • API to gather the run-time characteristics - header processing time and content delivery time.

Limitations / Constraints

Known Issues

These are known issues, not yet resolved.

  1. Occasionally fails to serve a page - one test will constantly reload a web page every 30 seconds. It may run for hours, or minutes, then fail to load. Behaviors then are:
    • Hit the reload button in the browser and away it goes.
    • Hit the reload and you'll see the Wifly LEDs energize from the request, but no response by the web server. It appears that the embedded code does not "accept()" the connection in the TCP Socket Server.
      • In this case, the Wifly module has gone through an internal watchdog reset and the configuration parameters are such that it does not gracefully recover. Microchip is aware of this issue, but has not solved it.

Wifly Limitations

  • Single thread - it doesn't respond to overlapping requests (e.g. an embedded image may be requested before the main page completes transfer - the request is lost and the image not shown).
  • Single client - goes along with the single thread, but it doesn't support more than one client at a time.

Smart-Wifly-WebServer

  • Dynamic memory allocation - it does use dynamic memory allocation, which would be discouraged/avoided in many embedded systems. Here it uses it in parsing a request and it releases those resources upon completion of that request. If there is no other dynamic allocation that persists beyond a transaction, it should not cause memory fragmentation. Note that with multi-threading (if this is implemented with an OS), you then have race conditions that could cause fragmentation.

Web Server

Here's the web server in action. A combination of static pages served from the file system and dynamically generated pages.

/media/uploads/WiredHome/swsvr_1.pngPart of the main demo page,
which basically has all the
specifications, configurations, and limitations.
/media/uploads/WiredHome/swsvr_2.pngA zoomed out view of the same page.
/media/uploads/WiredHome/swsvr_3.pngIt would be possible to configure
the server via the web.
/media/uploads/WiredHome/swsvr_4.pngOne of the dynamically generated pages.
This one has parsed the query parameters.
/media/uploads/WiredHome/swsvr_5.pngA simple form which has a dynamic handler on the back end.
Here it takes the value associated with "leds"
and uses that to set the 4 LEDs on the mbed module.
/media/uploads/WiredHome/swsvr_6.pngA dynamic handler can require authentication.
/media/uploads/WiredHome/swsvr_7.pngSuccess!

But I've now gone so far beyond that in the current version. Here's what this one can do:

  1. It serves static web pages from a file system. I've only tested with the local file system and an SD card, but should work for any, so long as you remember that the local file system can't read subdirectories.
  2. It can serve dynamically generated web pages. This lets you accept name=value pairs using the URL (using either a GET or POST method). It can also accept them from forms. The demo lets you control the 4 LEDs from a form.
  3. As safely as possible it retrieves your credentials to the Wi-Fi Access Point. After using them, it overwrites that ram so they can't be as easily extracted.
  4. I made a large number of changes to the Wifly driver. It had too short of a timeout and I found quite a number of optimizations for performance and robustness.
  5. I have the start on a security feature - you can configure a resource to require user credentials to access it. The browser typically provides a username and password dialog. Take care however, as it does not support a secure (https) connection, so the credentials are not as securely transferred as I would like.

Optimizations I'd like to do:

  1. speed it up - I'm running the mbed to wifly module interface at 230K, which is about the top speed w/o flow control. There are other places where some time delays remain - I have eliminated a number of them.
  2. make it non-blocking, so other work can happen.
  3. integrate it with the rtos
  4. When a web page has referenced resources (e.g. an image tag), it rarely loads the image on the first try. I think the request for the resource comes in while it is still in the WiflyInterface cleaning up from the last connection. The Wifly module supports only a single connection at a time. I worked around this with a small bit of javascript to load the images after the web page.

But all in all I think it is a good start.

Program prerequisite

Here's the link to the program, but when you open it up, note a few very important items.

  1. Port Numbers listed in the constructor match the SmartBoard Baseboard.
  2. I sped up the communication baud rate to the mbed from the default 9600. Match your terminal program accordingly.
  3. Download this zip. Place it and an unzipped copy into the mbed local file system. These are the demo files.
  4. The typical ssid and password are not shown. See below to set yours.

ssid and password

You need to create a simple text file on your mbed root folder named "config.ini". The easiest way perhaps is to create "config.txt", add the information shown below and then rename it. This will be read at startup to connect you to the network. Something quite simple, like this:

[Wifi]
ssid=your_ssid
pass=your_pass_code

The program

And the program.

Import programSmart-WiFly-WebServer

A sample program demonstrating a small but powerful web server using the Wifly module. This uses several libraries from others, but has a custom version of the WiflyInterface library, with numerous improvement to the mbed standard library.

Files at this revision

API Documentation at this revision

Comitter:
WiredHome
Date:
Thu Oct 10 20:38:25 2013 +0000
Parent:
25:77d4cc785046
Child:
27:840556cd3ec2
Commit message:
Diagnostic Improvements.

Changed in this revision

Examples/DynamicFileIn.cpp Show annotated file Show diff for this revision Revisions of this file
Examples/DynamicFileIn.h Show annotated file Show diff for this revision Revisions of this file
SW_HTTPServer.lib Show annotated file Show diff for this revision Revisions of this file
WiflyInterface.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Examples/DynamicFileIn.cpp	Thu Oct 10 20:38:25 2013 +0000
@@ -0,0 +1,170 @@
+/// Demonstration of dynamic page creation using the Smartware Web Server
+///
+/// Here is a sample for file upload.
+
+#include "SW_HTTPServer.h"
+#include "DynamicFileIn.h"
+#include "Utility.h"
+
+typedef enum {
+    IDLE,
+    ACCEPTED,
+    REJECTED_TOO_LARGE
+} AcceptStatus;
+
+/// DynamicFileIn
+///
+/// This page lets you submit a file.
+///
+/// You can see in main how this page was registered.
+///
+bool DynamicFileIn(HTTPServer *svr, HTTPServer::CallBackType type, const char * path, const HTTPServer::namevalue *params, int paramcount)
+{
+    char buf[100];
+    int clSize;     // content-length size to be transferred
+    char * p1;      // general purpose pointer for processing the results
+    char * p2;      // general purpose pointer for processing the results
+    static char * boundary = "";    // for pointing at the boundary marker part of multipart/form-data
+    static AcceptStatus accept = IDLE;
+    bool ret = false;
+    DIR *d;
+    struct dirent *p;
+
+    switch (type) {
+        case HTTPServer::SEND_PAGE:
+            // send the header
+            //printf("Sending page\r\n");
+            svr->header(200, "OK", svr->GetSupportedType(".htm"));
+            // send some data
+            svr->send("<html><head><title>Dynamic File Submit</title></head>\r\n");
+            svr->send("<body>\r\n");
+            svr->send("<h1>Smart WiFly Web Server</h1>\r\n");
+            svr->send("This example permits you to upload a small file. Select the file using ");
+            svr->send("the normal form gadget and submit it.");
+            switch(accept) {
+                case IDLE:
+                default:
+                    break;
+                case ACCEPTED:
+                    svr->send("<br/><b>Previous submission accepted.</b><br/>\r\n");
+                    break;
+                case REJECTED_TOO_LARGE:
+                    svr->send("<br/><b>Previous submission rejected.</b><br/>\r\n");
+                    break;
+            }
+            // note that to accept a file, the form enctype is multipart/form-data.
+            sprintf(buf, "<form method='post' enctype='multipart/form-data' action='%s'>\r\n", path);
+            svr->send(buf);
+            svr->send("<table border='1'>\r\n");
+            svr->send("<tr><td>&nbsp;</td><td>File</td><td><input type='file' name='InFile' size='40'></td></tr>\r\n");
+            svr->send("<tr><td>&nbsp;</td><td colspan='2'><input type='submit' value='submit'><input type='reset' value='clear'></td></tr>\r\n");
+            svr->send("</table>\r\n");
+            svr->send("</form>\r\n");
+#if 1
+            sprintf(buf, "Directory of [%s]:<br/>\r\n", svr->GetWebRoot());
+            svr->send(buf);
+            d = opendir(svr->GetWebRoot());
+            if ( d != NULL ) {
+                while ( (p = readdir(d)) != NULL ) {
+                    sprintf(buf, " - %s<br/>\r\n", p->d_name);
+                    svr->send(buf);
+                }
+                closedir(d);
+            } else {
+                svr->send("Unable to open directory!");
+            }
+#endif
+            svr->send("<br/><a href='/'>Back to main</a></body></html>\r\n");
+            ret = true;
+            break;
+        case HTTPServer::CONTENT_LENGTH_REQUEST:
+            // Here we can find out how much data is headed our way,
+            // and choose to accept or reject it.
+            clSize = atoi(svr->GetHeaderValue("Content-Length"));
+            //printf("Content Len RQST(%d)\r\n", clSize);
+            //printf("Content-Type: [%s]\r\n", svr->GetHeaderValue("Content-Type"));
+            // also, for the multipart data, we have to learn and track
+            // the boundary sequence, so we can parse the big block
+            // successfully.
+            boundary = strstr((char *)svr->GetHeaderValue("Content-Type"), "boundary=");
+            if (boundary)
+                boundary += strlen("boundary=");
+            //printf("InFile = [%s]\r\n", svr->GetHeaderValue("InFile"));
+            // If we're happy with the size, and we have the boundary marker we need
+            if (clSize < 3000 && boundary) {    // arbitrarily chosen size to accept
+                accept = ACCEPTED;
+                ret = true;                     // accept the data
+            } else {
+                accept = REJECTED_TOO_LARGE;
+            }
+            break;
+        case HTTPServer::DATA_TRANSFER:
+            // Here's the big blob of data. We've got some painful work here.
+            // The blob looks kind of like this:
+            //---
+            //Content-Disposition: form-data; name="InFile"; filename="somefile.txt"\r\n
+            //Content-Type: text/plain\r\n
+            //\r\n
+            //Text in the file starts here\r\n
+            //and continues as needed.
+            //------webKitFormBoundary9A3C872FAC9BE23--\r\n
+            //---
+            // So, we'll work our way thru by extracting the filename, we'll
+            // assume it is text/plain, as in this example.
+            // If all looks well, we'll write it to the local file system,
+            // and return true.
+            printf("Data Transfer\r\n");
+            p1 = strchr((char *)path, '\r');
+            if (p1) {
+                p2 = strstr((char *)path, "filename=\"");
+                if (p2) {
+                    p2 += strlen("filename=\"");
+                    p1 = strchr(p2, '"');
+                    if (p1) {
+                        char * fn = p2;
+                        *p1 = '\0';
+                        p1 = strstr(p1+1, "\r\n\r\n");
+                        if (p1) {
+                            p1 += 4;        // finally, sitting on the start of the file data
+                            p2 = strstr(p1, boundary);  // find the end
+                            if (p2) {
+                                // now, create the file
+                                FILE * fp;
+                                *p2 = '\0';
+                                strcpy(buf, svr->GetWebRoot());
+                                strcat(buf, "/");
+                                strcat(buf, fn);
+                                printf("Writing to [%s]\r\n", buf);
+                                fp = fopen(buf, "w");
+                                if (fp) {
+                                    printf("writing data...\r\n");
+                                    fwrite(p1, 1, p2 - p1, fp);
+                                    fclose(fp);
+                                    printf("done.\r\n");
+                                    ret = true;
+                                } else {
+                                    printf("failed to open [%s]\r\n", buf);
+                                }
+                            }
+                        }
+                    }
+                }
+            }
+#if 0
+            svr->header(200, "OK", svr->GetSupportedType(".htm"));
+            svr->send("<html><head><title>Dynamic File Submit</title></head>\r\n");
+            svr->send("<body>\r\n");
+            svr->send("<h1>Smart WiFly Web Server</h1>\r\n");
+            svr->send("data accepted for file transfer.\r\n");
+            svr->send("<br/><a href='/'>Back to main</a></body></html>\r\n");
+#endif
+            ret = true;
+            break;
+        default:
+            printf("unknown command %d\r\n", type);
+            ret = false;
+            break;
+    }
+    return ret;
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Examples/DynamicFileIn.h	Thu Oct 10 20:38:25 2013 +0000
@@ -0,0 +1,10 @@
+
+#ifndef DYNAMICFILEIN_H
+#define DYNAMICFILEIN_H
+#include "mbed.h"
+
+#include "SW_HTTPServer.h"
+
+bool DynamicFileIn(HTTPServer *svr, HTTPServer::CallBackType type, const char * path, const HTTPServer::namevalue *params, int paramcount);
+
+#endif // DYNAMICFILEIN_H
--- a/SW_HTTPServer.lib	Thu Oct 10 19:19:41 2013 +0000
+++ b/SW_HTTPServer.lib	Thu Oct 10 20:38:25 2013 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/WiredHome/code/SW_HTTPServer/#38102ef2b132
+http://mbed.org/users/WiredHome/code/SW_HTTPServer/#90a1f5a5392f
--- a/WiflyInterface.lib	Thu Oct 10 19:19:41 2013 +0000
+++ b/WiflyInterface.lib	Thu Oct 10 20:38:25 2013 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/WiredHome/code/WiflyInterface/#dc8ef27bd311
+http://mbed.org/users/WiredHome/code/WiflyInterface/#21b7bbaad62b
--- a/main.cpp	Thu Oct 10 19:19:41 2013 +0000
+++ b/main.cpp	Thu Oct 10 20:38:25 2013 +0000
@@ -17,6 +17,7 @@
 #include "DynamicPages.h"       // my dynamically generated pages
 #include "SecurePage.h"         // my secure pages
 #include "ServerConfig.h"
+#include "DynamicFileIn.h"      // Upload a file to the server
 
 #define HTTP_SERVER_PORT 80
 
@@ -163,7 +164,8 @@
     svr.RegisterHandler("/dyn2", SimpleDynamicForm);
     svr.RegisterHandler("/pass", SimpleSecurityCheck);
     svr.RegisterHandler("/config", ServerConfig);
-
+    svr.RegisterHandler("/FileIn", DynamicFileIn);
+    
     // Let the human know it is ready - if they are watching
     pc.printf("Waiting for a connection...\r\n");