{"id":88796,"date":"2019-08-29T14:10:22","date_gmt":"2019-08-29T14:10:22","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=88796"},"modified":"2025-03-17T12:23:48","modified_gmt":"2025-03-17T12:23:48","slug":"esp32-esp8266-input-data-html-form","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/esp32-esp8266-input-data-html-form\/","title":{"rendered":"Input Data on HTML Form ESP32\/ESP8266 Web Server using Arduino IDE"},"content":{"rendered":"\n<p>In this guide, you&#8217;ll learn how to create an ESP32\/ESP8266 web server with three input fields to pass values to your ESP using an HTML form. Then, you can use those values as variables in your code. We&#8217;ll be using the Arduino IDE to program the boards.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/08\/ESP32-ESP8266-Input-Fields-HTML-Form-Arduino-IDE.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 ESP8266 Input Data on HTML Form Web Server using Arduino IDE\" class=\"wp-image-88843\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/08\/ESP32-ESP8266-Input-Fields-HTML-Form-Arduino-IDE.jpg?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/08\/ESP32-ESP8266-Input-Fields-HTML-Form-Arduino-IDE.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/08\/ESP32-ESP8266-Input-Fields-HTML-Form-Arduino-IDE.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/08\/ESP32-ESP8266-Input-Fields-HTML-Form-Arduino-IDE.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\">Project Overview<\/h2>\n\n\n\n<p>In this tutorial, we&#8217;ll build an asynchronous web server using the <a href=\"https:\/\/github.com\/ESP32Async\/ESPAsyncWebServer\" target=\"_blank\" rel=\"noopener\" title=\"\">ESPAsyncWebServer library<\/a> that displays three input fields to pass values that you can use in your code to update variables.<\/p>\n\n\n\n<p>We&#8217;ll take a look at two similar examples. The following figure illustrates how the first example works. <\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"948\" height=\"506\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/08\/ESP32-ESP8266-HTML-Form-Input-Data-Project-Overview.png?resize=948%2C506&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 ESP8266 HTML Form Input Data Project Overview\" class=\"wp-image-88831\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/08\/ESP32-ESP8266-HTML-Form-Input-Data-Project-Overview.png?w=948&amp;quality=100&amp;strip=all&amp;ssl=1 948w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/08\/ESP32-ESP8266-HTML-Form-Input-Data-Project-Overview.png?resize=300%2C160&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/08\/ESP32-ESP8266-HTML-Form-Input-Data-Project-Overview.png?resize=768%2C410&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 948px) 100vw, 948px\" \/><\/figure><\/div>\n\n\n<p>You have a web page with three input fields that you can access with any browser in your network. When you type a new value and press the &#8220;Submit&#8221; button, your ESP will update a variable with the new value.<\/p>\n\n\n\n<p>If you ever needed to update a variable through an ESP web server, you should follow this project. With this method, you avoid hard coding variables because you can create an input field in a web page to update any variable with a new value. This can be specially useful to set threshold values, set SSID\/password, change API Keys, etc&#8230;<\/p>\n\n\n\n<p>Later, we&#8217;ll also show you how to save those variables permanently using LittleFS and how to access them. Here&#8217;s how this second example works.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"1024\" height=\"574\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-ESP8266-HTML-Form-Input-Data-Project-Overview-LittleFS.png?resize=1024%2C574&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 ESP8266 HTML Form Input Data Project Overview LittleFS\" class=\"wp-image-159001\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-ESP8266-HTML-Form-Input-Data-Project-Overview-LittleFS.png?resize=1024%2C574&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-ESP8266-HTML-Form-Input-Data-Project-Overview-LittleFS.png?resize=300%2C168&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-ESP8266-HTML-Form-Input-Data-Project-Overview-LittleFS.png?resize=768%2C430&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-ESP8266-HTML-Form-Input-Data-Project-Overview-LittleFS.png?w=1060&amp;quality=100&amp;strip=all&amp;ssl=1 1060w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p>This web page allows you to enter three types of variables: String, Int and Float. Then, every time you submit a new value, that value is stored in a file in the LittleFS filesystem. This web page also contains a placeholder to show the current values.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<p>Make sure you check all the prerequisites in this section before continuing with the project in order to compile the code.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. Install ESP32\/ESP8266 Board in Arduino IDE<\/h3>\n\n\n\n<p>We&#8217;ll program the ESP32 and ESP8266 using Arduino IDE. So, you must have the ESP32 or ESP8266 add-on installed. Follow one of the next tutorials to install the ESP add-on:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/installing-esp32-arduino-ide-2-0\/\">Installing ESP32 Board in Arduino IDE (Windows, Mac OS X, and Linux)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/installing-esp8266-nodemcu-arduino-ide-2-0\/\">Installing ESP8266 Board in Arduino IDE (Windows, Mac OS X, Linux)<\/a><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">2. Installing Libraries<\/h3>\n\n\n\n<p>To build the web server you need to install the following libraries:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>ESP32:<\/strong>&nbsp;install the&nbsp;<a href=\"https:\/\/github.com\/ESP32Async\/ESPAsyncWebServer\" target=\"_blank\" rel=\"noopener\" title=\"\">ESPAsyncWebServer<\/a> and the&nbsp;<a href=\"https:\/\/github.com\/ESP32Async\/AsyncTCP\" target=\"_blank\" rel=\"noopener\" title=\"\">AsyncTCP&nbsp;<\/a>libraries&nbsp;(by ESP32Async).<\/li>\n\n\n\n<li><strong>ESP8266:<\/strong>&nbsp;install the&nbsp;<a href=\"https:\/\/github.com\/ESP32Async\/ESPAsyncWebServer\" target=\"_blank\" rel=\"noopener\" title=\"\">ESPAsyncWebServer<\/a>&nbsp;and the&nbsp;<a href=\"https:\/\/github.com\/ESP32Async\/ESPAsyncTCP\" target=\"_blank\" rel=\"noopener\" title=\"\">ESPAsyncTCP<\/a>&nbsp;libraries (by ESP32Async).<\/li>\n<\/ul>\n\n\n\n<p>You can install those libraries in the Arduino IDE Library Manager. Go to <strong>Sketch <\/strong>&gt; <strong>Include Library<\/strong> &gt; <strong>Manage Libraries<\/strong> and search for the libraries&#8217; names.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. Parts Required<\/h3>\n\n\n\n<p>To follow this tutorial you just need an <a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/esp32-dev-board-wi-fi-bluetooth\/\" target=\"_blank\">ESP32<\/a> or <a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/esp8266-esp-12e-nodemcu-wi-fi-development-board\/\" target=\"_blank\">ESP8266<\/a> (read <a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/esp32-vs-esp8266\/\" target=\"_blank\">ESP32 vs ESP8266<\/a>). There&#8217;s no circuit for this project.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">1. ESP32\/ESP8266 Handle Input Fields on Web Page with HTML Form<\/h2>\n\n\n\n<p>Copy the following code to the Arduino IDE. Then, type your network credentials (SSID and password) to make it work for you.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*********\n  Rui Santos &amp; Sara Santos - Random Nerd Tutorials\n  Complete project details at https:\/\/RandomNerdTutorials.com\/esp32-esp8266-input-data-html-form\/\n  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.\n  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n*********\/\n\n#include &lt;Arduino.h&gt;\n#ifdef ESP32\n  #include &lt;WiFi.h&gt;\n  #include &lt;AsyncTCP.h&gt;\n#else\n  #include &lt;ESP8266WiFi.h&gt;\n  #include &lt;ESPAsyncTCP.h&gt;\n#endif\n#include &lt;ESPAsyncWebServer.h&gt;\n\nAsyncWebServer server(80);\n\n\/\/ REPLACE WITH YOUR NETWORK CREDENTIALS\nconst char* ssid = &quot;REPLACE_WITH_YOUR_SSID&quot;;\nconst char* password = &quot;REPLACE_WITH_YOUR_PASSWORD&quot;;\n\nconst char* PARAM_INPUT_1 = &quot;input1&quot;;\nconst char* PARAM_INPUT_2 = &quot;input2&quot;;\nconst char* PARAM_INPUT_3 = &quot;input3&quot;;\n\n\/\/ HTML web page to handle 3 input fields (input1, input2, input3)\nconst char index_html[] PROGMEM = R&quot;rawliteral(\n&lt;!DOCTYPE HTML&gt;&lt;html&gt;&lt;head&gt;\n  &lt;title&gt;ESP Input Form&lt;\/title&gt;\n  &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1&quot;&gt;\n  &lt;\/head&gt;&lt;body&gt;\n  &lt;form action=&quot;\/get&quot;&gt;\n    input1: &lt;input type=&quot;text&quot; name=&quot;input1&quot;&gt;\n    &lt;input type=&quot;submit&quot; value=&quot;Submit&quot;&gt;\n  &lt;\/form&gt;&lt;br&gt;\n  &lt;form action=&quot;\/get&quot;&gt;\n    input2: &lt;input type=&quot;text&quot; name=&quot;input2&quot;&gt;\n    &lt;input type=&quot;submit&quot; value=&quot;Submit&quot;&gt;\n  &lt;\/form&gt;&lt;br&gt;\n  &lt;form action=&quot;\/get&quot;&gt;\n    input3: &lt;input type=&quot;text&quot; name=&quot;input3&quot;&gt;\n    &lt;input type=&quot;submit&quot; value=&quot;Submit&quot;&gt;\n  &lt;\/form&gt;\n&lt;\/body&gt;&lt;\/html&gt;)rawliteral&quot;;\n\nvoid notFound(AsyncWebServerRequest *request) {\n  request-&gt;send(404, &quot;text\/plain&quot;, &quot;Not found&quot;);\n}\n\nvoid setup() {\n  Serial.begin(115200);\n  WiFi.mode(WIFI_STA);\n  WiFi.begin(ssid, password);\n  if (WiFi.waitForConnectResult() != WL_CONNECTED) {\n    Serial.println(&quot;WiFi Failed!&quot;);\n    return;\n  }\n  Serial.println();\n  Serial.print(&quot;IP Address: &quot;);\n  Serial.println(WiFi.localIP());\n\n  \/\/ Send web page with input fields to client\n  server.on(&quot;\/&quot;, HTTP_GET, [](AsyncWebServerRequest *request){\n    request-&gt;send(200, &quot;text\/html&quot;, index_html);\n  });\n\n  \/\/ Send a GET request to &lt;ESP_IP&gt;\/get?input1=&lt;inputMessage&gt;\n  server.on(&quot;\/get&quot;, HTTP_GET, [] (AsyncWebServerRequest *request) {\n    String inputMessage;\n    String inputParam;\n    \/\/ GET input1 value on &lt;ESP_IP&gt;\/get?input1=&lt;inputMessage&gt;\n    if (request-&gt;hasParam(PARAM_INPUT_1)) {\n      inputMessage = request-&gt;getParam(PARAM_INPUT_1)-&gt;value();\n      inputParam = PARAM_INPUT_1;\n    }\n    \/\/ GET input2 value on &lt;ESP_IP&gt;\/get?input2=&lt;inputMessage&gt;\n    else if (request-&gt;hasParam(PARAM_INPUT_2)) {\n      inputMessage = request-&gt;getParam(PARAM_INPUT_2)-&gt;value();\n      inputParam = PARAM_INPUT_2;\n    }\n    \/\/ GET input3 value on &lt;ESP_IP&gt;\/get?input3=&lt;inputMessage&gt;\n    else if (request-&gt;hasParam(PARAM_INPUT_3)) {\n      inputMessage = request-&gt;getParam(PARAM_INPUT_3)-&gt;value();\n      inputParam = PARAM_INPUT_3;\n    }\n    else {\n      inputMessage = &quot;No message sent&quot;;\n      inputParam = &quot;none&quot;;\n    }\n    Serial.println(inputMessage);\n    request-&gt;send(200, &quot;text\/html&quot;, &quot;HTTP GET request sent to your ESP on input field (&quot; \n                                     + inputParam + &quot;) with value: &quot; + inputMessage +\n                                     &quot;&lt;br&gt;&lt;a href=\\&quot;\/\\&quot;&gt;Return to Home Page&lt;\/a&gt;&quot;);\n  });\n  server.onNotFound(notFound);\n  server.begin();\n}\n\nvoid loop() {\n  \n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP\/ESP_HTML_Form_Input_Data.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How the code works<\/h3>\n\n\n\n<p>Let&#8217;s take a quick look at the code and see how it works.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Including libraries<\/h4>\n\n\n\n<p>First, include the necessary libraries. You include different libraries depending on the board you&#8217;re using. If you&#8217;re using an ESP32, the code loads the following libraries:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#include &lt;WiFi.h&gt;\n#include &lt;AsyncTCP.h&gt;\n#include &lt;ESPAsyncWebServer.h&gt;<\/code><\/pre>\n\n\n\n<p>If you&#8217;re using an ESP8266, you include these libraries:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#include &lt;ESP8266WiFi.h&gt;\n#include &lt;ESPAsyncTCP.h&gt;\n#include &lt;ESPAsyncWebServer.h&gt;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Network credentials<\/h4>\n\n\n\n<p>Don&#8217;t forget to insert your network credentials in the following variables, so that the ESP32 or ESP8266 can connect to your network.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>const char* ssid = \"REPLACE_WITH_YOUR_SSID\";\nconst char* password = \"REPLACE_WITH_YOUR_PASSWORD\";<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">HTML Forms and Input Fields<\/h4>\n\n\n\n<p>First, let&#8217;s take a look at the HTML that we need to display the input fields. <\/p>\n\n\n\n<p>In our example, we display three input fields and each field has a &#8220;Submit&#8221; button. When the user enters data and presses the &#8220;Submit&#8221; button, that value is sent to the ESP and updates the variable.<\/p>\n\n\n\n<p>For that, create three forms:<\/p>\n\n\n\n<pre class=\"wp-block-code language-html\"><code>&lt;form action=\"\/get\"&gt;\n  input1: &lt;input type=\"text\" name=\"input1\"&gt;\n  &lt;input type=\"submit\" value=\"Submit\"&gt;\n&lt;\/form&gt;&lt;br&gt;\n&lt;form action=\"\/get\"&gt;\n  input2: &lt;input type=\"text\" name=\"input2\"&gt;\n  &lt;input type=\"submit\" value=\"Submit\"&gt;\n&lt;\/form&gt;&lt;br&gt;\n&lt;form action=\"\/get\"&gt;\n  input3: &lt;input type=\"text\" name=\"input3\"&gt;\n  &lt;input type=\"submit\" value=\"Submit\"&gt;\n&lt;\/form&gt;<\/code><\/pre>\n\n\n\n<p>In HTML, the <span class=\"rnthl rntliteral\"><span style=\"color: #333399;\">&lt;form&gt;<\/span><\/span> tag is used to create an HTML form for user input. In our case, the form should contain an input field and a submit button. <\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"571\" height=\"356\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/08\/input_web_browser-2.png?resize=571%2C356&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 ESP8266 Web Server with Input Fields\" class=\"wp-image-88856\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/08\/input_web_browser-2.png?w=571&amp;quality=100&amp;strip=all&amp;ssl=1 571w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/08\/input_web_browser-2.png?resize=300%2C187&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 571px) 100vw, 571px\" \/><\/figure><\/div>\n\n\n<p>Let&#8217;s take a look at the first form to see how it works (the other forms work in a similar way).<\/p>\n\n\n\n<pre class=\"wp-block-code language-html\"><code>&lt;form action=\"\/get\"&gt;\n  input1: &lt;input type=\"text\" name=\"input1\"&gt;\n  &lt;input type=\"submit\" value=\"Submit\"&gt;\n&lt;\/form&gt;<\/code><\/pre>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">action<\/span> attribute specifies where to send the data inserted on the form after pressing submit. In this case, it makes an HTTP GET request to <span class=\"rnthl rntliteral\">\/get?input1=value<\/span>. The <span class=\"rnthl rntliteral\">value<\/span> refers to the text you enter in the input field.<\/p>\n\n\n\n<p>Then, we define two input fields: one text field and one submit button.<\/p>\n\n\n\n<p>The following line defines a one line text input field.<\/p>\n\n\n\n<pre class=\"wp-block-code language-html\"><code>input1: &lt;input type=\"text\" name=\"input1\"&gt;<\/code><\/pre>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">type<\/span> attribute specifies we want a text input field, and the <span class=\"rnthl rntliteral\">name<\/span> attribute specifies the name of the input element. <\/p>\n\n\n\n<p>The next line defines a button for submitting the HTML form data. <\/p>\n\n\n\n<pre class=\"wp-block-code language-html\"><code>&lt;input type=\"submit\" value=\"Submit\"&gt;<\/code><\/pre>\n\n\n\n<p>In this case, the <span class=\"rnthl rntliteral\">type<\/span> attribute specifies you want a submit button, and the <span class=\"rnthl rntliteral\">value<\/span> specifies the text on the button. <\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Connect to your network<\/h4>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">setup()<\/span>, connect to your local network.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Serial.begin(115200);\nWiFi.mode(WIFI_STA);\nWiFi.begin(ssid, password);\nif (WiFi.waitForConnectResult() != WL_CONNECTED) {\n  Serial.println(\"WiFi Failed!\");\n  return;\n}\nSerial.println();\nSerial.print(\"IP Address: \");\nSerial.println(WiFi.localIP());<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Handle HTTP GET requests<\/h4>\n\n\n\n<p>Then, you need to handle the HTTP GET requests.<\/p>\n\n\n\n<p>When, you access the route URL, you send the HTML page to the client. In this case, the HTML text is saved on the <span class=\"rnthl rntliteral\">index_html<\/span> variable.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>server.on(\"\/\", HTTP_GET, &#091;](AsyncWebServerRequest *request){\n  request-&gt;send_P(200, \"text\/html\", index_html);\n});<\/code><\/pre>\n\n\n\n<p>Then, you need to handle what happens when you receive a request on the <span class=\"rnthl rntliteral\">\/get<\/span> routes.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>server.on(\"\/get\", HTTP_GET, &#091;] (AsyncWebServerRequest *request) {<\/code><\/pre>\n\n\n\n<p>We create two variables: <span class=\"rnthl rntliteral\">inputMessage<\/span> and <span class=\"rnthl rntliteral\">inputParam<\/span> to save the input value and the input field.<\/p>\n\n\n\n<p>Then, we need to check whether the HTTP GET request contains the <span class=\"rnthl rntliteral\">input1<\/span>, <span class=\"rnthl rntliteral\">input2<\/span>, or <span class=\"rnthl rntliteral\">input3<\/span> parameters. These are saved on the <span class=\"rnthl rntliteral\">PARAM_INPUT_1<\/span>, <span class=\"rnthl rntliteral\">PARAM_INPUT_2<\/span> and <span class=\"rnthl rntliteral\">PARAM_INPUT_3<\/span> variables.<\/p>\n\n\n\n<p>If the request contains the <span class=\"rnthl rntliteral\">PARAM_INPUT_1<\/span> (i.e. <span class=\"rnthl rntliteral\">input1<\/span>), we set the <span class=\"rnthl rntliteral\">inputMessage<\/span> to the value inserted in the input1 field.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>inputMessage = request-&gt;getParam(PARAM_INPUT_1)-&gt;value();<\/code><\/pre>\n\n\n\n<p>Now you have the value you&#8217;ve just inserted on the first form saved on the <span class=\"rnthl rntliteral\">inputMessage<\/span> variable.<\/p>\n\n\n\n<p>Then, set the <span class=\"rnthl rntliteral\">inputParam<\/span> variable to <span class=\"rnthl rntliteral\">PARAM_INPUT_1<\/span> so that we know where the input value comes from.<\/p>\n\n\n\n<p>When you submit the form, you get a message saying the value you&#8217;ve inserted and in which field. We also display a link to get back to the route URL (Home Page).<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>request-&gt;send(200, \"text\/html\", \"HTTP GET request sent to your ESP on input field (\" \n                                + inputParam + \") with value: \" + inputMessage +\n                                \"&lt;br&gt;&lt;a href=\\\"\/\\\"&gt;Return to Home Page&lt;\/a&gt;\");<\/code><\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"666\" height=\"356\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/08\/input_web_browser-3.png?resize=666%2C356&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 ESP8266 HTML Form Input Data HTTP GET Request\" class=\"wp-image-88863\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/08\/input_web_browser-3.png?w=666&amp;quality=100&amp;strip=all&amp;ssl=1 666w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/08\/input_web_browser-3.png?resize=300%2C160&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 666px) 100vw, 666px\" \/><\/figure><\/div>\n\n\n<p>If you make a request on an invalid URL, we call the <span class=\"rnthl rntliteral\">notFound()<\/span> function, defined at the beginning of the sketch.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>void notFound(AsyncWebServerRequest *request) {\n  request-&gt;send(404, \"text\/plain\", \"Not found\");\n}<\/code><\/pre>\n\n\n\n<p>Finally, start the server to handle clients.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>server.begin();<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Demonstration<\/h3>\n\n\n\n<p>After uploading code to your board, open your Arduino IDE Serial Monitor at a baud rate of 115200 to find the ESP IP address.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"601\" height=\"477\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/08\/ESP32-ESP8266-IP-Address-Arduino-IDE-Serial-Monitor.png?resize=601%2C477&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 ESP8266 IP Address Arduino IDE Serial Monitor\" class=\"wp-image-88850\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/08\/ESP32-ESP8266-IP-Address-Arduino-IDE-Serial-Monitor.png?w=601&amp;quality=100&amp;strip=all&amp;ssl=1 601w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/08\/ESP32-ESP8266-IP-Address-Arduino-IDE-Serial-Monitor.png?resize=300%2C238&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 601px) 100vw, 601px\" \/><\/figure><\/div>\n\n\n<p>Then, open your browser and type the IP address. This web page should load:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"571\" height=\"356\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/08\/ESP32-ESP8266-HTML-Form-Input-Data-save.png?resize=571%2C356&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 ESP8266 HTML Form 3 Input Fields and Save data to ESP using Arduino IDE\" class=\"wp-image-88833\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/08\/ESP32-ESP8266-HTML-Form-Input-Data-save.png?w=571&amp;quality=100&amp;strip=all&amp;ssl=1 571w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/08\/ESP32-ESP8266-HTML-Form-Input-Data-save.png?resize=300%2C187&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 571px) 100vw, 571px\" \/><\/figure><\/div>\n\n\n<p>For example, type <strong>123456<\/strong> on <span class=\"rnthl rntliteral\">input1<\/span> field, then press the &#8220;Submit&#8221; button. A new page should load saying that value <strong>123456 <\/strong>was sent to your ESP:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"666\" height=\"356\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/08\/ESP32-ESP8266-HTML-Form-Input-Data-demonstration.png?resize=666%2C356&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 ESP8266 HTML Form Input Data demonstration\" class=\"wp-image-88829\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/08\/ESP32-ESP8266-HTML-Form-Input-Data-demonstration.png?w=666&amp;quality=100&amp;strip=all&amp;ssl=1 666w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/08\/ESP32-ESP8266-HTML-Form-Input-Data-demonstration.png?resize=300%2C160&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 666px) 100vw, 666px\" \/><\/figure><\/div>\n\n\n<hr class=\"wp-block-separator has-css-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">2. ESP32\/ESP8266 Save Input Fields to LittleFS<\/h2>\n\n\n\n<p>Now, let&#8217;s proceed to the second example. This example saves the data inserted on the input fields permanently on LittleFS. We&#8217;ve also added placeholders on the web page to show the current values.<\/p>\n\n\n\n<p>Copy the following sketch to Arduino IDE. Then, before uploading type your network credentials (SSID and password). <\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*********\n  Rui Santos &amp; Sara Santos - Random Nerd Tutorials\n  Complete project details at https:\/\/RandomNerdTutorials.com\/esp32-esp8266-input-data-html-form\/\n  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.\n  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n*********\/\n#include &lt;Arduino.h&gt;\n#ifdef ESP32\n  #include &lt;WiFi.h&gt;\n  #include &lt;AsyncTCP.h&gt;\n  #include &lt;LittleFS.h&gt;\n#else\n  #include &lt;ESP8266WiFi.h&gt;\n  #include &lt;ESPAsyncTCP.h&gt;\n  #include &lt;Hash.h&gt;\n  #include &lt;LittleFS.h&gt;\n#endif\n#include &lt;ESPAsyncWebServer.h&gt;\n\nAsyncWebServer server(80);\n\n\/\/ REPLACE WITH YOUR NETWORK CREDENTIALS\nconst char* ssid = &quot;REPLACE_WITH_YOUR_SSID&quot;;\nconst char* password = &quot;REPLACE_WITH_YOUR_PASSWORD&quot;;\n\nconst char* PARAM_STRING = &quot;inputString&quot;;\nconst char* PARAM_INT = &quot;inputInt&quot;;\nconst char* PARAM_FLOAT = &quot;inputFloat&quot;;\n\n\/\/ HTML web page to handle 3 input fields (inputString, inputInt, inputFloat)\nconst char index_html[] PROGMEM = R&quot;rawliteral(\n&lt;!DOCTYPE HTML&gt;&lt;html&gt;&lt;head&gt;\n  &lt;title&gt;ESP Input Form&lt;\/title&gt;\n  &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1&quot;&gt;\n  &lt;script&gt;\n    function submitMessage() {\n      alert(&quot;Saved value to ESP LittleFS&quot;);\n      setTimeout(function(){ document.location.reload(false); }, 500);   \n    }\n  &lt;\/script&gt;&lt;\/head&gt;&lt;body&gt;\n  &lt;form action=&quot;\/get&quot; target=&quot;hidden-form&quot;&gt;\n    inputString (current value %inputString%): &lt;input type=&quot;text&quot; name=&quot;inputString&quot;&gt;\n    &lt;input type=&quot;submit&quot; value=&quot;Submit&quot; onclick=&quot;submitMessage()&quot;&gt;\n  &lt;\/form&gt;&lt;br&gt;\n  &lt;form action=&quot;\/get&quot; target=&quot;hidden-form&quot;&gt;\n    inputInt (current value %inputInt%): &lt;input type=&quot;number &quot; name=&quot;inputInt&quot;&gt;\n    &lt;input type=&quot;submit&quot; value=&quot;Submit&quot; onclick=&quot;submitMessage()&quot;&gt;\n  &lt;\/form&gt;&lt;br&gt;\n  &lt;form action=&quot;\/get&quot; target=&quot;hidden-form&quot;&gt;\n    inputFloat (current value %inputFloat%): &lt;input type=&quot;number &quot; name=&quot;inputFloat&quot;&gt;\n    &lt;input type=&quot;submit&quot; value=&quot;Submit&quot; onclick=&quot;submitMessage()&quot;&gt;\n  &lt;\/form&gt;\n  &lt;iframe style=&quot;display:none&quot; name=&quot;hidden-form&quot;&gt;&lt;\/iframe&gt;\n&lt;\/body&gt;&lt;\/html&gt;)rawliteral&quot;;\n\nvoid notFound(AsyncWebServerRequest *request) {\n  request-&gt;send(404, &quot;text\/plain&quot;, &quot;Not found&quot;);\n}\n\nString readFile(fs::FS &amp;fs, const char * path){\n  Serial.printf(&quot;Reading file: %s\\r\\n&quot;, path);\n  File file = fs.open(path, &quot;r&quot;);\n  if(!file || file.isDirectory()){\n    Serial.println(&quot;- empty file or failed to open file&quot;);\n    return String();\n  }\n  Serial.println(&quot;- read from file:&quot;);\n  String fileContent;\n  while(file.available()){\n    fileContent+=String((char)file.read());\n  }\n  file.close();\n  Serial.println(fileContent);\n  return fileContent;\n}\n\nvoid writeFile(fs::FS &amp;fs, const char * path, const char * message){\n  Serial.printf(&quot;Writing file: %s\\r\\n&quot;, path);\n  File file = fs.open(path, &quot;w&quot;);\n  if(!file){\n    Serial.println(&quot;- failed to open file for writing&quot;);\n    return;\n  }\n  if(file.print(message)){\n    Serial.println(&quot;- file written&quot;);\n  } else {\n    Serial.println(&quot;- write failed&quot;);\n  }\n  file.close();\n}\n\n\/\/ Replaces placeholder with stored values\nString processor(const String&amp; var){\n  \/\/Serial.println(var);\n  if(var == &quot;inputString&quot;){\n    return readFile(LittleFS, &quot;\/inputString.txt&quot;);\n  }\n  else if(var == &quot;inputInt&quot;){\n    return readFile(LittleFS, &quot;\/inputInt.txt&quot;);\n  }\n  else if(var == &quot;inputFloat&quot;){\n    return readFile(LittleFS, &quot;\/inputFloat.txt&quot;);\n  }\n  return String();\n}\n\nvoid setup() {\n  Serial.begin(115200);\n  \/\/ Initialize LittleFS\n  #ifdef ESP32\n    if(!LittleFS.begin(true)){\n      Serial.println(&quot;An Error has occurred while mounting LittleFS&quot;);\n      return;\n    }\n  #else\n    if(!LittleFS.begin()){\n      Serial.println(&quot;An Error has occurred while mounting LittleFS&quot;);\n      return;\n    }\n  #endif\n\n  WiFi.mode(WIFI_STA);\n  WiFi.begin(ssid, password);\n  if (WiFi.waitForConnectResult() != WL_CONNECTED) {\n    Serial.println(&quot;WiFi Failed!&quot;);\n    return;\n  }\n  Serial.println();\n  Serial.print(&quot;IP Address: &quot;);\n  Serial.println(WiFi.localIP());\n\n  \/\/ Send web page with input fields to client\n  server.on(&quot;\/&quot;, HTTP_GET, [](AsyncWebServerRequest *request){\n    request-&gt;send(200, &quot;text\/html&quot;, index_html, processor);\n  });\n\n  \/\/ Send a GET request to &lt;ESP_IP&gt;\/get?inputString=&lt;inputMessage&gt;\n  server.on(&quot;\/get&quot;, HTTP_GET, [] (AsyncWebServerRequest *request) {\n    String inputMessage;\n    \/\/ GET inputString value on &lt;ESP_IP&gt;\/get?inputString=&lt;inputMessage&gt;\n    if (request-&gt;hasParam(PARAM_STRING)) {\n      inputMessage = request-&gt;getParam(PARAM_STRING)-&gt;value();\n      writeFile(LittleFS, &quot;\/inputString.txt&quot;, inputMessage.c_str());\n    }\n    \/\/ GET inputInt value on &lt;ESP_IP&gt;\/get?inputInt=&lt;inputMessage&gt;\n    else if (request-&gt;hasParam(PARAM_INT)) {\n      inputMessage = request-&gt;getParam(PARAM_INT)-&gt;value();\n      writeFile(LittleFS, &quot;\/inputInt.txt&quot;, inputMessage.c_str());\n    }\n    \/\/ GET inputFloat value on &lt;ESP_IP&gt;\/get?inputFloat=&lt;inputMessage&gt;\n    else if (request-&gt;hasParam(PARAM_FLOAT)) {\n      inputMessage = request-&gt;getParam(PARAM_FLOAT)-&gt;value();\n      writeFile(LittleFS, &quot;\/inputFloat.txt&quot;, inputMessage.c_str());\n    }\n    else {\n      inputMessage = &quot;No message sent&quot;;\n    }\n    Serial.println(inputMessage);\n    request-&gt;send(200, &quot;text\/text&quot;, inputMessage);\n  });\n  server.onNotFound(notFound);\n  server.begin();\n}\n\nvoid loop() {\n  \/\/ To access your stored values on inputString, inputInt, inputFloat\n  String yourInputString = readFile(LittleFS, &quot;\/inputString.txt&quot;);\n  Serial.print(&quot;*** Your inputString: &quot;);\n  Serial.println(yourInputString);\n  \n  int yourInputInt = readFile(LittleFS, &quot;\/inputInt.txt&quot;).toInt();\n  Serial.print(&quot;*** Your inputInt: &quot;);\n  Serial.println(yourInputInt);\n  \n  float yourInputFloat = readFile(LittleFS, &quot;\/inputFloat.txt&quot;).toFloat();\n  Serial.print(&quot;*** Your inputFloat: &quot;);\n  Serial.println(yourInputFloat);\n  delay(5000);\n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP\/ESP_HTML_Form_Input_Data_Save_to_LittleFS.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How the Code Works<\/h3>\n\n\n\n<p>This code is very similar with the previous one with a few tweaks. Let&#8217;s take a quick look at it and see how it works.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Including libraries<\/h4>\n\n\n\n<p>The code loads the following libraries if you&#8217;re using the ESP32. You need to load the LittleFS library to write to the LittleFS filesystem.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#include &lt;WiFi.h&gt;\n#include &lt;ESPAsyncWebServer.h&gt;\n#include &lt;AsyncTCP.h&gt;\n#include &lt;LittleFS.h&gt;<\/code><\/pre>\n\n\n\n<p>If you&#8217;re using an ESP8266, you need to include the following libraries instead.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#include &lt;ESP8266WiFi.h&gt;\n#include &lt;ESPAsyncWebServer.h&gt;\n#include &lt;ESPAsyncTCP.h&gt;\n#include &lt;Hash.h&gt;\n#include &lt;LittleFS.h&gt;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">HTML Form<\/h4>\n\n\n\n<p>In this example, when you submit the values, a window opens saying the value was saved to LittleFS, instead of being redirected to another page as in the previous example.<\/p>\n\n\n\n<p>For that, we need to a add a JavaScript function, in this case it&#8217;s called <span class=\"rnthl rntliteral\">submitMessage()<\/span> that pops an alert message saying the value was saved to LittleFS. After that pop up, it reloads the web page so that it displays the current values.<\/p>\n\n\n\n<pre class=\"wp-block-code language-javascript\"><code>&lt;script&gt;\n  function submitMessage() {\n    alert(\"Saved value to ESP LittleFS\");\n    setTimeout(function(){ document.location.reload(false); }, 500);\n  }\n&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<p>The forms are also a bit different from the previous ones. Here&#8217;s the form for the first input.<\/p>\n\n\n\n<pre class=\"wp-block-code language-html\"><code>&lt;form action=\"\/get\" target=\"hidden-form\"&gt;\n  inputString (current value %inputString%): &lt;input type=\"text\" name=\"inputString\"&gt;\n  &lt;input type=\"submit\" value=\"Submit\" onclick=\"submitMessage()\"&gt;\n&lt;\/form&gt;<\/code><\/pre>\n\n\n\n<p>In this case, the <span class=\"rnthl rntliteral\">target<\/span> attribute and an <span class=\"rnthl rntliteral\"><span style=\"color: #333399;\">&lt;iframe&gt;<\/span><\/span> are used so that you remain on the same page after submitting the form.<\/p>\n\n\n\n<p>The name that shows up for the input field contains a placeholder <span class=\"rnthl rntliteral\">%inputString%<\/span> that will then be replaced by the current value of the <span class=\"rnthl rntliteral\">inputString<\/span> variable.<\/p>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">onclick=&#8221;submitMessage()&#8221;<\/span> calls the <span class=\"rnthl rntliteral\">submitMessage()<\/span> JavaScript function after clicking the &#8220;Submit&#8221; button.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Read and Write to LittleFS<\/h4>\n\n\n\n<p>Then, we have some functions to read and write from LittleFS.<\/p>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">readFile()<\/span> reads the content from a file:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>String readFile(fs::FS &amp;fs, const char * path){\n  Serial.printf(\"Reading file: %s\\r\\n\", path);\n  File file = fs.open(path, \"r\");\n  if(!file || file.isDirectory()){\n    Serial.println(\"- empty file or failed to open file\");\n    return String();\n  }\n  Serial.println(\"- read from file:\");\n  String fileContent;\n  while(file.available()){\n    fileContent+=String((char)file.read());\n  }\n  Serial.println(fileContent);\n  return fileContent;<\/code><\/pre>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">writeFile()<\/span> writes content to a file:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>void writeFile(fs::FS &amp;fs, const char * path, const char * message){\n  Serial.printf(\"Writing file: %s\\r\\n\", path);\n  File file = fs.open(path, \"w\");\n  if(!file){\n    Serial.println(\"- failed to open file for writing\");\n    return;\n  }\n  if(file.print(message)){\n    Serial.println(\"- file written\");\n  } else {\n    Serial.println(\"- write failed\");\n  }\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">processor()<\/h4>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">processor()<\/span> is responsible for searching for placeholders in the HTML text and replacing them with actual values saved on LittleFS.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>String processor(const String&amp; var){\n  \/\/Serial.println(var);\n  if(var == \"inputString\"){\n    return readFile(LittleFS, \"\/inputString.txt\");\n  }\n  else if(var == \"inputInt\"){\n    return readFile(LittleFS, \"\/inputInt.txt\");\n  }\n  else if(var == \"inputFloat\"){\n    return readFile(LittleFS, \"\/inputFloat.txt\");\n  }\n  return String();\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Handle HTTP GET requests <\/h4>\n\n\n\n<p>Handling the HTTP GET requests works the same way as in the previous example, but this time we save the variables on LittleFS.<\/p>\n\n\n\n<p>For example, for the <span class=\"rnthl rntliteral\">inputString<\/span> field:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>if (request-&gt;hasParam(PARAM_STRING)) {\n  inputMessage = request-&gt;getParam(PARAM_STRING)-&gt;value();\n  writeFile(LittleFS, \"\/inputString.txt\", inputMessage.c_str());\n}<\/code><\/pre>\n\n\n\n<p>When the request contains <span class=\"rnthl rntliteral\">inputString<\/span> (i.e. <span class=\"rnthl rntliteral\">PARAM_STRING<\/span>), we set the <span class=\"rnthl rntliteral\">inputMessage<\/span> variable to the value submitted on the inputString form.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>inputMessage = request-&gt;getParam(PARAM_STRING)-&gt;value();<\/code><\/pre>\n\n\n\n<p>Then, save that value to LittleFS.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>writeFile(LittleFS, \"\/inputString.txt\", inputMessage.c_str());<\/code><\/pre>\n\n\n\n<p>A similar process happens for the other forms.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Accessing the variables<\/h4>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">loop()<\/span>, we demonstrate how you can access the variables.<\/p>\n\n\n\n<p>For example, create a String variable called <span class=\"rnthl rntliteral\">yourInputString<\/span> that reads the content of the <span class=\"rnthl rntliteral\">inputString.txt<\/span> file on LittleFS.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>String yourInputString = readFile(LittleFS, \"\/inputString.txt\");<\/code><\/pre>\n\n\n\n<p>Then, print that variable in the Serial Monitor.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Serial.println(yourInputString);<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Demonstration<\/h3>\n\n\n\n<p>After uploading code to your board, open your Arduino IDE Serial Monitor at a baud rate of 115200 to find the ESP IP address.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"601\" height=\"477\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/08\/ESP32-ESP8266-IP-Address-Arduino-IDE-Serial-Monitor.png?resize=601%2C477&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 ESP8266 IP Address Arduino IDE Serial Monitor\" class=\"wp-image-88850\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/08\/ESP32-ESP8266-IP-Address-Arduino-IDE-Serial-Monitor.png?w=601&amp;quality=100&amp;strip=all&amp;ssl=1 601w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/08\/ESP32-ESP8266-IP-Address-Arduino-IDE-Serial-Monitor.png?resize=300%2C238&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 601px) 100vw, 601px\" \/><\/figure><\/div>\n\n\n<p>Open your browser and type the IP address. A similar web page should load (at first your current values will be blank).<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"669\" height=\"431\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/08\/ESP32-ESP8266-HTML-Form-Input-Data-Save-to-SPIFFS-variables-files.png?resize=669%2C431&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 ESP8266 HTML Form 3 Input Fields and Save data to ESP SPIFFS files using Arduino IDE\" class=\"wp-image-88834\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/08\/ESP32-ESP8266-HTML-Form-Input-Data-Save-to-SPIFFS-variables-files.png?w=669&amp;quality=100&amp;strip=all&amp;ssl=1 669w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/08\/ESP32-ESP8266-HTML-Form-Input-Data-Save-to-SPIFFS-variables-files.png?resize=300%2C193&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 669px) 100vw, 669px\" \/><\/figure><\/div>\n\n\n<p>Type a String in the first input field and press &#8220;Submit&#8221;, repeat the same process for the Int and Float input values. Every time you press the Submit button for a field, you&#8217;ll see an alert message like this:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"769\" height=\"438\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-html-form-web-server-save-data-to-littlefs.png?resize=769%2C438&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 ESP8266 HTML Form Input Data demonstration alert message web page reload\" class=\"wp-image-159004\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-html-form-web-server-save-data-to-littlefs.png?w=769&amp;quality=100&amp;strip=all&amp;ssl=1 769w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-html-form-web-server-save-data-to-littlefs.png?resize=300%2C171&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 769px) 100vw, 769px\" \/><\/figure><\/div>\n\n\n<p>Press the &#8220;OK&#8221; button to reload the web page and see the current values updated.<\/p>\n\n\n\n<p>If you have your Arduino IDE Serial Monitor open, you&#8217;ll see that the stored values are being printed over and over again:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"601\" height=\"477\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/08\/ESP32-ESP8266-Input-Fields-Demonstration-Arduino-IDE-Serial-Monitor.png?resize=601%2C477&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 ESP8266 Input Fields HTML Form Demonstration Arduino IDE Serial Monitor\" class=\"wp-image-88852\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/08\/ESP32-ESP8266-Input-Fields-Demonstration-Arduino-IDE-Serial-Monitor.png?w=601&amp;quality=100&amp;strip=all&amp;ssl=1 601w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/08\/ESP32-ESP8266-Input-Fields-Demonstration-Arduino-IDE-Serial-Monitor.png?resize=300%2C238&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 601px) 100vw, 601px\" \/><\/figure><\/div>\n\n\n<p>For a final project, you can delete all those lines in your <span class=\"rnthl rntliteral\">loop()<\/span> that print all stored values every 5 seconds, we just left them on purpose for debugging.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>In this tutorial, you&#8217;ve learned how to handle input fields on a web page to update the ESP variable values. You can modify this project to set thresholds, change API Key values, set a PWM value, change a timer, set SSID\/password, etc.<\/p>\n\n\n\n<p>Here are other projects that you might like:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/display-images-esp32-esp8266-web-server\/\">How to Display Images in ESP32 and ESP8266 Web Server<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-cam-pir-motion-detector-photo-capture\/\">ESP32-CAM PIR Motion Detector with Photo Capture (saves to microSD card)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-web-server-with-bme280-mini-weather-station\/\">ESP32 Web Server with BME280 \u2013 Advanced Weather Station<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-pinout-reference-gpios\/\">ESP32 Pinout Reference: Which GPIO pins should you use?<\/a><\/li>\n<\/ul>\n\n\n\n<p>Learn more about the ESP32 with our best-selling course:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/learn-esp32-with-arduino-ide\/\">Learn ESP32 with Arduino IDE (eBook + Video Course)<\/a> <\/li>\n<\/ul>\n\n\n\n<p>Thank you for reading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this guide, you&#8217;ll learn how to create an ESP32\/ESP8266 web server with three input fields to pass values to your ESP using an HTML form. Then, you can use &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Input Data on HTML Form ESP32\/ESP8266 Web Server using Arduino IDE\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/esp32-esp8266-input-data-html-form\/#more-88796\" aria-label=\"Read more about Input Data on HTML Form ESP32\/ESP8266 Web Server using Arduino IDE\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":1,"featured_media":88843,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[281,276,277,299,264],"tags":[],"class_list":["post-88796","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-esp32-project","category-esp32","category-esp32-arduino-ide","category-0-esp32","category-project"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/08\/ESP32-ESP8266-Input-Fields-HTML-Form-Arduino-IDE.jpg?fit=1280%2C720&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/88796","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=88796"}],"version-history":[{"count":9,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/88796\/revisions"}],"predecessor-version":[{"id":168091,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/88796\/revisions\/168091"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/88843"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=88796"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=88796"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=88796"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}