jurs:
Here is some HTTP server example code that retrieves the requested “file” and “parameter” from a HTTP request and can react on them. Please set MAC and IP address to values that are unique in your local network.
Currently supported for handling are URL adresses like:
http://192.168.2.250/on
http://192.168.2.250/off
But also all other requests are answered with a “plain text” response to avoid your application from blocking for 30 or 45 seconds (typical HTTP timeout duration) when sending invalid requests using a webbrowser.
After a reqest is parsed, the requested “path and file name” is available in the global char arrays “httpFile” and the extra parameters sent is available in the char array “httpParam”. Watch out for serial debug! So you could change the source code easily to handle possible requests like:
http://192.168.2.250/motor?on
http://192.168.2.250/led?off
Here is the Arduino webserver code:
// Ethernet GET request example by 'jurs', created for Arduino forum
// http://forum.arduino.cc/index.php?topic=325984.msg2251372#msg2251372
#include <Ethernet.h>
#include <SPI.h>
// the next two lines must contain unique MAC and IP address in your LAN network
byte mac = { 0x00, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip = { 192, 168, 2, 250 };
EthernetServer server(80);
// the next two variables will be used by ‘readClientRequest()’ and ‘executeClientRequest()’
char httpFile[13];
char httpParam[13];
void sendClientResponse(EthernetClient &client)
{
// send a standard http response header
client.println(F(“HTTP/1.1 200 OK”));
client.println(F(“Content-Type: text/plain”));
client.println(F(“Connection: close”));
client.println(F(“Cache-Control: no-cache, no-store”));
client.println(F(“Pragma: no-cache”));
client.println();
client.println(httpFile);
client.println(httpParam);
}
void readClientRequest(EthernetClient &client)
{ // read the complete client request (HTTP header lines)
// header lines are temporarily stored in ‘linebuf’
// GET parameter is stored in global variable ‘httpGet’
//
char linebuf[80];
int charcount=0;
char* strPtr;
char* paramPtr;
memset(linebuf,0,sizeof(linebuf));
memset(httpFile,0,sizeof(httpFile));
memset(httpParam,0,sizeof(httpParam));
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected())
{
if (client.available())
{
char c = client.read();
if (charcount<sizeof(linebuf)-1)
{
linebuf[charcount]=c;
charcount++;
}
// Serial.write(c); // for serial debug if you like
// if you’ve gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == ‘\n’ && currentLineIsBlank) {
break; // end of header found, break while-loop
}
else if (c == ‘\n’)
{
// we have a complete line, look what’s in it
if (strncasecmp(linebuf,"GET “,4)==0) // this is a GET request
{ // extract the requested file and parameter
strPtr= strchr(&linebuf[4],’ ‘);
strPtr[0]=’\0’;
paramPtr= strchr(&linebuf[4],’?’);
if (paramPtr!=NULL)
{
paramPtr[0]=’\0’;
paramPtr++;
if (strlen(paramPtr)<sizeof(httpParam)) strcpy(httpParam, paramPtr);
}
strncpy(httpFile, &linebuf[4], sizeof(httpFile)-1);
Serial.print(httpFile);Serial.print(” ? ");Serial.println(httpParam);
}
// now we are starting a new line
currentLineIsBlank = true;
memset(linebuf,0,sizeof(linebuf));
charcount=0;
}
else if (c != ‘\r’)
{
// you’ve gotten a character on the current line
currentLineIsBlank = false;
}
} // if (client.available())
} // while (client.connected())
} // void readClientRequest(EthernetClient &client)
void handleClientRequest()
{ // function handles requests
if (strstr(httpFile,"/on")==httpFile)
Serial.println(“Requested action is ‘on’”);
else if (strstr(httpFile,"/off")==httpFile)
Serial.println(“Requested action is ‘off’”);
}
void setup(){
Serial.begin(115200);
Ethernet.begin(mac, ip);
Serial.print("Server is at: ");Serial.println(Ethernet.localIP());
}
void loop()
{
EthernetClient client = server.available();
if (client)
{
readClientRequest(client);
handleClientRequest();
sendClientResponse(client);
client.flush();
delay(1);
client.stop();
}
}
Thanks for your help!