ESP32 WiFi LED Control On-Off

แชร์
ฝัง
  • เผยแพร่เมื่อ 7 ก.พ. 2025
  • Controlling Two LED On-Off using ESP32 WiFi _Web Server and Tablet
    • ESP32 WiFi LED Control...
    ESP32 Wifi Access Point as Router LED Control #DIY Module (Filipino)
    • ESP32 Wifi Access Poin...

ความคิดเห็น • 1

  • @tagalogelectronics6615
    @tagalogelectronics6615  2 หลายเดือนก่อน

    // Load Wi-Fi library
    #include
    // Network credentials Here
    const char* ssid = "Bototoy";
    const char* password = "12345678";
    // Set web server port number to 80
    WiFiServer server(80);
    // Variable to store the HTTP request
    String header;
    //variables to store the current LED states
    String statePin15 = "off";
    String statePin18 = "off";
    //Output variable to GPIO pins
    const int ledPin15 = 15;
    const int ledPin18 = 18;
    // Current time
    unsigned long currentTime = millis();
    // Previous time
    unsigned long previousTime = 0;
    // Define timeout time in milliseconds
    const long timeoutTime = 2000;
    void setup() {
    Serial.begin(115200);

    pinMode(ledPin15, OUTPUT); // set the LED pin mode
    digitalWrite(ledPin15, 0); // turn LED off by default
    pinMode(ledPin18, OUTPUT); // set the LED pin mode
    digitalWrite(ledPin18, 0); // turn LED off by default
    WiFi.softAP(ssid,password);

    // Print IP address and start web server
    Serial.println("");
    Serial.println("IP address: ");
    Serial.println(WiFi.softAPIP());
    server.begin();
    }
    void loop() {
    WiFiClient client = server.available(); // Listen for incoming clients
    if (client) { // If a new client connects,
    currentTime = millis();
    previousTime = currentTime;
    Serial.println("New Client."); // print a message out in the serial port
    String currentLine = ""; // make a String to hold incoming data from the client
    while (client.connected() && currentTime - previousTime = 0) {
    statePin15 = "on";
    digitalWrite(ledPin15, HIGH); // turns the LED on
    } else if (header.indexOf("GET /15/off") >= 0) {
    statePin15 = "off";
    digitalWrite(ledPin15, LOW); //turns the LED off
    }

    if (header.indexOf("GET /18/on") >= 0) {
    statePin18 = "on";
    digitalWrite(ledPin18, HIGH); // turns the LED on
    } else if (header.indexOf("GET /18/off") >= 0) {
    statePin18 = "off";
    digitalWrite(ledPin18, LOW); //turns the LED off
    }
    // Display the HTML web page
    client.println("");
    client.println("");
    client.println("");
    // CSS to style the on/off buttons
    client.println("html { font-family: monospace; display: inline-block; margin: 0px auto; text-align: center;}");
    client.println(".button { background-color: yellowgreen; border: none; color: white; padding: 16px 40px;");
    client.println("text-decoration: none; font-size: 32px; margin: 2px; cursor: pointer;}");
    client.println(".button2 {background-color: gray;}");
    client.println("ESP32 Web Server");
    client.println("Control LED State");
    if (statePin15 == "off") {
    client.println("ON");
    } else {
    client.println("OFF");
    }
    if (statePin18 == "off") {
    client.println("ON");
    } else {
    client.println("OFF");
    }
    client.println("");
    // The HTTP response ends with another blank line
    client.println();
    // Break out of the while loop
    break;
    } else { // if you got a newline, then clear currentLine
    currentLine = "";
    }
    } else if (c != '
    ') { // if you got anything else but a carriage return character,
    currentLine += c; // add it to the end of the currentLine
    }
    }
    }
    // Clear the header variable
    header = "";
    // Close the connection
    client.stop();
    Serial.println("Client disconnected.");
    Serial.println("");
    }
    }