Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.

Jasper

Member Since 23 Dec 2010
Offline Last Active Jan 02 2012 03:55 PM
-----

#11770 Simple multi-threaded web server

Posted by Jasper on 07 April 2011 - 08:25 PM

Hi all!

I just created a simple server class for the Netduino Plus I would like to share with you. I found myself programming sockets on many of my little projects, so I wanted a simple non-blocking web server that was generic enough to be re-used often.

So I made a little helper class for this purpose. Once the server is started, it takes a command and argument(s) from the requested url. For instance, if you would program your Netduino to be a thermostat, you would call
http://192.168.1.100/settemp/livingroom/21
to set the temperature in the living room to 21 degrees.

To define such a command, you provide the server class with a list of possible commands, together with the number of arguments needed. Whenever a valid command is received, the server fires the CommandReceived event, passing the command and arguments in the event arguments. When handling this event, you can pass a result string (or some html) back, which is then returned in the server response. The final result (though simple) is a web server that can handle all kinds of commands while all you have to do is to handle an event.

To use this class, instantiate it, add the list of commands and a handler for the CommandReceived event:
// Instantiate a new web server on port 80.
 WebServer server = new WebServer(80);

// Add a handler for commands that are received by the server.
server.CommandReceived += new WebServer.CommandReceivedHandler(server_CommandReceived);

// Add a command (this one has a single argument)
server.AllowedCommands.Add(new WebCommand("SetLed", 1));

// Start the server
server.Start();

I have attached the code, including a sample project. To run the sample:
  • Start debugging
  • Note the IP address of your Netduino in the output window
  • Call "http://[your-ip-address]/setled/on" from your web browser

Please let me know what you think of it.

Cheers,
Jasper

Attached Files




home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.