The example code shows how to use the web server, and provides a framework for developing an application that can be controlled from a remote web client. I’ve included the entire project with three example web pages in the zip file.
The web pages should be copied to a formatted SD card and plugged into the Panda II before starting the application.
The application and network settings can be configured remotely via the Settings web page. After saving the settings in Flash memory, the device will reboot and restart the web server.
I tested the web server running over a period of 24 hours, servicing more than 700 web requests.
It performed flawlessly with no errors, no exceptions, and no memory leaks.
Release Notes:
[Version 1.0 - 01/11/2012]
Uses Panda II Firmware version 4.1.8.0
[Version 1.1 - 01/16/2012]
Modified NetUtils.ContentType method to return “text/css” for “.css” file types.
[Version 1.2 - 01/21/2012]
Modified SetSystemTime method to catch exceptions when calling RealTimeClock.GetTime().
[Version 1.3 - 03/02/2012]
Changed type from Extension to Driver.
Implemented changes suggested by Brett on the forum, including:
Replaced hardcoded app name with a constant string.
Added several mime types to NetUtils.ContentType method.
Changed “RTC Battery: Good” to “Realtime Clock: Working”.
Increased delay before automatic reboot from 10 secs to 30 secs.
Allowed the application to startup even if SD card is not mounted.
Changed fully qualified URLs for redirects to relative URLs.
[Version 1.4 - 03/04/2012]
Modified Program.cs and HTTPServer.cs to handle an unmounted SD card more gracefully.
The application now detects if an SD card is not mounted, and tries to remount it.
If not successful, the application sends back an application-level error message.
If a resource is requested that does not have a request handler registered, the http server will send back a 500 error message.
Thank you very mutch for the great work!
I have a problem running your sample-code + sample pages on my Panda II Controller + FEZ Connect Shield (Firmware ver: 4.1.8.0)
I have formatted my 2GB SD-Card and copied the files, changed the settings and started the webserver.
But when i call the pages from a browser, the debugger says:
Web Server Demo : Starting HTTP server…
Web Server Demo : Http status : Listening
Free mem : 27240
Web Server Demo : Http status : RequestRcvd : /
Free mem : 29256
Web Server Demo : Http status : ResponseSent
Free mem : 28944
Web Server Demo : Http status : Listening
Free mem : 32484
Web Server Demo : Http status : RequestRcvd : /Device.htm
Free mem : 29244
#### Exception System.NotSupportedException - CLR_E_NOT_SUPPORTED (5) ####
#### Message:
#### Microsoft.SPOT.IO.NativeIO::GetAttributes [IP: 0000] ####
#### System.IO.FileStream::.ctor [IP: 005c] ####
#### JDI.NETMF.Web.HtmlFileReader::.ctor [IP: 0015] ####
#### FEZConnect_WebServer.Program::DevicePageHandler [IP: 00ec] ####
#### JDI.NETMF.WIZnet.HTTPServer::ProcessHttpRequest [IP: 008b] ####
#### JDI.NETMF.WIZnet.HTTPServer::ServerThread [IP: 001f] ####
A first chance exception of type ‘System.NotSupportedException’ occurred in Microsoft.SPOT.IO.dll
A first chance exception of type ‘System.NotSupportedException’ occurred in System.IO.dll
Web Server Demo : Http status : Error : Exception was thrown: System.NotSupportedException
Free mem : 22920
Thanks
Thomas, it appears that the FileStream class does not like something about your SD card.
Could you add some Debug.Print statements after the SD card is mounted to print out the following:
You could also add a Debug.Print statement in the HtmlFileReader constructor just before the FileStream object is created that prints out the filePath, like this:
Debug.Print(filePath);
this.fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.None, constFileBufferSize);
I formatted my cards FAT32 on a Windows XP system.
I copied the web files from the ZIP file directly to the root of the SD card (do not copy them to a sub-folder of the root).
Your exception stacktrace indicates that the FileStream constructor was called:
It looks like the AppStyles.css file is not being downloaded to your browser.
Are you sure the file is on your SD card?
If you watch the debug messages, do you see that the AppStyles.css file is being requested and then downloaded?
@ jasdev
Thanks for your hint, but the debug output looks good to me
Web Server Demo : Startup...
Free mem : 32184
Web Server Demo : Setting system time...
Web Server Demo : System time set : 01/16/2012 07:51:34
Free mem : 31656
Web Server Demo : Mounting the SD card...
Web Server Demo : SD card mounted.
Free mem : 31404
Web Server Demo : Loading application settings...
Web Server Demo : Application settings loaded.
Free mem : 30348
Web Server Demo : Initializing FEZ Connect...
Free mem : 29604
Web Server Demo : Initializing network...
Web Server Demo : Network ready.
IP Address: 192.168.1.101
Subnet Mask: 255.255.255.0
Default Getway: 192.168.1.1
DNS Server: 192.168.178.1
Free mem : 35220
Web Server Demo : Requesting NTP date-time...
Web Server Demo : NTP date-time set : 01/16/2012 07:51:42
Free mem : 35220
Web Server Demo : Registering Http event handlers...
Free mem : 34308
Web Server Demo : Starting HTTP server...
Web Server Demo : Http status : Listening
Free mem : 30504
Web Server Demo : Http status : RequestRcvd : /Device.htm
Free mem : 29028
Web Server Demo : Http status : ResponseSent
Free mem : 25980
Web Server Demo : Http status : Listening
Free mem : 30108
Web Server Demo : Http status : RequestRcvd : /AppStyles.css
Free mem : 26796
Web Server Demo : Http status : ResponseSent
Free mem : 23784
Web Server Demo : Http status : Listening
Free mem : 27996
Web Server Demo : Http status : RequestRcvd : /images/favicon.ico
Free mem : 24840
Web Server Demo : Http status : ResponseSent
Free mem : 23928
Web Server Demo : Http status : Listening
Free mem : 27996
Web Server Demo : Http status : RequestRcvd : /images/favicon.ico
Free mem : 24840
Web Server Demo : Http status : ResponseSent
Free mem : 23928
Web Server Demo : Http status : Listening
Yes it does.
Are you able to use css settings on other “sites”?
What browser type and version are you using?
There is an excellent plug-in for IE that shows you the header contents of messages. You could try installing it and then examine the headers for any possible clues:
@ Jasdev
When testing the server I get the error “An Unhandled exception” quite often, you have a solution?
When debugging you get these messages:
Web Server Demo : Http status : Listening
Free mem : 27996
#### Exception System.NullReferenceException - CLR_E_NULL_REFERENCE (5) ####
#### Message:
#### JDI.NETMF.WIZnet.HTTPServer::ProcessHttpRequest [IP: 0010] ####
#### JDI.NETMF.WIZnet.HTTPServer::ServerThread [IP: 001f] ####
A first chance exception of type 'System.NullReferenceException' occurred in FEZConnect_WebServer.exe
An unhandled exception of type 'System.NullReferenceException' occurred in FEZConnect_WebServer.exe
Uncaught exception
The thread '<No Name>' (0x5) has exited with code 0 (0x0).