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.

Committer:
WiredHome
Date:
Mon Feb 02 03:03:08 2015 +0000
Revision:
38:962d71d6dd0e
Parent:
15:1f2b62130ffb
Refresh update, bug fix, mbed application board compatibility.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
WiredHome 3:b3e21f3306a1 1 #ifndef UTILITY_H
WiredHome 3:b3e21f3306a1 2 #define UTILITY_H
WiredHome 0:08da60dc31bc 3 #include "Utility.h"
WiredHome 0:08da60dc31bc 4
WiredHome 0:08da60dc31bc 5
WiredHome 0:08da60dc31bc 6 uint32_t Free() {
WiredHome 0:08da60dc31bc 7 uint32_t max = 100000;
WiredHome 0:08da60dc31bc 8 uint32_t x = max / 2;
WiredHome 0:08da60dc31bc 9 uint32_t min = 0;
WiredHome 0:08da60dc31bc 10
WiredHome 0:08da60dc31bc 11 while (min < max-1) {
WiredHome 0:08da60dc31bc 12 void * p = malloc(x);
WiredHome 0:08da60dc31bc 13 if (p) {
WiredHome 0:08da60dc31bc 14 free(p);
WiredHome 0:08da60dc31bc 15 min = x;
WiredHome 0:08da60dc31bc 16 } else {
WiredHome 0:08da60dc31bc 17 max = x;
WiredHome 0:08da60dc31bc 18 }
WiredHome 0:08da60dc31bc 19 x = (max + min)/2;
WiredHome 0:08da60dc31bc 20 }
WiredHome 0:08da60dc31bc 21 return(x);
WiredHome 3:b3e21f3306a1 22 }
WiredHome 3:b3e21f3306a1 23
WiredHome 3:b3e21f3306a1 24 #if 0
WiredHome 3:b3e21f3306a1 25 // CopyFile
WiredHome 3:b3e21f3306a1 26 //
WiredHome 3:b3e21f3306a1 27 // An idea that didn't pan out since the local file does not support folders
WiredHome 3:b3e21f3306a1 28 //
WiredHome 3:b3e21f3306a1 29 bool CopyFile(const char * dst, const char * src) {
WiredHome 3:b3e21f3306a1 30 FILE * fsrc;
WiredHome 3:b3e21f3306a1 31 FILE * fdst;
WiredHome 3:b3e21f3306a1 32 char fbuffer[1000];
WiredHome 3:b3e21f3306a1 33 int bytes;
WiredHome 3:b3e21f3306a1 34
WiredHome 3:b3e21f3306a1 35 pc.printf("Copy to %s from %s\r\n", dst, src);
WiredHome 3:b3e21f3306a1 36 fsrc = fopen(src,"rb");
WiredHome 3:b3e21f3306a1 37 if (fsrc) {
WiredHome 3:b3e21f3306a1 38 pc.printf(" opened %s\r\n", src);
WiredHome 3:b3e21f3306a1 39 fdst = fopen(dst,"wb");
WiredHome 3:b3e21f3306a1 40 if (fdst) {
WiredHome 3:b3e21f3306a1 41 pc.printf(" opened %s\r\n", dst);
WiredHome 3:b3e21f3306a1 42 bytes = fread(fbuffer,sizeof(char),sizeof(fbuffer),fsrc);
WiredHome 3:b3e21f3306a1 43 while (bytes > 0) {
WiredHome 3:b3e21f3306a1 44 fwrite(fbuffer, sizeof(char), bytes, fdst);
WiredHome 3:b3e21f3306a1 45 bytes = fread(fbuffer,sizeof(char),sizeof(fbuffer),fsrc);
WiredHome 3:b3e21f3306a1 46 }
WiredHome 3:b3e21f3306a1 47 fclose(fdst);
WiredHome 3:b3e21f3306a1 48 fclose(fsrc);
WiredHome 3:b3e21f3306a1 49 pc.printf(" success.\r\n");
WiredHome 3:b3e21f3306a1 50 return true;
WiredHome 3:b3e21f3306a1 51 }
WiredHome 3:b3e21f3306a1 52 fclose(fsrc);
WiredHome 3:b3e21f3306a1 53 }
WiredHome 3:b3e21f3306a1 54 pc.printf(" failed.\r\n");
WiredHome 3:b3e21f3306a1 55 return false;
WiredHome 3:b3e21f3306a1 56 }
WiredHome 3:b3e21f3306a1 57
WiredHome 3:b3e21f3306a1 58 // Copy /sd/web to /local/web [local file system does not support subdirs]
WiredHome 3:b3e21f3306a1 59 // I wanted to first image the sd card to the local file system for easier
WiredHome 3:b3e21f3306a1 60 // access, and then reverse it for easier updates, but was not able to do this.
WiredHome 3:b3e21f3306a1 61 if (0) {
WiredHome 3:b3e21f3306a1 62 printf("Open directory on /sd/web\r\n");
WiredHome 3:b3e21f3306a1 63 DIR *d = opendir("/sd/web");
WiredHome 3:b3e21f3306a1 64 struct dirent *p;
WiredHome 3:b3e21f3306a1 65 while((p = readdir(d)) != NULL) {
WiredHome 3:b3e21f3306a1 66 printf("%s\r\n", p->d_name);
WiredHome 3:b3e21f3306a1 67 char sfqfn[40], dfqfn[40];
WiredHome 3:b3e21f3306a1 68 sprintf(sfqfn,"%s/%s","/sd/web", p->d_name);
WiredHome 3:b3e21f3306a1 69 sprintf(dfqfn,"%s/%s","/local", p->d_name);
WiredHome 3:b3e21f3306a1 70 CopyFile(dfqfn,sfqfn);
WiredHome 3:b3e21f3306a1 71 }
WiredHome 3:b3e21f3306a1 72 closedir(d);
WiredHome 3:b3e21f3306a1 73 printf("Directory closed\r\n");
WiredHome 3:b3e21f3306a1 74 }
WiredHome 3:b3e21f3306a1 75 #endif
WiredHome 3:b3e21f3306a1 76
WiredHome 3:b3e21f3306a1 77
WiredHome 3:b3e21f3306a1 78 #endif