// #include <WebServer.h>
// #include <WiFiMulti.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Ultrasonic.h>
// WiFiMulti wifiMulti;
// WebServer server(80);
// // Set your Static IP address
// IPAddress local_IP(192, 168, 1, 184);
// // Set your Gateway IP address
// IPAddress gateway(192, 168, 1, 1);
// IPAddress subnet(255, 255, 0, 0);
#define echopin 12 // ESP32 pin GPIO12 connected to Ultrasonic Sensor's ECHO pin
#define trigpin 14 // ESP32 pin GPIO14 connected to Ultrasonic Sensor's TRIG pin
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define CM_TO_INCH 0.393701
#define cwat 0.0163871
#define ccm 0.001
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
float cm, ctnks, itnks, tper ;
int rred = 0; //red led red sts
int yred = 0; //yel led red sts
int gred = 0; //grn led red sts
long itnkl = 70 ;
long itnkb = 51 ;
long itnkh = 46 ;
long itnklvl;
long itnkf = (itnkl * itnkb * itnkh) * cwat;
long ctnkl = itnkl * 2.54 ;
long ctnkb = itnkb * 2.54 ;
long ctnkh = itnkh * 2.54 ;
long ctnklvl;
long ctnkf = (ctnkl * ctnkb * ctnkh) * ccm;
//String Sinch;
//String Sper;
//String Sltr;
class Flasher
{
// Class Member Variables
// These are initialized at startup
int ledPin; // the number of the LED pin
long OnTime; // milliseconds of on-time
long OffTime; // milliseconds of off-time
// These maintain the current state
int ledState; // ledState used to set the LED
unsigned long previousMillis; // will store last time LED was updated
// Constructor - creates a Flasher
// and initializes the member variables and state
public:
Flasher(int pin, long on, long off)
{
ledPin = pin;
pinMode(ledPin, OUTPUT);
OnTime = on;
OffTime = off;
ledState = LOW;
previousMillis = 0;
}
void Update() {
// check to see if it's time to change the state of the LED
unsigned long currentMillis = millis();
if ((ledState == HIGH) && (currentMillis - previousMillis >= OnTime))
{
ledState = LOW; // Turn it off
previousMillis = currentMillis; // Remember the time
digitalWrite(ledPin, ledState); // Update the actual LED
}
else if ((ledState == LOW) && (currentMillis - previousMillis >= OffTime))
{
ledState = HIGH; // turn it on
previousMillis = currentMillis; // Remember the time
digitalWrite(ledPin, ledState); // Update the actual LED
}
}
};
// class Buzer
// {
// // Class Member Variables
// // These are initialized at startup
// int buzPin; // the number of the LED pin
// long OnTime; // milliseconds of on-time
// long OffTime; // milliseconds of off-time
// // These maintain the current state
// int buzState; // ledState used to set the LED
// unsigned long previousMillis; // will store last time LED was updated
// // Constructor - creates a Flasher
// // and initializes the member variables and state
// public:
// Buzer(int pin, long on, long off)
// {
// buzPin = pin;
// pinMode(buzPin, OUTPUT);
// OnTime = on;
// OffTime = off;
// buzState = LOW;
// previousMillis = 0;
// }
// void Update()
// {
// // check to see if it's time to change the state of the LED
// unsigned long currentMillis = millis();
// if ((buzState == HIGH) && (currentMillis - previousMillis >= OnTime))
// {
// buzState = LOW; // Turn it off
// previousMillis = currentMillis; // Remember the time
// //noTone(13);
// }
// else if ((buzState == LOW) && (currentMillis - previousMillis >= OffTime))
// {
// buzState = HIGH; // turn it on
// previousMillis = currentMillis; // Remember the time
// //tone(buzPin, 1000);
// }
// }
// };
//Flasher ledro(5, 1000, 0);
//Flasher ledrf(5, 0, 1000);
Flasher ledrb(18, 400, 300);
//Flasher ledyo(18, 1000, 0);
//Flasher ledyf(18, 0, 1000);
Flasher ledyb(19, 350, 350);
//Flasher ledgo(19, 1000, 0);
//Flasher ledgf(19, 0, 1000);
Flasher ledgb(4, 350, 350);
// Buzer buz0(13, 1000, 15000);
// Buzer buz1(13, 1000, 10000);
// Buzer buz2(13, 50, 5000);
// Buzer buz3(13, 100, 500);
// Buzer buz4(13, 50, 50);
// Buzer buz5(13, 0, 200000);
Ultrasonic ultrasonic(trigpin, echopin);
int distance;
void setup() {
Serial.begin(115200);
delay(100);
// wifiMulti.addAP("stars", "sureshbabu123");
// wifiMulti.addAP("Airtel_sure_6677", "air06648");
// wifiMulti.addAP("Infinix mb", "876594321");
// configure the trigger pin to output mode
pinMode(trigpin, OUTPUT);
// configure the echo pin to input mode
pinMode(echopin, INPUT);
oled.begin(SSD1306_SWITCHCAPVCC, 0x3C); //initialize with the I2C addr 0x3C (128x64)
oled.clearDisplay();
delay(2000); // wait two seconds for initializing
oled.clearDisplay(); // clear display
// if (!WiFi.config(local_IP, gateway, subnet)) {
// Serial.println("STA Failed to configure");
// }
// Serial.println("Connecting to ");
// // initialize OLED display with I2C address 0x3C
// //Serial.println(ssid);
// // //connect to your local wi-fi network
// // WiFi.begin(ssid, password);
// if (wifiMulti.run() == WL_CONNECTED) {
// Serial.println("");
// Serial.println("WiFi connected");
// Serial.println("IP address: ");
// Serial.println(WiFi.localIP());
// oled.clearDisplay(); // clear display
// oled.setTextSize(2); // set text size
// oled.setTextColor(WHITE); // set text color
// oled.setCursor(10, 30); // set position to display
// //oled.drawString(0,0, ssid);
// oled.println("chk");
// oled.display();
// // delay(8000);
// }
// server.on("/", handle_OnConnect);
// server.onNotFound(handle_NotFound);
// server.begin();
// Serial.println("HTTP server started");
}
void loop() {
// // generate 10-microsecond pulse to TRIG pin
// digitalWrite(trigpin, HIGH);
// delayMicroseconds(10);
// digitalWrite(trigpin, LOW);
// // measure duration of pulse from ECHO pin
// duration = pulseIn(echopin, HIGH);
// // calculate the distance
cm = ultrasonic.read(CM);
ctnklvl = cm * ccm;
ctnks = ((ctnkh - ctnklvl) * ctnkl * ctnkb ) * ccm;
//tnks = ((tnkh * 2.54 - cm) * tnkl * tnkb) * ccm;
tper = (ctnks / ctnkf) * 100;
Serial.print("distance inch: ");
Serial.print(ctnklvl);
Serial.print(" distance cm: ");
Serial.print(cm);
//server.handleClient();
oled.clearDisplay(); // clear display
oled.setTextSize(2); // set text size
oled.setTextColor(WHITE); // set text color
oled.setCursor(10, 0); // set position to display
//oled.drawString(0,0, ssid);
oled.print("L: ");
oled.println(ctnkl);
oled.setCursor(10, 15); // set position to display
//oled.drawString(0,0, ssid);
oled.print("b: ");
oled.println(ctnkb);
oled.setCursor(10, 30); // set position to display
//oled.drawString(0,0, ssid);
oled.print("h: ");
oled.println(ctnkh);
oled.display();
delay(500);
oled.clearDisplay(); // clear display
oled.setCursor(10, 15);
oled.print("Cap: ");
oled.println(ctnkf);
oled.display();
delay(500);
if (tper <= 15 ) {
ledrb.Update();
digitalWrite(18, LOW);
digitalWrite(19, LOW);
//EasyBuzzer.beep(100, 2);
//buz0.Update();
Serial.println("r");
}
else if (tper > 15 && tper <= 50 ) {
digitalWrite(5, HIGH);
ledyb.Update();
digitalWrite(19, LOW);
//EasyBuzzer.beep(100,2);
//buz1.Update();
Serial.println("w");
}
else if (tper > 50 && tper <= 80 ) {
digitalWrite(5, HIGH);
digitalWrite(18, HIGH);
ledgb.Update();
//buz3.Update();
Serial.println("g");
}
else if (tper > 80 ) {
digitalWrite(5, HIGH);
digitalWrite(18, HIGH);
digitalWrite(19, HIGH);
//buz4.Update();
Serial.println("f");
}
delay(500);
}
// void handle_OnConnect() {
// server.send(200, "text/html", SendHTML(tnklvl, tnkf, tnks, tper,cm));
// delay(1);
// }
// void handle_NotFound() {
// server.send(404, "text/plain", "Not found");
// }
// String SendHTML(float inches, float tnkf, float tnks, float tper,float cm) {
// String ptr = "<!DOCTYPE html> <html>\n";
// ptr += "<meta http-equiv='refresh' content='1'>";
// ptr += "<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
// ptr += "<title>ESP32 Weather Report</title>\n";
// ptr += "<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n";
// ptr += "body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;}\n";
// ptr += "p {font-size: 24px;color: #444444;margin-bottom: 10px;}\n";
// ptr += "</style>\n";
// ptr += "</head>\n";
// ptr += "<body>\n";
// ptr += "<div id=\"webpage\">\n";
// ptr += "<h1>Tank Level</h1>\n";
// ptr += "<p>Distance in cm: ";
// ptr += (float)cm;
// ptr += " CM </p>";
// ptr += "<p>Level: ";
// ptr += (float)inches;
// ptr += " / ";
// ptr += tnkh ;
// ptr += " Inch </p>";
// ptr += "<p>Capcity: " ;
// ptr += (float)tnks ;
// ptr += " / ";
// //ptr +=" Ltrs</p>";
// //ptr +="<p>Stored: ";
// ptr += (float)tnkf;
// ptr += " Ltrs</p>";
// ptr += "<p>Filled : ";
// ptr += (float)tper;
// ptr += " %</p>";
// return ptr;
//}