Source Code Bab 3
Source Code Bab 3
eth_websrv_SD_Ajax_in_out
Description: Arduino web server that displays 4 analog inputs, the state of 3
switches and controls 4 outputs,
2 using checkboxes and 2 using buttons.
The web page is stored on the micro SD card.
Hardware : Arduino Uno and official Arduino Ethernet
shield. Should work with other Arduinos and
compatible Ethernet shields.
2Gb micro SD card formatted FAT16.
A2 to A4 analog inputs, pins 2, 3 and 5 for
the switches, pins 6 to 9 as outputs (LEDs).
void loop()
{ // try to get client
EthernetClient client = server.available();
if (client) { // got client?
boolean currentLineIsBlank = true;
while (client.connected()) {
// client data available to read
if (client.available()) {
// read 1 byte (character) from client
char c = client.read();
// limit the size of the stored received HTTP
request
// buffer first part of HTTP request in HTTP_req
array (string)
// leave last element in array as 0 to null
terminate string (REQ_BUF_SZ - 1)
if (req_index < (REQ_BUF_SZ - 1)) {
// save HTTP request character
HTTP_req[req_index] = c;
req_index++;
}
// last line of client request is blank
and ends with \n
// respond to client only after last line received
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
// remainder of header follows below,
depending on if
// web page or XML page is requested
// Ajax request - send XML file
if (StrContains(HTTP_req, "ajax_inputs")) {
// send rest of HTTP header
client.println("Content-Type: text/xml");
client.println("Connection: keep-alive");
client.println();
SetLEDs();
// send XML file containing input states
XML_response(client);
}
else { // web page request
// send rest of HTTP header
client.println("Content-Type: text/html");
client.println("Connection: keep-alive");
client.println();
// send web page
// open web page file
webFile = SD.open("index.htm");
if (webFile) {
while(webFile.available()) {
// send web page to client
client.write(webFile.read());
}
webFile.close();
}
}
// display received HTTP request
on serial port
Serial.print(HTTP_req);
// reset buffer index and all buffer
elements to 0
req_index = 0;
StrClear(HTTP_req, REQ_BUF_SZ);
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())
// give the web browser time to receive the data
delay(1);
client.stop(); // close the connection
} // end if (client)
}
void SetLEDs(void)
{
// LED 1 (pin 6)
if (StrContains(HTTP_req, "LED1=1")) {
LED_state[0] = 1; // save LED state
digitalWrite(6, HIGH);
}
else if (StrContains(HTTP_req, "LED1=0")) {
LED_state[0] = 0; // save LED state
digitalWrite(6, LOW);
}
// LED 2 (pin 7)
if (StrContains(HTTP_req, "LED2=1")) {
LED_state[1] = 1; // save LED state
digitalWrite(7, HIGH);
}
else if (StrContains(HTTP_req, "LED2=0")) {
LED_state[1] = 0; // save LED state
digitalWrite(7, LOW);
}
// LED 3 (pin 8)
if (StrContains(HTTP_req, "LED3=1")) {
LED_state[2] = 1; // save LED state
digitalWrite(8, HIGH);
}
else if (StrContains(HTTP_req, "LED3=0")) {
LED_state[2] = 0; // save LED state
digitalWrite(8, LOW);
}
// LED 4 (pin 9)
if (StrContains(HTTP_req, "LED4=1")) {
LED_state[3] = 1; // save LED state
digitalWrite(9, HIGH);
}
else if (StrContains(HTTP_req, "LED4=0")) {
LED_state[3] = 0; // save LED state
digitalWrite(9, LOW);
}
}