Advertisement
microrobotics

ESP32 Toggle Onboard LED Web Server

Apr 24th, 2023 (edited)
13,488
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. 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.
  3.  
  4. 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.
  5.  
  6. 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.
  7.  
  8. 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.
  9. */
  10.  
  11. #include <Arduino.h>
  12. #include <WiFi.h>
  13. #include <ESPAsyncWebServer.h>
  14.  
  15. // WiFi credentials
  16. const char* ssid = "your_SSID";
  17. const char* password = "your_PASSWORD";
  18.  
  19. // GPIO pin definitions
  20. const int ledPin = 2; // Built-in LED pin
  21.  
  22. AsyncWebServer server(80);
  23.  
  24. // HTML for the web page
  25. const char index_html[] PROGMEM = R"rawliteral(
  26. <!DOCTYPE html>
  27. <html>
  28. <head>
  29.  <title>ESP32 LED Control</title>
  30.  <style>
  31.    html {font-family: Arial; display: inline-block; text-align: center;}
  32.    h2 {font-size: 3.0rem;}
  33.    p {font-size: 1.5rem;}
  34.    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;}
  35.  </style>
  36. </head>
  37. <body>
  38.  <h2>ESP32 Web Server - LED Control</h2>
  39.  <p><button onclick="toggleLED()">Toggle LED</button></p>
  40.  <script>
  41.    async function toggleLED() {
  42.      const response = await fetch("/toggle");
  43.    }
  44.  </script>
  45. </body>
  46. </html>
  47. )rawliteral";
  48.  
  49. void setup() {
  50.   Serial.begin(115200);
  51.   delay(1000);
  52.  
  53.   // Configure LED pin as OUTPUT
  54.   pinMode(ledPin, OUTPUT);
  55.  
  56.   // Connect to WiFi
  57.   Serial.println("Connecting to WiFi...");
  58.   WiFi.begin(ssid, password);
  59.  
  60.   while (WiFi.status() != WL_CONNECTED) {
  61.     delay(500);
  62.     Serial.print(".");
  63.   }
  64.  
  65.   Serial.println("");
  66.   Serial.println("WiFi connected");
  67.   Serial.print("IP address: ");
  68.   Serial.println(WiFi.localIP());
  69.  
  70.   // Serve the HTML page
  71.   server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
  72.     request->send_P(200, "text/html", index_html);
  73.   });
  74.  
  75.   // Handle LED toggle request
  76.   server.on("/toggle", HTTP_GET, [](AsyncWebServerRequest *request){
  77.     digitalWrite(ledPin, !digitalRead(ledPin));
  78.     request->send(200, "text/plain", "Toggled");
  79.   });
  80.  
  81.   // Start the server
  82.   server.begin();
  83. }
  84.  
  85. void loop() {
  86. }
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement