We have learned how to display dynamic information using a Web Server. In this project, we will take it a step further by enabling interaction with the user to capture input from the website. The user can toggle a checkbox to turn the LED on or off. The ESP32 will then respond by sending the LED’s current state back to the webpage, where it will be displayed in real-time.
We have two routes in this project. The root route serves the HTML page to the user. When the user toggles the checkbox, JavaScript sends the checkbox status to the /setLedStatus route, which is handled by the handleSetLedStatus function.
This function sends the status of a checkbox (checked or unchecked) to the ESP32 using a fetch request. It then updates the text next to the checkbox with the response received from the ESP32.
This line retrieves the value of the “status” parameter from the HTTP request sent to the server and stores the extracted value as a String. It captures the “status” sent from the web page, such as “true” or “false”.
This code checks the value of status. If status is “true”, it turns the LED on and sends “LED is On” as a response. Otherwise, it turns the LED off and sends “LED is Off” as a response.
