Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- You can toggle the onboard LED through a simple web server using your network. The ESP32 will host a web page with a button to turn the LED ON or OFF.
- Upload this code to your ESP32, and replace "your_SSID" and "your_PASSWORD" with your WiFi network credentials. After uploading, open the Serial Monitor, and note the IP address assigned to the ESP32. Open a web browser and enter the IP address. You should see a web page with a "Toggle LED" button. Press the button to toggle the onboard LED.
- The code sets up a simple web server using the ESPAsyncWebServer library (https://github.com/me-no-dev/ESPAsyncWebServer). If you haven't already, install the library using the Arduino Library Manager or download it from the link provided. Also, install the "AsyncTCP" library (https://github.com/me-no-dev/AsyncTCP) as it is a dependency for the ESPAsyncWebServer library when using an ESP32.
- The web server serves an HTML page containing a button to toggle the LED. When the button is clicked, a request is sent to the "/toggle" endpoint, which toggles the LED's state.
- */
- #include <Arduino.h>
- #include <WiFi.h>
- #include <ESPAsyncWebServer.h>
- // WiFi credentials
- const char* ssid = "your_SSID";
- const char* password = "your_PASSWORD";
- // GPIO pin definitions
- const int ledPin = 2; // Built-in LED pin
- AsyncWebServer server(80);
- // HTML for the web page
- const char index_html[] PROGMEM = R"rawliteral(
- <!DOCTYPE html>
- <html>
- <head>
- <title>ESP32 LED Control</title>
- <style>
- html {font-family: Arial; display: inline-block; text-align: center;}
- h2 {font-size: 3.0rem;}
- p {font-size: 1.5rem;}
- button {background-color: #4CAF50; border: none; color: white; padding: 16px 40px; text-align: center; text-decoration: none; display: inline-block; font-size: 30px; margin: 4px 2px; cursor: pointer;}
- </style>
- </head>
- <body>
- <h2>ESP32 Web Server - LED Control</h2>
- <p><button onclick="toggleLED()">Toggle LED</button></p>
- <script>
- async function toggleLED() {
- const response = await fetch("/toggle");
- }
- </script>
- </body>
- </html>
- )rawliteral";
- void setup() {
- Serial.begin(115200);
- delay(1000);
- // Configure LED pin as OUTPUT
- pinMode(ledPin, OUTPUT);
- // Connect to WiFi
- Serial.println("Connecting to WiFi...");
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.println("WiFi connected");
- Serial.print("IP address: ");
- Serial.println(WiFi.localIP());
- // Serve the HTML page
- server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
- request->send_P(200, "text/html", index_html);
- });
- // Handle LED toggle request
- server.on("/toggle", HTTP_GET, [](AsyncWebServerRequest *request){
- digitalWrite(ledPin, !digitalRead(ledPin));
- request->send(200, "text/plain", "Toggled");
- });
- // Start the server
- server.begin();
- }
- void loop() {
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement