Advertisement
Guest User

Untitled

a guest
Jul 17th, 2021
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*MIT License
  2.  
  3. Copyright (c) 2019 DrAsso
  4.  
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11.  
  12. The above copyright notice and this permission notice shall be included in all
  13. copies or substantial portions of the Software.
  14.  
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. SOFTWARE.
  22.  
  23. Note:
  24. N'oubliez pas de :
  25. -Modifier les PIN pour faire correspondre avec votre réalisation
  26. -Mettre à jour l'adresse I2C pour le LCD
  27. -Relire le code pour comprendre et adapter
  28.  
  29. Buy me a drink
  30. Donation XMR: 82cFrUkRanH2qrsx3qTstyGc9SKcb9VVPL4v8iSpQ8NzEbvc6f4wbT1BQrX8ftrGE74ck6tcwzYt31hY4yYRHxcM94FfTFG (buy me a drink)
  31. */
  32.  
  33. #include <Wire.h>
  34. #include <LiquidCrystal_I2C.h>
  35. #include <RTClib.h>
  36. #include <DHT.h>
  37.  
  38. #define TIMEDISPLAYSTART 6 // Heure de démarrage de la sonde moisture
  39. #define TIMEDISPLAYSTOP 23 // Heure d'arret de la sonde moisture
  40. #define DUREEFANJOUR 45 // minutes de fonctionnement par H
  41. #define DUREEFANNUIT 15
  42. #define MOISTUREMIN 1
  43. #define MOISTUREMAX 1024
  44. #define PAUSETIME 1
  45.  
  46. #define I2C_ADDR 0x27 // Adresse I2C LCD
  47. #define LCDWIDTH 16
  48. #define LCDHEIGHT 2
  49.  
  50. #define DHTTYPE DHT22
  51.  
  52. #define VERSIONSOFT "Gardi V0.6rc2"
  53.  
  54.  
  55. //WIRE CONF
  56. const int moistureSensor = A0;
  57. const int buttonOne = 2;
  58. const int relayLamp = 6;
  59. const int relayFAN = 5;
  60. const int powerMoisturePIN = 7;
  61. const int dhtPIN = 8;
  62.  
  63. LiquidCrystal_I2C lcd(I2C_ADDR,LCDWIDTH,LCDHEIGHT);
  64. RTC_DS1307 rtc;
  65. DHT dht(dhtPIN, DHTTYPE);
  66.  
  67. int moisture =0;
  68. int humainMoisture =0;
  69. int stateLamp = 0;
  70. int stateFAN = 0;
  71. int temperature =0;
  72. int humidite = 0;
  73.  
  74. volatile int heureStart = 6;
  75. volatile int heureStop = 23;
  76. //CYCLES: 6-23:59 / 4-23:59 / 6-17:59
  77. unsigned long debouncing_time = 1000; //Debouncing Time in Milliseconds 600:Ok
  78. volatile unsigned long last_micros;
  79.  
  80. void setup() {
  81. // SETUP
  82. //PIN MODE
  83. pinMode (moistureSensor, INPUT);
  84. pinMode (relayLamp, OUTPUT);
  85. pinMode (relayFAN, OUTPUT);
  86. pinMode (buttonOne, INPUT_PULLUP);
  87. pinMode (powerMoisturePIN, OUTPUT);
  88.  
  89. //INTERRUPT
  90. attachInterrupt(digitalPinToInterrupt(buttonOne), changeLightCycle, RISING);
  91.  
  92. //Poweroff the relays
  93. digitalWrite(relayFAN,HIGH);
  94. digitalWrite(relayLamp,HIGH);
  95.  
  96. //Serial
  97. //Serial.begin(9600);
  98.  
  99. //LCD
  100. lcd.init();
  101. lcd.backlight();
  102. lcd.home(); // go home
  103. lcd.setCursor(2,0);
  104. lcd.print(VERSIONSOFT);
  105. lcd.setCursor(4,1);
  106. lcd.print("By DrAsso");
  107. lcd.home();
  108.  
  109. //First moisture analysis
  110. digitalWrite(powerMoisturePIN,HIGH);
  111. delay(1000);delay(1000);delay(1000);delay(1000);delay(1000);delay(1000);
  112. moisture = analogRead(moistureSensor);
  113.  
  114. //DHT
  115. dht.begin();
  116.  
  117. //RTC
  118. if (! rtc.begin()) {
  119.   Serial.println("Couldn't find RTC");
  120.   lcd.clear();
  121.   lcd.print("Error with RTC");
  122.   while (1);
  123. }
  124. if (! rtc.isrunning()) {
  125.     Serial.println("RTC is NOT running!");
  126.     // following line sets the RTC to the date & time this sketch was compiled
  127.     rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  128.     // This line sets the RTC with an explicit date & time, for example to set
  129.     // January 21, 2014 at 3am you would call:
  130.     //rtc.adjust(DateTime(2014, 1, 2, 6, 28, 40));
  131. }
  132. }
  133.  
  134. void loop() {
  135.   // LOOP
  136. DateTime now = rtc.now();
  137. //Serial.print(now.year(), DEC);
  138. //Serial.print('/');
  139. //Serial.print(now.month(), DEC);
  140. //Serial.print('/');
  141. //Serial.print(now.day(), DEC);
  142. //Serial.print(" - ");
  143. //Serial.print(now.hour(), DEC);
  144. //Serial.print(':');
  145. //Serial.print(now.minute(), DEC);
  146. //Serial.print(':');
  147. //Serial.print(now.second(), DEC);
  148. //Serial.println(' ');
  149.  
  150. //CAPTEUR HUMIDE Moisture (DISPLAY)
  151. // On ne laisse pas la sonde alimentée 24/24 car elle ne supporte pas trop... C'est une sonde fragile
  152. if(now.hour() >= TIMEDISPLAYSTART && now.hour() < TIMEDISPLAYSTOP ){
  153.   digitalWrite(powerMoisturePIN,HIGH);
  154.   moisture = analogRead(moistureSensor);
  155. }
  156. else{
  157.   digitalWrite(powerMoisturePIN,LOW);
  158. }
  159.  
  160.  
  161. humainMoisture = map(moisture,MOISTUREMIN,MOISTUREMAX,0,1023);
  162. //Serial.print("Actual Moisture: ");
  163. //Serial.println(moisture);
  164. //Serial.print("Human Moisture: ");
  165. //Serial.print(humainMoisture);
  166. //Serial.println("%");
  167. //Serial.print("Moisture Limit: ");
  168. //Serial.println(moistureLimit);
  169. //Serial.print("Humain Moisture Limit: ");
  170. //Serial.println(map(moistureLimit,MOISTUREMIN,MOISTUREMAX,0,100));
  171.  
  172.  
  173. //VENTILATION
  174. if (now.hour() >= heureStart && now.hour() <= heureStop){
  175.   //JOUR
  176.   if (now.minute() > DUREEFANJOUR){
  177.     //FAN OFF
  178.     if (stateFAN==1){
  179.       last_micros = micros();
  180.     }
  181.     stateFAN = 0;
  182.     Serial.println("FAN OFF");
  183.     digitalWrite(relayFAN,HIGH);
  184.   }
  185.   else{
  186.     //FAN ON
  187.     if (stateFAN==0){
  188.       last_micros = micros();
  189.     }
  190.     stateFAN = 1;
  191.     Serial.println("FAN ON");
  192.     digitalWrite(relayFAN,LOW);
  193.   }
  194. }
  195. else{  
  196.   //NUIT
  197.   if (now.minute() > DUREEFANNUIT){
  198.     //FAN OFF
  199.     if (stateFAN==1){
  200.       last_micros = micros();
  201.     }
  202.     stateFAN = 0;
  203.     Serial.println("FAN OFF");
  204.     digitalWrite(relayFAN,HIGH);
  205.   }
  206.   else{
  207.     //FAN ON
  208.     if (stateFAN==0){
  209.       last_micros = micros();
  210.     }
  211.     stateFAN = 1;
  212.     Serial.println("FAN ON");
  213.     digitalWrite(relayFAN,LOW);
  214.   }
  215. }
  216.  
  217.  
  218.  
  219. //TEMP + HUMIDITE
  220. if (now.second() % 5 == 0){
  221.   Serial.println("Calcul de la T°");
  222.   temperature = dht.readTemperature();
  223.   humidite = dht.readHumidity();
  224. }
  225.  
  226.  
  227. //LUMIERE
  228. if (now.hour() >= heureStart && now.hour() <= heureStop){
  229.   //JOUR
  230.   if (stateLamp==0){
  231.     last_micros = micros();
  232.   }
  233.   digitalWrite(relayLamp,LOW);
  234.   Serial.println("Lampe ON");
  235.   stateLamp = 1;
  236. }
  237. else{  
  238.   //NUIT
  239.   if (stateLamp==1){
  240.     last_micros = micros();
  241.   }
  242. //  Serial.println("Pas de lumière pour le moment");
  243.   digitalWrite(relayLamp,HIGH);
  244. //  Serial.println("Lampe OFF");
  245.   stateLamp = 0;
  246. }
  247.  
  248. //LCD DISPLAY
  249. //Coupure du retroeclerage pendant la nuit - entre minuit et 5h
  250. if(now.hour() >= 0 && now.hour() <= 5){
  251.   lcd.noBacklight();
  252. }
  253. else{
  254.   lcd.backlight();
  255. }
  256.  
  257. //clear screen every 5 minutes
  258. if (now.minute() % 5 == 0 && now.second()==1){
  259.   lcd.clear();
  260. }
  261.  
  262. lcd.home();
  263. //lcd.clear();
  264. lcd.print("T");
  265. lcd.print(temperature);
  266. lcd.print("C H");
  267. lcd.print(humidite);
  268. lcd.print("% W");
  269. lcd.print(humainMoisture);
  270. lcd.setCursor(0,1);
  271. lcd.print("L");
  272. lcd.print(stateLamp);
  273. lcd.print(" F");
  274. lcd.print(stateFAN);
  275. lcd.print(" C");
  276. lcd.print((heureStop+1)-heureStart);
  277. lcd.print(" ");
  278. lcd.print(now.hour());
  279. lcd.print(":");
  280. lcd.print(now.minute());
  281.  
  282. Serial.println("--");
  283. delay(1000*PAUSETIME);
  284.  
  285. }
  286.  
  287. void changeLightCycle(){
  288.  if((unsigned long)(micros() - last_micros) >= debouncing_time * 1000) {
  289.   //CYCLES: 6-23:59 / 4-23:59 / 6-17:59
  290.   //Serial.println("LIGHTCYCLE INTERRUPT");
  291.   if (heureStart == 6 && heureStop == 23){
  292.     heureStart = 4;
  293.      //Serial.println("INTERRUPT 1");
  294.      last_micros = micros();
  295.      return;
  296.   }
  297.   if (heureStart == 4 && heureStop == 23){
  298.     heureStart = 6;
  299.     heureStop = 17;
  300.      //Serial.println("INTERRUPT 2");
  301.      last_micros = micros();
  302.      return;
  303.   }
  304.   if (heureStart == 6 && heureStop == 17){
  305.     heureStop = 23;
  306.      //Serial.println("INTERRUPT 3");
  307.      last_micros = micros();
  308.      return;
  309.     }
  310. }
  311. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement