Web Server On ESP32: How To Update and Display Sensor Values?
Web Server On ESP32: How To Update and Display Sensor Values?
Web Server On ESP32: How To Update and Display Sensor Values?
In many IoT Applications we monitor sensor data and we want to display it in wab page. Web page requires
frequent refresh to get the update from ESP32. To solve this problem you have two options, first is refresh
page with HTML Tag: ex. refresh at every 30 seconds.
<head>
<meta http-equiv=”refresh” content=”30″>
</head>
Disadvantage of using HTML refresh tag is, it flickers the screen and loads complete page again and again.
In this tutorial I will show you how to display ADC data and update without refreshing web page. You can
do a lot of things with this. At the end we will see some advance applications of this. To make this possible we
need to use javaScript Ajax.
What is AJAX?
1 of 7 5/7/20, 6:01 PM
Web server on ESP32: How to update and displa... https://circuits4you.com/2018/11/20/web-server...
AJAX allows web pages to be updated asynchronously by exchanging small amounts of data with the server
behind the scenes. This means that it is possible to update parts of a web page, without reloading the whole
page.
Classic web pages, (which do not use AJAX) must reload the entire page if the content should change.
Examples of applications using AJAX: Google Maps, Gmail, Youtube, and Facebook tabs.
In ESP32 NodeMCU we create two pages on server. First that loads as normal webpage and second webpage
is behind the scene i.e. AJAX.
2 of 7 5/7/20, 6:01 PM
Web server on ESP32: How to update and displa... https://circuits4you.com/2018/11/20/web-server...
How to create a web page in ESP and uploading it read this post ?
Make your HTML page and test it in browser. Then make it header file as shown in below code. DON’T JUST
CHANGE THE EXTENSION TO .h
Create index.h file near to your final sketch. Copy and paste below code. Program is commented well. You
can read more on HTML and AJAX at w3schools.com
index.h File
1 const char MAIN_page[] PROGMEM = R"=====(
2 <!DOCTYPE html>
3 <html>
4 <style>
5 .card{
6 max-width: 400px;
7 min-height: 250px;
8 background: #02b875;
9 padding: 30px;
10 box-sizing: border-box;
11 color: #FFF;
12 margin:20px;
13 box-shadow: 0px 2px 18px -4px rgba(0,0,0,0.75);
14 }
15 </style>
16 <body>
17
18 <div class="card">
19 <h4>The ESP32 Update web page without refresh</h4><br>
20 <h1>Sensor Value:<span id="ADCValue">0</span></h1><br>
21 <br><a href="https://circuits4you.com">Circuits4you.com</a>
22 </div>
23 <script>
24
25 setInterval(function() {
26 // Call a function repetatively with 2 Second interval
27 getData();
28 }, 2000); //2000mSeconds update rate
29
30 function getData() {
31 var xhttp = new XMLHttpRequest();
32 xhttp.onreadystatechange = function() {
33 if (this.readyState == 4 && this.status == 200) {
34 document.getElementById("ADCValue").innerHTML =
35 this.responseText;
36 }
37 };
38 xhttp.open("GET", "readADC", true);
39 xhttp.send();
40 }
41 </script>
42 </body>
43 </html>
44 )=====";
3 of 7 5/7/20, 6:01 PM
Web server on ESP32: How to update and displa... https://circuits4you.com/2018/11/20/web-server...
4 of 7 5/7/20, 6:01 PM
Web server on ESP32: How to update and displa... https://circuits4you.com/2018/11/20/web-server...
78 server.handleClient();
79 delay(1);
80 }
Testing
Make WiFi network configuration changes as per your network. Upload the program and test it. Open serial
monitor to observe the IP address and other actions.
Results
Open Serial monitor and get the IP address
In many application you may want to use jQuery and Bigger size HTML, CSS files and images. You can get
clear idea from these two advanced posts
5 of 7 5/7/20, 6:01 PM
Web server on ESP32: How to update and displa... https://circuits4you.com/2018/11/20/web-server...
Related
Omnimusha
Omni
Thank you for the great tutorial. I have a problem that a�er few hours the web server
stops responding. The loop is still runing but no web. I read some sources that it is
MagnusExpe needed to call client.stop() but they use Wifiserver and wifi.h. I am not sure for
rior
6 of 7 5/7/20, 6:01 PM
Web server on ESP32: How to update and displa... https://circuits4you.com/2018/11/20/web-server...
WebServer object.
Manoj R.
Thakur
7 of 7 5/7/20, 6:01 PM