From cd10e5003a6fbd7a3e69724eefcebeffbe751af7 Mon Sep 17 00:00:00 2001 From: Leonie <135235065+DenialOfIntelligence@users.noreply.github.com> Date: Thu, 28 Sep 2023 17:16:10 +0200 Subject: [PATCH] Add code --- README.md | 2 + simple_web/simple_web.ino | 83 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 simple_web/simple_web.ino diff --git a/README.md b/README.md index f7f423f..74a4459 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,4 @@ # ESP32 Testing with the ESP32 +## Contains +This repo contains my projekts to get a feel for the esp32. diff --git a/simple_web/simple_web.ino b/simple_web/simple_web.ino new file mode 100644 index 0000000..dffb4f8 --- /dev/null +++ b/simple_web/simple_web.ino @@ -0,0 +1,83 @@ +// +// A simple server implementation showing how to: +// * serve static messages +// * read GET and POST parameters +// * handle missing pages / 404s +// +#define LED_BUILTIN 2 +#include +#ifdef ESP32 +#include +#include +#elif defined(ESP8266) +#include +#include +#endif +#include + +AsyncWebServer server(80); + +const char* ssid = "*******"; +const char* password = "*******"; + +const char* PARAM_MESSAGE = "message"; + +void notFound(AsyncWebServerRequest *request) { + request->send(404, "text/plain", "Not found"); +} + +void setup() { + pinMode(LED_BUILTIN, OUTPUT); + + Serial.begin(115200); + WiFi.mode(WIFI_STA); + WiFi.begin(ssid, password); + if (WiFi.waitForConnectResult() != WL_CONNECTED) { + Serial.printf("WiFi Failed!\n"); + return; + } + + Serial.print("IP Address: "); + Serial.println(WiFi.localIP()); + + server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){ + request->send(200, "text/html", "Turn the LED ONTurn the LED OFF"); + }); + server.on("/on", HTTP_GET, [](AsyncWebServerRequest *request){ + request->send(200, "text/html", "Turn the LED ONTurn the LED OFF"); + digitalWrite(LED_BUILTIN, HIGH); + }); + server.on("/off", HTTP_GET, [](AsyncWebServerRequest *request){ + request->send(200, "text/html", "Turn the LED ONTurn the LED OFF"); + digitalWrite(LED_BUILTIN, LOW); + }); + + // Send a GET request to /get?message= + server.on("/get", HTTP_GET, [] (AsyncWebServerRequest *request) { + String message; + if (request->hasParam(PARAM_MESSAGE)) { + message = request->getParam(PARAM_MESSAGE)->value(); + } else { + message = "No message sent"; + } + request->send(200, "text/plain", "Hello, GET: " + message); + }); + + // Send a POST request to /post with a form field message set to + server.on("/post", HTTP_POST, [](AsyncWebServerRequest *request){ + String message; + if (request->hasParam(PARAM_MESSAGE, true)) { + message = request->getParam(PARAM_MESSAGE, true)->value(); + } else { + message = "No message sent"; + } + request->send(200, "text/plain", "Hello, POST: " + message); + }); + + server.onNotFound(notFound); + + server.begin(); +} + +void loop() { +}