top of page
MEESP32.jpg
wireless-signal-ga358db19f_1280.png

Esp 32 Web server

>Material

>ESP 32;
>CABO USB / MICRO USB

>Código

#include <WiFi.h>

const char* ssid = "YOUR WIFI NAME";
const char* password = "YOUR WIFI PASSWORD";
int LED = 2;
WiFiServer server(80);

void setup() {
  Serial.begin(115200);
  pinMode(LED, OUTPUT);

  Serial.println();
  Serial.print("Conectando-se a ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
  delay(500);
  Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi conectada.");
  Serial.println("Endereço de IP: ");
  Serial.println(WiFi.localIP());
  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("<h1>HOME CONTROL</h1>");
           client.println("<a href=\"/H\"><button><h3>ON</h3></button></a><pt>");
           client.println();
           client.println ("<a href=\"/L\"><button><h3>OFF</h3></button></a><pt>");
           break;
         } else {
           currentLine = "";
         }
       } else if (c != '\r') {
         currentLine += c;
       }
       if (currentLine.endsWith("GET /H")) {
         digitalWrite(LED, HIGH);
       }
       if (currentLine.endsWith("GET /L")) {
         digitalWrite(LED, LOW);
       }
     }
  }
  client.stop();
  Serial.println("Client Disconnected.");
  }
}

© 2023 por Arduino Power Max

bottom of page