Morning!
Trying to wrap my head around this basic WEBSERVER sketch and would love some experienced programmers comments because I am really trying to understand the process:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(176, 10 1, 143);
EthernetServer server(80);
void setup() {
Serial.begin(9600);
Ethernet.begin(mac, ip);
server.begin(); //start listening for clients
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}
void loop() {
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
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"); // the connection will be closed after completion
client.println("Refresh: 5"); // refresh the page automatically every 5 sec
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
// output the value of each analog input pin
for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
int sensorReading = analogRead(analogChannel);
client.print("analog input ");
client.print(analogChannel);
client.print(" is ");
client.print(sensorReading);
client.println("
");
}
client.println("</html>");
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
Serial.println("client disconnected");
}
}
If I understand the process:
-
EthernetServer server(80); defines the SERVER as talking THROUGH the default port of 80.
-
Ethernet.begin(mac, ip); initializes the server at defined MAC/IP location
server.begin(); Starts listening for clients -
EthernetClient client = server.available(); this one trips me up; don’t know how to interpret this
-
if (client) Returns POS if client is detected
-
char c = client.read(); defines ‘c’ and identifies it as a read from the client
Serial.write(c); writes it to the serial monitor -
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”); // the connection will be closed after completion
client.println(“Refresh: 5”); // refresh the page automatically every 5 sec
client.println();
client.println("");
client.println(""); when the character returned is ‘n’ AND line is blank
these ‘headers’ are sent… -
if (c == ‘\n’) {
// you’re starting a new line
currentLineIsBlank = true;
}
else if (c != ‘\r’) {
// you’ve gotten a character on the current line
currentLineIsBlank = false;
this shuts down the communication with the client
QUESTIONS:
A. Line 3 has me in a quandry; server.available should return a ‘true’ if there is a client object- why is
it being ‘written into’ EthernetClient Client?
B. Line 5 reads a character from the client; I am assuming this is protocol characters that are being
sent by the client?
C. in Line 6; c == ‘\n’ && currentLineIsBlank : WHAT EXACTLY is it looking for?
D. as for the standard HTTP headers, I understand when it goes into HTML mode, but could someone
comment on the function of the first 5 println?
E. In line 7- what is the program looking for? I see the WEBPAGE being ‘refreshed’ every 5 seconds or
so as per the HTTP instruction, but I am doing nothing on my end, just watching the web
page;
I appreciate any experienced programmers taking the time to talk; hope I wasn’t being too verbose.
Gerry