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

Network Connectivity with Arduino

Importance and Utility of Network Connectivity

Network connectivity is a crucial aspect in today's interconnected world. It allows devices to communicate with each other, enabling data transfer, remote control, and automation. With the help of Arduino, we can easily establish network connectivity and create projects that leverage the power of the internet.

Project: Arduino Web Server

In this project, we will create a simple web server using Arduino and Ethernet Shield. The objective is to control an LED remotely through a web interface. This project can be expanded to control other devices or gather data from sensors.

List of Components:

Examples:

  1. Setting up the Ethernet Shield:
#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

void setup() {
  Ethernet.begin(mac);
}

void loop() {
  // Your code here
}

Explanation:

  • We include the necessary libraries for Ethernet communication.
  • Define a MAC address for the Ethernet shield.
  • In the setup function, we initialize the Ethernet connection using the MAC address.
  1. Creating a web server:
#include <SPI.h>
#include <Ethernet.h>

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

EthernetServer server(80);

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

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

  if (client) {
    // Handle client requests
  }
}

Explanation:

  • We define an IP address for the Arduino to be accessible on the local network.
  • Create an Ethernet server object on port 80.
  • In the loop function, we check for incoming client connections and handle the requests.
  1. Controlling an LED through a web interface:
#include <SPI.h>
#include <Ethernet.h>

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

EthernetServer server(80);

int ledPin = 2;
bool ledState = false;

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

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

  if (client) {
    if (client.connected()) {
      String request = client.readStringUntil('\r');
      client.flush();

      if (request.indexOf("/led/on") != -1) {
        ledState = true;
        digitalWrite(ledPin, HIGH);
      } else if (request.indexOf("/led/off") != -1) {
        ledState = false;
        digitalWrite(ledPin, LOW);
      }

      client.println("HTTP/1.1 200 OK");
      client.println("Content-Type: text/html");
      client.println("");
      client.println("<html><body>");
      client.println("<h1>Arduino Web Server</h1>");
      client.println("<p>LED is " + String(ledState ? "on" : "off") + "</p>");
      client.println("<a href=\"/led/on\">Turn LED On</a><br/>");
      client.println("<a href=\"/led/off\">Turn LED Off</a>");
      client.println("</body></html>");

      client.stop();
    }
  }
}

Explanation:

  • We define an LED pin and its initial state.
  • In the loop function, we handle client requests and check for specific URLs ("/led/on" and "/led/off").
  • Depending on the request, we update the LED state and send an HTML response to the client.

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.