//lierrrrrrrrrrrrr
#define BLYNK_TEMPLATE_ID "TMPLHzIVodY6"
#define BLYNK_DEVICE_NAME "water monitor"
#define BLYNK_AUTH_TOKEN "VrT2DFqZwICq8K7t86O7MvdCa98v2dBp"
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
//Set Water Level Distance in CM
int emptyTankDistance = 150 ; //Distance when tank is empty
int fullTankDistance = 30 ; //Distance when tank is full
//Set trigger value in percentage
int triggerPer = 20 ; //start when water level drop below triggerPer
int triggerPer1 = 10 ; //alarm millis
int buttonState1 = HIGH; // out stop
int buttonState2 = HIGH; //out manual
int buzzerstate = LOW;
int buzzerstate1 = LOW;
int pumpstate = HIGH;
int fullstate = LOW;
#include <Adafruit_SSD1306.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <AceButton.h>
using namespace ace_button;
// Define connections to sensor
#define TRIGPIN 27 //D27
#define ECHOPIN 26 //D26
#define wifiLed 2 //D2
#define ButtonPin1 12 //D12
#define BuzzerPin 13 //D13
#define GreenLed 14 //D14
#define fullled 25 //D14
#define resetout 33 //D14
//Change the virtual pins according the rooms
#define VPIN_BUTTON_1 V1
#define VPIN_BUTTON_2 V2
WidgetLED led1(V0);
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
//millis program
int ledState = LOW; // ledState used to set the LED
unsigned long previousMillis = 0; // will store last time LED was updated
//loww tank buzzer alarm
long OnTime = 30; // milliseconds of on-time
long OffTime = 1500; // milliseconds of off-time
//full tank buzzer alarm
long OnTime1 = 150; // milliseconds of on-time
long OffTime1 = 150; // milliseconds of off-time
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
float duration;
float distance;
int waterLevelPer;
bool toggleBuzzer = HIGH; //Define to remember the toggle state
char auth[] = BLYNK_AUTH_TOKEN;
ButtonConfig config1;
AceButton button1(&config1);
void handleEvent1(AceButton*, uint8_t, uint8_t);
BlynkTimer timer;
void checkBlynkStatus() { // called every 3 seconds by SimpleTimer
bool isconnected = Blynk.connected();
if (isconnected == false) {
//Serial.println("Blynk Not Connected");
digitalWrite(wifiLed, LOW);
}
if (isconnected == true) {
digitalWrite(wifiLed, HIGH);
//Serial.println("Blynk Connected");
}
}
BLYNK_CONNECTED() {
Blynk.syncVirtual(VPIN_BUTTON_1);
Blynk.syncVirtual(VPIN_BUTTON_2);
}
void displayData(int value) {
display.clearDisplay();
display.setTextSize(4);
display.setCursor(8, 2);
display.print(value);
display.print(" ");
display.print("%");
display.display();
}
void measureDistance() {
// Set the trigger pin LOW for 2uS
digitalWrite(TRIGPIN, LOW);
delayMicroseconds(2);
// Set the trigger pin HIGH for 20us to send pulse
digitalWrite(TRIGPIN, HIGH);
delayMicroseconds(20);
// Return the trigger pin to LOW
digitalWrite(TRIGPIN, LOW);
// Measure the width of the incoming pulse
duration = pulseIn(ECHOPIN, HIGH);
// Determine distance from duration
// Use 343 metres per second as speed of sound
// Divide by 1000 as we want millimeters
distance = ((duration / 2) * 0.343) / 10;
if (distance > (fullTankDistance - 10) && distance < emptyTankDistance ) {
waterLevelPer = map((int)distance , emptyTankDistance, fullTankDistance, 0, 100);
displayData(waterLevelPer);
Blynk.virtualWrite(VPIN_BUTTON_1, waterLevelPer);
Blynk.virtualWrite(VPIN_BUTTON_2, (String(distance) + " cm"));
// Print result to serial monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
if (waterLevelPer < triggerPer) {
if (pumpstate == HIGH) {
digitalWrite(GreenLed, HIGH);
led1.on();
}
else{
}
if (waterLevelPer < triggerPer1) {
if (toggleBuzzer == HIGH) {
buzzerstate = HIGH;
}
}
}
if (distance < fullTankDistance) {
led1.off();
Blynk.virtualWrite(V3, 0);
digitalWrite(GreenLed, LOW);
digitalWrite(fullled, HIGH);
fullstate = HIGH;
pumpstate = HIGH;
if (distance < fullTankDistance - 2 ) {
if (toggleBuzzer == HIGH) {
buzzerstate1 = HIGH;
}
}
}
else{
digitalWrite(fullled, LOW);
fullstate = LOW;
}
if (distance > (fullTankDistance + 1) && waterLevelPer > (triggerPer + 2)) {
toggleBuzzer = HIGH;
buzzerstate = LOW;
buzzerstate1 = LOW;
digitalWrite(BuzzerPin, LOW);
}
}
// Delay before repeating measurement
delay(100);
}
BLYNK_WRITE(V4) {
if (param.asInt() == HIGH) {
led1.off();
buttonState1 = LOW; //reset out
toggleBuzzer = LOW;
buzzerstate = LOW;
buzzerstate1 = LOW;
digitalWrite(BuzzerPin, LOW);
pumpstate = LOW;
digitalWrite(GreenLed, LOW);
}
else {
buttonState1 = HIGH;
}
}
BLYNK_WRITE(V3) {
if (param.asInt() == HIGH) {
led1.on();
buttonState2 = LOW;
digitalWrite(BuzzerPin, LOW);
pumpstate = LOW;
digitalWrite(GreenLed, HIGH);
}
else {
buttonState2 = HIGH;
pumpstate = HIGH;
}
}
void setup() {
// Set up serial monitor
Serial.begin(115200);
// Set pinmodes for sensor connections
pinMode(ECHOPIN, INPUT);//sensor
pinMode(TRIGPIN, OUTPUT);//sensor
pinMode(wifiLed, OUTPUT);//wifi d2
pinMode(GreenLed, OUTPUT);//pump indicator
pinMode(BuzzerPin, OUTPUT);//alarm
pinMode(fullled, OUTPUT);//full tank
pinMode(resetout, OUTPUT);
pinMode(ButtonPin1, INPUT_PULLUP);
digitalWrite(wifiLed, LOW);
digitalWrite(GreenLed, LOW);
digitalWrite(BuzzerPin, LOW);
Blynk.virtualWrite(V3, 0);
Blynk.virtualWrite(V4, 0);
config1.setEventHandler(button1Handler);
button1.init(ButtonPin1);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
delay(1000);
display.setTextSize(1);
display.setTextColor(WHITE);
display.clearDisplay();
WiFi.begin(ssid, pass);
timer.setInterval(2000L, checkBlynkStatus); // check if Blynk server is connected every 2 seconds
Blynk.config(auth);
delay(1000);
inisiasi();
}
void loop() {
Blynk.run();
timer.run(); // Initiates SimpleTimer
button1.check();
measureDistance();
buzzerstateout();
buttonloop();
}
void button1Handler(AceButton* button, uint8_t eventType, uint8_t buttonState) {
Serial.println("EVENT1");
switch (eventType) {
case AceButton::kEventReleased:
//Serial.println("kEventReleased");
digitalWrite(BuzzerPin, LOW);
toggleBuzzer = LOW;
buzzerstate = LOW;
buzzerstate1 = LOW;
break;
}
}
// millis loop
void blink() {
// 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(BuzzerPin, ledState); // Update the actual LED
}
else if ((ledState == LOW) && (currentMillis - previousMillis >= OffTime))
{
ledState = HIGH; // turn it on
previousMillis = currentMillis; // Remember the time
digitalWrite(BuzzerPin, ledState); // Update the actual LED
}
}
void blink1() {
// check to see if it's time to change the state of the LED
unsigned long currentMillis = millis();
if ((ledState == HIGH) && (currentMillis - previousMillis >= OnTime1))
{
ledState = LOW; // Turn it off
previousMillis = currentMillis; // Remember the time
digitalWrite(BuzzerPin, ledState); // Update the actual LED
}
else if ((ledState == LOW) && (currentMillis - previousMillis >= OffTime1))
{
ledState = HIGH; // turn it on
previousMillis = currentMillis; // Remember the time
digitalWrite(BuzzerPin, ledState); // Update the actual LED
}
}
void inisiasi() {
digitalWrite(BuzzerPin, HIGH);
delay (100);
digitalWrite(BuzzerPin, LOW);
delay (300);
digitalWrite(BuzzerPin, HIGH);
delay (100);
digitalWrite(BuzzerPin, LOW);
}
void buzzerstateout() {
if (buzzerstate == HIGH) {
blink();
}
if (buzzerstate1 == HIGH) {
blink1();
}
}
void buttonloop() {
if (buttonState1 == LOW) {//state stop on
Serial.println("LIER");
}
else{
Serial.println("LIER2222");
}
//button on auto
if (buttonState2 == LOW ) {
Serial.println("KONTOL");
}
//off manual start
else{
Serial.println("KONTOL2222");
}
}