Reference Home

HTTP Library Introduction

This library has the purpose of drastically reducing work when you are in WEB based programming activity.

WEB communication is based on http protocol. This protocol is, essentially based on a couple of messages : a request and consequent response.

 

http protocoll

 

Suppose we want to be a WEB server.

First of all, we connect Arduino with the net. Then we act as a server socket and listen for incoming link. When link is accepted, we can start to listen for incoming request message.

The function getRequest() do noting until a request message arrives.

When request message arrives the function decodes header and detects the resource (URL) required. The resource names are automatically mapped with functions we have prepared.

This map is done preparing an array of typedef WEBRES and provide this as argument of getRequest().

WEBRES type is a struct of resource-name,function-name coupled.

So, if the name of resource (URL) required, matches one resource-name in the array, corresponding function is activated. In other case, a Resource not found message is automatically send as response.

Function activated has access to a possible query string, and can decode parameters and its values.

Function coupled with resource name must send a response.

Response can be a html static page stored in PROGMEM space of Arduino, or a dynamic page stored in PROGMEM. Static page is a html text sent exactly like it. Dynamic page contains one or more tags @. Each tag is substitutes by a string in an array in sequential order.

This array can be made at the moment of response, and has to be provided to the function sendDynResponse() as argument.

Example:

/**************************/

// we prepare links resouce-name->routine 
WEBRES rs[]={{"/index",pindex},{"/Analog",panalog},{"/RDigital",rdigital},
{"/WDigital",wdigital},{"/wdig",wdig},{"/Pwm",pwmpage},{"/pwmset",pwmset}}; /*****************************/ void loop() // Arduino loop { WIFI.getRequest(csocket,7,rs); // if request arrives for resource "http://...../Analog // function panalog() is activated } /*****************************/ prog_char pageAnalog[] PROGMEM= // html page containing a tag @ "<html>.........." "....A1 value = @....." "......</hml>"; /*****************************/ void panalog(char *query) // routine activated by request of /Analog resource { // if need function can retrive parameters and its value from string query // using svpar=getParameter(query,querylength,parameter-name) // prepare dynamic response making strings to substitute tag @ char sval[5]; //buffer to hold analogic value of pin A1 sprintf(sval,"%d",anlogRead(1)); //put integer value in sval as string char *tags[1]; //make an array of only one string (for semplicity) tags[0]=sval; //put string value into the array sendResponse(socket,pageAnalog,1,tags);//provide array (end length) to response function } // function substitutes tag and send html page /*******************************/

If Arduino acts as client, it has to send request (mode GET ) with parameters and to listen for response.

sendRequest() function as a string resource as argument. This query string is made by a resource-name+query-string.

Example: /page1?par1=100&par2=60

Query string can by added to resource name manually or using the addParameter() function.

Add parameter function decode automatically special characters and blank (see query format on the WEB)

getResponse() function can return data sent in response message.

 

See Also:

Reference Home