Let's suppose we dont use the SD card of the Ethernet Shield and we have the following segment of code:
void loop()
{
EthernetClient client = server.available(); // try to get client
if (client) {
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println();
// send web page
client.println("");
client.println("");
client.println("");
client.println("Arduino Web Page");
client.println("");
client.println("");
client.println("
Hello from Arduino!
");client.println("
A web page from the Arduino server
");client.println("");
client.println("");
break;
}
if (c == '\n') {
currentLineIsBlank = true;
}
else if (c != '\r') {
currentLineIsBlank = false;
}
}
}
delay(1);
client.stop();
}
}
Now, let's suppose we use the SD card...
The above code is changed to the following:
if (client) {
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println();
// send web page
webFile = SD.open("index.htm"); // open web page file which is found in the micro SD card
if (webFile) {
while(webFile.available()) {
client.write(webFile.read()); // send web page to client
}
webFile.close();
}
break;
}
// every line of text received from the client ends with \r\n
if (c == '\n') {
// last character on line of received text
// starting new line with next character read
currentLineIsBlank = true;
}
else if (c != '\r') {
// a text character was received from client
currentLineIsBlank = false;
}
} // end if (client.available())
} // end while (client.connected())
delay(1); // give the web browser time to receive the data
client.stop(); // close the connection
} // end if (client)
}
with storing in the SD card the following index.htm file:
Arduino Web PageHello from Arduino
A web page from the Arduino SD card server.
Namely, all the commands such as:
client.println("
A web page from the Arduino server
");are changed to:
A web page from the Arduino server
inside the index.htm file.But what if we have, inside the Web Page code, lines such as:
........................
//controls the Arduino if you press the buttons
if (readString.indexOf("?button1on") >0){
digitalWrite(led, HIGH);
LED = HIGH;
}
if (readString.indexOf("?button1off") >0){
digitalWrite(led, LOW);
LED = LOW;
}
.............
client.println("<a href="/?button1on"">Turn On LED");
client.println("<a href="/?button1off"">Turn Off LED");
client.println("
");
client.println("
");
client.println("LED is now: ");
if(LED == HIGH)
{
client.println("ON");
}
else
{
client.println("OFF");
}
..........
client.println("");
client.println("");
delay(1);
//stopping client
client.stop();
//clearing string for next read
readString="";
}
}
}
}
}
What do we do in such a case? Could u tell me how the Arduino code and the index.htm file would be done????