{"id":96089,"date":"2020-04-18T15:08:46","date_gmt":"2020-04-18T15:08:46","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=96089"},"modified":"2025-03-17T12:15:12","modified_gmt":"2025-03-17T12:15:12","slug":"esp32-esp8266-web-server-outputs-momentary-switch","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/esp32-esp8266-web-server-outputs-momentary-switch\/","title":{"rendered":"ESP32\/ESP8266 Web Server: Control Outputs with Momentary Switch"},"content":{"rendered":"\n<p>This tutorial shows how to create a web server with a button that acts as momentary switch to remotely control ESP32 or ESP8266 outputs. The output state is HIGH as long as you keep holding the button in your web page. Once you release it, it changes to LOW. As an example, we&#8217;ll control an LED, but you can control any other output.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/04\/ESP32-ESP8266-Web-Server-Control-Outpus-with-Momentary-Switch-Arduino-IDE.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 ESP8266 Web Server Control Outputs with Momentary Switch Arduino IDE\" class=\"wp-image-96172\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/04\/ESP32-ESP8266-Web-Server-Control-Outpus-with-Momentary-Switch-Arduino-IDE.jpg?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/04\/ESP32-ESP8266-Web-Server-Control-Outpus-with-Momentary-Switch-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\/2020\/04\/ESP32-ESP8266-Web-Server-Control-Outpus-with-Momentary-Switch-Arduino-IDE.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/04\/ESP32-ESP8266-Web-Server-Control-Outpus-with-Momentary-Switch-Arduino-IDE.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n<p>The ESP32\/ESP8266 boards will be programmed using Arduino IDE. So make sure you have these boards installed:<\/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<h2 class=\"wp-block-heading\">Project Overview<\/h2>\n\n\n\n<p>The following diagram shows a simple overview of the project we&#8217;ll build.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"800\" height=\"558\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/04\/Momentary-switch-web-server-esp32-esp8266-project-overview.png?resize=800%2C558&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Momentary switch web server esp32 esp8266 project overview\" class=\"wp-image-96137\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/04\/Momentary-switch-web-server-esp32-esp8266-project-overview.png?w=800&amp;quality=100&amp;strip=all&amp;ssl=1 800w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/04\/Momentary-switch-web-server-esp32-esp8266-project-overview.png?resize=300%2C209&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/04\/Momentary-switch-web-server-esp32-esp8266-project-overview.png?resize=768%2C536&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/figure><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li>The ESP32 or ESP8266 hosts a web server that you can access to control an output;<\/li>\n\n\n\n<li>The output&#8217;s default state is LOW, but you can change it depending on your project application;<\/li>\n\n\n\n<li>There&#8217;s a button that acts like a momentary switch:\n<ul class=\"wp-block-list\">\n<li>if you press the button, the output changes its state to HIGH as long as you keep holding the button;<\/li>\n\n\n\n<li>once the button is released, the output state goes back to LOW.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Installing Libraries &#8211; Async Web Server<\/h2>\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<h2 class=\"wp-block-heading\">Code<\/h2>\n\n\n\n<p>Copy the following code to your Arduino IDE.<\/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-web-server-outputs-momentary-switch\/\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#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\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 int output = 2;\n\n\/\/ HTML web page\nconst char index_html[] PROGMEM = R&quot;rawliteral(\n&lt;!DOCTYPE HTML&gt;&lt;html&gt;\n  &lt;head&gt;\n    &lt;title&gt;ESP Pushbutton Web Server&lt;\/title&gt;\n    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1&quot;&gt;\n    &lt;style&gt;\n      body { font-family: Arial; text-align: center; margin:0px auto; padding-top: 30px;}\n      .button {\n        padding: 10px 20px;\n        font-size: 24px;\n        text-align: center;\n        outline: none;\n        color: #fff;\n        background-color: #2f4468;\n        border: none;\n        border-radius: 5px;\n        box-shadow: 0 6px #999;\n        cursor: pointer;\n        -webkit-touch-callout: none;\n        -webkit-user-select: none;\n        -khtml-user-select: none;\n        -moz-user-select: none;\n        -ms-user-select: none;\n        user-select: none;\n        -webkit-tap-highlight-color: rgba(0,0,0,0);\n      }  \n      .button:hover {background-color: #1f2e45}\n      .button:active {\n        background-color: #1f2e45;\n        box-shadow: 0 4px #666;\n        transform: translateY(2px);\n      }\n    &lt;\/style&gt;\n  &lt;\/head&gt;\n  &lt;body&gt;\n    &lt;h1&gt;ESP Pushbutton Web Server&lt;\/h1&gt;\n    &lt;button class=&quot;button&quot; onmousedown=&quot;toggleCheckbox('on');&quot; ontouchstart=&quot;toggleCheckbox('on');&quot; onmouseup=&quot;toggleCheckbox('off');&quot; ontouchend=&quot;toggleCheckbox('off');&quot;&gt;LED PUSHBUTTON&lt;\/button&gt;\n   &lt;script&gt;\n   function toggleCheckbox(x) {\n     var xhr = new XMLHttpRequest();\n     xhr.open(&quot;GET&quot;, &quot;\/&quot; + x, true);\n     xhr.send();\n   }\n  &lt;\/script&gt;\n  &lt;\/body&gt;\n&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\nAsyncWebServer server(80);\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;ESP IP Address: http:\/\/&quot;);\n  Serial.println(WiFi.localIP());\n  \n  pinMode(output, OUTPUT);\n  digitalWrite(output, LOW);\n  \n  \/\/ Send web page 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  \/\/ Receive an HTTP GET request\n  server.on(&quot;\/on&quot;, HTTP_GET, [] (AsyncWebServerRequest *request) {\n    digitalWrite(output, HIGH);\n    request-&gt;send(200, &quot;text\/plain&quot;, &quot;ok&quot;);\n  });\n\n  \/\/ Receive an HTTP GET request\n  server.on(&quot;\/off&quot;, HTTP_GET, [] (AsyncWebServerRequest *request) {\n    digitalWrite(output, LOW);\n    request-&gt;send(200, &quot;text\/plain&quot;, &quot;ok&quot;);\n  });\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_Web_Server_Momentary_Switch.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>You just need to enter your network credentials (SSID and password) and the web server will work straight away. The code is compatible with both the <a href=\"https:\/\/makeradvisor.com\/tools\/esp32-dev-board-wi-fi-bluetooth\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"ESP32 (opens in a new tab)\">ESP32<\/a> and <a href=\"https:\/\/makeradvisor.com\/tools\/esp8266-esp-12e-nodemcu-wi-fi-development-board\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"ESP8266 (opens in a new tab)\">ESP8266<\/a> boards and controls the on-board LED <span class=\"rnthl rntcblue\">GPIO 2<\/span> &#8211; you can change the code to control any other GPIO.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How the Code Works<\/h2>\n\n\n\n<p>We&#8217;ve already explained in great details how web servers like this work in previous tutorials (<a href=\"https:\/\/randomnerdtutorials.com\/esp32-dht11-dht22-temperature-humidity-web-server-arduino-ide\/\">DHT Temperature Web Server<\/a>), so we&#8217;ll just take a look at the relevant parts for this project.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Network Credentials<\/h3>\n\n\n\n<p>As said previously, you need to insert your network credentials in the following lines:<\/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<h3 class=\"wp-block-heading\">Momentary Switch Button (web server)<\/h3>\n\n\n\n<p>The following line creates the momentary switch button.<\/p>\n\n\n\n<pre class=\"wp-block-code language-html\"><code>&lt;button class=\"button\" onmousedown=\"toggleCheckbox('on');\" ontouchstart=\"toggleCheckbox('on');\" onmouseup=\"toggleCheckbox('off');\" ontouchend=\"toggleCheckbox('off');\"&gt;LED PUSHBUTTON&lt;\/button&gt;<\/code><\/pre>\n\n\n\n<p>Let&#8217;s break this down into small parts.<\/p>\n\n\n\n<p>In HTML, to create a button, use the <span style=\"color: #333399;\">&lt;button&gt;&lt;\/button&gt;<\/span> tags. In between you write the button text. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code language-html\"><code>&lt;button&gt;LED PUSHBUTTON&lt;\/button&gt;<\/code><\/pre>\n\n\n\n<p>The button can have several attributes. In HTML, the attributes provide additional information about HTML elements, in this case, about the button.<\/p>\n\n\n\n<p>Here, we have the following attributes:<\/p>\n\n\n\n<p><strong><span class=\"rnthl rntclblue\">class:<\/span><\/strong> provides a class name for the button. This way, it can be used by CSS or JavaScript to perform certain tasks for the button. In this case, it is used to format the button using CSS. The class attribute has the name &#8220;button&#8221;, but you could have called it any other name.<\/p>\n\n\n\n<pre class=\"wp-block-code language-html\"><code>&lt;button class=\"button\"&gt;LED PUSHBUTTON&lt;\/button&gt;<\/code><\/pre>\n\n\n\n<p><strong><span class=\"rnthl rntclblue\">onmousedown:<\/span><\/strong> this is an event attribute. It executes a JavaScript function when you press the button. In this case it calls <span class=\"rnthl rntliteral\">toggleCheckbox(&#8216;on&#8217;)<\/span>. This function makes a request to the ESP32\/ESP8266 on a specific URL, so that it knows it needs to change the output state to HIGH. <\/p>\n\n\n\n<p><strong><span class=\"rnthl rntclblue\">ontouchstart:<\/span><\/strong> this is an event attribute similar to the previous one, but it works for devices with a touch screen like a smartphone or table. It calls the same JavaScript function to change the output state to HIGH.<\/p>\n\n\n\n<p><strong><span class=\"rnthl rntclblue\">onmouseup:<\/span><\/strong> this is an event attribute that executes a JavaScript function when you release the mouse over the button. In this case, it calls <span class=\"rnthl rntliteral\">toggleCheckbox(&#8216;off&#8217;)<\/span>. This function makes a request to the ESP32\/ESP8266 on a specific URL, so that it knows it needs to change the output state to LOW.<\/p>\n\n\n\n<p><strong><span class=\"rnthl rntclblue\">ontouchend<\/span>:<\/strong> similar to the previous attribute but for devices with touchscreen.<\/p>\n\n\n\n<p>So, in the end, our button looks like this:<\/p>\n\n\n\n<pre class=\"wp-block-code language-html\"><code>&lt;button class=\"button\" onmousedown=\"toggleCheckbox('on');\" ontouchstart=\"toggleCheckbox('on');\" onmouseup=\"toggleCheckbox('off');\" ontouchend=\"toggleCheckbox('off');\"&gt;LED PUSHBUTTON&lt;\/button&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">HTTP GET Request to Change Button State (JavaScript)<\/h3>\n\n\n\n<p>We&#8217;ve seen previously, that when you press or release the button, the <span class=\"rnthl rntliteral\">toggleCheckbox()<\/span> function is called. You either pass the &#8220;on&#8221; or &#8220;off&#8221; arguments, depending on the state you want.<\/p>\n\n\n\n<p>That function, makes an HTTP request to the ESP32 either on the <em><strong>\/on<\/strong><\/em> or <strong><em>\/off<\/em><\/strong> URLs:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>function toggleCheckbox(x) {\n  var xhr = new XMLHttpRequest();\n  xhr.open(\"GET\", \"\/\" + x, true);\n  xhr.send();\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Handle Requests<\/h3>\n\n\n\n<p>Then, we need to handle what happens when the ESP32 or ESP8266 receives requests on those URLs.<\/p>\n\n\n\n<p>When a request is received on the <strong><em>\/on<\/em><\/strong> URL, we turn the GPIO on (HIGH) as shown below:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>server.on(\"\/on\", HTTP_GET, &#091;] (AsyncWebServerRequest *request) {\n  digitalWrite(output, HIGH);\n  request-&gt;send(200, \"text\/plain\", \"ok\");\n});<\/code><\/pre>\n\n\n\n<p>When a request is received on the <strong><em>\/off<\/em><\/strong> URL, we turn the GPIO off (LOW):<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>server.on(\"\/off\", HTTP_GET, &#091;] (AsyncWebServerRequest *request) {\n  digitalWrite(output, LOW);\n  request-&gt;send(200, \"text\/plain\", \"ok\");\n});<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Demonstration<\/h2>\n\n\n\n<p>Upload the code to your ESP32 or ESP8266 board. <\/p>\n\n\n\n<p>Then, open the Serial Monitor at a baud rate of 115200. Press the on-board EN\/RST button to get is IP address.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"711\" height=\"255\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/04\/ESP32-IP-Address-Serial-Monitor.png?resize=711%2C255&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 ESP8266 Web Server Demonstration IP Address Arduino IDE Serial Monitor\" class=\"wp-image-96131\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/04\/ESP32-IP-Address-Serial-Monitor.png?w=711&amp;quality=100&amp;strip=all&amp;ssl=1 711w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/04\/ESP32-IP-Address-Serial-Monitor.png?resize=300%2C108&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 711px) 100vw, 711px\" \/><\/figure><\/div>\n\n\n<p>Open a browser on your local network, and type the ESP IP address. You should have access to the web server as shown below.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"662\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/04\/Momentary-switch-web-server-esp32-esp8266.png?resize=662%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Momentary Switch Web Server ESP32 and ESP8266 Arduino IDE Demonstration\" class=\"wp-image-96132\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/04\/Momentary-switch-web-server-esp32-esp8266.png?w=662&amp;quality=100&amp;strip=all&amp;ssl=1 662w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/04\/Momentary-switch-web-server-esp32-esp8266.png?resize=300%2C191&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 662px) 100vw, 662px\" \/><\/figure><\/div>\n\n\n<p>The on-board LED stays on as long as you keep holding down the button on the web page.<\/p>\n\n\n\n<p><strong>Note: <\/strong> it works the other way around for the ESP8266 because the on-board LED works with inverted logic.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/04\/ESP32-board-Built_in-LED-turned-on-HIGH.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 board Built in LED turned on HIGH\" class=\"wp-image-96167\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/04\/ESP32-board-Built_in-LED-turned-on-HIGH.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/04\/ESP32-board-Built_in-LED-turned-on-HIGH.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>When you release the button, the LED goes back to its default state (LOW).<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/04\/ESP32-board-Built_in-LED-turned-off-LOW.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 board Built in LED turned off LOW\" class=\"wp-image-96166\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/04\/ESP32-board-Built_in-LED-turned-off-LOW.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/04\/ESP32-board-Built_in-LED-turned-off-LOW.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>Watch the next quick video for a live demonstration:<\/p>\n\n\n<div style=\"text-align:center\"><iframe src=\"https:\/\/player.vimeo.com\/video\/410200424?color=ff9933&title=1&byline=0&portrait=0\" width=\"720\" height=\"405\" frameborder=\"0\" webkitallowfullscreen mozallowfullscreen allowfullscreen><\/iframe><\/div><\/br>\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 add event attributes to the buttons on your web server to make them act as momentary switches. This allows you to change the default&#8217;s output state as long as you&#8217;re pressing the button.<\/p>\n\n\n\n<p>Other projects you may like:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/display-images-esp32-esp8266-web-server\/\">Display Images in ESP32 and ESP8266 Web Server<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-esp8266-input-data-html-form\/\">Input Data on HTML Form ESP32\/ESP8266 Web Server<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-esp8266-thermostat-web-server\/\">ESP32\/ESP8266 Thermostat Web Server \u2013 Control Output Based on Temperature<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-esp8266-web-server-physical-button\/\">ESP32\/ESP8266: Control Outputs with Web Server and a Physical Button Simultaneously<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-web-server-spiffs-spi-flash-file-system\/\">ESP32 Web Server using SPIFFS (SPI Flash File System)<\/a><\/li>\n<\/ul>\n\n\n\n<p>Learn more about the ESP32 and ESP8266 with our resources:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/learn-esp32-with-arduino-ide\/\">Learn ESP32 using Arduino IDE<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/home-automation-using-esp8266\/\">Home Automation using ESP8266<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/micropython-programming-with-esp32-and-esp8266\/\">MicroPython Programming using ESP32\/ESP8266<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/projects-esp32\/\">More ESP32 tutorials &#8230;<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/projects-esp8266\/\">More ESP8266 tutorials &#8230;<\/a><\/li>\n<\/ul>\n\n\n\n<p>Thanks for reading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to create a web server with a button that acts as momentary switch to remotely control ESP32 or ESP8266 outputs. The output state is HIGH as &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"ESP32\/ESP8266 Web Server: Control Outputs with Momentary Switch\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/esp32-esp8266-web-server-outputs-momentary-switch\/#more-96089\" aria-label=\"Read more about ESP32\/ESP8266 Web Server: Control Outputs with Momentary Switch\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":1,"featured_media":96172,"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":[276,281,277,299,264,1],"tags":[],"class_list":["post-96089","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-esp32","category-esp32-project","category-esp32-arduino-ide","category-0-esp32","category-project","category-uncategorized"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/04\/ESP32-ESP8266-Web-Server-Control-Outpus-with-Momentary-Switch-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\/96089","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=96089"}],"version-history":[{"count":2,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/96089\/revisions"}],"predecessor-version":[{"id":168082,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/96089\/revisions\/168082"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/96172"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=96089"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=96089"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=96089"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}