Driver - FEZ Connect Web Server

FEZ Connect Web Server

This project demonstrates a full-featured Web Server running on a Panda II controller connected to a FEZ Connect shield.

The design of the web server and application framework is based on ideas derived from these two projects:

http://www.tinyclr.com/codeshare/entry/186
http://www.tinyclr.com/codeshare/entry/247

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.

The project includes these classes:

HttpServer
NTPClient
HtmlReader
HtmlDataReader
HtmlFileReader
FEZ Connect Driver
SDCard
FlashSettings
NetSettings
NetUtils

Description of these classes and example usage code can be found at:

[http://code.tinyclr.com/project/421/fez-connect-driver/]
[http://code.tinyclr.com/project/410/html-file-reader-with-dynamic-token-replacement/]
[http://code.tinyclr.com/project/413/saving-application-settings-to-flash/]

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

can you help me a little?

Thanks :slight_smile:
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:

[ulist]VolumeInfo.DeviceFlags
VolumeInfo.FileSystemFlags
VolumeInfo.FileSystem[/ulist]

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);

Thank you for your quick reply.
I can not access
VolumeInfo.DeviceFlags
VolumeInfo.FileSystemFlags
VolumeInfo.FileSystem

because it says that the volume is not formated :o

  • so i formated the SD-Card again (FAT16, 32kb) - copied the files - same problem.

The FileReader Constructor is not called.

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:

#### Microsoft.SPOT.IO.NativeIO::GetAttributes [IP: 0000] ####
 #### [b]System.IO.FileStream::.ctor [/b][IP: 005c] ####
 #### JDI.NETMF.Web.HtmlFileReader::.ctor [IP: 0015] ####

Debug output:
filePath: \SD\Device.htm

is this correct?

Yes, that is the correct path and file name.
Are you still getting the error?

Yes, the same error. I tried a different SD-Card, too.

All i have done is:

  • Formated the SD-Card - FAT32 (tried 2 different, formated with Windows 7)
  • copied the files to the SD root.
  • changed GetDefaultSettings

Thomas,

I’m sorry, but I’m lost for an answer :frowning:

@ Pierre2 on this post is having a similar problem as you are:

[url]http://www.tinyclr.com/forum/2/5476/[/url]

Maybe the file path must be: “SD\Device.htm” without starting slash

@ Thomas8 - Aron from GHI has a possible solution in the other thread.

I try to project as the code page, however, provided the beautiful layout of the ccs work for me.
Does anyone have an idea of what could be wrong?

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:

[url]http://www.blunck.info/iehttpheaders.html[/url]
[url]http://www.digital-web.com/articles/powering_the_web_with_http/[/url]
[url]http://livehttpheaders.mozdev.org/[/url]

The css work is ofline if I load from disk!
When using the web server is the FireFox web console the following message is given:

[18:30:00.732] The style sheet http://192.168.1.101/AppStyles.css was not loaded because its MIME type, “text / html ‘, not’ text / css” is. @ http://192.168.1.101/Device.htm

I have a small adjustment in the function “contentType” and now it works.


		public static string ContentType(string extension)
		{
			string contentType = "text/html";
			switch (extension.ToLower())
			{
                                case "css":
                                        contentType = "text/css";
                                        break;
				case "jpg":
					contentType = "image/jpeg";
					break;
				case "png":
					contentType = "image/png";
					break;
				case "gif":
					contentType = "image/gif";
					break;
				case "ico":
					contentType = "image/x-icon";
					break;
				case "pdf":
					contentType = "application/pdf";
					break;
			}
			return contentType;
		}

@ Hinnie
Thank you! That was a good catch!

I’ll update the code on the Code site later this evening.

@ 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).