This is an old revision of the document!


Environmental Comfort Monitoring System with ESP32

Autor: Burciu Iustin Florian Email: iustin.burciu@stud.fiir.upb.ro Grupa: AAC

Introduction

This project focuses on building an embedded system to monitor environmental comfort using the ESP32 development board, along with an LM35 temperature sensor and an MQ-4 gas sensor. It continuously evaluates environmental conditions and computes a comfort score based on temperature and gas readings.

The implementation was initially created as a local web server hosted directly on the ESP32, allowing real-time monitoring from any browser within the same Wi-Fi network. Later, a second version was developed using Firebase Realtime Database, enabling remote data access and logging in real time from anywhere in the world.

The project includes:

  • Real-time data acquisition from temperature and gas sensors
  • Live display of sensor values and computed comfort score
  • Visual charts and indicator LEDs (both virtual and physical)
  • Automatic alerts and a persistent log for critical events
  • Optional Firebase integration for remote access

Hardware

Used Components:

  • ESP32 DevKit V1 – WiFi + Bluetooth enabled development board
  • LM35 – analog temperature sensor
  • MQ-4 – analog gas sensor
  • 2x LEDs (red and blue) – for visual alerts
  • Jumper wires, breadboard

ESP32 Connections:

Component ESP32 Pin Description
LM35 VCC 3.3V Power supply
LM35 GND GND Ground
LM35 OUT GPIO34 Temperature signal
MQ-4 VCC VIN Power supply (regulated)
MQ-4 GND GND Ground
MQ-4 A0 GPIO32 Gas level (analog)
LED Blue GPIO25 Lights up at comfort score 100
LED Red GPIO26 Lights up if gas > 30

System Diagram

Functionality

The designed system aims to monitor indoor environmental comfort by evaluating two essential parameters: temperature and gas concentration. These values are processed to calculate a comfort score ranging from 0 to 100, which reflects the overall air quality and thermal comfort.

The core functionalities of the system include:

  • Reading analog values from the LM35 temperature sensor (in °C) and the MQ-4 gas sensor (in ppm).
  • Computing the comfort score based on pre-defined thresholds for temperature and gas, penalizing deviations from optimal ranges.
  • Real-time display of measurements through a web-based dashboard, hosted locally on the ESP32 microcontroller.
  • Automatic dashboard refresh every 2 seconds, showing:
    • Current temperature
    • Gas level (methane/LPG)
    • Comfort score out of 100
    • Two animated line charts visualizing historical values
  • Visual indicators (LEDs):
    • Red LED – lights up when gas level exceeds 30 ppm
    • Blue LED – lights up when the comfort score reaches 100
  • Virtual LEDs on the web interface, mimicking the behavior of physical ones
  • Persistent event log displaying timestamps, temperature, gas level, and comfort score whenever a threshold is crossed
  • *(Optional)* Cloud integration with Firebase Realtime Database, using a separate sketch, allowing data to be viewed remotely

This system provides continuous assessment of the indoor environment, with a user-friendly interface that enables real-time monitoring and alerts, both locally and remotely via cloud services.

Implementation

The implementation of this project was carried out in two main phases, using two distinct Arduino sketches for flexibility and modularity.

Phase 1: Local Web Server

In the first implementation, a local web server is hosted directly on the ESP32 microcontroller. This server delivers an interactive dashboard accessible via any device connected to the same Wi-Fi network. Key elements include:

  • A full HTML/CSS/JavaScript dashboard embedded in the Arduino sketch
  • Real-time temperature and gas readings every 2 seconds
  • Visualization using canvas-based line graphs
  • Web-controlled virtual LEDs that change color based on sensor conditions
  • A scrollable alert history that logs every gas threshold violation or comfort score event

This implementation is self-contained and requires no internet connection beyond local Wi-Fi access.

Phase 2: Firebase Cloud Integration

To enable remote monitoring and cloud-based data storage, a second Arduino sketch was developed using the Firebase ESP Client library. In this version:

  • Sensor data is sent to a Firebase Realtime Database every 2 seconds
  • The sketch authenticates using email/password credentials and an API key
  • Comfort metrics are uploaded under a structured path:
    • `/monitor/temperatura`
    • `/monitor/gaz`
    • `/monitor/scor`
  • This enables the creation of external web or mobile dashboards for visualization

Each sketch controls the same hardware setup (sensors and LEDs) but uses different methods to present and store the data. This modular design ensures flexibility and adaptability for different deployment scenarios (local vs. cloud).

Arduino Sketch

The ESP32 board runs two separate Arduino sketches, each responsible for different functionalities: one for the local server interface and another for Firebase integration.

Local Web Server Sketch

This sketch creates a basic HTTP server on the ESP32, responding with both raw sensor data (`/data` endpoint) and a full HTML dashboard. The key responsibilities include:

  • Reading values from LM35 (temperature) and MQ-4 (gas) sensors
  • Calculating a comfort score based on thresholds
  • Activating physical LEDs for visual alerts
  • Serving a live-updating web page with:
    • Temperature and gas values
    • Comfort score display
    • Canvas-based live graphs
    • Alert log
    • Virtual LEDs (colored indicators)

Snippet: Sensor reading and score calculation

float citesteTemperatura() {
int adc = analogRead(pinLM35);
float voltaj = adc * (5.0 / 4095.0);
return voltaj * 100.0;
}
int calculeazaScor(float temp, int gaz) {
int scor = 100;
if (temp < 19 || temp > 25) scor -= 30;
if (temp < 16 || temp > 28) scor -= 30;
if (gaz > 30) scor -= 10;
if (gaz > 50) scor -= 20;
if (gaz > 100) scor -= 30;
return max(scor, 0);
}

Firebase Integration Sketch

A second Arduino sketch was created to send environmental data to a Firebase Realtime Database, allowing remote access and monitoring from any internet-connected device.

This implementation uses the Firebase ESP Client library to authenticate, connect, and write sensor data securely to the Firebase database.

Main responsibilities:

  • Connects the ESP32 to Wi-Fi
  • Authenticates using an email, password, and API key
  • Initializes communication with Firebase
  • Reads temperature and gas levels
  • Calculates the comfort score
  • Uploads all values to Firebase paths every 2 seconds
  • Activates LED indicators (same behavior as the local sketch)

Firebase Database Structure:

iothings/proiecte/2025sric/esp32environmentalcomfortmonitoringsystemweb.1748483117.txt.gz · Last modified: 2025/05/29 04:45 by iustin.burciu
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0