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.

main.cpp

Committer:
WiredHome
Date:
2013-10-10
Revision:
26:eea4db568404
Parent:
24:849cbe0b36ac
Child:
29:14c47d31a9dc

File content as of revision 26:eea4db568404:

/** @file main.cpp contains the main program that a user would write.
 * see the documentation above "main"
 */
#include "mbed.h"               // ver 66
//#include "SDFileSystem.h"       // ver 2, standard

// Modified standard components
#include "WiflyInterface.h"     // ver 37, derived from version 4 with my mods

// My components
#include "SW_HTTPServer.h"      // ver 19, the initial release.

// Project features
#include "Utility.h"
#include "Credentials.h"        // ver 0, credential manager

#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

Serial pc(USBTX, USBRX);

LocalFileSystem local("local");         // some place to hold settings and maybe the static web pages
//SDFileSystem sd(p5, p6, p7, p8, "sd");  // for the static web pages


DigitalOut test(LED1);

Timer onceinawhile;


// A time-bound hook on startup to permit the user to access the wifly module
//
void WiFlyShell(Wifly & wifly, PC & pc)
{
    Timer userTimer;
    const int baudrates[] = {2400, 4800, 9600, 19200, 38400, 57600, 115200, 230400};
    static int i = 0;
    
    pc.printf("Pausing 5 sec for shell access to WiFly (press <enter>).\r\n");
    userTimer.start();
    do {
        if (pc.readable()) {
            bool loop = true;
            int c = pc.getc();
            pc.printf("Shell opened to WiFly module: <esc> to exit,\r\n  ctrl-B to reboot, ctrl-C to step baud\r\n");
            while (loop) {
                if (pc.readable()) {
                    int c = pc.getc();
                    switch (c) {
                        case '\x1B':        // <ESC>
                            loop = false;
                            break;
                        case '\x02':        // Ctrl-B
                            wifly.reboot();
                            break;
                        case '\x03':        // Ctrl-C
                            pc.printf("Setting to %d baud.\r\n", baudrates[i]);
                            wifly.baud(baudrates[i]);
                            i++;
                            if (i >= (sizeof(baudrates)/sizeof(baudrates[0])))
                                i = 0;
                            break;
                        default:
                            wifly.putc(c);
                            break;
                    }
                }
                if (wifly.readable())
                    pc.putc(wifly.getc());
            }
        }
    } while (userTimer.read_ms() < 5000);
    pc.printf("  WiFly shell closed.\r\n");
}



/// Smart-WiFly-WebServer is the creation of a web server using a WiFly module.
///
/// This is a working version, but it is not using the standardized wifly
/// library, which would not work for me... I had to make a number
/// of changes to get it to work well. After trying to minmimize those
/// changes, additional improvements became more and more clumsy, so
/// I have now been working to refactor where it makes sense, even as
/// it further deviates from the mbed-library version.
///
/// I created this because I couldn't find one that worked and wanted to
/// learn the WiFly module. There are a lot of possible improvements:
/// @li I think I'm not using the Socket interface as fully as I should.
/// @li I would like it to be faster (the interface from mbed to wifly is
///     limited to 230400 baud before it drops chars. HW handshake could
///     improve this, but the HW handshake pins on the LPC1768 are not
///     brought out.
/// @li I would like to integrate this with the rtos.
/// @li If a page has multiple components (e.g. images), it appears
///     unreliable. It doesn't see the request for the extra component.
///
/// history:
/// @li 20130602 added .txt to the supported types (e.g. robots.txt), so
///         revised the credentials to .crd, which is an unsupported type
///         therefore won't be delivered to the user.
///
/// @note Copyright &copy; 2013 by Smartware Computing, all rights reserved.
///     Individuals may use this application for evaluation or non-commercial
///     purposes. Within this restriction, changes may be made to this application
///     as long as this copyright notice is retained. The user shall make
///     clear that their work is a derived work, and not the original.
///     Users of this application and sources accept this application "as is" and
///     shall hold harmless Smartware Computing, for any undesired results while
///     using this application - whether real or imagined.
///
/// @author David Smart, Smartware Computing
///
int main()
{
    credentials cr;     // handles the credentials

    pc.baud(460800);    // I like a snappy terminal, so crank it up!
    pc.printf("\r\nSmart WiFly - Build " __DATE__ " " __TIME__ "\r\n");

    if (!ReadCredentials("/local/user.crd", &cr)) {
        pc.printf("**** ERROR, no /local/user.crd file was found. ****\r\n");
        wait(1.0);
        error("     Waiting for user to fix this problem.         \r\n"); // flash and die
    }

    // Instantiate the WiflyInterface
    WiflyInterface wifly(p28, p27, p23, p24, cr.ssid, cr.pass, WPA);
    // Release the credentials, which also secures them
    FreeCredentials(&cr);

    // Bring the WiFly interface online
    do {
        wifly.init(); // start it up as a client of my network using DHCP
        wifly.baud(230400);     // Only 230K w/o HW flow control
        if (wifly.connect())
            break;
        pc.printf(" Failed to connect, retrying...\r\n");
        wait(1.0);
        wifly.reset();
    } while (1);

    WiFlyShell(wifly, pc);  // 5 second access at startup - in case you need it.

    // Now let's instantiate the web server - along with a few settings:
    // the Wifly object, the port of interest (typically 80),
    // file system path to the static pages,
    // the maximum parameters per transaction (in the query string),
    // the maximum number of dynamic pages that can be registered,
    // the serial port back thru USB (for development/logging)
    HTTPServer svr(&wifly, HTTP_SERVER_PORT, "/local", 15, 30, 10, &pc);
    pc.printf("Connect to this server at http://%s:%d\r\n", wifly.getIPAddress(), HTTP_SERVER_PORT);

    // But for even more fun, I'm registering a few dynamic pages
    // You see the handlers for in DynamicPages.cpp.
    // Here you can see the path to place on the URL.
    // ex. http://192.168.1.140/dyn
    svr.RegisterHandler("/dyn",  SuperSimpleDynamicPage);
    svr.RegisterHandler("/dyn1", SimpleDynamicPage);
    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");

    onceinawhile.start();
    while (true) {
        if (onceinawhile.read_ms() >= 200)
            {
            onceinawhile.reset();
            //pc.printf("Largest free mem block is %d\r\n", Free());
            test = !test;
            }
        svr.Poll();   // non-blocking, but not deterministic
        //pc.printf("this %d, longest %d\r\n", thisRun, longestRun);
        if (pc.readable())
            WiFlyShell(wifly, pc);
    }
}