HTTPServer | |
---|---|
Quick Look | |
Hardware | WF32 or WI-Fire |
Include | HTTPServer.h |
The HTTPServer allows web pages and a limited set of file types stored on a SD Card to be served to a web browser.
The HTTP Server library abstracts the complexities of creating HTTP Web server hosting pages that can be written in almost any HTML editor. Once implemented, just copy your pages onto an SD card and plug it into the SD card reader on your chipKIT™ board. Restart the server. All links specified in the pages should be relative to the current page, or relative to the root of the SD file system. The default page is called HomePage.htm, and this page must exist at the root of the SD file system. All files on the SD file system must be limited to the 8.3 file naming convention; appropriate extensions should be used on your files. The SD file system can contain HTML pages, JPEGs, GIFs, ICOs, TXT, MPEGs, WMVs, JSs, and XMLs for download to the browser; however, only the two/three-letter extensions may be used for each file type. The content type specified to the requesting browser is determined by the two/three-letter file extension.
This is only a partial code excerpt. For the entire example sketch visit the github repository linked at the bottom of this page. This example demonstrates how to serve both static and dynamically generated web pages. The dynamic web page in this example shows how to manipulate pin states on the chipKIT from the client web browser.
/************************************************************************/
/* */
/* deWebIOServerSrc */
/* */
/* A Example chipKIT HTTP Server implementation */
/* This sketch is designed to work with web browser clients */
/* */
/************************************************************************/
/* Author: Keith Vogel */
/* Copyright 2014-2015, Digilent Inc. */
/************************************************************************/
#include <HTTPServer.h>
/************************************************************************/
/* HTTP URL Matching Strings */
/************************************************************************/
// These are the HTTP URL match strings for the dynamically created
// HTML rendering functions.
// Make these static const so they get put in flash
static const char szHTMLRestart[] = "GET /Restart ";
static const char szHTMLTerminate[] = "GET /Terminate ";
static const char szHTMLReboot[] = "GET /Reboot ";
static const char szHTMLFavicon[] = "GET /favicon.ico ";
static const char szHTMLGetPins[] = "GET /PinsPage.htm ";
static const char szHTMLPostPins[] = "POST /PinsPage.htm ";
// here is our sample/example dynamically created HTML page
GCMD::ACTION ComposeHTMLGetPINS(CLIENTINFO * pClientInfo);
GCMD::ACTION ComposeHTMLPostPINS(CLIENTINFO * pClientInfo);
// get rid of as much of the heap as we can, the SD library requires some heap
#define CHANGE_HEAP_SIZE(size) __asm__ volatile ("\t.globl _min_heap_size\n\t.equ _min_heap_size, " #size "\n")
CHANGE_HEAP_SIZE(0x200);
/*** void setup(void)
*
* Parameters:
* None
*
* Return Values:
* None
*
* Description:
*
* Arduino Master Initialization routine
*
*
* ------------------------------------------------------------ */
void setup(void)
{
// Must do a Serial.begin because the HTTP Server
// has diagnostic prints in it.
Serial.begin(9600);
Serial.println("IOServer v1.0");
Serial.println("Copyright 2015, Digilent Inc.");
Serial.println("Written by Keith Vogel");
Serial.println();
InitializePins();
// add rendering functions for dynamically created web pages
// max of 10 AddHTMLPage() allowed
// these are the Select picture pages in Post.htm
AddHTMLPage(szHTMLGetPins, ComposeHTMLGetPINS);
AddHTMLPage(szHTMLPostPins, ComposeHTMLPostPINS);
// comment this out if you do not want to support
// restarting the network stack from a browser
AddHTMLPage(szHTMLRestart, ComposeHTMLRestartPage);
// comment this out if you do not want to support
// terminating the server from a browser
AddHTMLPage(szHTMLTerminate, ComposeHTMLTerminatePage);
// comment this out if you do not want to support
// rebooting (effectively hitting MCLR) the server from a browser
AddHTMLPage(szHTMLReboot, ComposeHTMLRebootPage);
// This example supports favorite ICONs,
// those are those icon's next to the URL in the address line
// on the browser once the page is displayed.
// To support those icons, have at the root of the SD file direcotory
// an ICON (.ico) file with your ICON in it. The file MUST be named
// favicon.ico. If you do not have an icon, then uncomment the following
// line so the server will tell the browser with an HTTP file not found
// error that we don't have a favoite ICON.
// AddHTMLPage(szHTMLFavicon, ComposeHTTP404Error);
// Make reading files from the SD card the default compose function
SetDefaultHTMLPage(ComposeHTMLSDPage);
// Initialize the SD card
SDSetup();
// Initialize the HTTP server
ServerSetup();
}
/*** void loop(void)
*
* Parameters:
* None
*
* Return Values:
* None
*
* Description:
*
* Arduino Master Loop routine
*
*
* ------------------------------------------------------------ */
void loop(void)
{
// process the HTTP Server
ProcessServer();
}
Parameter | Library File | Description |
---|---|---|
ComposeHTTP404Error | HTTPHelpers.cpp | Renders the file not found 404 error page and prints "HTTP Error" to the serial port. |
ComposeHTMLRestartPage | HTMLRestart.cpp | Renders the server restart HTML page. |
ComposeHTMLTerminatePage | HTMLTerminate.cpp | Renders the server terminate HTML page. |
ComposeHTMLRebootPage | HTMLReboot.cpp | Renders the server restart HTML page. |
ComposeHTMLSDPage | HTMLSDPage.cpp | This renders a page off of the SD filesystem. Pages of type .htm, .html, .jpeg, .png, .txt and more may be rendered The file extension on the filename determine the MIME type returned to the client. |
void ServerSetup(void);
Initialize the HTTP server.
void ProcessServer(void);
Process the HTTP Server.
bool AddHTMLPage(const char * szMatchStr, FNRENDERHTML FnComposeHTMLPage);
Adds a dynamically generated page. When the URL (I.E. "GET /PinsPage.htm ") matches the string of parameter szMatchStr then the function passed to parameter FNComposeHTMLPage is executed. Returns true if it was added, false if there was no room to add the page search string
void SetDefaultHTMLPage(FNRENDERHTML FnDefaultHTMLPage);
If no match strings match (see AddHTMLPage), than the default compose HTML page is called. This is optional as the HTTP 404 file not found page is initialized as the default page. This function allows you to replace the default page.
void SDSetup(void);
Initializes SD Reader for HTML file operations.