Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*MIT License
- Copyright (c) 2019 DrAsso
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
- The above copyright notice and this permission notice shall be included in all
- copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- SOFTWARE.
- Note:
- N'oubliez pas de :
- -Modifier les PIN pour faire correspondre avec votre réalisation
- -Mettre à jour l'adresse I2C pour le LCD
- -Relire le code pour comprendre et adapter
- Buy me a drink
- Donation XMR: 82cFrUkRanH2qrsx3qTstyGc9SKcb9VVPL4v8iSpQ8NzEbvc6f4wbT1BQrX8ftrGE74ck6tcwzYt31hY4yYRHxcM94FfTFG (buy me a drink)
- */
- #include <Wire.h>
- #include <LiquidCrystal_I2C.h>
- #include <RTClib.h>
- #include <DHT.h>
- #define TIMEDISPLAYSTART 6 // Heure de démarrage de la sonde moisture
- #define TIMEDISPLAYSTOP 23 // Heure d'arret de la sonde moisture
- #define DUREEFANJOUR 45 // minutes de fonctionnement par H
- #define DUREEFANNUIT 15
- #define MOISTUREMIN 1
- #define MOISTUREMAX 1024
- #define PAUSETIME 1
- #define I2C_ADDR 0x27 // Adresse I2C LCD
- #define LCDWIDTH 16
- #define LCDHEIGHT 2
- #define DHTTYPE DHT22
- #define VERSIONSOFT "Gardi V0.6rc2"
- //WIRE CONF
- const int moistureSensor = A0;
- const int buttonOne = 2;
- const int relayLamp = 6;
- const int relayFAN = 5;
- const int powerMoisturePIN = 7;
- const int dhtPIN = 8;
- LiquidCrystal_I2C lcd(I2C_ADDR,LCDWIDTH,LCDHEIGHT);
- RTC_DS1307 rtc;
- DHT dht(dhtPIN, DHTTYPE);
- int moisture =0;
- int humainMoisture =0;
- int stateLamp = 0;
- int stateFAN = 0;
- int temperature =0;
- int humidite = 0;
- volatile int heureStart = 6;
- volatile int heureStop = 23;
- //CYCLES: 6-23:59 / 4-23:59 / 6-17:59
- unsigned long debouncing_time = 1000; //Debouncing Time in Milliseconds 600:Ok
- volatile unsigned long last_micros;
- void setup() {
- // SETUP
- //PIN MODE
- pinMode (moistureSensor, INPUT);
- pinMode (relayLamp, OUTPUT);
- pinMode (relayFAN, OUTPUT);
- pinMode (buttonOne, INPUT_PULLUP);
- pinMode (powerMoisturePIN, OUTPUT);
- //INTERRUPT
- attachInterrupt(digitalPinToInterrupt(buttonOne), changeLightCycle, RISING);
- //Poweroff the relays
- digitalWrite(relayFAN,HIGH);
- digitalWrite(relayLamp,HIGH);
- //Serial
- //Serial.begin(9600);
- //LCD
- lcd.init();
- lcd.backlight();
- lcd.home(); // go home
- lcd.setCursor(2,0);
- lcd.print(VERSIONSOFT);
- lcd.setCursor(4,1);
- lcd.print("By DrAsso");
- lcd.home();
- //First moisture analysis
- digitalWrite(powerMoisturePIN,HIGH);
- delay(1000);delay(1000);delay(1000);delay(1000);delay(1000);delay(1000);
- moisture = analogRead(moistureSensor);
- //DHT
- dht.begin();
- //RTC
- if (! rtc.begin()) {
- Serial.println("Couldn't find RTC");
- lcd.clear();
- lcd.print("Error with RTC");
- while (1);
- }
- if (! rtc.isrunning()) {
- Serial.println("RTC is NOT running!");
- // following line sets the RTC to the date & time this sketch was compiled
- rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
- // This line sets the RTC with an explicit date & time, for example to set
- // January 21, 2014 at 3am you would call:
- //rtc.adjust(DateTime(2014, 1, 2, 6, 28, 40));
- }
- }
- void loop() {
- // LOOP
- DateTime now = rtc.now();
- //Serial.print(now.year(), DEC);
- //Serial.print('/');
- //Serial.print(now.month(), DEC);
- //Serial.print('/');
- //Serial.print(now.day(), DEC);
- //Serial.print(" - ");
- //Serial.print(now.hour(), DEC);
- //Serial.print(':');
- //Serial.print(now.minute(), DEC);
- //Serial.print(':');
- //Serial.print(now.second(), DEC);
- //Serial.println(' ');
- //CAPTEUR HUMIDE Moisture (DISPLAY)
- // On ne laisse pas la sonde alimentée 24/24 car elle ne supporte pas trop... C'est une sonde fragile
- if(now.hour() >= TIMEDISPLAYSTART && now.hour() < TIMEDISPLAYSTOP ){
- digitalWrite(powerMoisturePIN,HIGH);
- moisture = analogRead(moistureSensor);
- }
- else{
- digitalWrite(powerMoisturePIN,LOW);
- }
- humainMoisture = map(moisture,MOISTUREMIN,MOISTUREMAX,0,1023);
- //Serial.print("Actual Moisture: ");
- //Serial.println(moisture);
- //Serial.print("Human Moisture: ");
- //Serial.print(humainMoisture);
- //Serial.println("%");
- //Serial.print("Moisture Limit: ");
- //Serial.println(moistureLimit);
- //Serial.print("Humain Moisture Limit: ");
- //Serial.println(map(moistureLimit,MOISTUREMIN,MOISTUREMAX,0,100));
- //VENTILATION
- if (now.hour() >= heureStart && now.hour() <= heureStop){
- //JOUR
- if (now.minute() > DUREEFANJOUR){
- //FAN OFF
- if (stateFAN==1){
- last_micros = micros();
- }
- stateFAN = 0;
- Serial.println("FAN OFF");
- digitalWrite(relayFAN,HIGH);
- }
- else{
- //FAN ON
- if (stateFAN==0){
- last_micros = micros();
- }
- stateFAN = 1;
- Serial.println("FAN ON");
- digitalWrite(relayFAN,LOW);
- }
- }
- else{
- //NUIT
- if (now.minute() > DUREEFANNUIT){
- //FAN OFF
- if (stateFAN==1){
- last_micros = micros();
- }
- stateFAN = 0;
- Serial.println("FAN OFF");
- digitalWrite(relayFAN,HIGH);
- }
- else{
- //FAN ON
- if (stateFAN==0){
- last_micros = micros();
- }
- stateFAN = 1;
- Serial.println("FAN ON");
- digitalWrite(relayFAN,LOW);
- }
- }
- //TEMP + HUMIDITE
- if (now.second() % 5 == 0){
- Serial.println("Calcul de la T°");
- temperature = dht.readTemperature();
- humidite = dht.readHumidity();
- }
- //LUMIERE
- if (now.hour() >= heureStart && now.hour() <= heureStop){
- //JOUR
- if (stateLamp==0){
- last_micros = micros();
- }
- digitalWrite(relayLamp,LOW);
- Serial.println("Lampe ON");
- stateLamp = 1;
- }
- else{
- //NUIT
- if (stateLamp==1){
- last_micros = micros();
- }
- // Serial.println("Pas de lumière pour le moment");
- digitalWrite(relayLamp,HIGH);
- // Serial.println("Lampe OFF");
- stateLamp = 0;
- }
- //LCD DISPLAY
- //Coupure du retroeclerage pendant la nuit - entre minuit et 5h
- if(now.hour() >= 0 && now.hour() <= 5){
- lcd.noBacklight();
- }
- else{
- lcd.backlight();
- }
- //clear screen every 5 minutes
- if (now.minute() % 5 == 0 && now.second()==1){
- lcd.clear();
- }
- lcd.home();
- //lcd.clear();
- lcd.print("T");
- lcd.print(temperature);
- lcd.print("C H");
- lcd.print(humidite);
- lcd.print("% W");
- lcd.print(humainMoisture);
- lcd.setCursor(0,1);
- lcd.print("L");
- lcd.print(stateLamp);
- lcd.print(" F");
- lcd.print(stateFAN);
- lcd.print(" C");
- lcd.print((heureStop+1)-heureStart);
- lcd.print(" ");
- lcd.print(now.hour());
- lcd.print(":");
- lcd.print(now.minute());
- Serial.println("--");
- delay(1000*PAUSETIME);
- }
- void changeLightCycle(){
- if((unsigned long)(micros() - last_micros) >= debouncing_time * 1000) {
- //CYCLES: 6-23:59 / 4-23:59 / 6-17:59
- //Serial.println("LIGHTCYCLE INTERRUPT");
- if (heureStart == 6 && heureStop == 23){
- heureStart = 4;
- //Serial.println("INTERRUPT 1");
- last_micros = micros();
- return;
- }
- if (heureStart == 4 && heureStop == 23){
- heureStart = 6;
- heureStop = 17;
- //Serial.println("INTERRUPT 2");
- last_micros = micros();
- return;
- }
- if (heureStart == 6 && heureStop == 17){
- heureStop = 23;
- //Serial.println("INTERRUPT 3");
- last_micros = micros();
- return;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement