From 45b5cc884accd1dfe8f9e79a71cdc95cc39c6ec2 Mon Sep 17 00:00:00 2001 From: Tilman Date: Thu, 18 May 2023 11:15:21 +0200 Subject: [PATCH] esp32 async webserver --- .../esp32_async_webserver_with_button.ino | 201 ++++++++++++++++++ 1 file changed, 201 insertions(+) create mode 100644 esp32/esp32_async_webserver_with_button/esp32_async_webserver_with_button.ino diff --git a/esp32/esp32_async_webserver_with_button/esp32_async_webserver_with_button.ino b/esp32/esp32_async_webserver_with_button/esp32_async_webserver_with_button.ino new file mode 100644 index 0000000..df3be4e --- /dev/null +++ b/esp32/esp32_async_webserver_with_button/esp32_async_webserver_with_button.ino @@ -0,0 +1,201 @@ +/********* + Rui Santos + Complete project details at https://RandomNerdTutorials.com/esp32-esp8266-web-server-physical-button/ + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. +*********/ + +// Import required libraries +#ifdef ESP32 + #include + #include +#else + #include + #include +#endif +#include + +// Replace with your network credentials +const char* ssid = "LeWe"; +const char* password = "5275274365464843"; + +const char* PARAM_INPUT_1 = "state"; + +const int output = 2; +const int buttonPin = 4; + +// Variables will change: +int ledState = LOW; // the current state of the output pin +int buttonState; // the current reading from the input pin +int lastButtonState = LOW; // the previous reading from the input pin + +// the following variables are unsigned longs because the time, measured in +// milliseconds, will quickly become a bigger number than can be stored in an int. +unsigned long lastDebounceTime = 0; // the last time the output pin was toggled +unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers + +// Create AsyncWebServer object on port 80 +AsyncWebServer server(80); + +const char index_html[] PROGMEM = R"rawliteral( + + + ESP Web Server + + + + +

ESP Web Server

+ %BUTTONPLACEHOLDER% + + + +)rawliteral"; + +// Replaces placeholder with button section in your web page +String processor(const String& var){ + //Serial.println(var); + if(var == "BUTTONPLACEHOLDER"){ + String buttons =""; + String outputStateValue = outputState(); + buttons+= "

Output - GPIO 2 - State

"; + return buttons; + } + return String(); +} + +String outputState(){ + if(digitalRead(output)){ + return "checked"; + } + else { + return ""; + } + return ""; +} + +void setup(){ + // Serial port for debugging purposes + Serial.begin(115200); + + pinMode(output, OUTPUT); + digitalWrite(output, LOW); + pinMode(buttonPin, INPUT); + + // Connect to Wi-Fi + WiFi.begin(ssid, password); + while (WiFi.status() != WL_CONNECTED) { + delay(1000); + Serial.println("Connecting to WiFi.."); + } + + // Print ESP Local IP Address + Serial.println(WiFi.localIP()); + + // Route for root / web page + server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){ + request->send_P(200, "text/html", index_html, processor); + }); + + // Send a GET request to /update?state= + server.on("/update", HTTP_GET, [] (AsyncWebServerRequest *request) { + String inputMessage; + String inputParam; + // GET input1 value on /update?state= + if (request->hasParam(PARAM_INPUT_1)) { + inputMessage = request->getParam(PARAM_INPUT_1)->value(); + inputParam = PARAM_INPUT_1; + digitalWrite(output, inputMessage.toInt()); + ledState = !ledState; + } + else { + inputMessage = "No message sent"; + inputParam = "none"; + } + Serial.println(inputMessage); + request->send(200, "text/plain", "OK"); + }); + + // Send a GET request to /state + server.on("/state", HTTP_GET, [] (AsyncWebServerRequest *request) { + request->send(200, "text/plain", String(digitalRead(output)).c_str()); + }); + // Start server + server.begin(); +} + +void loop() { + // read the state of the switch into a local variable: + int reading = digitalRead(buttonPin); + + // check to see if you just pressed the button + // (i.e. the input went from LOW to HIGH), and you've waited long enough + // since the last press to ignore any noise: + + // If the switch changed, due to noise or pressing: + if (reading != lastButtonState) { + // reset the debouncing timer + lastDebounceTime = millis(); + } + + if ((millis() - lastDebounceTime) > debounceDelay) { + // whatever the reading is at, it's been there for longer than the debounce + // delay, so take it as the actual current state: + + // if the button state has changed: + if (reading != buttonState) { + buttonState = reading; + + // only toggle the LED if the new button state is HIGH + if (buttonState == HIGH) { + ledState = !ledState; + } + } + } + + // set the LED: + digitalWrite(output, ledState); + + // save the reading. Next time through the loop, it'll be the lastButtonState: + lastButtonState = reading; +}