Introduction

About this project:

In this lesson, we will show how to use W5100 Ethernet Shield and Arduino UNO board to work as Web Server , show “hello world!” message in browser.

Preparation

Hardware:

Software:

Connection

Just simply insert W5100 Ethernet shield to Arduino UNO board, then connect W5100 to Router through RJ45 cable.Connect  a network cable to W5100 network port .If need to use phone control, please keep the phone network consistent with the W5100 network.

Upload Sketch

Connect the Arduino UNO board to computer via USB cable,download sketch from https://osoyoo.com/driver/ethernet_iot/lesson1/w5100webserver.ino and load it into Arduino

Code Program

/*  ___   ___  ___  _   _  ___   ___   ____ ___  ____  
 * / _ \ /___)/ _ \| | | |/ _ \ / _ \ / ___) _ \|    \ 
 *| |_| |___ | |_| | |_| | |_| | |_| ( (__| |_| | | | |
 * \___/(___/ \___/ \__  |\___/ \___(_)____)___/|_|_|_|
 *                  (____/ 
 * Osoyoo W5100 web server lesson 1
 * 
 * tutorial url: http://osoyoo.com/?p=29578
 */

#include 

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
//IPAddress ip(192.168.50.109); uncomment this line if you want define static IP instead of dynamic IP from DHCP server

// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  Serial.println("waiting for DHCP IP...");
  /*
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
*/

  // start the Ethernet connection and the server:
  Ethernet.begin(mac); // if you defined static ip in line 28, change this line to   Ethernet.begin(mac,ip); 
  server.begin();
  Serial.print("Please use your browser to visit http://");
  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 you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        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 of the response
          client.println("Refresh: 5");  // refresh the page automatically every 5 sec
          client.println();
          client.println("");
          client.println("

Osoyoo Ethernet IoT Lesson 1

Hello World!"); 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"); } }

After do that,choose the corresponding board type and port type as below

Now you can upload the sketch code to Arduino UNO by click UPLOAD button(press Ctrl+U key)

Explanation of w5100webservo.ino

The most important code lines are line 64,65 as following:

client.println(“”);
client.println(“

Osoyoo Ethernet IoT Lesson 1

Hello World!“);

These two lines send following string to browser:

Osoyoo Ethernet IoT Lesson 1

color=red size=5>Hello World!

Running Result

When the code is running  in Arduino board, open the Serial Monitor(set baud rate to 9600in lower right corner), you will see  serial monitor suggest you visit  some url  as following page:

In above example, we can visit http://192.168.50.14 , you will see browser page shows html data from Arduino:

Osoyoo Ethernet IoT Lesson 1

Hello World!