Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade

Web Server

Building a Web Server with Arduino

Introduction: The purpose of this article is to provide a comprehensive guide on creating a web server using Arduino. This project is highly useful as it allows users to control and monitor their Arduino-based devices remotely through a web browser. By understanding the concepts and examples provided, readers will be able to implement their own web server projects and explore the possibilities of remote control and monitoring.

Project: In this project, we will create a web server using Arduino that can control an LED remotely. The objectives of this project are to showcase the basic functionality of a web server, demonstrate how to interact with Arduino remotely, and provide a foundation for more complex web server projects.

List of Components:

  1. Arduino Uno
  2. Ethernet Shield
  3. LED
  4. Resistor (220 ohms)
  5. Breadboard
  6. Jumper wires

Examples: Example 1: Setting up the Ethernet Shield

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 177);

EthernetServer server(80);

void setup() {
  Ethernet.begin(mac, ip);
  server.begin();
}

void loop() {
  EthernetClient client = server.available();
  if (client) {
    // handle client requests
  }
}

Explanation:

  • We include the necessary libraries for Ethernet communication.
  • We define the MAC address and IP address for the Ethernet shield.
  • We create an Ethernet server object on port 80.
  • In the setup function, we initialize the Ethernet shield and start the server.
  • In the loop function, we check for incoming client connections.

Example 2: Controlling an LED via Web Server

void handleClient(EthernetClient client) {
  if (client.available()) {
    String request = client.readStringUntil('\r');
    client.flush();

    if (request.indexOf("/LED=ON") != -1) {
      digitalWrite(LED_PIN, HIGH);
    } else if (request.indexOf("/LED=OFF") != -1) {
      digitalWrite(LED_PIN, LOW);
    }

    client.println("HTTP/1.1 200 OK");
    client.println("Content-Type: text/html");
    client.println("");
    client.println("<h1>Arduino Web Server</h1>");
    client.println("<p>LED Status: " + String(digitalRead(LED_PIN)) + "</p>");
    client.println("<a href=\"/LED=ON\">Turn LED ON</a>&nbsp;");
    client.println("<a href=\"/LED=OFF\">Turn LED OFF</a>");
  }

  delay(1);
  client.stop();
}

void loop() {
  EthernetClient client = server.available();
  if (client) {
    handleClient(client);
  }
}

Explanation:

  • We define a function to handle client requests.
  • We read the client's request and check for specific commands to control the LED.
  • We update the LED status and send a response back to the client.
  • The response includes the current LED status and links to turn the LED on or off.

To share Download PDF

Gostou do artigo? Deixe sua avaliação!
Sua opinião é muito importante para nós. Clique em um dos botões abaixo para nos dizer o que achou deste conteúdo.