Close Menu
  • Articles
    • Learn Electronics
    • Product Review
    • Tech Articles
  • Electronics Circuits
    • 555 Timer Projects
    • Op-Amp Circuits
    • Power Electronics
  • Microcontrollers
    • Arduino Projects
    • STM32 Projects
    • AMB82-Mini IoT AI Camera
    • BLE Projects
  • IoT Projects
    • ESP8266 Projects
    • ESP32 Projects
    • ESP32 MicroPython
    • ESP32-CAM Projects
    • LoRa/LoRaWAN Projects
  • Raspberry Pi
    • Raspberry Pi Projects
    • Raspberry Pi Pico Projects
    • Raspberry Pi Pico W Projects
  • Electronics Calculator
Facebook X (Twitter) Instagram
  • About Us
  • Disclaimer
  • Privacy Policy
  • Contact Us
  • Advertise With Us
Facebook X (Twitter) Instagram Pinterest YouTube LinkedIn
How To Electronics
  • Articles
    • Learn Electronics
    • Product Review
    • Tech Articles
  • Electronics Circuits
    • 555 Timer Projects
    • Op-Amp Circuits
    • Power Electronics
  • Microcontrollers
    • Arduino Projects
    • STM32 Projects
    • AMB82-Mini IoT AI Camera
    • BLE Projects
  • IoT Projects
    • ESP8266 Projects
    • ESP32 Projects
    • ESP32 MicroPython
    • ESP32-CAM Projects
    • LoRa/LoRaWAN Projects
  • Raspberry Pi
    • Raspberry Pi Projects
    • Raspberry Pi Pico Projects
    • Raspberry Pi Pico W Projects
  • Electronics Calculator
How To Electronics
Home » ESP32 SHT31 Temperature & Humidity Monitor on Web Server
ESP32 Projects IoT Projects

ESP32 SHT31 Temperature & Humidity Monitor on Web Server

Mamtaz AlamBy Mamtaz AlamUpdated:November 27, 20231 Comment4 Mins Read
Share Facebook Twitter LinkedIn Telegram Reddit WhatsApp
ESP32 SHT31 Temperature Humidity
Share
Facebook Twitter LinkedIn Pinterest Email Reddit Telegram WhatsApp

In this post we will interface SHT31 Humidity & Temperature Sensor with ESP32 Board. Thus using ESP32 & SHT31, we will create build an asynchronous web server and display the temperature-humidity value on a web page. The web server we’ll build updates the readings automatically without the need to refresh the web page.


Overview

There are various humidity & temperature sensors like DHT11 & HTU21D. But while talking about the accuracy none of them are suitable for industrial level temperature humidity monitoring due to the accuracy and precision. They have poor accuracy as well as sensitivity. So here we will use the SHT31 Temperature/Humidity sensor. They are the finest & highest-accuracy devices you can get. It is a digital sensor with an I2C interface which makes for easy reading of humidity and temperature. The SHT31 sensor has an excellent ±2% relative humidity and ±0.3°C temperature accuracy for most uses.

In this project we will interface SHT31 Temperature/Humidity Sensor with ESP32. We will learn how to read humidity and temperature value with SHT31 & ESP32. We will also build an asynchronous ESP32 web server using the ESPAsyncWebServer library with the SHT31 that displays temperature and humidity using Arduino IDE. The web server we’ll build updates the readings automatically without the need to refresh the web page.

If you want to learn more about SHT31 you can check our earlier post:
1. Interfacing SHT3x Humidity & Temperature Sensor with Arduino
2. ESP8266 SHT31 Humidity Temperature Monitoring on Thingspeak




Bill of Materials

Following are the components required for this simple IOT project. All the components can be easily purchased from Amazon. The component purchase link is given below.

S.N.Components QuantityPurchase Links
1ESP32 Board1Amazon | AliExpress
2SHT31 Sensor1Amazon | AliExpress
3Connecting Wires5Amazon | AliExpress
4Breadboard1Amazon | AliExpress

