From the prehistory: an Arduino Uno with ENC28J60 Etherrnet card, serving a webpage. Just out of nostalgia if someone needs it
#include <UIPEthernet.h> // Library for ENC28J60 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 given MAC address and IP address. Adjust the IP address as needed for your network.
- The
OneWire
library is used to communicate with the DS18B20 temperature sensor, and theDallasTemperature
library is used to read the temperature in Celsius. - The Ethernet server listens on port 80 for incoming HTTP requests.
- When a client connects, the sketch reads the incoming data and responds with a simple HTML page showing the temperature in degrees Celsius.
- The sketch handles client connections by waiting for a complete HTTP request and then sending the temperature reading in the response.
To use this sketch, make sure you have installed the UIPEthernet
, OneWire
, and DallasTemperature
libraries. You can install them via the Arduino Library Manager. Adjust the IP address and MAC address as needed to fit your network configuration.