Arduino Webserver with W5500 Shield

For nostalgia reasons an arduino Webserver using the W5500 shield:

#include <Ethernet.h> // Library for W5500-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() {
  Serial.begin(9600); // Start the serial communication for debugging

  // 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 connected");

    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) {
          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"); // Connection will close after this response
          client.println();

          // Send the HTML content with temperature
          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 specified MAC address and a static IP address. You can adjust the IP address as needed to fit your network configuration.
  • It creates an Ethernet server on port 80 to listen for HTTP requests.
  • The sketch initializes the DS18B20 temperature sensor on the OneWire bus (connected to pin 2).
  • When a client connects, the code reads the temperature from the sensor and serves a simple HTML page showing the temperature in degrees Celsius.
  • The connection to the client is closed after sending the HTTP response.

Before uploading this sketch, ensure you have installed the Ethernet, OneWire, and DallasTemperature libraries. Adjust the IP address and MAC address as needed to suit your network configuration. If you’re using DHCP, you can remove the explicit IP address and use Ethernet.begin(mac) to get an IP address from your router.

Leave a comment

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