Several Examples with the Webserver for the ESP8266 and ESP32 - maxi
ESP8266 and ESP32 are great microcontrollers with WiFi. Using a webserver to control pins or display sensor data on a webpage comes in mind. On this page I would like to show a quick way how to get a working webserver on an ESP.
The right webserver examples to start using Arduino are:
- on the ESP32 the example Webserver / HelloServer
- on the ESP8266 the example ESP8266Webserver / HelloServer
What you get here
The ZIP file at the end of the page will download an ESP32/ESP8266 compatible webserver. It contains several webserver examples
- How to setup a webserver the first simple page (example 10)
- An extendend webpage to be reused in real world programs (example 20)
- A page to output several variables, for example your sensor data (Example 30)
- A form to transmit user data from the browser to the ESP (example 40)
- A form to switch on/off pins on the ESP (example 50)
- A form to start/stop an action / to trigger a function (example 55)
- Activate OTA for easier (and faster) sketch upload
This ESP webserver sketch is compatible for the ESP8266 and the ESP32. Hence there are several precompiler defines to differentiate the boards based on your upload settings. The setup() in the main tab connects to your Wifi, defines which "pages" are available on your server and starts OTA.
In the loop() you just need to call the webserver and OTA. Furthermore you would need to read your sensors, update your variables,...
In the main tab you have to configure your board. Mainly you will need to enter your WiFi SSID and password or link to an include file with these informations.
A simple HTML5 Page (tab server10)
Example 10 shows the principle of HTML generation.
Simple Output of Text, styled with CSS (tab server20)
The server20 contains functions to output the HTML pages, and the CSS. The server can also output plain text (used for 404 page not found) or any other file type you need.
- A page 404 if the user enters a wrong page
- Handle the favicon.ico which is requested by several browsers.
- How to style the page using CSS
Simple Output of Variables (tab server30)
The server30 shows how to display variables on a webpage. This example uses some global variables. The function example30Loop() should be called in your loop() to make the variables change.
Process Input Fields from a Form (tab server40)
The server40 demonstrates how to write a form with several input fields and how to collect data from the user.
Switch Pins with Buttons (tab server50)
The server50 contains buttons so the user can switch on / off pins on the ESP. The example assumes LOW active GPIOs like the LEDs on the ESP8266.
Initiate an Action with Buttons (tab server55)
The server55 shows, how to start actions ESP. The example uses separate functions for each button press.
Update Values on Webpage without Refreshing the whole Page (tab server70)
The Tab "ota"
The sketch adds OTA (over the air) update capability. OTA is not necessary to operate a webserver. OTA enables you to update your ESP8266 / ESP32 via WIFI without a serial connection.
- Download as ZIP: ESP Webserver Examples (maxi)
- Previous step: ESP8266 ESP32 webserver mini - easier introduction
- The next step: Generic ESP Webserver (switch pins) - a use case
- 8 channel ESP8266 Relayboard available on Aliexpress *) Affiliate Link)
- NodeMCU V2 makerfriendly ESP8266 board on Amazon *)
- ESP32 makerfriendly on Amazon *)
(*) Disclosure: Some of the links above are affiliate links, meaning, at no additional cost to you I will earn a (little) comission if you click through and make a purchase. I only recommend products I own myself and I'm convinced they are useful for other makers.
First upload: 2023-07-27 | Version: 2023-11-27