This tutorial will guide you through the process of creating a basic web server using Arduino. You will learn how to set up the server, connect to it via a web browser, and display simple web pages.

Prerequisites

Steps

  1. Connect the Ethernet Shield

    • Insert the Ethernet shield into your Arduino board.
    • Connect the shield to your computer using a USB cable.
  2. Upload the Web Server Sketch

    • Open the Arduino IDE.
    • Connect your Arduino board to the computer using the USB cable.
    • Copy and paste the following code into the IDE:
#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x0F, 0x2F };
EthernetServer server(80);

void setup() {
  Ethernet.begin(mac);
  server.begin();
  Serial.begin(9600);
}

void loop() {
  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) {
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close");
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          client.println("<h1>Arduino Web Server</h1>");
          client.println("<p>This is a simple web server created using Arduino.</p>");
          client.println("<p><a href=\"/reset\">Reset</a></p>");
          client.println("</html>");
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        } else if (c != '\r') {
          // you've just read a character
          currentLineIsBlank = false;
        }
      }
    }
    delay(1);
    // give the web browser time to receive the data
    client.stop();
    Serial.println("client disconnected");
  }
}
  1. Upload the Code to Your Arduino

    • Click the "Upload" button in the Arduino IDE to upload the sketch to your Arduino board.
  2. Access the Web Server

    • Open a web browser and enter the IP address of your Arduino board in the address bar. The IP address can be found in the serial monitor window of the Arduino IDE after uploading the sketch.
    • You should see a web page with the title "Arduino Web Server" and a link to reset the Arduino.

Additional Resources

Conclusion

Congratulations! You have successfully created a basic web server using Arduino. With this knowledge, you can explore more advanced web applications and integrate your Arduino projects with the internet. Happy coding! 🚀