Arduino Webserver with Ethernet Shield

·

·

,

Introduction:

In this project, we will explore how to create a webserver using an Arduino with an Ethernet shield. This webserver allows us to control electronic devices, such as LEDs and servo motors, over the internet. You can adapt this method to control various other devices like DC motors, buzzers, relays, and stepper motors.

Understanding the Basics:

Before diving into the project, let’s understand the fundamentals. We’re using two key components: the Arduino with an Ethernet shield.

  • Arduino with Ethernet Shield: This combination allows the Arduino to connect to the internet and serve web pages to clients (like your computer or smartphone).
  • IP Address: When connected to your local network, the Arduino is assigned an IP address, which is essential for accessing the webserver.

Project Components:

Here’s a list of what you’ll need for this project:

  1. Arduino UNO
  2. Ethernet Shield
  3. 220 Ohm Resistor
  4. LED
  5. Micro Servo Motor
  6. Breadboard
  7. Jumper Cables

Setting up the Hardware:

  1. Connect the Ethernet shield to your Arduino UNO.
  2. Attach a 220-ohm resistor and an LED to pin 4 on the Arduino.
  3. Connect the micro servo motor to pin 7.
  4. You can set up the circuit on a breadboard for convenience.

Uploading the Code:

Here’s the Arduino code to upload for this project:

#include <SPI.h>
#include <Ethernet.h>
#include <Servo.h> 

// Define pins and variables
int ledPin = 4;                   // LED connected to pin 4
Servo servoMotor;                 // Create a Servo object
int servoPosition = 0;            // Initial servo position
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};   // MAC address
byte ip[] = {192, 168, 1, 178};   // IP address
byte gateway[] = {192, 168, 1, 1}; // Gateway
byte subnet[] = {255, 255, 255, 0}; // Subnet mask
EthernetServer server(80);        // Server port
String httpRequest = "";          // HTTP request string

void setup() {
  // Start serial communication
  Serial.begin(9600);
  while (!Serial) {
    ; // Wait for serial port to connect (for some Arduino boards)
  }

  // Initialize pins
  pinMode(ledPin, OUTPUT);
  servoMotor.attach(7);

  // Start Ethernet connection and server
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();
  
  // Print server IP address to serial monitor
  Serial.print("Server is at ");
  Serial.println(Ethernet.localIP());
}

void loop() {
  // Listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        httpRequest += c; // Append received characters to HTTP request string
        
        // Check if HTTP request is complete
        if (c == '\n') {
          // Process HTTP request
          processRequest();
          
          // Send HTTP response
          sendResponse(client);
          
          // Clear HTTP request for next read
          httpRequest = "";
        }
      }
    }
    // Close the connection
    client.stop();
  }
}

void processRequest() {
  // Check for button presses in the HTTP request
  if (httpRequest.indexOf("button1on") > 0) {
    digitalWrite(ledPin, HIGH); // Turn on the LED
  }
  if (httpRequest.indexOf("button1off") > 0) {
    digitalWrite(ledPin, LOW); // Turn off the LED
  }
  if (httpRequest.indexOf("button2on") > 0) {
    for (servoPosition = 0; servoPosition < 180; servoPosition += 3) {
      servoMotor.write(servoPosition); // Rotate the servo left
      delay(15); // Wait for the servo to reach the position
    }
  }
  if (httpRequest.indexOf("button2off") > 0) {
    for (servoPosition = 180; servoPosition >= 1; servoPosition -= 3) {
      servoMotor.write(servoPosition); // Rotate the servo right
      delay(15); // Wait for the servo to reach the position
    }
  }
}

void sendResponse(EthernetClient client) {
  // HTTP response headers
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println();
  
  // HTML page content
  client.println("<!DOCTYPE html>");
  client.println("<html>");
  client.println("<head>");
  client.println("<title>Arduino Web Control</title>");
  client.println("<style>");
  client.println("body { text-align: center; }");
  client.println(".button { padding: 10px 20px; font-size: 24px; }");
  client.println("</style>");
  client.println("</head>");
  client.println("<body>");
  client.println("<h1>Arduino Web Control</h1>");
  client.println("<p>Click the buttons below to control the devices:</p>");
  client.println("<a class='button' href='/?button1on'>Turn On LED</a>");
  client.println("<a class='button' href='/?button1off'>Turn Off LED</a><br>");
  client.println("<a class='button' href='/?button2on'>Rotate Left</a>");
  client.println("<a class='button' href='/?button2off'>Rotate Right</a>");
  client.println("<p>Powered by CircuitMonster.co.in</p>");
  client.println("</body>");
  client.println("</html>");
}

This code initializes the webserver and handles client requests. It also controls the LED and servo motor based on button clicks from the web interface.

Accessing the Webserver:

Once you’ve uploaded the code, the Arduino will create a webserver on your local network. By default, it uses the IP address “192.168.1.178.” You can access the web interface by opening your web browser and typing this IP address into the address bar.

Controlling Devices:

The web interface displays buttons like “Turn On LED” and “Rotate Left.” When you click these buttons, the Arduino processes the request and takes action accordingly. For example, clicking “Turn On LED” will illuminate the LED connected to pin 4.

Important Note:

You can only access the webserver from devices connected to the same router as the Arduino with the Ethernet shield. This means you must be on the same local network to control the devices. by same network i mean same wifi/ethernet (for noobs)

Frequently Asked Questions:

Conclusion:

This project demonstrates how an Arduino with an Ethernet shield can serve as a webserver to control electronic devices remotely. You can expand on this concept to create more complex home automation systems or IoT projects. Understanding the basics of Arduino and networking opens up a world of possibilities for creative projects.



Leave a Reply

Your email address will not be published. Required fields are marked *


Explore our other blogs.

  • 8-bit vs. 32-bit Microcontrollers in Today’s Projects

  • Nintendo Sues Creators of Popular Switch Emulator Yuzu, Citing Piracy Concerns

  • Raspberry Pi CPU Temperature Range – Everything You Need to Know

  • image of tunnel

    Reverse Tunneling with Raspberry Pi: A Comprehensive Guide