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

Creating a Web Server with Arduino

In this article, we will explore how to create a web server using an Arduino board. This project is significant for anyone interested in IoT (Internet of Things) as it allows the user to control and monitor devices remotely through a web interface. By integrating a web server with Arduino, users can interact with their projects from anywhere in the world, provided they have internet access. This project aligns well with the Arduino environment due to its simplicity and the vast library support available for networking components.

Project: In this example project, we will create a simple web server using an Arduino board and an Ethernet shield. The objective is to control an LED connected to the Arduino through a web interface. The web server will host a basic HTML page with buttons to turn the LED on and off. This project demonstrates the fundamental concepts of web server creation and provides a foundation for more complex IoT applications.

Lista de componentes:

  • Arduino Uno (1)
  • Ethernet Shield (1)
  • LED (1)
  • 220-ohm resistor (1)
  • Breadboard (1)
  • Jumper wires (several)

Exemplos:

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

// MAC address and IP address for the Ethernet shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 177);

// Initialize the Ethernet server on port 80
EthernetServer server(80);

// Pin where the LED is connected
const int ledPin = 4;

void setup() {
  // Start the Ethernet connection and the server
  Ethernet.begin(mac, ip);
  server.begin();

  // Set the LED pin as an output
  pinMode(ledPin, OUTPUT);

  // Start serial communication for debugging
  Serial.begin(9600);
  Serial.print("Server is at ");
  Serial.println(Ethernet.localIP());
}

void loop() {
  // Listen for incoming clients
  EthernetClient client = server.available();

  if (client) {
    Serial.println("New client");
    boolean currentLineIsBlank = true;
    String request = "";

    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        request += c;

        // If the request ends with a newline character
        if (c == '\n' && currentLineIsBlank) {
          // Send a standard HTTP response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close");
          client.println();

          // HTML content
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          client.println("<h1>Arduino Web Server</h1>");
          client.println("<p><a href=\"/LED=ON\"><button>Turn LED ON</button></a></p>");
          client.println("<p><a href=\"/LED=OFF\"><button>Turn LED OFF</button></a></p>");
          client.println("</html>");

          // Check the HTTP request
          if (request.indexOf("GET /LED=ON") != -1) {
            digitalWrite(ledPin, HIGH); // Turn the LED on
          } else if (request.indexOf("GET /LED=OFF") != -1) {
            digitalWrite(ledPin, LOW); // Turn the LED off
          }

          break;
        }

        // If the character is a newline, check if the line is blank
        if (c == '\n') {
          currentLineIsBlank = true;
        } else if (c != '\r') {
          currentLineIsBlank = false;
        }
      }
    }

    // Give the web browser time to receive the data
    delay(1);

    // Close the connection
    client.stop();
    Serial.println("Client disconnected");
  }
}

Explanation:

  1. Library Inclusions: The SPI.h and Ethernet.h libraries are included to handle the Ethernet communication.
  2. MAC and IP Address: The MAC address and IP address are defined for the Ethernet shield.
  3. Server Initialization: The Ethernet server is initialized on port 80.
  4. Pin Setup: The LED pin is set as an output.
  5. Ethernet Connection: The Ethernet connection is established, and the server begins listening for clients.
  6. Client Handling: When a client connects, the server reads the incoming request and sends an HTTP response with an HTML page.
  7. LED Control: The server checks the request to determine if the LED should be turned 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.