Tom DeCricle
Published © GPL3+

Home Assistant ESP32 Wireless Sump Pit Level Monitoring IoT

Monitor the level of a basement sump-pit and trigger notifications/alarms if the level becomes too high

IntermediateFull instructions provided2 hours1,307
Home Assistant ESP32 Wireless Sump Pit Level Monitoring IoT

Things used in this project

Hardware components

Seeed Studio esp32-c3
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE
Home Assistant
Home Assistant
Circuit Maker
CircuitMaker by Altium Circuit Maker

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
Premium Female/Male Extension Jumper Wires, 40 x 6" (150mm)
Premium Female/Male Extension Jumper Wires, 40 x 6" (150mm)

Story

Read more

Custom parts and enclosures

Gerbers

Altium Output Gerbers for the PCB

Schematics

HC-SR04 Wiring Diagram

Use this pinout for connecting the HC-SR04 to an ESP32-C3

Altium Designer Ultrasonic_Boardv1 Schematic

Schematic of the Ultrasonic Board

Code

Sump-Pit_Water_Level_Monitoring

Arduino
Use this code with the Arduino IDE to write the distance the water is from the top of the pit to a web page for Home Assistant to scrape and report on it's dashboard
/*
Script for monitoring the water level of a sump-pit in a basement.
The distance from the sensor is reported in cm. The ESP32-C3 will make
multiple measurements and store these values in an array, The contents of the array 
are averaged together to handle measurement noise. The average distance value is
then published to a website where a raspberry-pi or Home Assistant can scrape
the page and report the value to our Home Assistant dashboard.
 
Tom DeCircle
5/9/2024
 */
#include <WiFi.h>
#include <WebServer.h>
#include <DistanceSensor.h>


// Wifi Constants
const char* ssid = "YOURWIFI";
const char* password = "YOURPASSWORD";

//HC-SR04 Pins
const int echoPin = D5;
const int trigPin = D4;

//LCV for while loop
int i = 1;
float distance[10];
float sum;
float avg;

// Start the sensor
DistanceSensor sensor(trigPin, echoPin);

//Start the wifi-server
WiFiServer server(80);

void setup() {
  
  // Begin reporting data to the serial port
  Serial.begin(9600);
  delay(10);

  // Connect to a WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);

  // Staying in a loop until WiFi is connected 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print("Connecting to WiFi...");
  }

  Serial.println("");
  Serial.println("WiFi connected.");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  // Host a web page
  server.begin();
  Serial.println("HTTP server started");
}

void loop() {
  
  // Loop control variable
  i++;
  
  // Check if i exceeds the distance array size
  if (i >= 10) {
    i = 0;    // Reset i to 0
  }

  // Send the trigger signal and wait for the echo. Write the value to an array indexed by the LCV 'i'
  distance[i]= sensor.getCM();
  
  // Sum the elements of the distance array
  sum = distance[1] + distance[2] + distance[3] + distance[4] + distance[5] + distance[6] + distance[7] + distance[8] + distance[9] + distance[10];
  // Average the the sum
  avg = sum / 10;  // Calculate average after incrementing i
  

  // //-----Comment or UnComment this section for debugging-----------------------------
  // // Print the first 5 elements to terminal
  // Serial.print("Distance[1] = ");
  // Serial.print(distance[1]);
  // Serial.print("  Distance[2] = ");
  // Serial.print(distance[2]);
  // Serial.print("  Distance[3] = ");
  // Serial.print(distance[3]);
  // Serial.print("  Distance[4] = ");
  // Serial.print(distance[4]);
  // Serial.print("  Distance[5] = ");
  // Serial.print(distance[5]);
  // // Print the value of sum
  // Serial.print("   Sum = ");
  // Serial.print(sum);
  // // Print the value of avg
  // Serial.print("   Avg = ");
  // Serial.println(avg);
  // // Slow down the terminal otherwise ESP-32 will lock up and can't be programmed
  // delay(100);

//-------------------------------------------------------------------------------------

  WiFiClient client = server.accept();  // listen for incoming clients

  if (client) {                     // if you get a client,
    Serial.println("New Client.");  // print a message out the serial port
    String currentLine = "";        // make a String to hold incoming data from the client
    while (client.connected()) {    // loop while the client's connected
      if (client.available()) {     // if there's bytes to read from the client,
        char c = client.read();     // read a byte
      
        // if the current line is blank, you got two newline characters in a row.
        // that's the end of the client HTTP request, so send a response:
        if (currentLine.length() == 0) {
          // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
          // and a content-type so the client knows what's coming, then a blank line:
          client.println("HTTP/1.1 200 OK");
          client.println("Content-type:text/html");
          client.println();

          // the content of the HTTP response follows the header:
          //client.print("Distance Measurement =");
          client.print(avg);
          //client.print("cm");

          // The HTTP response ends with another blank line:
          client.println();
          // break out of the while loop:
          break;
        }
      }
      // close the connection:
      client.stop();
      Serial.println("Client Disconnected.");
    }
  }
  sum = 0;  // Reset sum
}

Credits

Tom DeCricle
6 projects • 2 followers
Electrical Engineer with experience in Schematic Design, PCB Layout, PCB Fabrication & Assembly Procurement, Hardware/Firmware/Software Dev
Contact

Comments

Please log in or sign up to comment.