Arduino Webserver for W5100 Shield

For Nostalgia’s sake: An Arduino Uno with a W5100 ethernetshield, serving a webpage with results of a DS18N20

#include <Ethernet.h> // Library for W5100-based Ethernet shield
#include <OneWire.h>  // Library for OneWire communication
#include <DallasTemperature.h> // Library for DS18B20 temperature sensor

// Pin where the DS18B20 is connected
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

EthernetServer server(80); // Ethernet server on port 80

void setup() {
  // Start the serial communication for debugging
  Serial.begin(9600);

  // Set up the Ethernet shield with a fixed MAC address and IP address
  byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // MAC address
  IPAddress ip(192, 168, 1, 177); // Local IP address (adjust as needed for your network)

  // Start the Ethernet and the server
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.print("Server is at ");
  Serial.println(Ethernet.localIP());

  // Start the temperature sensor
  sensors.begin();
}

void loop() {
  // Check for incoming client connections
  EthernetClient client = server.available();
  if (client) {
    Serial.println("New client");

    // Wait for data from the client
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        // If the request is complete (double newline), send the response
        if (c == '\n' && currentLineIsBlank) {
          // Get the temperature reading
          sensors.requestTemperatures(); // Request temperature
          float temperature = sensors.getTempCByIndex(0); // Get temperature in Celsius

          // Send the HTTP response headers
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close"); // The connection will close after this response
          client.println();

          // Send the HTML content
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          client.println("<h1>Temperature Monitor</h1>");
          client.print("<p>Temperature: ");
          client.print(temperature);
          client.println(" °C</p>");
          client.println("</html>");
          break;
        }

        if (c == '\n') {
          currentLineIsBlank = true;
        } else if (c != '\r') {
          currentLineIsBlank = false;
        }
      }
    }

    // Close the connection with the client
    client.stop();
    Serial.println("Client disconnected");
  }
}

  • This sketch initializes the Ethernet shield with a MAC address and a static IP address. Adjust the IP address and MAC address to fit your network configuration.
  • It creates an Ethernet server listening on port 80 for HTTP requests.
  • The sketch initializes the DS18B20 temperature sensor on a OneWire bus (here on digital pin 2).
  • When a client connects, the code reads from the temperature sensor and serves a simple HTML page with the temperature in degrees Celsius.
  • The connection to the client is closed after sending the HTTP response.

With this sketch, the Ethernet shield serves as a basic web server that provides temperature data from the DS18B20 sensor. Before uploading, ensure the Ethernet, OneWire, and DallasTemperature libraries are installed in your Arduino IDE.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.