The “Hello world” sketch is the base of all the examples and it replies a static HTML content to the browser when it requests for a web page.

Create the sketch

Open the Arduino IDE application and select “File” in the menu bar and then click “New” to create a new sketch. A new Arduino IDE window is opened with both setup and loop functions. You can click “File” and “Save as…” option to set a more descriptive sketch name, i.e., WebServer.

Notice that the Arduino IDE creates a folder called “WebServer” with the “WebServer.ino” file inside.


Include the Ethernet Library

First of all, it is necessary to include the Ethernet Library to use the Ethernet hardware as the communication port. The Ethernet Library is included for WIZNET W5100 based hardware -the Ethernet shield- and the Ethernet2 Library is included for WIZNET W5500 based hardware -the Ethernet2 shield-. It is also possible to use other libraries and hardware, like WIFI and WIFI101 Libraries, but the code should be adapted to the specific library.

In this case, the Ethernet2 Library is used.

#include <Ethernet2.h>  


Run the Ethernet hardware

To initialize the Ethernet hardware, it is called the Ethernet.begin() function into the setup(). It requires, at least, the MAC address of the device. It also accepts:

  • an IP address,

  • the DNS server address,

  • the network mask,

  • the gateway address.

When the IP address is not used, the library gets an IP address using the DHCP protocol, but in this case, as the sketch implements a web server, it is common to fix a static IP address. In this case, the IP address is 192.168.1.2.

It is not necessary to set a DNS server due to the sketch is not trying to connect to another server by name. It is not necessary to set the network mask and the gateway either, since they are defined automatically from the IP address.

Code

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; //
byte ip[] = {192, 168, 1, 2}; Ethernet.begin(mac, ip); // 

Create an EthernetServer object

The component used to listen for HTTP requests is the EthernetServer object. As the server is an HTTP server, it is used the port 80. It is possible to use another port, but the most common is this one. Add the next line just before the setup().

EthernetServer server(80);  // port 80                 


Start the server

After the server creation and the Ethernet hardware initialization, it is important to remember the initialization of the server using the server.begin() function into the setup(). It prepares the server for listening for incoming connections from clients.

server.begin();                          


Wait for client requests

Once everything is initialized in the setup(), the server should start accepting incoming HTTP requests and process them. To accept requests, it is used in the server.available() function. It doesn’t need any argument, but it returns an EthernetClient object. It is possible to check for a client request using the if (client) statement: it notifies that the client is ready.

EthernetClient client = server.available(); 
if (client) {
 // ... 
}  


Parse the request as an HTTP request

It is supposed the server will receive and HTTP request from the connected client, so the sketch should understand that request. The principle is to read available data until an empty line is found, which indicates that the request is finished.

Code example

bool emptyLine = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
if (emptyLine && (c == '\n')) {
// Send response
}
// The request is finished when an empty line is received
if (c == '\n') {
emptyLine = true;
} else if (c != '\r') {
emptyLine = false;
}
}
}
while (client.connected()) {
if (client.available()) {
char c = client.read();
if (emptyLine && (c == '\n')) {
// Send response
}
// The request is finished when an empty line is received
if (c == '\n') {
emptyLine = true;
} else if (c != '\r') {
emptyLine = false;
}
}
}


Send a static always-the-same HTTP response with HTML content

When the request is finished, the client waits for a response, so the sketch should send it while the connection is still alive. The response consists of an HTTP response status line, the response content type header, and many other optional headers. After the headers, and depending on the response content type, the content is sent. In this case, the server always replies a static HTML page with a “Hello PLC!” text.

Code Example

client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.println( "<!doctype html>" "<html>" "<head>" "<title>Hello PLC!</title>" "</head>" "<body>" "<h1>Hello PLC!</h1>" "</body>" "</html>" );
client.flush();


Close the connection

After sending the response to the client, it is necessary to close its connection. One connection contains just a request and its response, and nothing more.

client.stop(); 

Final Code Example

#include <Ethernet2.h> // Server creation
EthernetServer server(80);
void setup() { // Ethernet initialization
  byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; byte ip[] = {10, 10, 10, 25};
  Ethernet.begin(mac, ip); // Server initialization
  server.begin();
}
void loop() { // Wait for clients
  EthernetClient client = server.available();
  if (client) { // Parse request
    bool emptyLine = true; while (client.connected()) {
      if (client.available()) {
        char c = client.read(); // The request is finished when an empty line is received
        if (emptyLine && (c == '\n')) { // Send the response
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println(); client.println( "<!doctype html>" "<html>" "<head>" "<title>Hello PLC!</title>" "</head>" "<body>" "<h1>Hello PLC!</h1>" "</body>" "</html>" );
          client.flush(); // Close the connection
          client.stop(); break;
        } // The request is finished when a blank line is received
        if (c == '\n') {
          emptyLine = true;
        } else if (c != '\r') {
          emptyLine = false;
        }
      }
    }
  }
}
                          

               


Commenting is not enabled on this course.