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

How to Create a Simple Web Server with Arduino

Creating a web server using an Arduino is a fascinating way to explore the capabilities of microcontrollers in the realm of IoT (Internet of Things). With an Arduino board and an Ethernet shield or WiFi module, you can set up a basic web server to control devices or monitor sensor data remotely.

Examples:

Required Components:

  • Arduino Uno (or compatible board)
  • Ethernet Shield (or ESP8266/ESP32 for WiFi capabilities)
  • Ethernet cable (if using Ethernet Shield)
  • USB cable for programming the Arduino
  • Computer with Arduino IDE installed

Setting Up a Web Server with Ethernet Shield:

  1. Hardware Setup:

    • Attach the Ethernet Shield to the Arduino Uno.
    • Connect the Ethernet Shield to your router using an Ethernet cable.
    • Connect the Arduino to your computer using a USB cable.
  2. Arduino Code:

    #include <SPI.h>
    #include <Ethernet.h>
    
    // Enter a MAC address and IP address for your controller below.
    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();
     Serial.begin(9600);
     Serial.println("Server is at ");
     Serial.println(Ethernet.localIP());
    }
    
    void loop() {
     EthernetClient client = server.available();
     if (client) {
       Serial.println("New client");
       boolean currentLineIsBlank = true;
       while (client.connected()) {
         if (client.available()) {
           char c = client.read();
           Serial.write(c);
           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>Hello from Arduino!</h1>");
             client.println("</html>");
             break;
           }
           if (c == '\n') {
             currentLineIsBlank = true;
           } else if (c != '\r') {
             currentLineIsBlank = false;
           }
         }
       }
       delay(1);
       client.stop();
       Serial.println("Client disconnected");
     }
    }
  3. Upload the Code:

    • Open the Arduino IDE.
    • Copy the above code into a new sketch.
    • Select the correct board and port from the Tools menu.
    • Click on the upload button to program the Arduino.
  4. Access the Web Server:

    • Open a web browser.
    • Enter the IP address assigned in the code (e.g., http://192.168.1.177).
    • You should see a web page displaying "Hello from Arduino!".

Using WiFi with ESP8266/ESP32:

If you prefer a wireless setup, you can use the ESP8266 or ESP32 module, which has built-in WiFi capabilities. Here's a simple example using the ESP8266:

  1. Arduino Code for ESP8266:

    #include <ESP8266WiFi.h>
    
    const char* ssid = "your_SSID";
    const char* password = "your_PASSWORD";
    
    WiFiServer server(80);
    
    void setup() {
     Serial.begin(115200);
     WiFi.begin(ssid, password);
    
     while (WiFi.status() != WL_CONNECTED) {
       delay(1000);
       Serial.println("Connecting to WiFi...");
     }
    
     Serial.println("Connected to WiFi");
     server.begin();
    }
    
    void loop() {
     WiFiClient client = server.available();
     if (client) {
       Serial.println("New Client.");
       String currentLine = "";
       while (client.connected()) {
         if (client.available()) {
           char c = client.read();
           Serial.write(c);
           if (c == '\n') {
             if (currentLine.length() == 0) {
               client.println("HTTP/1.1 200 OK");
               client.println("Content-type:text/html");
               client.println();
               client.println("<html><body><h1>Hello from ESP8266!</h1></body></html>");
               break;
             } else {
               currentLine = "";
             }
           } else if (c != '\r') {
             currentLine += c;
           }
         }
       }
       client.stop();
       Serial.println("Client Disconnected.");
     }
    }
  2. Upload and Test:

    • Follow similar steps as with the Ethernet Shield to upload the code.
    • Ensure you replace your_SSID and your_PASSWORD with your WiFi credentials.
    • Access the web server using the IP address assigned to the ESP8266.

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.