This tutorial shows how to create a web server with a button that act 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’ll control an LED, but you can control any other output.
The following diagram shows a simple overview of the project we’ll build.
Installing Libraries – Async Web Server
To build the web server you need to install the following libraries:
These libraries aren’t available to install through the Arduino Library Manager, so you need to copy the library files to the Arduino Installation Libraries folder. Alternatively, in your Arduino IDE, you can go to Sketch > Include Library > Add .zip Library and select the libraries you’ve just downloaded.
Copy the following code to your Arduino IDE.
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 ESP32 and ESP8266 boards and controls the on-board LED GPIO 2 – you can change the code to control any other GPIO.
How the Code Works
We’ve already explained in great details how web servers like this work in previous tutorials (DHT Temperature Web Server), so we’ll just take a look at the relevant parts for this project.
As said previously, you need to insert your network credentials in the following lines:
Momentary Switch Button (web server)
The following line creates the momentary switch button.
Let’s break this down into small parts.
In HTML, to create a button, use the <button></button> tags. In between you write the button text. For example:
The button can have several attributes. In HTML, the attributes provide additional information about HTML elements, in this case, about the button.
Here, we have the following attributes:
ontouchend: similar to the previous attribute but for devices with touchscreen.
We’ve seen previously, that when you press or release the button, the toggleCheckbox() function is called. You either pass the “on” or “off” arguments, depending on the state you want.
That function, makes an HTTP request to the ESP32 either on the /on or /off URLs:
Then, we need to handle what happens when the ESP32 or ESP8266 receives requests on those URLs.
When a request is received on the /on URL, we turn the GPIO on (HIGH) as shown below:
When a request is received on the /off URL, we turn the GPIO off (LOW):
Upload the code to your ESP32 or ESP8266 board.
Then, open the Serial Monitor at a baud rate of 115200. Press the on-board EN/RST button to get is IP address.
Open a browser on your local network, and type the ESP IP address. You should have access to the web server as shown below.
The on-board LED stays on as long as you keep holding down the button on the web page.
Note: it works the other way around for the ESP8266 because the on-board LED works with inverted logic.
When you release the button, the LED goes back to its default state (LOW).
In this tutorial you’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’s output state as long as you’re pressing the button.
This content was originally published here.