Advertisement
CuriousScientist

Watering system using Arduino and RTC module

Dec 15th, 2019
395
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //This source code belongs to the following tutorial video: https://youtu.be/Qb7H7ubF6DE
  2. //If you used the code, please consider subscribing to my channel:https://www.youtube.com/channel/UCKp1MzuAceJnDqAvsZl_46g
  3. //Thank you!
  4.  
  5. /*Automatic irrigation system
  6.  * Arduino NANO
  7.  * Relay module
  8.  * RTC module
  9.  * 16x2 LCD module
  10.  * 12 V DC water pump (240L/h = 66,67 ml / sec)
  11.  * ON-ON Swtich, resistor for debouncing
  12.  *
  13.  */
  14.  
  15. /*
  16. You can buy the parts and help me by using the following links:
  17. Arduino Nano with expansion board: https://www.banggood.com/custlink/vDGD9KnOHl
  18. 16x2 LCD (I2C): https://www.banggood.com/custlink/KmGGgtP2Lv
  19. DS3231 RTC Module: https://www.banggood.com/custlink/GKDGNrNnmS
  20. 5V Relay for Arduino: https://www.banggood.com/custlink/DKmDQJn4sZ
  21. 2.1 mm DC jack (male-female): https://www.banggood.com/custlink/mmK3bJA4No
  22. 12V 2A Power Adapter: https://www.banggood.com/custlink/Gvmvnrbesb
  23. Submersible Pump: https://www.banggood.com/custlink/vDmmH67p9C
  24. ON-ON Switch: https://www.banggood.com/custlink/mKG3QcAnKB
  25. */
  26.  
  27. #include <Wire.h>
  28. #include <RTClib.h>
  29. #include <LiquidCrystal_I2C.h>
  30. LiquidCrystal_I2C lcd(0x27, 16, 2);
  31. RTC_DS1307 RTC;
  32.  
  33. //--------------------------------
  34. bool newData; //new data on serial
  35. bool watering = false; //status if watering is allowed or not
  36. bool wateredtoday = false;
  37.  
  38. int wateringhour = 9; // water at 9:00 o'clock in the morning (24h time format!)
  39. float wateringlength = 6000; //10 sec default watering (pumping time)
  40.  
  41. const int relaypin = 2;
  42. const int switchpin = 3;
  43. int switchstate = 0;
  44. char receivedCommand;
  45.  
  46. //--------------------------------
  47.  
  48.  
  49. void setup()
  50. {
  51.   //------------------------------------------------------
  52.   lcd.init();                      // initialize the lcd
  53.   lcd.init();
  54.   lcd.backlight();
  55.   //------------------------------------------------------
  56.  
  57.  
  58.   pinMode(relaypin, OUTPUT);
  59.   pinMode(switchpin, INPUT);
  60.  
  61.   Serial.begin(9600);
  62.  
  63.   Wire.begin();
  64.  
  65.   RTC.begin();
  66.  
  67.   //Refreshing time
  68.   //remove "!", compile, upload to refresh the time in the RTC module
  69.   //the time will be set to the time of the compilation
  70.   if (! RTC.isrunning())
  71.   {
  72.     Serial.println("RTC is NOT running!");
  73.     // following line sets the RTC to the date & time this sketch was compiled
  74.     RTC.adjust(DateTime(__DATE__, __TIME__));
  75.   }
  76.  
  77.   Serial.println("Irrigation system.");
  78.  
  79.   //Fancy startup
  80.   lcd.clear();
  81.   lcd.setCursor(0,0); //Defining positon to write from first row,first column .
  82.   lcd.print("Watering System");
  83.   lcd.setCursor(0,1);
  84.   lcd.print("Start in 5..."); //You can write 16 Characters per line .
  85.   delay(1000); //wait 3 sec
  86.   lcd.setCursor(0,1);
  87.   lcd.print("Start in 4..."); //You can write 16 Characters per line .
  88.   delay(1000); //wait 3 sec
  89.   lcd.setCursor(0,1);
  90.   lcd.print("Start in 3..."); //You can write 16 Characters per line .
  91.   delay(1000); //wait 3 sec
  92.   lcd.setCursor(0,1);
  93.   lcd.print("Start in 2..."); //You can write 16 Characters per line .
  94.   delay(1000); //wait 3 sec
  95.   lcd.setCursor(0,1);
  96.   lcd.print("Start in 1..."); //You can write 16 Characters per line .
  97.   delay(1000); //wait 3 sec
  98.  
  99.  
  100. }
  101.  
  102. void loop()
  103. {
  104.  
  105.  printtime();
  106.  checkSerial();
  107.  checktime();
  108.  switchcheck();
  109.  executecommand();
  110.  
  111. }
  112. //----------------------------------------------------------------------------------
  113. void checkSerial()
  114. {
  115.   //First we check if there is data on the serial port, i.e. we sent a command
  116.      if (Serial.available() > 0)
  117.      {
  118.         receivedCommand = Serial.read();
  119.         newData = true;
  120.      }
  121.  
  122.      //if there is a command, then we step inside and go through the possible commands
  123.      if (newData == true) {
  124.  
  125.       if(receivedCommand == 'w') //w = watering
  126.       {
  127.         watering = true;        //change the boolean, so the watering will be ENABLED
  128.       }
  129.  
  130.       if(receivedCommand == 's') //s = stop
  131.       {
  132.          watering = false;    //change the boolean, so the watering will be DISABLED    
  133.       }
  134.  
  135.      
  136.      
  137.     }
  138.     else
  139.     {}
  140.     newData = false;  
  141.  
  142. }
  143. //----------------------------------------------------------------------------------
  144. void executecommand() //execute the commands, i.e. checking which flag is en/dis-abled
  145. {
  146.   if(watering == true)
  147.   {
  148.     //start pump
  149.     digitalWrite(relaypin, HIGH);
  150.    
  151.  
  152.     //------------------------------------------------------
  153.     //---print info on LCD
  154.     lcd.clear();
  155.     lcd.setCursor(0,0); //Defining positon to write from first row,first column .
  156.     lcd.print("Watering");
  157.     lcd.setCursor(0,1);
  158.     lcd.print("Running"); //You can write 16 Characters per line .
  159.     //---Run pump for some time (defined in the beginning of the code as a variable)
  160.     delay(wateringlength);
  161.     //---Shut down pump, set pin to LOW
  162.     digitalWrite(relaypin, LOW);
  163.     //---Clean screen
  164.     lcd.clear();
  165.     //---Disable watering
  166.     watering = false;
  167.     //--Change the value of the bool, so we'll know that the watering had happened
  168.     wateredtoday = true;      
  169.   }
  170.   else
  171.   {
  172.    //do not do anything
  173.   }
  174.   //---------------------------  
  175.  
  176. }
  177.  
  178. //----------------------------------------------------------------------------------
  179.  
  180. void printtime()
  181. {
  182.     //it is just a basic RTC and LCD exercise: reading the date and time and printing it with some formatting
  183.     DateTime now = RTC.now();
  184.     //-----------------
  185.     //--First line
  186.     lcd.clear();
  187.     lcd.setCursor(0,0); //Defining positon to write from first row,first column .
  188.     lcd.print(now.year());
  189.     lcd.setCursor(4,0);
  190.     lcd.print(".");
  191.     lcd.setCursor(5,0);
  192.     lcd.print(now.month());
  193.     lcd.setCursor(7,0);
  194.     lcd.print(".");
  195.     lcd.setCursor(8,0);
  196.     lcd.print(now.day());
  197.     lcd.setCursor(11,0);
  198.     lcd.print("water");
  199.     //-----------------
  200.     //--Second line
  201.     lcd.setCursor(0,1);
  202.     lcd.print(now.hour()); //You can write 16 Characters per line .
  203.     lcd.setCursor(2,1);
  204.     lcd.print(":");
  205.     lcd.setCursor(3,1);
  206.     lcd.print(now.minute());
  207.     lcd.setCursor(5,1);
  208.     lcd.print(":");
  209.     lcd.setCursor(6,1);
  210.     lcd.print(now.second());
  211.     lcd.setCursor(10,1);
  212.     //if the watering had happened, then we print it on the screen.
  213.     if(wateredtoday == true)
  214.     {lcd.print("true");}
  215.     else
  216.     {lcd.print("false");}    
  217.    
  218.     //----------------------------------------
  219.     Serial.print(now.year(), DEC);
  220.     Serial.print('/');
  221.     Serial.print(now.month(), DEC);
  222.     Serial.print('/');
  223.     Serial.print(now.day(), DEC);
  224.     Serial.print(' ');
  225.     Serial.print(now.hour(), DEC);
  226.     Serial.print(':');
  227.     Serial.print(now.minute(), DEC);
  228.     Serial.print(':');
  229.     Serial.print(now.second(), DEC);
  230.     Serial.println();
  231.     delay(1000);
  232. }
  233. //----------------------------------------------------------------------------------
  234.  
  235. void checktime()
  236. {
  237.   //Here, we check the time, so we can see if the watering should be started
  238.   DateTime now = RTC.now();  
  239.  
  240.   //after the watering time occured, we switch the variable back to false so when the
  241.   //time occurs again (i.e. it is 9:00 in the morning) the watering will be started again
  242.   if(now.hour() == (wateringhour +1))
  243.   {    
  244.     //Serial.println("Watered");
  245.     wateredtoday = false;
  246.   }
  247.  
  248.   //If it is time to water, and we haven't watered, we enable watering
  249.   if(now.hour() == wateringhour && wateredtoday == false)
  250.   {
  251.     //Serial.println("Not watered");
  252.     watering = true;  
  253.     wateredtoday = true;
  254.   }
  255.  
  256. }
  257.  
  258. void switchcheck()
  259. {
  260.   //We check the switch which is used to manually run the pump
  261.   //this does not change ANYTHING with the regular, scheduled pumping.
  262.  
  263.   //Serial.println("SwitchCheck");
  264.  
  265.   switchstate = digitalRead(switchpin); //read the pin
  266.  
  267.   if (switchstate == HIGH) {    
  268.  
  269.   //Serial.println("switch is HIGH");
  270.   //Serial.println(digitalRead(switchpin));
  271.  
  272.   digitalWrite(relaypin, HIGH);
  273.    
  274.   } else {
  275.    
  276.     digitalWrite(relaypin, LOW);
  277.     //Serial.println("switch is LOW");
  278.    // Serial.println(digitalRead(switchpin));
  279.  
  280.   }
  281.  
  282. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement