Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: **Water Control**
- - Source Code NOT compiled for: ESP8266 NodeMCU V1.0
- - Source Code created on: 2024-12-18 21:21:59
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* O sensor deve medir a distância da água é exibir */
- /* em percentagem no lcd, ligar o relé quando o nível */
- /* estiver no 10% e desligar o relé quando estiver no */
- /* 100%. Controlar remotamente apartir da plata forma */
- /* blynk. */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <Wire.h>
- #include <LCDIC2.h> //https://github.com/offcircuit/LCDIC2
- #include <Relay.h> //https://github.com/rafaelnsantos/Relay
- #include <Adafruit_SSD1306.h>
- #include <ESP8266WiFi.h> // Include ESP8266 WiFi library
- #include <BlynkSimpleEsp8266.h>
- #include <AceButton.h>
- #include <LiquidCrystal_I2C.h> // This is compatible with the LCDIC2 library
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- /***** DEFINITION OF DIGITAL INPUT PINS *****/
- const uint8_t Sensor_HC-SR04_Echo_PIN_D7 = 7;
- #define ECHOPIN D7 //D7 (this line is compatible with the existing pin definition)
- #define TRIGPIN D6 //D6 (this line is compatible with the existing pin definition)
- /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
- const uint8_t Rele_RelayModule_Signal_PIN_D5 = 5;
- const uint8_t Sensor_HC-SR04_Trigger_PIN_D6 = 6;
- #define RelayPin D5 //D5 (this line is compatible with the existing pin definition)
- #define BuzzerPin D3 //D3 (this line is compatible with the existing pin definition)
- #define wifiLed 16 //D0 (this line is compatible, but ensure D0 is available for use)
- /***** DEFINITION OF I2C PINS *****/
- const uint8_t Lcd_LCD1602I2C_I2C_PIN_SDA_D2 = 2;
- const uint8_t Lcd_LCD1602I2C_I2C_PIN_SCL_D1 = 1;
- const uint8_t Lcd_LCD1602I2C_I2C_SLAVE_ADDRESS = 39;
- /***** USER CODE VARIABLES *****/
- char ssid[] = "LC-ESP8266"; //WiFi Name
- char pass[] = "0987654321"; //WiFi Password
- int emptyTankDistance = 60; //Distance when tank is empty
- int fullTankDistance = 35; //Distance when tank is full (must be greater than 25cm)
- int triggerPer = 10; //alarm/pump will start when water level drop below triggerPer
- float duration;
- float distance;
- int waterLevelPer;
- bool toggleBuzzer = HIGH; //Define to remember the toggle state
- bool toggleRelay = true; //Define the toggle state for relay
- bool modeFlag = true; // Define mode flag
- String currMode;
- char auth[] = BLYNK_AUTH_TOKEN; // Ensure BLYNK_AUTH_TOKEN is defined in the USER CODE
- // Button configuration
- ButtonConfig config1;
- AceButton button1(&config1);
- ButtonConfig config2;
- AceButton button2(&config2);
- ButtonConfig config3;
- AceButton button3(&config3);
- BlynkTimer timer;
- void handleEvent1(AceButton*, uint8_t, uint8_t);
- void handleEvent2(AceButton*, uint8_t, uint8_t);
- void handleEvent3(AceButton*, uint8_t, uint8_t);
- void checkBlynkStatus() {
- bool isconnected = Blynk.connected();
- digitalWrite(wifiLed, isconnected ? LOW : HIGH);
- }
- // Function to measure distance
- void measureDistance() {
- digitalWrite(TRIGPIN, LOW);
- delayMicroseconds(2);
- digitalWrite(TRIGPIN, HIGH);
- delayMicroseconds(20);
- digitalWrite(TRIGPIN, LOW);
- duration = pulseIn(ECHOPIN, HIGH);
- distance = ((duration / 2) * 0.343) / 10;
- if (distance > (fullTankDistance - 10) && distance < emptyTankDistance) {
- waterLevelPer = map((int)distance, emptyTankDistance, fullTankDistance, 0, 100);
- Blynk.virtualWrite(VPIN_BUTTON_1, waterLevelPer);
- Blynk.virtualWrite(VPIN_BUTTON_2, (String(distance) + " cm"));
- // Display water level on LCD
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("Nivel Agua: ");
- lcd.print(waterLevelPer);
- lcd.print("%");
- if (waterLevelPer < triggerPer) {
- if (modeFlag) {
- if (!toggleRelay) {
- controlBuzzer(500);
- digitalWrite(RelayPin, HIGH); //turn on relay
- toggleRelay = true;
- Blynk.virtualWrite(VPIN_BUTTON_4, toggleRelay);
- }
- } else {
- if (toggleBuzzer == HIGH) {
- digitalWrite(BuzzerPin, HIGH);
- }
- }
- }
- if (waterLevelPer >= 100) {
- if (modeFlag) {
- if (toggleRelay) {
- digitalWrite(RelayPin, LOW); //turn off relay
- toggleRelay = false;
- Blynk.virtualWrite(VPIN_BUTTON_4, toggleRelay);
- controlBuzzer(500);
- }
- } else {
- if (toggleBuzzer == HIGH) {
- digitalWrite(BuzzerPin, HIGH);
- }
- }
- }
- if (distance > (fullTankDistance + 5) && waterLevelPer > (triggerPer + 5)) {
- toggleBuzzer = HIGH;
- Blynk.virtualWrite(VPIN_BUTTON_5, toggleBuzzer);
- digitalWrite(BuzzerPin, LOW);
- }
- }
- }
- // Function to control the buzzer
- void controlBuzzer(int duration) {
- digitalWrite(BuzzerPin, HIGH);
- delay(duration);
- digitalWrite(BuzzerPin, LOW);
- }
- void setup(void) {
- Serial.begin(115200);
- lcd.begin(); // Initialize LCD
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("nivelagua: ");
- lcd.print(waterLevelPer);
- lcd.print("%");
- pinMode(ECHOPIN, INPUT);
- pinMode(TRIGPIN, OUTPUT);
- pinMode(wifiLed, OUTPUT);
- pinMode(RelayPin, OUTPUT);
- pinMode(BuzzerPin, OUTPUT);
- digitalWrite(wifiLed, HIGH);
- digitalWrite(RelayPin, LOW);
- digitalWrite(BuzzerPin, LOW);
- config1.setEventHandler(button1Handler);
- config2.setEventHandler(button2Handler);
- config3.setEventHandler(button3Handler);
- button1.init(ButtonPin1);
- button2.init(ButtonPin2);
- button3.init(ButtonPin3);
- currMode = modeFlag ? "AUTO" : "MANUAL";
- WiFi.begin(ssid, pass);
- timer.setInterval(2000L, checkBlynkStatus); // check if Blynk server is connected every 2 seconds
- timer.setInterval(1000L, measureDistance); // measure water level every 1 seconds
- Blynk.config(auth);
- delay(1000);
- Blynk.virtualWrite(VPIN_BUTTON_3, modeFlag);
- Blynk.virtualWrite(VPIN_BUTTON_4, toggleRelay);
- Blynk.virtualWrite(VPIN_BUTTON_5, toggleBuzzer);
- delay(500);
- }
- void loop(void) {
- Blynk.run();
- timer.run(); // Initiates SimpleTimer
- button1.check(); //mode change
- button3.check(); //buzzer reset
- if (!modeFlag) {
- button2.check(); //if in manual mode
- }
- }
- void button1Handler(AceButton* button, uint8_t eventType, uint8_t buttonState) {
- switch (eventType) {
- case AceButton::kEventReleased:
- if (modeFlag && toggleRelay) {
- digitalWrite(RelayPin, LOW); //turn off the pump
- toggleRelay = false;
- controlBuzzer(500);
- }
- modeFlag = !modeFlag;
- currMode = modeFlag ? "AUTO" : "MANUAL";
- Blynk.virtualWrite(VPIN_BUTTON_3, modeFlag);
- controlBuzzer(200);
- break;
- }
- }
- void button2Handler(AceButton* button, uint8_t eventType, uint8_t buttonState) {
- switch (eventType) {
- case AceButton::kEventReleased:
- if (toggleRelay) {
- digitalWrite(RelayPin, LOW); //turn off the pump
- toggleRelay = false;
- } else {
- digitalWrite(RelayPin, HIGH); //turn on the pump
- toggleRelay = true;
- }
- Blynk.virtualWrite(VPIN_BUTTON_4, toggleRelay);
- controlBuzzer(500);
- delay(1000);
- break;
- }
- }
- void button3Handler(AceButton* button, uint8_t eventType, uint8_t buttonState) {
- switch (eventType) {
- case AceButton::kEventReleased:
- digitalWrite(BuzzerPin, LOW);
- toggleBuzzer = LOW;
- Blynk.virtualWrite(VPIN_BUTTON_5, toggleBuzzer);
- break;
- }
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement