Advertisement
xdesig

proba entrada de datos

Jun 15th, 2023
624
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.09 KB | Source Code | 0 0
  1. /*********
  2.    Complete project details at https://RandomNerdTutorials.com/esp32-esp8266-input-data-html-form/
  3.  *********/
  4.  
  5. #include <Arduino.h>
  6. #ifdef ESP32
  7.   #include <WiFi.h>
  8.   #include <AsyncTCP.h>
  9. #else
  10.   #include <ESP8266WiFi.h>
  11.   #include <ESPAsyncTCP.h>
  12. #endif
  13. #include <ESPAsyncWebServer.h>
  14.  
  15. AsyncWebServer server(80);
  16.  
  17. // REPLACE WITH YOUR NETWORK CREDENTIALS
  18. const char* ssid = "wifi_aula05";
  19. const char* password = "konradlorenz";
  20.  
  21. const char* PARAM_INPUT_1 = "input1";
  22. const char* PARAM_INPUT_2 = "input2";
  23. const char* PARAM_INPUT_3 = "input3";
  24.  
  25. // HTML web page to handle 3 input fields (input1, input2, input3)
  26. const char index_html[] PROGMEM = R"rawliteral(
  27. <!DOCTYPE HTML><html><head>
  28.  <title>ESP Input Form</title>
  29.  <meta name="viewport" content="width=device-width, initial-scale=1">
  30.  </head><body>
  31.  <form action="/get">
  32.    Entrada 1: <input type="text" name="input1">
  33.    <input type="submit" value="Env&iacute;a">
  34.  </form><br>
  35.  <form action="/get">
  36.    Entrada 2: <input type="text" name="input2">
  37.    <input type="submit" value="Env&iacute;a">
  38.  </form><br>
  39.  <form action="/get">
  40.    Entrada 3: <input type="text" name="input3">
  41.    <input type="submit" value="Env&iacute;a">
  42.  </form>
  43. </body></html>)rawliteral";
  44.  
  45. void notFound(AsyncWebServerRequest *request) {
  46.   request->send(404, "text/plain", "Not found");
  47. }
  48.  
  49. void setup() {
  50.   Serial.begin(115200);
  51.   WiFi.mode(WIFI_STA);
  52.   WiFi.begin(ssid, password);
  53.   if (WiFi.waitForConnectResult() != WL_CONNECTED) {
  54.     Serial.println("WiFi Failed!");
  55.     return;
  56.   }
  57.   Serial.println();
  58.   Serial.print("IP Address: ");
  59.   Serial.println(WiFi.localIP());
  60.  
  61.   // Send web page with input fields to client
  62.   server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
  63.     request->send_P(200, "text/html", index_html);
  64.   });
  65.  
  66.   // Send a GET request to <ESP_IP>/get?input1=<inputMessage>
  67.   server.on("/get", HTTP_GET, [] (AsyncWebServerRequest *request) {
  68.     String inputMessage;
  69.     String inputParam;
  70.     // GET input1 value on <ESP_IP>/get?input1=<inputMessage>
  71.     if (request->hasParam(PARAM_INPUT_1)) {
  72.       inputMessage = request->getParam(PARAM_INPUT_1)->value();
  73.       inputParam = PARAM_INPUT_1;
  74.  
  75.     }
  76.     // GET input2 value on <ESP_IP>/get?input2=<inputMessage>
  77.     else if (request->hasParam(PARAM_INPUT_2)) {
  78.       inputMessage = request->getParam(PARAM_INPUT_2)->value();
  79.       inputParam = PARAM_INPUT_2;
  80.  
  81.     }
  82.     // GET input3 value on <ESP_IP>/get?input3=<inputMessage>
  83.     else if (request->hasParam(PARAM_INPUT_3)) {
  84.       inputMessage = request->getParam(PARAM_INPUT_3)->value();
  85.       inputParam = PARAM_INPUT_3;
  86.  
  87.     }
  88.     else {
  89.       inputMessage = "No message sent";
  90.       inputParam = "none";
  91.     }
  92.     Serial.println(inputMessage);
  93.     request->send(200, "text/html", "HTTP GET no campo de env&iacute;o ("
  94.                                      + inputParam + ") co valor: " + inputMessage +
  95.                                      "<br><a href=\"/\">retorna a p&aacute;xina</a>");
  96.   });
  97.   server.onNotFound(notFound);
  98.   server.begin();
  99. }
  100.  
  101. void loop() {
  102.  
  103. }
  104.  
  105.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement