Showing temperature, humidity and water leak in the Web

The ESP32 board itself does not provide information like temperature and humidity. Specific sensors are needed to read physical properties. This project uses the BME280 device, that provides calibrated digital reading for temperature and humidity via I2C interface. This project provides also information about water leakage with an alarm. The FC-37 rain sensor, without the LM393 module, is used to detect water leak.

In order to show time on a website, the ESP32 board connects itself to internet using the Wifi access.

The ESP32 board can be programmed as web client to POST temperature, humidity and water leak values towards an external web server, where data are stored and showed by a script. In this project a PHP script is used in the web server for data acquisition and another PHP script is used as dashboard. The following figure shows the main parts of this ESP32 project:
Web thermometer


Hardware set-up

Following hardware is required to build the experiment:
  • ESP32 board
  • FC-37 rain sensor, without the LM393 module
  • BME280 device
  • resistor 10kΩ
  • capacitor 100nF
  • piezo buzzer

The following connections shall be implemented, without any power connected:
  1. Connect GND(ESP32) together with GND(BME280), one pin of FC-37 rain sensor, one pin of the capacitor 100nF, one pin of piezo buzzer
  2. Connect 3.3V(ESP32) together with VIN(BME280)
  3. Connect GPIO 21(ESP32) together with SDA(BME280)
  4. Connect GPIO 22(ESP32) together with SCL(BME280)
  5. Connect 3.3V(ESP32) together with one pin of the pull-up resistor 10kΩ
  6. Connect GPIO 36(ESP32) to the other pin of the pull-up resistor 10kΩ, capacitor 100nF and FC-37 rain sensor
  7. Connect digital GPIO 5(ESP32) to the other pin of piezo buzzer

Before writing software make sure to have installed the software development environment, downlodable from Arduino Software web site.
It may be needed to download some library if not already present: Adafruit BME280 library.

The ESP32 application for this project uses the deep sleep mode to reduce power consumption. The main logic is implemented in the setup() part, but the loop() part is not used due to deep sleep.
The main logic is the following:
  1. Attach to Wifi network
  2. Init RTC using NTP
  3. Read temperature and humidity via BME280 sensor
  4. Read water leak via FC-37 sensor
  5. In case of water leak activate alert using piezo buzzer
  6. Post data to web server
  7. Disconnect Wifi and start deep sleep

All the logic is implemented in the setup() part:
void setup () {
  Serial.begin(115200);
  pinMode(LED_PIN, OUTPUT);
  pinMode(PIEZO_GPIO_PIN, OUTPUT);
  
//connect to wifi to reach ntp server and to send data to remote web server
//timeout is 10 seconds to avoid hanging in case of unavailable wifi!
  connectWifi(wifi_timeout * 1000);

// init and get the time
  configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
  printLocalTime();

  bool status = bme.begin(0x76); // bme280 i2c address = 0x76
  if (!status) {
      Serial.println("Could not find a valid BME280 sensor, check wiring!");
//continue without BME280      
  } else {
    Serial.println("Valid BME280 sensor found!");
    bme280_active = true;
  }

  float temperature = -273;//invalid value in case bme280 is not active
  float humidity = -1;//invalid value in case bme280 is not active

  if (bme280_active) {
    temperature = bme.readTemperature();
    humidity = bme.readHumidity();
    Serial.print("Temperature = ");
    Serial.print(temperature);
    Serial.println(" *C");
    Serial.print("Humidity = ");
    Serial.print(humidity);
    Serial.println(" %");
  }

  int water = analogRead(WATER_GPIO_PIN);
  Serial.print("Water: ");  
  Serial.println(water);
  if ((temperature > 50) || (water < 3000)) {//alarm level @ water sensor reading < 3000 (lower value means more water on sensor)
// buzzer alarm for 20 seconds in case of high temperature (fire?) or water leakage
    for (int i = 0; i < 20; i++) {
      digitalWrite(PIEZO_GPIO_PIN, HIGH);
      delay(600);
      digitalWrite(PIEZO_GPIO_PIN, LOW);
      delay(400);  
    }     
  }
//send data to website if connected to wifi
  if (WiFi.status() == WL_CONNECTED) {
    int temp10 = (int)(temperature * 10);
    int hum10 = (int)(humidity * 10);
    int waterl = 4095 - water;
    int result = sendPostRequest(endPoint, temp10, hum10, waterl, totalCount);
    if (result >= 0) {
      Serial.print("HTTP Response code: ");
      Serial.println(result);
    } else Serial.println("web server connection error!");
  }
  totalCount++;
          
  //disconnect WiFi before deep sleep
  disconnectWifi();

  //start deep sleep to reduce power consumption
  esp_sleep_enable_timer_wakeup(time_to_sleep * 1000000);  
  Serial.println("Going to sleep now");
  Serial.flush(); 
  esp_deep_sleep_start();  
}


