Акс

Project code



#include <WiFi.h>
#include <WebServer.h>

// WiFi Access Point name and password
const char* ssid = "ESP32-Access-Point";
const char* password = "12345678";

WebServer server(80);

// LED pin definitions
const int ledLeft = 2;    // Left
const int ledRight = 4;   // Right
const int ledUp = 5;      // Up
const int ledDown = 18;   // Down

void setup() {
  Serial.begin(115200);

  // Set LED pins as OUTPUT
  pinMode(ledLeft, OUTPUT);
  pinMode(ledRight, OUTPUT);
  pinMode(ledUp, OUTPUT);
  pinMode(ledDown, OUTPUT);

  // Initialize all LEDs to OFF
  digitalWrite(ledLeft, LOW);
  digitalWrite(ledRight, LOW);
  digitalWrite(ledUp, LOW);
  digitalWrite(ledDown, LOW);

  // Start WiFi Access Point
  WiFi.softAP(ssid, password);
  Serial.println("WiFi Access Point is active");
  Serial.println(WiFi.softAPIP());

  // Serve the web page
  server.on("/", HTTP_GET, []() {
    server.send(200, "text/html", R"rawliteral(
      <!DOCTYPE html>
      <html lang="en">
      <head>
        <meta charset="UTF-8">
        <title>LED Control</title>
        <style>
          body {
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            height: 100vh;
            margin: 0;
          }
          .row {
            display: flex;
            justify-content: center;
          }
          button {
            width: 100px;
            height: 100px;
            font-size: 40px;
            border: none;
            margin: 10px;
            color: white;
            border-radius: 10px;
          }
          .up { background-color: red; }
          .down { background-color: yellow; color: black; }
          .left { background-color: green; }
          .right { background-color: blue; }
        </style>
        <script>
          function sendRequest(path) {
            fetch(path);
          }
        </script>
      </head>
      <body>
        <div class="row">
          <button class="up" onmousedown="sendRequest('/flash/up')">&#8593;</button>
        </div>
        <div class="row">
          <button class="left" onmousedown="sendRequest('/flash/left')">&#8592;</button>
          <button class="right" onmousedown="sendRequest('/flash/right')">&#8594;</button>
        </div>
        <div class="row">
          <button class="down" onmousedown="sendRequest('/flash/down')">&#8595;</button>
        </div>
      </body>
      </html>
    )rawliteral");
  });

  // Handle LED flash routes
  server.on("/flash/left", []() { flashLed(ledLeft); server.send(200, "text/plain", "Left flashed"); });
  server.on("/flash/right", []() { flashLed(ledRight); server.send(200, "text/plain", "Right flashed"); });
  server.on("/flash/up", []() { flashLed(ledUp); server.send(200, "text/plain", "Up flashed"); });
  server.on("/flash/down", []() { flashLed(ledDown); server.send(200, "text/plain", "Down flashed"); });

  server.begin();
}

void loop() {
  server.handleClient();
}

// Flash LED for 100 milliseconds
void flashLed(int pin) {
  digitalWrite(pin, HIGH);
  delay(100); // Stay ON for 100ms
  digitalWrite(pin, LOW);
}