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/21to 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
- Arron Chapman and Michel Trahan like this