SHT31 Humidity Temperature Sensor

SHT31 is the next generation of Sensirion’s temperature and humidity sensors. The SHT31 has increased intelligence, reliability, and improved accuracy specifications compared to its predecessor. Its functionality includes enhanced signal processing, temperature, and humidity that can be read out using I2C communications. This I2C Mini Module makes it easy to read temperature and humidity using our standardized sensor footprint. Plug into a Particle interface module for cloud access from anywhere in the world.

SHT31 Sensor

All I2C Mini Modules are designed to operate at 5V DC. Using a convenient 4-Pin plug, devices can be daisy-chained onto the I2C Bus, eliminating the need for soldering. Simply plug together the devices you need for your next automation application.

Features of SHT31 Sensor

1. Dual Purpose Temperature and Humidity Sensor
2. Accuracy of ±2% Relative Humidity
3. 0-100% Humidity Sensing Range
4. -40 to +125°C (-40 to +257°F) Operating Temperature
5. 8-Second Sensor Response Time
6. 0x44 Start Address
7. Modular SHT31 breakout board




Schematic: Interfacing SHT31 with ESP32

Here is the schematic for Interfacing SHT31 Humidity Temperature Sensor with ESP32.

SHT31 ESP32 Connection

Connect the VCC pin of SHT31 to 3.3V of ESP32 & GND to GND. Connect the SCL & SDA pin of SHT31 to ESP32 SCL (GPIO22) & SDA (GPIO21) pin respectively as shown in the figure above.

ESP32 SHT31


Asynchronous Web Server

To build the webserver we’ll use the ESPAsyncWebServer library that provides an easy way to build an asynchronous web server. Building an asynchronous web server has several advantages as mentioned below:

1. Handle more than one connection at the same time
2. When you send the response, you are immediately ready to handle other connections while the server is taking care of sending the response in the background
3. Simple template processing engine to handle templates
4. And much more.


Source Code/program

The source code for ESP32 SHT31 Webserver is given below. You can copy this code and upload it to ESP32 Board. But before that you need few libraries. So download the libraries from the link below.

  1. SHT31 Library: Download
  2. AsyncTCP-Library: Download
  3. ESPAsyncWebServer Library: Download


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include "WiFi.h"
#include "ESPAsyncWebServer.h"
#include <Arduino.h>
#include <Wire.h>
#include "Adafruit_SHT31.h"
 
// Replace with your network credentials
const char* ssid = "BYNARK";
const char* password = "bynark@123";
 
Adafruit_SHT31 sht31 = Adafruit_SHT31();
 
// Create AsyncWebServer object on port 80
AsyncWebServer server(80);
 
  String readSHT31Temperature(){
  
  float t = sht31.readTemperature();
 
  if (! sht31.begin(0x44))        // Set to 0x45 for alternate i2c addr
{
Serial.println("SHT31 test");
while (1) delay(1);
}
  if (isnan(t)) {    
    Serial.println("Failed to read temperature!");
    return "--";
  }
  else {
    Serial.print("");
    Serial.print("Temperature: ");
    Serial.println(t);
    return String(t);
  }
}
 
String readSHT31Humidity() {
 
  float h = sht31.readHumidity();
  if (isnan(h)) {
    Serial.println("Failed to read Humidity!");
    return "--";
  }
  else {
    Serial.print("Humidity: ");
    Serial.println(h);
    Serial.println("");
    return String(h);
  }
}
 
const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML><html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">
  <style>
    html {
     font-family: Arial;
     display: inline-block;
     margin: 0px auto;
     text-align: center;
    }
    h2 { font-size: 3.0rem; }
    p { font-size: 3.0rem; }
    .units { font-size: 1.2rem; }
    .sht31-labels{
      font-size: 1.5rem;
      vertical-align:middle;
      padding-bottom: 15px;
    }
  </style>
