Example Code for Arduino-View IO Status Web Server

Last revision 2025/12/09

This sample turns UNO into a Web server that other devices in the same LAN can access to view IO status.

Hardware Preparation

  • DFRduino UNO Controller x 1
  • DFRduino Ethernet W5100S Expansion Board x 1
  • Network Cable x 1
  • Router or Switch x 1
  • M-M/F-M/F-F Jumper wires

Software Preparation

Wiring Diagram

Connection Diagram

Other Preparation Work

Please replace the original W5100. CPP file with the downloaded file before use (the purpose of replacement is to be compatible with W5100S chip).

File path: Arduino\libraries\Ethernet\SRC\Utility in the Arduino installation directory.

Sample Code

/*!
 * @file webServer.ino
 * @brief A simple web server with DHPC capbabilty.
 * @n       1)Get IP address from router automatically
 * @n       2)Show the value of the analog input pins
 * @copyright  Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
 * @licence     The MIT License (MIT)
 * @author [yangfeng]<[email protected]>
 * @version  V1.0
 * @date  2021-07-14
 * @get from https://www.dfrobot.com
 */
#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address and IP address for your controller below.
byte mac[] = {0xDE, 0xCD, 0xAE, 0x0F, 0xFE, 0xED };

// 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);
  while (!Serial); // wait for serial port to connect. Needed for Leonardo only


  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    for(;;);
  }
  // print your local IP address:
  Serial.print("My IP address: ");
  for (byte thisByte = 0; thisByte < 4; thisByte++) {
    // print the value of each byte of the IP address:
    Serial.print(Ethernet.localIP()[thisByte], DEC);
    Serial.print(".");
  }
  Serial.println();

  // start the Ethernet connection and the server:

  server.begin();
  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 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("Connnection: close");
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          // add a meta refresh tag, so the browser pulls again every 5 seconds:
          client.println("<meta http-equiv=\"refresh\" content=\"5\">");
          client.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"http://www.dfrobot.com/ihome/stylesheet/stylesheet.css\" />");

          client.println("<center> <a href=\"http://www.dfrobot.com\"><img src=\"http://alturl.com/qf6vz\"></a> </center> ");
          client.println("<br />");

          client.println("<div>");
/********************************Page display section***********************************/
          // 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("<br />");

          }

          // output the value of each digital input pin
          for (int digitalChannel = 2; digitalChannel < 10; digitalChannel++) {
            int sensorReading = digitalRead(digitalChannel);
            if(digitalChannel!=7&&digitalChannel!=8)
            {
              client.print("Digital input ");
              client.print(digitalChannel);
              client.print(" is ");
              client.print(sensorReading);
              client.println("<br />");
            }
          }
/****************************************************************************/
          client.println("</div>");
          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 disonnected");
  }
}

Result

Open the serial monitor after uploading the code, and enter the IP address in the browser of another device on the same LAN to access the Web page and view the IO status on it.

result

Was this article helpful?

TOP