// // #include <Wire.h>
// #include <Adafruit_GFX.h>
// #include <Adafruit_SSD1306.h>
// #define TRIG_PIN 2
// #define ECHO_PIN 3
// #define BUZZER_PIN 13
// // #define LED_PIN = 13
// Adafruit_SSD1306 oled (128, 64);
// // Adafruit_SSD1306 oled (128, 64, &Wire, -1);
// const float tank_empty = 42;
// const float tank_full = 12;
// const float tank_volume = tank_empty - tank_full;
// long duration_pulseMicros = 0;
// int pulse_to_cm = 58;
// int pulse_to_inches = 148;
// int distance_cm = 0;
// int water_level = 0;
// void setup() {
// // put your setup code here, to run once:
// Serial.begin(115200);
// pinMode(TRIG_PIN, OUTPUT);
// pinMode(ECHO_PIN, INPUT);
// pinMode(BUZZER_PIN, OUTPUT);
// /*
// Starting OLED Display Module
// */
// if(!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
// Serial.println("Display memory allocation error !!!");
// for(;;);
// // while(1);
// }
// showSplashScreen();
// }
// void showSplashScreen() {
// /*
// Display SplashScreen Animation (once)
// */
// oled.clearDisplay();
// oled.setTextSize(2);
// oled.setTextColor(WHITE);
// oled.setCursor(25, 20);
// oled.println("Welcome");
// oled.display();
// oled.println();
// oled.setTextSize(1);
// for(int i=0; i<=20; i++) {
// oled.print("=");
// oled.display();
// delay(250);
// }
// }
// void loop() {
// // put your main code here, to run repeatedly:
// getWaterLevel();
// displayWaterLevel();
// }
// void getWaterLevel() {
// /*
// Getting data from ultrasonic sensor,
// and calculate water level
// */
// // digitalWrite(TRIG_PIN, LOW);
// // delayMicroseconds(2);
// digitalWrite(TRIG_PIN, HIGH);
// delayMicroseconds(10);
// digitalWrite(TRIG_PIN, LOW);
// duration_pulseMicros = pulseIn(ECHO_PIN, HIGH);
// distance_cm = duration_pulseMicros / pulse_to_cm;
// Serial.println(distance_cm);
// if((distance_cm <= tank_empty) && (distance_cm >= tank_full)) {
// water_level = (tank_empty - distance_cm) / tank_volume * 100;
// } else {
// water_level = 101;
// }
// delay(200);
// }
// void displayWaterLevel() {
// oled.clearDisplay();
// oled.setTextSize(2);
// oled.setTextColor(WHITE);
// oled.setCursor(0, 0);
// if(water_level <= 100) {
// /*
// Display Water Level
// */
// oled.println("Water :");
// oled.println();
// oled.setTextSize(4);
// String tank_detail = String(water_level);
// tank_detail += " %";
// oled.println(tank_detail);
// oled.display();
// } else {
// /*
// Over-Flow Alert
// */
// oled.setCursor(10, 25);
// oled.println("Over-Flow");
// oled.display();
// digitalWrite(BUZZER_PIN, HIGH);
// delay(500);
// oled.clearDisplay();
// oled.display();
// digitalWrite(BUZZER_PIN, LOW);
// delay(100);
// }
// }
// #include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define TRIG_PIN 2
#define ECHO_PIN 3
#define BUZZER_PIN 13
// #define LED_PIN 13
// #define POT_PIN 12
// Wifi Connection
#include <ESP8266WiFi.h>
#define MAIN_SSID "Airtel_Spam"
#define MAIN_PWD "Arsh007$"
#define ESP_SSID "aadi_esp8266"
#define ESP_PWD "Aadi8667"
Adafruit_SSD1306 oled (128, 64);
// Adafruit_SSD1306 oled (128, 64, &Wire, -1);
const float tank_empty = 42;
const float tank_full = 12;
const float tank_volume = tank_empty - tank_full;
long duration_pulseMicros = 0;
int pulse_to_cm = 58;
int pulse_to_inches = 148;
int distance_cm = 0;
int water_level = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
// pinMode(POT_PIN, INPUT);
/*
Starting OLED Display Module
*/
if(!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println("Display memory allocation error !!!");
for(;;);
// while(1);
}
showSplashScreen();
// playTone();
connectWifi();
}
void showSplashScreen() {
/*
Display SplashScreen Animation (once)
*/
oled.clearDisplay();
oled.setTextSize(2);
oled.setTextColor(WHITE);
oled.setCursor(25, 20);
oled.println("Welcome");
oled.display();
oled.println();
oled.setTextSize(1);
for(int i=0; i<=20; i++) {
oled.print("=");
oled.display();
delay(100);
}
}
void playTone() {
}
void connectWifi() {
WiFi.begin(MAIN_SSID, MAIN_PWD);
while(WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println();
Serial.println("IP Address : ");
Serial.println(WiFi.localIP());
Serial.println("Starting ESP WiFi....");
WiFi.softAP(ESP_SSID, ESP_PWD);
Serial.println("IP Address : ");
Serial.println(WiFi.softAPIP());
}
/**
LOOP() Method
*/
void loop() {
// put your main code here, to run repeatedly:
getWaterLevel();
displayWaterLevel();
// getPotData();
}
void getWaterLevel() {
/*
Getting data from ultrasonic sensor,
and calculate water level
*/
// digitalWrite(TRIG_PIN, LOW);
// delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
duration_pulseMicros = pulseIn(ECHO_PIN, HIGH);
distance_cm = duration_pulseMicros / pulse_to_cm;
Serial.println(distance_cm);
if((distance_cm <= tank_empty) && (distance_cm >= tank_full)) {
water_level = (tank_empty - distance_cm) / tank_volume * 100;
} else {
water_level = 101;
}
delay(200);
}
void displayWaterLevel() {
oled.clearDisplay();
oled.setTextSize(2);
oled.setTextColor(WHITE);
oled.setCursor(0, 0);
if(water_level <= 100) {
/*
Display Water Level
*/
oled.println("Water :");
oled.println();
oled.setTextSize(4);
String tank_detail = String(water_level);
tank_detail += " %";
oled.println(tank_detail);
oled.display();
} else {
/*
Over-Flow Alert
*/
oled.setCursor(10, 25);
oled.println("Over-Flow");
oled.display();
// digitalWrite(BUZZER_PIN, HIGH);
tone(BUZZER_PIN, 3000);
delay(500);
oled.clearDisplay();
oled.display();
// digitalWrite(BUZZER_PIN, LOW);
noTone(BUZZER_PIN);
delay(100);
}
}
// void getPotData() {
// Serial.println(analogRead(POT_PIN));
// }