</head>
<body>
  <h2>ESP32 SHT31 Web Server</h2>
  <p>
    <i class="fas fa-thermometer-half" style="color:#059e8a;"></i>
    <span class="sht31-labels">Temperature</span>
    <span id="temperature">%TEMPERATURE%</span>
    <sup class="units">&deg;C</sup>
  </p>
  <p>
    <i class="fas fa-tint" style="color:#00add6;"></i>
    <span class="sht31-labels">Humidity</span>
    <span id="humidity">%HUMIDITY%</span>
    <sup class="units">%</sup>
  </p>
</body>
<script>
setInterval(function ( ) {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      document.getElementById("temperature").innerHTML = this.responseText;
    }
  };
  xhttp.open("GET", "/temperature", true);
  xhttp.send();
}, 10000 ) ;
 
setInterval(function ( ) {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      document.getElementById("humidity").innerHTML = this.responseText;
    }
  };
  xhttp.open("GET", "/humidity", true);
  xhttp.send();
}, 10000 ) ;
</script>
</html>)rawliteral";
 
// Replaces placeholder with sht31 values
String processor(const String& var){
  //Serial.println(var);
  if(var == "TEMPERATURE"){
    return readSHT31Temperature();
  }
  else if(var == "HUMIDITY"){
    return readSHT31Humidity();
  }
  return String();
}
 
void setup(){
  // Serial port for debugging purposes
  Serial.begin(115200);
  
  // Connect to Wi-Fi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi..");
  }
 
  // Print ESP32 Local IP Address
  Serial.println(WiFi.localIP());
 
  // Route for root / web page
  server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/html", index_html, processor);
  });
  server.on("/temperature", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/plain", readSHT31Temperature().c_str());
  });
  server.on("/humidity", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/plain", readSHT31Humidity().c_str());
  });
 
  // Start server
  server.begin();
}
void loop(){
  
}




ESP32 SHT31 Web Server Demonstration

After uploading, open the Serial Monitor at a baud rate of 115200. Press the ESP32 reset button. The ESP32 IP address should be printed in the serial monitor along with the humidity and temperature value.

Now copy the IP Address and then open a browser and type the ESP32 IP address. Your web server should display the latest sensor readings. You will notice that the temperature and humidity readings are updated automatically without the need to refresh the web page.

Share. Facebook Twitter Pinterest LinkedIn Tumblr Email Reddit Telegram WhatsApp
Previous ArticleNextion Display Based Mobile Phone using GSM & Arduino
Next Article Brain Computer Interface (BCI) System Overview & Applications

Related Posts

IoT Based PM & Air Quality Monitoring System using ESP32

IoT Based PM & Air Quality Monitoring System using ESP32

DIY ESP32 MLX90640 IR Thermal Camera with Live Web Display

DIY ESP32 MLX90640 IR Thermal Camera with Live Web Display

Updated:May 10, 20261K
IoT Activity Tracker with ESP32 & Accelerometer Gyroscope

IoT Activity Tracker with ESP32 & Accelerometer/Gyroscope

Updated:May 2, 2026

ESP32 IoT Vehicle Motion Analyzer with MPU6050 & LIS3MDL

Updated:April 27, 20261K
High-Accuracy Pitch, Roll, Yaw with ESP32 & BNO08x IMU

High-Accuracy Pitch, Roll, Yaw with ESP32 & BNO08x IMU

Updated:April 27, 20262K
DIY Colorimeter using AS7265x Spectroscopy Sensor & ESP32

DIY Colorimeter using AS7265x Spectroscopy Sensor & ESP32

Updated:February 1, 20261K
View 1 Comment

1 Comment

  1. Ivan on August 15, 2025 12:14 PM

    it is not working! Always reconnect wifi!

    Reply

CommentsCancel reply

Latest Posts
IoT Based PM & Air Quality Monitoring System using ESP32

IoT Based PM & Air Quality Monitoring System using ESP32

May 31, 2026
DIY ESP32 MLX90640 IR Thermal Camera with Live Web Display

DIY ESP32 MLX90640 IR Thermal Camera with Live Web Display

May 10, 2026
IoT Activity Tracker with ESP32 & Accelerometer Gyroscope

IoT Activity Tracker with ESP32 & Accelerometer/Gyroscope

May 2, 2026
A Guide to Sourcing Obsolete ICs for Vintage Projects

Beyond AliExpress: A Guide to Sourcing Obsolete ICs for Vintage Projects

April 21, 2026

ESP32 IoT Vehicle Motion Analyzer with MPU6050 & LIS3MDL

April 27, 2026
Building a Smart Sensor Node with a BLE Microcontroller

Building a Smart Sensor Node with a BLE Microcontroller

February 26, 2026
High-Accuracy Pitch, Roll, Yaw with ESP32 & BNO08x IMU

High-Accuracy Pitch, Roll, Yaw with ESP32 & BNO08x IMU

April 27, 2026
DIY Colorimeter using AS7265x Spectroscopy Sensor & ESP32

DIY Colorimeter using AS7265x Spectroscopy Sensor & ESP32

February 1, 2026
Top Posts & Pages
  • 12V DC to 220V AC Inverter Circuit & PCB
    12V DC to 220V AC Inverter Circuit & PCB
  • IoT AC Energy Meter with PZEM-004T & ESP32 WebServer
    IoT AC Energy Meter with PZEM-004T & ESP32 WebServer
  • IoT Based Drinking Water Quality Monitoring with ESP32
    IoT Based Drinking Water Quality Monitoring with ESP32
  • LD2410 Sensor with ESP32 - Human Presence Detection
    LD2410 Sensor with ESP32 - Human Presence Detection
  • ESP32 CAN Bus Tutorial | Interfacing MCP2515 CAN Module with ESP32
    ESP32 CAN Bus Tutorial | Interfacing MCP2515 CAN Module with ESP32
  • DIY IoT Water pH Meter using pH Sensor & ESP32
    DIY IoT Water pH Meter using pH Sensor & ESP32
  • Buck Converter: Basics, Working, Design & Application
    Buck Converter: Basics, Working, Design & Application
  • How to use Modbus RTU with ESP32 to read Sensor Data
    How to use Modbus RTU with ESP32 to read Sensor Data
Categories
  • Arduino Projects (197)
  • Articles (60)
    • Learn Electronics (19)
    • Product Review (15)
    • Tech Articles (28)
  • Electronics Circuits (46)
    • 555 Timer Projects (21)
    • Op-Amp Circuits (7)
    • Power Electronics (13)
  • IoT Projects (204)
    • ESP32 MicroPython (7)
    • ESP32 Projects (81)
    • ESP32-CAM Projects (15)
    • ESP8266 Projects (76)
    • LoRa/LoRaWAN Projects (22)
  • Microcontrollers (38)
    • AMB82-Mini IoT AI Camera (4)
    • BLE Projects (18)
    • STM32 Projects (19)
  • Raspberry Pi (93)
    • Raspberry Pi Pico Projects (57)
    • Raspberry Pi Pico W Projects (12)
    • Raspberry Pi Projects (24)
Follow Us
  • Facebook
  • Twitter
  • Pinterest
  • Instagram
  • YouTube
About Us

“‘How to Electronics’ is a vibrant community for electronics enthusiasts and professionals. We deliver latest insights in areas such as Embedded Systems, Power Electronics, AI, IoT, and Robotics. Our goal is to stimulate innovation and provide practical solutions for students, organizations, and industries. Join us to transform learning into a joyful journey of discovery and innovation.

Copyright © How To Electronics. All rights reserved.
  • About Us
  • Disclaimer
  • Privacy Policy
  • Contact Us
  • Advertise With Us

Type above and press Enter to search. Press Esc to cancel.

Ad Blocker Enabled!
Ad Blocker Enabled!
Looks like you're using an ad blocker. Please allow ads on our site. We rely on advertising to help fund our site.