From df58ffc98fae1a14891d0afe6e16a8a48bbb30aa Mon Sep 17 00:00:00 2001 From: Leonie <135235065+DenialOfIntelligence@users.noreply.github.com> Date: Tue, 31 Oct 2023 12:52:01 +0100 Subject: [PATCH] Add an example for http post --- post/ESP32_post.ino | 71 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 post/ESP32_post.ino diff --git a/post/ESP32_post.ino b/post/ESP32_post.ino new file mode 100644 index 0000000..e752649 --- /dev/null +++ b/post/ESP32_post.ino @@ -0,0 +1,71 @@ +// +// 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); + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + + Serial.println("Connected"); + Serial.print("IP Address: "); + Serial.println(WiFi.localIP()); + + // Send a POST request to /post with a form field message set to + server.on("/LED", HTTP_POST, [](AsyncWebServerRequest *request){ + String message; + if (request->hasParam(PARAM_MESSAGE, true)) { + message = request->getParam(PARAM_MESSAGE, true)->value(); + } else { + message = "No message sent"; + } + + if (message == "ON"){ + digitalWrite(LED_BUILTIN, HIGH); + request->send(200, "text/plain", "OK \n"); + } else if (message == "OFF") { + digitalWrite(LED_BUILTIN, LOW); + request->send(200, "text/plain", "OK \n"); + }else { + request->send(400, "text/plain", "Bad Request\n"); + } + + + }); + + server.onNotFound(notFound); + + server.begin(); +} + +void loop() { +}