Simplified webserver module using Webduino library

I was building a tiny websever with arduino uno and the ethernet shield, and I found that the arduino ethernet library that i was used before, wasn’t help me alot to make enhancement inside the webserver. The library itself was perfectly fine, and it works properly well, but it makes the developer build everything from the scratch, not good at all for the complex implementation. So, I just wonder someone out there build the simplified wrapper library of the one, and I found it, named “Webduino”. I done the test usability with the example that they provided, and indeed was a fantastic library, very simple to use, works perfectly fast and well.

U can find webduino library plus the source codes at https://github.com/sirleech/Webduino, they also provided some code example that u can simply follow to understand how to use that library. Ok, after successfuly downloaded and applied it on arduino ide, lets go into the sketches. First, I can just put “include <WebServer.h>” to use the webduino, like the following code below.

[gist https://gist.github.com/3852096 /]

I like to use the “Serial” arduino library alot as debug purpose, because it very simple by connecting the board into the laptop with usb cable, and open the serial terminal application to see the text that provided by the board directly. Inside the sketches, you can see the way of the code write for start and running the webserver is similar with the original ethernet library provided by the arduino.

The webduino use the callback function mechanism to trigger when there is new request coming from the third party browser or device, the dispatch is based on the url path, and this is my favorite one. It made my life more easier, without scrambling the other codes, it focus on the function or modular oriented, so i just focus inside the custom function that i made if i found something wrong with the request on specific url path.

In this example, when there was any request accepted, the “defaultCommand” function will be called. It was setup inside by statement “webserver.setDefaultCommand”, I just used one function to handle any kind of requests  with any kind of url address.

[gist https://gist.github.com/3852222 /]

Here the sketch codes inside the “defaultCommand” function, without any further thoughts, I just put the first line as “webserver.httpSuccess()” for accepting all kind of requests by default first. You shall know about the http standard implementation, there were several methods inside need to be defined before creating the request object, in this example i just applied for “Get” and “Post” method. Under the http “Get” method we can read the querystring in order to process the request parameters inside, the webduino itself support for the one by putting the extra parameter like “tailUrl” param inside the sample codes. The webduino also provides “print” to build the content and display on http response body, in this case i used the json format to display, by calling “jsonResponse” function that i made before.

[gist https://gist.github.com/3852249 /]

Ok, I’m done with the code, lets do the compiling and the transfer the binary into the arduino board. Please find the following sample request data from the third party that i used “CocoaRestClient”, this application has benefit to help us build the sample data with json format composed before send, but remember the one run under MacOSX system.

Screen_shot_2012-10-06_at_3

And for the debug information, it will display under the serial terminal application “CoolTerm”. It’s an free app that you can download easly, it’s a good terminal application that i had found so far, no crash found so far at least :-)

Screen_shot_2012-10-06_at_3

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s