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:
Thu Jul 04 02:02:02 2013 +0000
Revision:
11:183b3893eb7d
Parent:
10:b0b6da272a7b
Child:
12:479ff89c190b
Added a WiFly shell at startup before the server runs.; Documentation changes.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
WiredHome 4:178df829d62b 1 /** @file main.cpp contains the main program that a user would write.
WiredHome 4:178df829d62b 2 * see the documentation above "main"
WiredHome 4:178df829d62b 3 */
WiredHome 4:178df829d62b 4 #include "mbed.h" // ver 63
WiredHome 10:b0b6da272a7b 5 //#include "SDFileSystem.h" // ver 2, standard
WiredHome 4:178df829d62b 6
WiredHome 4:178df829d62b 7 // Modified standard components
WiredHome 4:178df829d62b 8 #include "WiflyInterface.h" // ver 4+, derived from version 4 with my mods
WiredHome 4:178df829d62b 9
WiredHome 4:178df829d62b 10 // My components
WiredHome 4:178df829d62b 11 #include "SW_HTTPServer.h" // ver 0, the initial release.
WiredHome 10:b0b6da272a7b 12 #include "DynamicPages.h" // my dynamically generated pages
WiredHome 10:b0b6da272a7b 13 #define HTTP_SERVER_PORT 80
WiredHome 4:178df829d62b 14
WiredHome 10:b0b6da272a7b 15 #include "Utility.h"
WiredHome 4:178df829d62b 16
WiredHome 8:50fc3ddf828a 17 extern "C" void mbed_reset();
WiredHome 8:50fc3ddf828a 18
WiredHome 4:178df829d62b 19 Serial pc(USBTX, USBRX);
WiredHome 11:183b3893eb7d 20 //BusOut testLEDs(LED2, LED1);
WiredHome 4:178df829d62b 21
WiredHome 4:178df829d62b 22 LocalFileSystem local("local"); // some place to hold settings and maybe he static web pages
WiredHome 4:178df829d62b 23 //SDFileSystem sd(p5, p6, p7, p8, "sd"); // for the static web pages
WiredHome 4:178df829d62b 24
WiredHome 4:178df829d62b 25
WiredHome 4:178df829d62b 26 /// This section helps manage the credentials. I don't like the examples
WiredHome 10:b0b6da272a7b 27 /// that show clear text credentials, even if they are simply the
WiredHome 4:178df829d62b 28 /// access point ssid and passphrase. So, I created this to let me
WiredHome 4:178df829d62b 29 /// edit a simple file on the file system and put them in there.
WiredHome 4:178df829d62b 30 /// Marginally more secure, but at least not in the source code that
WiredHome 4:178df829d62b 31 /// I might inadvertently share with others.
WiredHome 4:178df829d62b 32 ///
WiredHome 10:b0b6da272a7b 33 typedef struct {
WiredHome 4:178df829d62b 34 char * ssid;
WiredHome 4:178df829d62b 35 char * pass;
WiredHome 4:178df829d62b 36 } credentials;
WiredHome 4:178df829d62b 37
WiredHome 4:178df829d62b 38 #define CREDENTIAL_SIZE 50
WiredHome 4:178df829d62b 39 bool ReadCredentials(char * file, credentials * cr)
WiredHome 4:178df829d62b 40 {
WiredHome 4:178df829d62b 41 FILE *fp = fopen(file, "r");
WiredHome 10:b0b6da272a7b 42
WiredHome 4:178df829d62b 43 cr->ssid = (char *)malloc(CREDENTIAL_SIZE);
WiredHome 4:178df829d62b 44 cr->pass = (char *)malloc(CREDENTIAL_SIZE);
WiredHome 4:178df829d62b 45 if (fp) {
WiredHome 4:178df829d62b 46 fgets(cr->ssid, CREDENTIAL_SIZE, fp);
WiredHome 4:178df829d62b 47 fgets(cr->pass, CREDENTIAL_SIZE, fp);
WiredHome 4:178df829d62b 48 fclose(fp);
WiredHome 4:178df829d62b 49 RTrim(cr->ssid);
WiredHome 4:178df829d62b 50 RTrim(cr->pass);
WiredHome 4:178df829d62b 51 return true;
WiredHome 4:178df829d62b 52 } else {
WiredHome 4:178df829d62b 53 return false;
WiredHome 4:178df829d62b 54 }
WiredHome 4:178df829d62b 55 }
WiredHome 4:178df829d62b 56
WiredHome 4:178df829d62b 57 void FreeCredentials(credentials * cr)
WiredHome 4:178df829d62b 58 {
WiredHome 4:178df829d62b 59 // first secure them by wiping them out
WiredHome 4:178df829d62b 60 for (int i=0; i<CREDENTIAL_SIZE; i++) {
WiredHome 4:178df829d62b 61 cr->ssid[i] = cr->pass[i] = '*';
WiredHome 4:178df829d62b 62 }
WiredHome 4:178df829d62b 63 // then free the memory
WiredHome 4:178df829d62b 64 free(cr->ssid);
WiredHome 4:178df829d62b 65 free(cr->pass);
WiredHome 4:178df829d62b 66 }
WiredHome 4:178df829d62b 67
WiredHome 4:178df829d62b 68
WiredHome 11:183b3893eb7d 69 // A time-bound hook on startup to permit the user to access the wifly module
WiredHome 11:183b3893eb7d 70 //
WiredHome 11:183b3893eb7d 71 void WiFlyShell(Wifly & wifly, PC & pc)
WiredHome 11:183b3893eb7d 72 {
WiredHome 11:183b3893eb7d 73 Timer userTimer;
WiredHome 11:183b3893eb7d 74 const int baudrates[] = {2400, 4800, 9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600};
WiredHome 11:183b3893eb7d 75 static int i = 0;
WiredHome 11:183b3893eb7d 76
WiredHome 11:183b3893eb7d 77 pc.printf("Pausing 5 sec for user to interact with WiFly (press <enter>).\r\n");
WiredHome 11:183b3893eb7d 78 userTimer.start();
WiredHome 11:183b3893eb7d 79 do {
WiredHome 11:183b3893eb7d 80 if (pc.readable()) {
WiredHome 11:183b3893eb7d 81 bool loop = true;
WiredHome 11:183b3893eb7d 82 int c = pc.getc();
WiredHome 11:183b3893eb7d 83 pc.printf("Shell opened to WiFly module: <esc> to exit,\r\n ctrl-B to reboot, ctrl-C to step baud\r\n");
WiredHome 11:183b3893eb7d 84 while (loop) {
WiredHome 11:183b3893eb7d 85 if (pc.readable()) {
WiredHome 11:183b3893eb7d 86 int c = pc.getc();
WiredHome 11:183b3893eb7d 87 switch (c) {
WiredHome 11:183b3893eb7d 88 case '\x1B': // <ESC>
WiredHome 11:183b3893eb7d 89 loop = false;
WiredHome 11:183b3893eb7d 90 break;
WiredHome 11:183b3893eb7d 91 case '\x02': // Ctrl-B
WiredHome 11:183b3893eb7d 92 wifly.reboot();
WiredHome 11:183b3893eb7d 93 break;
WiredHome 11:183b3893eb7d 94 case '\x03': // Ctrl-C
WiredHome 11:183b3893eb7d 95 pc.printf("Setting to %d baud.\r\n", baudrates[i]);
WiredHome 11:183b3893eb7d 96 wifly.baud(baudrates[i]);
WiredHome 11:183b3893eb7d 97 i++;
WiredHome 11:183b3893eb7d 98 if (i >= (sizeof(baudrates)/sizeof(baudrates[0])))
WiredHome 11:183b3893eb7d 99 i = 0;
WiredHome 11:183b3893eb7d 100 break;
WiredHome 11:183b3893eb7d 101 default:
WiredHome 11:183b3893eb7d 102 wifly.putc(c);
WiredHome 11:183b3893eb7d 103 break;
WiredHome 11:183b3893eb7d 104 }
WiredHome 11:183b3893eb7d 105 }
WiredHome 11:183b3893eb7d 106 if (wifly.readable())
WiredHome 11:183b3893eb7d 107 pc.putc(wifly.getc());
WiredHome 11:183b3893eb7d 108 }
WiredHome 11:183b3893eb7d 109 }
WiredHome 11:183b3893eb7d 110 } while (userTimer.read_ms() < 5000);
WiredHome 11:183b3893eb7d 111 pc.printf("shell closed.\r\n");
WiredHome 11:183b3893eb7d 112 }
WiredHome 11:183b3893eb7d 113
WiredHome 11:183b3893eb7d 114
WiredHome 11:183b3893eb7d 115
WiredHome 11:183b3893eb7d 116 /// Smart-WiFly-WebServer is the creation of a web server using a WiFly module.
WiredHome 10:b0b6da272a7b 117 ///
WiredHome 4:178df829d62b 118 /// This is a working version, but it is not using the standardized wifly
WiredHome 4:178df829d62b 119 /// library, which would not work for me... I had to make a number
WiredHome 11:183b3893eb7d 120 /// of changes to get it to work well. After trying to minmimize those
WiredHome 11:183b3893eb7d 121 /// changes, additional improvements became more and more clumsy, so
WiredHome 10:b0b6da272a7b 122 /// I have now been working to refactor where it makes sense, even as
WiredHome 11:183b3893eb7d 123 /// it further deviates from the mbed-library version.
WiredHome 4:178df829d62b 124 ///
WiredHome 4:178df829d62b 125 /// I created this because I couldn't find one that worked and wanted to
WiredHome 4:178df829d62b 126 /// learn the WiFly module. There are a lot of possible improvements:
WiredHome 4:178df829d62b 127 /// @li I think I'm not using the Socket interface as fully as I should.
WiredHome 10:b0b6da272a7b 128 /// @li I would like it to be faster (the interface from mbed to wifly is
WiredHome 10:b0b6da272a7b 129 /// limited to 230400 baud before it drops chars. HW handshake could
WiredHome 10:b0b6da272a7b 130 /// improve this, but the HW handshake pins on the LPC1768 are not
WiredHome 10:b0b6da272a7b 131 /// brought out.
WiredHome 4:178df829d62b 132 /// @li I would like to integrate this with the rtos.
WiredHome 10:b0b6da272a7b 133 /// @li If a page has multiple components (e.g. images), it appears
WiredHome 4:178df829d62b 134 /// unreliable. It doesn't see the request for the extra component.
WiredHome 4:178df829d62b 135 ///
WiredHome 4:178df829d62b 136 /// history:
WiredHome 4:178df829d62b 137 /// @li 20130602 added .txt to the supported types (e.g. robots.txt), so
WiredHome 4:178df829d62b 138 /// revised the credentials to .crd, which is an unsupported type
WiredHome 10:b0b6da272a7b 139 /// therefore won't be delivered to the user.
WiredHome 4:178df829d62b 140 ///
WiredHome 4:178df829d62b 141 /// @note Copyright &copy; 2013 by Smartware Computing, all rights reserved.
WiredHome 4:178df829d62b 142 /// Individuals may use this application for evaluation or non-commercial
WiredHome 4:178df829d62b 143 /// purposes. Within this restriction, changes may be made to this application
WiredHome 4:178df829d62b 144 /// as long as this copyright notice is retained. The user shall make
WiredHome 4:178df829d62b 145 /// clear that their work is a derived work, and not the original.
WiredHome 4:178df829d62b 146 /// Users of this application and sources accept this application "as is" and
WiredHome 4:178df829d62b 147 /// shall hold harmless Smartware Computing, for any undesired results while
WiredHome 4:178df829d62b 148 /// using this application - whether real or imagined.
WiredHome 4:178df829d62b 149 ///
WiredHome 4:178df829d62b 150 /// @author David Smart, Smartware Computing
WiredHome 4:178df829d62b 151 ///
WiredHome 10:b0b6da272a7b 152 int main()
WiredHome 10:b0b6da272a7b 153 {
WiredHome 4:178df829d62b 154 credentials cr; // handles the credentials
WiredHome 10:b0b6da272a7b 155
WiredHome 4:178df829d62b 156 pc.baud(460800); // I like a snappy terminal, so crank it up!
WiredHome 10:b0b6da272a7b 157
WiredHome 4:178df829d62b 158 pc.printf("\r\nSmart WiFly 4 - build " __DATE__ " " __TIME__ "\r\n");
WiredHome 4:178df829d62b 159
WiredHome 4:178df829d62b 160 if (!ReadCredentials("/local/user.crd", &cr)) {
WiredHome 4:178df829d62b 161 pc.printf("**** ERROR, no /local/user.crd file was found. ****\r\n");
WiredHome 10:b0b6da272a7b 162 pc.printf(" Nothing else I can do until you fix this.\r\n");
WiredHome 8:50fc3ddf828a 163 wait(1.0);
WiredHome 10:b0b6da272a7b 164 error(" Waiting for user to fix this problem. \r\n"); // flash and die
WiredHome 4:178df829d62b 165 }
WiredHome 10:b0b6da272a7b 166
WiredHome 4:178df829d62b 167 // instantiate the WiflyInterface, then release the credentials
WiredHome 4:178df829d62b 168 WiflyInterface wifly(p28, p27, p23, p24, cr.ssid, cr.pass, WPA);
WiredHome 4:178df829d62b 169 FreeCredentials(&cr);
WiredHome 4:178df829d62b 170
WiredHome 11:183b3893eb7d 171 do {
WiredHome 10:b0b6da272a7b 172 wifly.init(); // start it up as a client of my network using DHCP
WiredHome 10:b0b6da272a7b 173 wifly.baud(230400); // Only 230K w/o HW flow control
WiredHome 10:b0b6da272a7b 174 if (wifly.connect())
WiredHome 10:b0b6da272a7b 175 break;
WiredHome 10:b0b6da272a7b 176 pc.printf(" Failed to connect, retrying...\r\n");
WiredHome 10:b0b6da272a7b 177 wait(1.0);
WiredHome 4:178df829d62b 178 wifly.reset();
WiredHome 10:b0b6da272a7b 179 } while (1);
WiredHome 11:183b3893eb7d 180
WiredHome 11:183b3893eb7d 181 WiFlyShell(wifly, pc);
WiredHome 4:178df829d62b 182
WiredHome 4:178df829d62b 183 // Now let's instantiate the web server - along with a few settings
WiredHome 4:178df829d62b 184 // among which is the file system path to the static pages.
WiredHome 4:178df829d62b 185 HTTPServer svr(&wifly, HTTP_SERVER_PORT, "/local/", 30, 10, &pc);
WiredHome 11:183b3893eb7d 186 pc.printf("Connect to this server at http://%s\r\n", wifly.getIPAddress());
WiredHome 10:b0b6da272a7b 187
WiredHome 4:178df829d62b 188 // But for even more fun, I'm registering a few dynamic pages
WiredHome 10:b0b6da272a7b 189 // which you see the handlers for in DynamicPages.cpp.
WiredHome 4:178df829d62b 190 // Here you can see the path to place on the URL.
WiredHome 4:178df829d62b 191 // ex. http://192.168.1.140/dyn
WiredHome 4:178df829d62b 192 svr.RegisterHandler("/dyn", SuperSimpleDynamicPage);
WiredHome 4:178df829d62b 193 svr.RegisterHandler("/dyn1", SimpleDynamicPage);
WiredHome 4:178df829d62b 194 svr.RegisterHandler("/dyn2", SimpleDynamicForm);
WiredHome 10:b0b6da272a7b 195
WiredHome 4:178df829d62b 196 // Let the human know it is ready - if they are watching
WiredHome 4:178df829d62b 197 pc.printf("Waiting for a connection...\r\n");
WiredHome 10:b0b6da272a7b 198 while (true) {
WiredHome 11:183b3893eb7d 199 //if (testLEDs == 1)
WiredHome 11:183b3893eb7d 200 // testLEDs = 2;
WiredHome 11:183b3893eb7d 201 //else
WiredHome 11:183b3893eb7d 202 // testLEDs = 1;
WiredHome 11:183b3893eb7d 203 svr.Poll(); // non-blocking, but not deterministic
WiredHome 4:178df829d62b 204 //pc.printf("this %d, longest %d\r\n", thisRun, longestRun);
WiredHome 10:b0b6da272a7b 205 }
WiredHome 4:178df829d62b 206 }