Fanatic Series
Published

Water Level Meter

Water Level Meter using the Ultrasonic Sensor to sense the Water Level and ESP8266-01 to send the data over WiFi.

BeginnerShowcase (no instructions)4 hours39,005
Water Level Meter

Things used in this project

Hardware components

Arduino Micro
Arduino Micro
I have used Pro-Micro. There is no library of ESP8266-01 for Arduino Micro
×1
ESP8266 ESP-01
Espressif ESP8266 ESP-01
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE
Visual Studio 2015
Microsoft Visual Studio 2015
Used for HTML Code testing

Hand tools and fabrication machines

Hot glue gun (generic)
Hot glue gun (generic)
Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

Water Level Meter using Ultrasonic HC-SR04 and WiFi module ESP-8266-01

Water Level Meter using Ultrasonic HC-SR04 and WiFi module ESP-8266-01

Code

Water Level Meter using Ultrasonic HC-SR04 and WiFi module ESP-8266-01

Arduino
#include <SoftwareSerial.h>
#define DEBUG true  
#define dbg Serial  // USB local debug
#define trigPin 5  //IO of Ultrasonic Sensor
#define echoPin 6  //IO of Ultrasonic Sensor
long duration;  
float distance;
const int max_hight=30;  // maximum deapth of the Water Tank. in my case its a bucket

String ssid = "SSID";

String pass = "PASSWORD";
String ip = "192.168.1.202";  //desired IP Address
String ref="refresh";         //Text for refeshing the HTML Page  
String num_delay = "3";		// Delay is second to refresh the HTML Page	

String sendData(String command, const int timeout, boolean debug) {
  String response = "";
  Serial1.print(command); // send the read character to the esp8266
  long int time = millis();
  while ( (time + timeout) > millis()) {
    while (Serial1.available()) {
      // The esp has data so display its output to the serial window
      char c = Serial1.read(); // read the next character.
      response += c;
    }
  }

  if (debug)
  {
    Serial.print(response);
  }

  return response;
}





void setup() {

  // Set baud rates
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input

  Serial1.begin(115200);
  dbg.begin(115200);
  dbg.println("DEBUG: Running Setup");



  sendData("AT+RST\r\n", 2000, DEBUG); // reset module
  sendData("AT\r\n", 2000, DEBUG); // reset module
  sendData("AT+CWMODE=1\r\n", 1000, DEBUG); // configure as access point

sendData("AT+CWJAP=\"" + ssid + "\",\"" + pass + "\"\r\n", 6000, DEBUG); // configure as access point
sendData("AT+CIPSTA=\""+ip+"\"\r\n",1000,true); // configure softAPip address
sendData("AT+CIPMUX=1\r\n",1000,DEBUG); // configure for multiple connections
sendData("AT+CIPSERVER=1,80\r\n",1000,DEBUG); // turn on server on port 80




delay(1000);


  dbg.println("DEBUG: Setup complete\n\n");
}


void loop()
{


  
  if(Serial1.available()) // check if the esp is sending a message 
  {
    Serial.println("hello");
    //delay(1000);

    /*
    while(Serial1.available())
    {
      // The esp has data so display its output to the serial window 
      char c = Serial1.read(); // read the next character.
      Serial.write(c);
    } */
    
    if(Serial1.find("+IPD,"))
    {
     delay(1000);
     sensor_data();
     String data = String((int)distance);
     String webpage="";
 
     int connectionId = Serial1.read()-48; // subtract 48 because the read() function returns 
                                           // the ASCII decimal value and 0 (the first decimal num_delayber) starts at 48
String colorgreen="background-color:green;";  //#87F467- Green, #FF7979-Red
String colorred="background-color:red;";  //#87F467- Green, #FF7979-Red

  if(distance<=10 || distance>=90){webpage="<html><body style=\""+colorred+"\"></body>";}
  else {webpage="<html><body style=\""+colorgreen+"\"></body>";}
  webpage+="<meta http-equiv=\""+ref+ "\" content=\"" + num_delay+ "\"/> <h1><center>WATER LEVEL<\center></h1> <h2><center>"+String(data)+" %<\center></h2><\html>";


     String cipSend = "AT+CIPSEND=";
     cipSend += connectionId;
     cipSend += ",";
     cipSend +=webpage.length();
     cipSend +="\r\n";
     
     sendData(cipSend,1000,DEBUG);
     sendData(webpage,1000,DEBUG);
     
//Serial.println(webpage);   


 
     String closeCommand = "AT+CIPCLOSE="; 
     closeCommand+=connectionId; // append connection id
     closeCommand+="\r\n";    
     sendData(closeCommand,3000,DEBUG);
    //Serial.println("send0");
    //delay(2000);
    }
    
  }
}
       
  
  // END - Borrowed Code


  void sensor_data()
  {
    
  digitalWrite(trigPin, LOW);  // Added this line
  delayMicroseconds(2); // Added this line
  digitalWrite(trigPin, HIGH);
//  delayMicroseconds(1000); - Removed this line
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
    distance=max_hight-distance;
    distance=(distance/max_hight)*100;
    
    
    }

Credits

Fanatic Series

Fanatic Series

5 projects • 23 followers
Electrical Engineer, who has worked on numerous HW/SW projects. He has worked as a Lead Researcher, Designer & Developer

Comments