Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*********
- Complete project details at https://RandomNerdTutorials.com/esp32-esp8266-input-data-html-form/
- *********/
- #include <Arduino.h>
- #ifdef ESP32
- #include <WiFi.h>
- #include <AsyncTCP.h>
- #else
- #include <ESP8266WiFi.h>
- #include <ESPAsyncTCP.h>
- #endif
- #include <ESPAsyncWebServer.h>
- AsyncWebServer server(80);
- // REPLACE WITH YOUR NETWORK CREDENTIALS
- const char* ssid = "wifi_aula05";
- const char* password = "konradlorenz";
- const char* PARAM_INPUT_1 = "input1";
- const char* PARAM_INPUT_2 = "input2";
- const char* PARAM_INPUT_3 = "input3";
- // HTML web page to handle 3 input fields (input1, input2, input3)
- const char index_html[] PROGMEM = R"rawliteral(
- <!DOCTYPE HTML><html><head>
- <title>ESP Input Form</title>
- <meta name="viewport" content="width=device-width, initial-scale=1">
- </head><body>
- <form action="/get">
- Entrada 1: <input type="text" name="input1">
- <input type="submit" value="Envía">
- </form><br>
- <form action="/get">
- Entrada 2: <input type="text" name="input2">
- <input type="submit" value="Envía">
- </form><br>
- <form action="/get">
- Entrada 3: <input type="text" name="input3">
- <input type="submit" value="Envía">
- </form>
- </body></html>)rawliteral";
- void notFound(AsyncWebServerRequest *request) {
- request->send(404, "text/plain", "Not found");
- }
- void setup() {
- Serial.begin(115200);
- WiFi.mode(WIFI_STA);
- WiFi.begin(ssid, password);
- if (WiFi.waitForConnectResult() != WL_CONNECTED) {
- Serial.println("WiFi Failed!");
- return;
- }
- Serial.println();
- Serial.print("IP Address: ");
- Serial.println(WiFi.localIP());
- // Send web page with input fields to client
- server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
- request->send_P(200, "text/html", index_html);
- });
- // Send a GET request to <ESP_IP>/get?input1=<inputMessage>
- server.on("/get", HTTP_GET, [] (AsyncWebServerRequest *request) {
- String inputMessage;
- String inputParam;
- // GET input1 value on <ESP_IP>/get?input1=<inputMessage>
- if (request->hasParam(PARAM_INPUT_1)) {
- inputMessage = request->getParam(PARAM_INPUT_1)->value();
- inputParam = PARAM_INPUT_1;
- }
- // GET input2 value on <ESP_IP>/get?input2=<inputMessage>
- else if (request->hasParam(PARAM_INPUT_2)) {
- inputMessage = request->getParam(PARAM_INPUT_2)->value();
- inputParam = PARAM_INPUT_2;
- }
- // GET input3 value on <ESP_IP>/get?input3=<inputMessage>
- else if (request->hasParam(PARAM_INPUT_3)) {
- inputMessage = request->getParam(PARAM_INPUT_3)->value();
- inputParam = PARAM_INPUT_3;
- }
- else {
- inputMessage = "No message sent";
- inputParam = "none";
- }
- Serial.println(inputMessage);
- request->send(200, "text/html", "HTTP GET no campo de envío ("
- + inputParam + ") co valor: " + inputMessage +
- "<br><a href=\"/\">retorna a páxina</a>");
- });
- server.onNotFound(notFound);
- server.begin();
- }
- void loop() {
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement