DIYables ESP32 Web Apps Web Temperature

Overview

The WebTemperature example creates a visual thermometer interface accessible from any web browser. Designed for ESP32 educational platform with enhanced sensor monitoring capabilities, built-in temperature sensing features, and seamless integration with environmental monitoring educational modules. Perfect for monitoring temperature sensors, environmental conditions, or any temperature-based measurements requiring visual feedback.

Arduino WebTemperature Example - Thermometer Display Interface Tutorial

Features

  • Visual Thermometer Display: Interactive thermometer via web interface
  • Configurable Temperature Range: Custom minimum and maximum temperatures with units
  • Real-time Updates: Live temperature display with mercury-style animation
  • Multiple Units Support: Celsius (°C), Fahrenheit (°F), Kelvin (K)
  • Automatic Config Handling: Set range and unit once in constructor
  • WebSocket Communication: Instant updates without page refresh
  • Mobile Responsive: Works perfectly on desktop, tablet, and mobile devices
  • Professional Design: Clean thermometer visualization with smooth animations

Hardware Used In This Tutorial

1×ESP-WROOM-32 Dev Module
1×USB Cable Type-A to Type-C (for USB-A PC)
1×USB Cable Type-C to Type-C (for USB-C PC)
1×DS18B20 Temperature Sensor (optional)
1×DHT22 Temperature/Humidity Sensor (optional)
1×Breadboard
1×Jumper Wires
1×Recommended: Screw Terminal Expansion Board for ESP32
1×Recommended: Breakout Expansion Board for ESP32
1×Recommended: Power Splitter for ESP32

Or you can buy the following kits:

1×DIYables ESP32 Starter Kit (ESP32 included)
1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links in this section are Amazon affiliate links, meaning we may earn a commission at no additional cost to you if you make a purchase through them. Additionally, some links direct you to products from our own brand, DIYables .

Buy Note: Numerous DS18B20 sensors available in the market are of poor quality. We strongly advise purchasing the sensor from the DIYables brand via the link above; we conducted tests, and it performed reliably.

Setup Instructions

Quick Instructions

Follow these instructions step by step:

  • If this is your first time using the ESP32, refer to the tutorial on setting up the environment for ESP32 in the Arduino IDE.
  • Connect the ESP32 board to your computer using a USB cable.
  • Launch the Arduino IDE on your computer.
  • Select the appropriate ESP32 board (e.g. ESP32 Dev Module) and COM port.
  • Navigate to the Libraries icon on the left bar of the Arduino IDE.
  • Search "DIYables ESP32 WebApps", then find the DIYables ESP32 WebApps Library by DIYables
  • Click Install button to install the library.
DIYables ESP32 WebApps Library
  • You will be asked for installing some other library dependencies
  • Click Install All button to install all library dependencies.
DIYables ESP32 WebApps dependency
  • On Arduino IDE, Go to File Examples DIYables ESP32 WebApps WebTemperature example, or copy the above code and paste it to the editor of Arduino IDE
/* * This example demonstrates how to create a web-base Serial.println("Starting Web Temperature Server..."); // Add web apps to server server.addApp(&homePage); server.addApp(&temperaturePage); // Set 404 Not Found page (optional - for better user experience) server.setNotFoundPage(DIYablesNotFoundPage());re display * using the DIYables ESP32 WebApps Library with ESP32. * * The library automatically detects the ESP32 platform and * includes the appropriate abstraction layer for seamless operation. * * The web page displays a thermometer visualization that shows temperature * readings in real-time through WebSocket communication. * * Features: * - Real-time temperature display with thermometer visualization * - Configurable temperature range and units * - Auto-connecting WebSocket for seamless communication * - Mobile-responsive design with professional UI * - Automatic platform detection and abstraction * - Compatible with both WiFi and Ethernet connections * Hardware: ESP32 Boards */ #include <DIYables_ESP32_Platform.h> #include <DIYablesWebApps.h> // WiFi credentials - UPDATE THESE WITH YOUR NETWORK const char WIFI_SSID[] = "YOUR_WIFI_SSID"; const char WIFI_PASSWORD[] = "YOUR_WIFI_PASSWORD"; // Create web app instances ESP32ServerFactory serverFactory; DIYablesWebAppServer server(serverFactory, 80, 81); // HTTP port 80, WebSocket port 81 DIYablesHomePage homePage; DIYablesWebTemperaturePage temperaturePage(-10.0, 50.0, "°C"); // Min: -10°C, Max: 50°C // Temperature simulation variables float currentTemp = 25.0; // Starting temperature unsigned long lastUpdate = 0; void setup() { Serial.begin(9600); Serial.println("Starting Web Temperature Server..."); // Add web apps to server server.addApp(&homePage); server.addApp(&temperaturePage); // Set 404 Not Found page (optional - for better user experience) server.setNotFoundPage(DIYablesNotFoundPage()); // Set up temperature callback for value requests temperaturePage.onTemperatureValueRequested(onTemperatureValueRequested); // Start the server server.begin(WIFI_SSID, WIFI_PASSWORD); } void loop() { // Handle web server and WebSocket communications server.loop(); // Simulate temperature readings simulateTemperature(); // Send temperature update every 2 seconds if (millis() - lastUpdate >= 2000) { temperaturePage.sendTemperature(currentTemp); // Print temperature to Serial Monitor Serial.println("Temperature: " + String(currentTemp, 1) + "°C"); lastUpdate = millis(); } delay(10); // Small delay for stability } void simulateTemperature() { // Simple temperature simulation - slowly increases and decreases static bool increasing = true; if (increasing) { currentTemp += 0.1; // Increase temperature slowly if (currentTemp >= 35.0) { increasing = false; // Start decreasing when it reaches 35°C } } else { currentTemp -= 0.1; // Decrease temperature slowly if (currentTemp <= 15.0) { increasing = true; // Start increasing when it reaches 15°C } } } /** * Callback function called when web interface requests temperature value * Send current temperature value to web interface */ void onTemperatureValueRequested() { Serial.println("Temperature value requested from web interface"); // Send current temperature value (config is automatically sent by the library) temperaturePage.sendTemperature(currentTemp); } /* * Alternative setup for real temperature sensor (DS18B20 example): * * #include <OneWire.h> * #include <DallasTemperature.h> * * #define ONE_WIRE_BUS 2 * OneWire oneWire(ONE_WIRE_BUS); * DallasTemperature sensors(&oneWire); * * void setup() { * // ... existing setup code ... * sensors.begin(); * } * * float readTemperature() { * sensors.requestTemperatures(); * return sensors.getTempCByIndex(0); * } * * // In loop(), replace simulateTemperature() with: * // currentTemp = readTemperature(); */
  • Configure WiFi credentials in the code by updating these lines:
const char WIFI_SSID[] = "YOUR_WIFI_NETWORK"; const char WIFI_PASSWORD[] = "YOUR_WIFI_PASSWORD";
  • Click Upload button on Arduino IDE to upload code to ESP32
  • Open the Serial Monitor
  • Check out the result on Serial Monitor. It looks like the below
COM6
Send
Starting Web Temperature Server... INFO: Added app / INFO: Added app /web-temperature DIYables WebApp Library Platform: ESP32 Network connected! IP address: 192.168.0.2 HTTP server started on port 80 Configuring WebSocket server callbacks... WebSocket server started on port 81 WebSocket URL: ws://192.168.0.2:81 WebSocket server started on port 81 ========================================== DIYables WebApp Ready! ========================================== 📱 Web Interface: http://192.168.0.2 🔗 WebSocket: ws://192.168.0.2:81 📋 Available Applications: 🏠 Home Page: http://192.168.0.2/ 🌡️ Web Temperature: http://192.168.0.2/web-temperature ==========================================
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  
  • If you do not see anything, reboot ESP32 board.
  • Open a web browser on your PC or mobile phone.
  • Type the IP address shown in the Serial Monitor to the web browser
  • Example: http://192.168.1.100
  • You will see the home page like below image:
ESP32 DIYables WebApp Home page with Web Temperature app
  • Click to the Web Temperature link, you will see the Web Temperature app's UI like the below:
ESP32 DIYables WebApp Web Temperature app
  • Or you can also access the page directly by IP address followed by /web-temperature. For example: http://192.168.1.100/web-temperature
  • You will see a visual thermometer display showing real-time temperature readings

Web Interface Features

Thermometer Display

  • Visual Thermometer: Classic thermometer design with mercury-style animation
  • Temperature Scale: Clear scale markings with configurable range
  • Real-time Updates: Live temperature display with smooth transitions
  • Unit Display: Shows configured temperature units (°C, °F, K)
  • Professional Design: Clean, educational-style thermometer visualization

Real-time Monitoring

  • Live Data: Temperature updates automatically via WebSocket connection
  • Smooth Animation: Mercury level moves smoothly between readings
  • Status Feedback: Connection status indicator
  • Mobile Optimized: Touch-friendly interface for all devices

Code Configuration

Temperature Setup

// Configure temperature range and units DIYablesWebTemperaturePage temperaturePage(-10.0, 50.0, "°C"); // -10 to 50°C // Set up temperature value request callback temperaturePage.onTemperatureValueRequested(onTemperatureValueRequested);

Sending Temperature Values

void onTemperatureValueRequested() { // Read temperature from sensor (example with simulation) float currentTemp = readTemperatureSensor(); // Send to thermometer display temperaturePage.sendTemperature(currentTemp); }

Temperature Sensor Integration

DS18B20 Digital Temperature Sensor

#include <OneWire.h> #include <DallasTemperature.h> #define ONE_WIRE_BUS 2 OneWire oneWire(ONE_WIRE_BUS); DallasTemperature sensors(&oneWire); void setup() { sensors.begin(); // Configure temperature page for sensor range DIYablesWebTemperaturePage tempPage(-55.0, 125.0, "°C"); } float readTemperatureSensor() { sensors.requestTemperatures(); return sensors.getTempCByIndex(0); }

DHT22 Temperature/Humidity Sensor

#include <DHT.h> #define DHT_PIN 2 #define DHT_TYPE DHT22 DHT dht(DHT_PIN, DHT_TYPE); void setup() { dht.begin(); // Configure for DHT22 range DIYablesWebTemperaturePage tempPage(-40.0, 80.0, "°C"); } float readTemperatureSensor() { return dht.readTemperature(); // Returns °C }

Analog Temperature Sensor (TMP36)

#define TEMP_PIN A0 float readTemperatureSensor() { int reading = analogRead(TEMP_PIN); float voltage = reading * 5.0 / 1024.0; float temperatureC = (voltage - 0.5) * 100.0; // TMP36 formula return temperatureC; }

Unit Conversion

Supporting Multiple Units

// Temperature in different units DIYablesWebTemperaturePage celsiusPage(-10.0, 50.0, "°C"); DIYablesWebTemperaturePage fahrenheitPage(14.0, 122.0, "°F"); DIYablesWebTemperaturePage kelvinPage(263.15, 323.15, "K"); // Conversion functions float celsiusToFahrenheit(float celsius) { return (celsius * 9.0/5.0) + 32.0; } float celsiusToKelvin(float celsius) { return celsius + 273.15; }

Customization Options

Temperature Range

  • Minimum Temperature: Set lowest expected reading
  • Maximum Temperature: Set highest expected reading
  • Units: Display unit string (°C, °F, K, or custom)
  • Scale: Thermometer scale automatically adjusts to range

Update Frequency

// Control update rate to avoid overwhelming the interface unsigned long lastUpdate = 0; const unsigned long UPDATE_INTERVAL = 1000; // 1 second void loop() { server.loop(); if (millis() - lastUpdate >= UPDATE_INTERVAL) { // Update temperature display lastUpdate = millis(); } }

Common Use Cases

Educational Projects

  • Weather Monitoring: Indoor/outdoor temperature tracking
  • Physics Experiments: Heat transfer, thermal dynamics
  • Environmental Science: Climate monitoring, greenhouse control
  • Electronics Learning: Sensor interfacing, data visualization

Practical Applications

  • Home Automation: HVAC control, energy monitoring
  • Greenhouse Control: Plant growth optimization
  • Food Safety: Temperature monitoring for storage
  • Industrial: Process monitoring, quality control

Troubleshooting

Temperature Not Updating

  • Check WiFi connection and WebSocket status
  • Verify sensor wiring and power supply
  • Ensure callback function is properly set
  • Check Serial Monitor for sensor readings

Incorrect Temperature Values

  • Verify sensor calibration and accuracy
  • Check voltage reference for analog sensors
  • Ensure proper sensor initialization
  • Test sensor independently with basic code

Sensor Connection Issues

  • Check wiring connections (power, ground, data)
  • Verify pull-up resistors for digital sensors
  • Test sensor with multimeter for proper operation
  • Check sensor library installation and compatibility

Advanced Features

Multiple Temperature Sensors

Monitor multiple locations with separate thermometers:

DIYablesWebTemperaturePage indoorTemp(-10.0, 40.0, "°C"); DIYablesWebTemperaturePage outdoorTemp(-30.0, 50.0, "°C"); DIYablesWebTemperaturePage waterTemp(0.0, 100.0, "°C"); server.addApp(&indoorTemp); server.addApp(&outdoorTemp); server.addApp(&waterTemp);

Temperature Logging

Combine with Web Plotter for historical temperature data:

// Send same value to both thermometer and plotter temperaturePage.sendTemperature(currentTemp); plotterPage.sendData("Temperature", currentTemp);

Alert System

Implement temperature alerts:

void checkTemperatureAlerts(float temp) { if (temp > 35.0) { Serial.println("⚠️ High temperature alert!"); // Send alert via web interface } else if (temp < 5.0) { Serial.println("🧊 Low temperature alert!"); // Send alert via web interface } }

Educational Integration

STEM Learning Objectives

  • Temperature Physics: Understanding thermal concepts
  • Sensor Technology: Learning digital and analog sensors
  • Data Visualization: Real-time data display techniques
  • Programming: Callback functions, sensor integration

Classroom Activities

  • Sensor Comparison: Compare different temperature sensor types
  • Calibration Exercise: Learn measurement accuracy principles
  • Environmental Monitoring: Track temperature changes over time
  • System Integration: Combine with other environmental sensors

Science Experiments

  • Heat Transfer: Monitor temperature changes during experiments
  • Phase Changes: Observe temperature during melting/boiling
  • Insulation Testing: Compare insulation effectiveness
  • Climate Study: Long-term temperature monitoring

This example provides a comprehensive foundation for temperature monitoring and visualization, perfect for both educational and practical environmental monitoring applications.

※ OUR MESSAGES