Advertisement
pleasedontcode

**Water Control** rev_01

Dec 18th, 2024
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: **Water Control**
  13.     - Source Code NOT compiled for: ESP8266 NodeMCU V1.0
  14.     - Source Code created on: 2024-12-18 21:21:59
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* O sensor deve medir a distância da água é exibir */
  21.     /* em percentagem no lcd, ligar o relé quando o nível */
  22.     /* estiver no 10% e desligar o relé quando estiver no */
  23.     /* 100%. Controlar remotamente apartir da plata forma */
  24.     /* blynk. */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /* START CODE */
  28.  
  29. /****** DEFINITION OF LIBRARIES *****/
  30. #include <Wire.h>
  31. #include <LCDIC2.h>   //https://github.com/offcircuit/LCDIC2
  32. #include <Relay.h>    //https://github.com/rafaelnsantos/Relay
  33. #include <Adafruit_SSD1306.h>
  34. #include <ESP8266WiFi.h>        // Include ESP8266 WiFi library
  35. #include <BlynkSimpleEsp8266.h>
  36. #include <AceButton.h>
  37. #include <LiquidCrystal_I2C.h> // This is compatible with the LCDIC2 library
  38.  
  39. /****** FUNCTION PROTOTYPES *****/
  40. void setup(void);
  41. void loop(void);
  42.  
  43. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  44. const uint8_t Sensor_HC-SR04_Echo_PIN_D7        = 7;
  45. #define ECHOPIN    D7  //D7 (this line is compatible with the existing pin definition)
  46. #define TRIGPIN    D6  //D6 (this line is compatible with the existing pin definition)
  47.  
  48. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  49. const uint8_t Rele_RelayModule_Signal_PIN_D5    = 5;
  50. const uint8_t Sensor_HC-SR04_Trigger_PIN_D6      = 6;
  51. #define RelayPin   D5  //D5 (this line is compatible with the existing pin definition)
  52. #define BuzzerPin  D3  //D3 (this line is compatible with the existing pin definition)
  53. #define wifiLed    16  //D0 (this line is compatible, but ensure D0 is available for use)
  54.  
  55. /***** DEFINITION OF I2C PINS *****/
  56. const uint8_t Lcd_LCD1602I2C_I2C_PIN_SDA_D2     = 2;
  57. const uint8_t Lcd_LCD1602I2C_I2C_PIN_SCL_D1     = 1;
  58. const uint8_t Lcd_LCD1602I2C_I2C_SLAVE_ADDRESS   = 39;
  59.  
  60. /***** USER CODE VARIABLES *****/
  61. char ssid[] = "LC-ESP8266";   //WiFi Name
  62. char pass[] = "0987654321";   //WiFi Password
  63. int emptyTankDistance = 60;  //Distance when tank is empty
  64. int fullTankDistance =  35;   //Distance when tank is full (must be greater than 25cm)
  65. int triggerPer =   10;  //alarm/pump will start when water level drop below triggerPer
  66. float duration;
  67. float distance;
  68. int waterLevelPer;
  69. bool toggleBuzzer = HIGH; //Define to remember the toggle state
  70. bool toggleRelay = true; //Define the toggle state for relay
  71. bool modeFlag = true; // Define mode flag
  72. String currMode;
  73. char auth[] = BLYNK_AUTH_TOKEN; // Ensure BLYNK_AUTH_TOKEN is defined in the USER CODE
  74.  
  75. // Button configuration
  76. ButtonConfig config1;
  77. AceButton button1(&config1);
  78. ButtonConfig config2;
  79. AceButton button2(&config2);
  80. ButtonConfig config3;
  81. AceButton button3(&config3);
  82.  
  83. BlynkTimer timer;
  84.  
  85. void handleEvent1(AceButton*, uint8_t, uint8_t);
  86. void handleEvent2(AceButton*, uint8_t, uint8_t);
  87. void handleEvent3(AceButton*, uint8_t, uint8_t);
  88.  
  89. void checkBlynkStatus() {
  90.     bool isconnected = Blynk.connected();
  91.     digitalWrite(wifiLed, isconnected ? LOW : HIGH);
  92. }
  93.  
  94. // Function to measure distance
  95. void measureDistance() {
  96.     digitalWrite(TRIGPIN, LOW);
  97.     delayMicroseconds(2);
  98.     digitalWrite(TRIGPIN, HIGH);
  99.     delayMicroseconds(20);
  100.     digitalWrite(TRIGPIN, LOW);
  101.     duration = pulseIn(ECHOPIN, HIGH);
  102.     distance = ((duration / 2) * 0.343) / 10;
  103.  
  104.     if (distance > (fullTankDistance - 10) && distance < emptyTankDistance) {
  105.         waterLevelPer = map((int)distance, emptyTankDistance, fullTankDistance, 0, 100);
  106.         Blynk.virtualWrite(VPIN_BUTTON_1, waterLevelPer);
  107.         Blynk.virtualWrite(VPIN_BUTTON_2, (String(distance) + " cm"));
  108.  
  109.         // Display water level on LCD
  110.         lcd.clear();
  111.         lcd.setCursor(0, 0);
  112.         lcd.print("Nivel Agua: ");
  113.         lcd.print(waterLevelPer);
  114.         lcd.print("%");
  115.  
  116.         if (waterLevelPer < triggerPer) {
  117.             if (modeFlag) {
  118.                 if (!toggleRelay) {
  119.                     controlBuzzer(500);
  120.                     digitalWrite(RelayPin, HIGH); //turn on relay
  121.                     toggleRelay = true;
  122.                     Blynk.virtualWrite(VPIN_BUTTON_4, toggleRelay);
  123.                 }
  124.             } else {
  125.                 if (toggleBuzzer == HIGH) {
  126.                     digitalWrite(BuzzerPin, HIGH);
  127.                 }
  128.             }
  129.         }
  130.  
  131.         if (waterLevelPer >= 100) {
  132.             if (modeFlag) {
  133.                 if (toggleRelay) {
  134.                     digitalWrite(RelayPin, LOW); //turn off relay
  135.                     toggleRelay = false;
  136.                     Blynk.virtualWrite(VPIN_BUTTON_4, toggleRelay);
  137.                     controlBuzzer(500);
  138.                 }
  139.             } else {
  140.                 if (toggleBuzzer == HIGH) {
  141.                     digitalWrite(BuzzerPin, HIGH);
  142.                 }
  143.             }
  144.         }
  145.  
  146.         if (distance > (fullTankDistance + 5) && waterLevelPer > (triggerPer + 5)) {
  147.             toggleBuzzer = HIGH;
  148.             Blynk.virtualWrite(VPIN_BUTTON_5, toggleBuzzer);
  149.             digitalWrite(BuzzerPin, LOW);
  150.         }
  151.     }
  152. }
  153.  
  154. // Function to control the buzzer
  155. void controlBuzzer(int duration) {
  156.     digitalWrite(BuzzerPin, HIGH);
  157.     delay(duration);
  158.     digitalWrite(BuzzerPin, LOW);
  159. }
  160.  
  161. void setup(void) {
  162.     Serial.begin(115200);
  163.     lcd.begin(); // Initialize LCD
  164.     lcd.clear();
  165.     lcd.setCursor(0, 0);
  166.     lcd.print("nivelagua: ");
  167.     lcd.print(waterLevelPer);
  168.     lcd.print("%");
  169.  
  170.     pinMode(ECHOPIN, INPUT);
  171.     pinMode(TRIGPIN, OUTPUT);
  172.     pinMode(wifiLed, OUTPUT);
  173.     pinMode(RelayPin, OUTPUT);
  174.     pinMode(BuzzerPin, OUTPUT);
  175.     digitalWrite(wifiLed, HIGH);
  176.     digitalWrite(RelayPin, LOW);
  177.     digitalWrite(BuzzerPin, LOW);
  178.    
  179.     config1.setEventHandler(button1Handler);
  180.     config2.setEventHandler(button2Handler);
  181.     config3.setEventHandler(button3Handler);
  182.    
  183.     button1.init(ButtonPin1);
  184.     button2.init(ButtonPin2);
  185.     button3.init(ButtonPin3);
  186.    
  187.     currMode = modeFlag ? "AUTO" : "MANUAL";
  188.    
  189.     WiFi.begin(ssid, pass);
  190.     timer.setInterval(2000L, checkBlynkStatus); // check if Blynk server is connected every 2 seconds
  191.     timer.setInterval(1000L, measureDistance); // measure water level every 1 seconds
  192.     Blynk.config(auth);
  193.     delay(1000);
  194.    
  195.     Blynk.virtualWrite(VPIN_BUTTON_3, modeFlag);
  196.     Blynk.virtualWrite(VPIN_BUTTON_4, toggleRelay);
  197.     Blynk.virtualWrite(VPIN_BUTTON_5, toggleBuzzer);
  198.    
  199.     delay(500);
  200. }
  201.  
  202. void loop(void) {
  203.     Blynk.run();
  204.     timer.run(); // Initiates SimpleTimer
  205.     button1.check(); //mode change
  206.     button3.check(); //buzzer reset
  207.     if (!modeFlag) {
  208.         button2.check(); //if in manual mode
  209.     }
  210. }
  211.  
  212. void button1Handler(AceButton* button, uint8_t eventType, uint8_t buttonState) {
  213.     switch (eventType) {
  214.         case AceButton::kEventReleased:
  215.             if (modeFlag && toggleRelay) {
  216.                 digitalWrite(RelayPin, LOW);  //turn off the pump
  217.                 toggleRelay = false;
  218.                 controlBuzzer(500);
  219.             }
  220.             modeFlag = !modeFlag;
  221.             currMode = modeFlag ? "AUTO" : "MANUAL";
  222.             Blynk.virtualWrite(VPIN_BUTTON_3, modeFlag);
  223.             controlBuzzer(200);
  224.             break;
  225.     }
  226. }
  227.  
  228. void button2Handler(AceButton* button, uint8_t eventType, uint8_t buttonState) {
  229.     switch (eventType) {
  230.         case AceButton::kEventReleased:
  231.             if (toggleRelay) {
  232.                 digitalWrite(RelayPin, LOW);  //turn off the pump
  233.                 toggleRelay = false;
  234.             } else {
  235.                 digitalWrite(RelayPin, HIGH);  //turn on the pump
  236.                 toggleRelay = true;
  237.             }
  238.             Blynk.virtualWrite(VPIN_BUTTON_4, toggleRelay);
  239.             controlBuzzer(500);
  240.             delay(1000);
  241.             break;
  242.     }
  243. }
  244.  
  245. void button3Handler(AceButton* button, uint8_t eventType, uint8_t buttonState) {
  246.     switch (eventType) {
  247.         case AceButton::kEventReleased:
  248.             digitalWrite(BuzzerPin, LOW);
  249.             toggleBuzzer = LOW;
  250.             Blynk.virtualWrite(VPIN_BUTTON_5, toggleBuzzer);
  251.             break;
  252.     }
  253. }
  254.  
  255. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement