From ae60ad5b6bbec7230198cc5f96b0dc424d9c70a6 Mon Sep 17 00:00:00 2001 From: Pierre Tellier Date: Thu, 10 Apr 2025 15:26:14 +0200 Subject: [PATCH] feat: added an UNTESTED wifi server --- wifi/wifi.ino | 131 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 wifi/wifi.ino diff --git a/wifi/wifi.ino b/wifi/wifi.ino new file mode 100644 index 0000000..203c6aa --- /dev/null +++ b/wifi/wifi.ino @@ -0,0 +1,131 @@ +/********* + Rui Santos + Complete project details at https://RandomNerdTutorials.com/esp32-async-web-server-espasyncwebserver-library/ + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + +*********/ + +// Import required libraries +#include +#include +#include + +// Replace with your network credentials +const char* ssid = "ESP32-ping-pong"; +const char* password = "REPLACE_WITH_YOUR_PASSWORD"; + +const char* PARAM_INPUT_1 = "output"; +const char* PARAM_INPUT_2 = "state"; + +// 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 = ""; + buttons += "

Output - GPIO 2

"; + buttons += "

Output - GPIO 4

"; + buttons += "

Output - GPIO 33

"; + return buttons; + } + return String(); +} + +String outputState(int output){ + if(digitalRead(output)){ + return "checked"; + } + else { + return ""; + } +} + +void setup(){ + // Serial port for debugging purposes + Serial.begin(115200); + + pinMode(2, OUTPUT); + digitalWrite(2, LOW); + pinMode(4, OUTPUT); + digitalWrite(4, LOW); + pinMode(33, OUTPUT); + digitalWrite(33, LOW); + + + Serial.print("Setting AP (Access Point)…"); + WiFi.softAP(ssid); + + IPAddress IP = WiFi.softAPIP(); + Serial.print("AP IP address: "); + Serial.println(IP); + + // 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?output=&state= + server.on("/update", HTTP_GET, [] (AsyncWebServerRequest *request) { + String inputMessage1; + String inputMessage2; + // GET input1 value on /update?output=&state= + if (request->hasParam(PARAM_INPUT_1) && request->hasParam(PARAM_INPUT_2)) { + inputMessage1 = request->getParam(PARAM_INPUT_1)->value(); + inputMessage2 = request->getParam(PARAM_INPUT_2)->value(); + digitalWrite(inputMessage1.toInt(), inputMessage2.toInt()); + } + else { + inputMessage1 = "No message sent"; + inputMessage2 = "No message sent"; + } + Serial.print("GPIO: "); + Serial.print(inputMessage1); + Serial.print(" - Set to: "); + Serial.println(inputMessage2); + request->send(200, "text/plain", "OK"); + }); + + // Start server + server.begin(); +} + +void loop() { + +}