Download the complete sketch here, change at least the following parameters:
  • secret value, used for basic security
  • source value, used to identify this instance of sensor
  • web end point, used to run the PHP acquisition script
Connect ESP32 board to your computer using a USB cable and upload the updated sketch to the ESP32 board.

Server side software

Two PHP scripts shall be uploaded in the web server for two tasks:
  • waterleak.php: it receives information from ESP32.
  • dashboard.php: it renders the temperature when request is received from a browser. This script supports multiple instance of the ESP32 board, by using the 'source' parameter.


Both scripts use MYSQL database to handle the measureaments data, the following SQL statement creates the table that stores data:

CREATE TABLE water (date DATETIME NOT NULL, source CHAR(16) NOT NULL, temp FLOAT, humidity FLOAT, water SMALLINT, cellular SMALLINT)

The file mysqldb.cfg shall be updated with the configuration of your MYSQL database in order to allow access to the DB.

dashboard.php

The dashboard.php scripts provide a very basic user interface to view data coming from the ESP32 board.
The main screen shows a table with one row for each source, showing latest reading (Temperature, Humidity, Water leak). Abnormal values are highlighted in red color. It is possible to view detailed data:

waterleak.php

Information is received in a POST request and processed in the script waterleak.php. A secret value is used to check that the information is sent by ESP32 board:
define("secret", "rshe*i76"); // <-- change secret value here and inside sketch ESP_Water_Leakage.ino
The main part of the functionality is provided by the following code:
if ($_SERVER['REQUEST_METHOD'] == "POST") { // POST
	if ($_POST['secret'] == secret) { 
		$content = date("d-M-y, G:i:s",time())."\t".$_POST['count']."\t".$_POST['source']."\t".$_POST['temp10']."\t".$_POST['hum10']."\t".$_POST['waterl'];
		file_put_contents("water100.dat", $content);
		$config = xml2array(simplexml_load_file($config_file_name));
		$link = connect_MySQL($config["engine"]);
		if ($link) {
			$_mydate = date("Y-m-d H:i:s",time());
			$source = $_POST['source'];
			$temp = $_POST['temp10'] / 10;
			$humidity = $_POST['hum10'] / 10;
			$water = $_POST['waterl'];
			$cellular = 0;//todo - insert cellular signal level
			
			if ($humidity >= 0)
				$query = "INSERT INTO water (date, source, temp, humidity, water, cellular) VALUES ('$_mydate', '$source', '$temp', '$humidity', '$water', '$cellular')";	
			else $query = "INSERT INTO water (date, source, water, cellular) VALUES ('$_mydate', '$source', '$water', '$cellular')";	

			mysqli_query($link, $query);

			delete_old_records($link, "water", "INTERVAL 30 DAY");//purge all records that are older than 1 month
			mysqli_close($link);
		}
	} else echo "invalid parameters"; // else authentication failure
}

Download both scripts here, unzip it, change the secret value and upload to your webserver. Now you should be able to see the temperature and humidity using a web browser!

Demo

Here is an example of the output in the web browser:
Web thermometer
Share Share on Facebook Share on Twitter Bookmark on Reddit Share via mail
Creative Commons Attribution-Share Alike | Terms and conditions | Privacy policy