This is about as simple as I can make it.
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip( 192,168,0,2 );
IPAddress gateway( 192,168,0,1 );
IPAddress subnet( 255,255,255,0 );
IPAddress dns( 192,168,0,1 );
EthernetServer server(80);
void setup()
{
Serial.begin(9600);
pinMode(4,OUTPUT);
digitalWrite(4,HIGH);
Ethernet.begin(mac, ip, dns, gateway, subnet);
delay(2000);
server.begin();
Serial.println("Ready");
}
void loop()
{
EthernetClient client = server.available();
if(client) {
boolean currentLineIsBlank = true;
boolean currentLineIsGet = true;
int tCount = 0;
char tBuf[64];
int rate;
char *pch;
Serial.print("Client request: ");
while (client.connected()) {
while(client.available()) {
char c = client.read();
if(currentLineIsGet && tCount < 63)
{
tBuf[tCount] = c;
tCount++;
tBuf[tCount] = 0;
}
if (c == '\n' && currentLineIsBlank) {
while(client.available()) client.read();
Serial.println(tBuf);
pch = strtok(tBuf,"?");
while(pch != NULL)
{
if(strcmp(pch,"mode=blink") == 0)
{
Serial.println("mode=blink");
// do mode = blink stuff
}
else if(strcmp(pch,"mode=steady") == 0)
{
Serial.println("mode=steady");
// do mode = steady stuff
}
else if(strcmp(pch,"color=yellow") == 0)
{
Serial.println("color=yellow");
// do color=yellow stuff
}
else if(strcmp(pch,"color=red") == 0)
{
Serial.println("color=red");
// do color=red stuff
}
else if(strncmp(pch,"rate=",5) == 0)
{
rate = atoi(pch+5);
Serial.print("rate = ");
Serial.println(rate,DEC);
}
pch = strtok(NULL,"& ");
}
Serial.println("Sending response");
client.write("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n<html><body><H1>TEST</H1>");
client.write("<form method=GET>Mode: <input type=text name=mode>
");
client.write("Rate: <input type=text name=rate>
");
client.write("Color: <input type=text name=color>
<input type=submit></form>");
client.write("</body></html>\r\n\r\n");
client.stop();
}
else if (c == '\n') {
currentLineIsBlank = true;
currentLineIsGet = false;
}
else if (c != '\r') {
currentLineIsBlank = false;
}
}
}
Serial.println("done");
}
}
Do you understand the code?
edit: changed text field names to reflect the changed variables.