Newbie question on ETHERNET library commands.....

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:

  1. EthernetServer server(80); defines the SERVER as talking THROUGH the default port of 80.

  2. Ethernet.begin(mac, ip); initializes the server at defined MAC/IP location
    server.begin(); Starts listening for clients

  3. EthernetClient client = server.available(); this one trips me up; don’t know how to interpret this

  4. if (client) Returns POS if client is detected

  5. char c = client.read(); defines ‘c’ and identifies it as a read from the client
    Serial.write(c); writes it to the serial monitor

  6. 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…

  7. 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

Read the WebServer web page....

A) server.available does not return true/false, it returns an EthernetClient object if a client is connected, otherwise, a NULL. if(client) execute the code in the if only if there is a client connected.

B) client.read returns the next character, if any, from the client. This will be one character of the request. It's exactly like reading from Serial.

C) It is testing to see if the current character is a line end, and the rest of that line was blank. This indicated the end of the HTTP request line from the client.

D) The first 5 printlns are sending the response header to the client. If you understand HTTP, this should be very familiar....

E) Again, checking for a blank line, which is the indicator of the split between the request header and the request payload, if any. Again, this is standard HTTP.

Regards,
Ray L.

3 - If the server has something trying to make a connection create a client object to handle that connection attempt.

4 - True to be precise, its Boolean logic not electronics :slight_smile:

6 - No - This is writing the web page to the client. If you select "View source" on a web page (even this one) then it will be full of HTML. The first 5 lines setting up the connection between the server and client (protocol stuff).

A. The value returned by server.available() is an EthernetClient object (look at the code in the library to see this). So this is being assigned to the variable client.

B. Yes - the client will request the page from the server, when the server receives a new line it knows the full request has been made. If it was a 'proper' webserver it would parse the request and work out what to return. This isn't its a 'dumb' server and only sends a single page regardless of what you ask.

C. If the code has seen one blank line (currentLineIsBlank == true) and the current character is a new line symbol (\n) then reply. So if you get two new lines without getting a \r in between (can't remember if it is carriage return, check google).

D. They are setting up the protocol between the web server and the client. Line 4 tells the client to refresh every 5 seconds. Personally I wouldn't worry about them, but if you really need to know I would have a look on google Stackoverflow or w3Schools.

E. That is what you should be seeing. Its being refreshed because of the answer to D.

Is that OK? If there is anything I've not been clear enough on, just say.

It might be worth trying to vary the signals at the analog pins of the board to prove to yourself that it is working, use a wire that shorts A0 to ground when you want and you should see the data on the page change too.

Ray and TTAM: thank you SO MUCH! I have just enough knowledge to be dangerous :slight_smile: and find it difficult to find one source of information that covers everything I need. Again, thank you for your time.

Ger

  1. EthernetServer server(80); defines the SERVER as talking THROUGH the default port of 80.

I think the server listens on port 80, but the server dynamically selects a port not in use to send the request back to the client, usually a high number.