/*
FECHA 05/03/2016
NOMBRE DEL PROYECTO: EasyGrowWeed
VERSIÓN: 2.7 (Para Arduino MEGA)
AUTORES: hiddenotebook, Surbyte, Noter del foro Arduino.cc
USO: Este sketch es un cultivo autónomo avanzado.
Fuente: podeis visitar el tema en arduino.cc aqui http://forum.arduino.cc/index.php?topic=303135.0
Agradecimientos:
En especial por su gran ayuda a colaboradores del foro arduino:
- surbyte, por sus múltiples aportaciones sin duda una gran persona.
- noter, Por hacer posible el uso correcto del LCD y por todos sus aportes.
Licencia:
Este sketch es Libre para mejorar y compartir! Se prohibe su uso con fines comerciales sin autorizacion de los autores. */
// TFT:
#include "Adafruit_GFX.h" // Librerias para pantalla QVGA 2.2 TFT SPI 240*320
#include "Adafruit_ILI9341.h"
// RTC:
#include "RTClib.h" // Cargar Libreria RTC.
#include <Wire.h> // Cargar Libreria comunicacion I2C.
#include "SPI.h" // Cargar Libreria comunicacion SPI.
RTC_DS1307 RTC; // Crear RTC
const char* DayTXT[7]= {"Domingo ", "Lunes ", "Martes ", "Miercoles ","Jueves ", "Viernes ", "Sabado "};
// DHT22
#include "DHT.h" // Cargar Librería DHT.
#define DHTPIN 25 // Seleccionamos el pin en el que se conectará el sensor.
#define DHTTYPE DHT22 // Se selecciona el DHT22(hay otros DHT).
DHT dht(DHTPIN, DHTTYPE); // Variable usada para comunicarse con el sensor DHT22.
// ARDUINO MEGA:
#define TFT_DC 47
#define TFT_CS 48
#define TFT_RST 49
Adafruit_ILI9341 tft = Adafruit_ILI9341 (TFT_CS, TFT_DC, TFT_RST);
// RELAY PUMP1
#define RELAY_ON 0 // Rele encendido en estado 0
#define RELAY_OFF 1 // Rele apagado en estado 1
#define RELAY_1 39 // Rele 1 al pin 39
// END RELAY PUMP1
int SoilSensor; // Variable para sensor humedad en tierra.
int hysterisis = 10; // Margen de riego.
char textBuff[40]; // Buffer Para imprimir datos de sensores en TFT.
unsigned long updateScreen; // Creamos variable para refrecar la pantalla
unsigned long moistureTest; //
// POWER ON/OFF ATX
bool PowerON = false; // Variable booleana para el encendido.
void setup() {
//-------------- POWER ON/OFF ATX -------------------
pinMode(44, OUTPUT); // Para usar un LED de encendido.
pinMode(23, OUTPUT); // Para encender la fuente con un LOW.
digitalWrite(23, HIGH); // Inicia la Fuente apagada.
pinMode(45, INPUT_PULLUP); // Para leer un boton de encendido.
//-------------- RELAY PUMP1 ------------------------
pinMode(RELAY_1, OUTPUT); // define el pin 39 como salida activa rele 1 para la bomba.
//-------------- TFT SETTINGS -----------------------
tft.begin(); // Inicia TFT 2.2"
tft.setRotation(3); // Rotación de pantalla.
tft.setTextWrap(true); // Si el texto no cabe pasarlo a la siguiente linea.
tft.fillScreen(ILI9341_BLACK); // Rellenar la pantalla con fondo negro.
//-------------- RTC --------------------------------
Wire.begin(); // Inicia el puerto I2C.
RTC.begin(); // Inicia la comunicacion con el RTC.
// RTC.adjust(DateTime(2015,010,19, 15,51,00));
//-------------- DHT --------------------------------
dht.begin(); //Se inicia el sensor DHT22.
}
void loop() {
//-------------- POWER ON/OFF ATX -------------------
bool boton = digitalRead(45); // Variale booleana para leer el boton.
if (boton == LOW) // Si pulsamos el boton
{
digitalWrite(23, PowerON); // Como el pin va invertido, lo establecemos antes de cambiar el estado.
PowerON = ! PowerON ; // Ivertimos el estatus de la fuente.
digitalWrite(44, PowerON); // Encender o apagar el LED.
delay(300);
}
//-------------- REFRESH THE SCREEN -------------------
if (millis() - updateScreen > 1000UL) // Refrescamos la pantalla cada segundo
{
display();
updateScreen = millis();
}
if (millis() - moistureTest > 5000UL) // Sondeamos la humedad de la tierra cada 5 segundos
{
moistureSampling();
moistureTest = millis();
}
}
void display() {
//-------------- RTC --------------------------------
DateTime now = RTC.now(); // Obtiene la fecha y hora del RTC.
//-------------- DHT --------------------------------
int h = dht.readHumidity(); // Lee la humedad DHT22.
int t = dht.readTemperature(); // Lee la temperatura DHT22.
// ------------- SOIL SENSOR ------------------------
SoilSensor = analogRead(A15);
SoilSensor = map (SoilSensor, 0, 1023, 100, 0);
//-------------- TFT DISPLAY ----------------------
//(header)
// DAY ON TEXT:
tft.setTextColor(ILI9341_GREEN, ILI9341_BLACK);
tft.setTextSize(2);
tft.setCursor(0, 0);
tft.print(DayTXT[now.dayOfWeek()]);
tft.println(" ");
tft.setCursor(160, 0);
tft.println("EasyGrowWeed");
// DATE:
char DateBuffer[16];
sprintf(DateBuffer, "%02d:%02d:%02d", now.day(), now.month(), now.year());
tft.print(DateBuffer);
tft.setCursor(160, 19);
// ATX STATE
tft.print("ATX: ");
if (PowerON == LOW)
{
tft.print("OFF");
tft.println(" ");
}
else
{
tft.print("ON");
tft.println(" ");
}
// CLOCK RTC:
char ClockBuffer[16]; // Creamos myBuffer para mostrar bien los dijitos del 1 al 9.
sprintf(ClockBuffer, "%02d:%02d", now.hour(), now.minute());
tft.setTextSize(3);
tft.println(ClockBuffer);
// DATA:
tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
tft.setTextSize(2);
tft.println(" ");
tft.println("Datos del ambiente:");
tft.println(" ");
//(body)
// DHT:
tft.setTextSize(2);
tft.print("Humedad aire: ");
sprintf(textBuff, "%3d", h);
tft.print(textBuff);
tft.println("%");
tft.println(" ");
tft.print("Temperatura aire:");
sprintf(textBuff, "%3d", t);
tft.print(textBuff);
tft.print((char)247);
tft.println("C");
tft.println(" ");
//(footer)
// REFERENCES:
tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
tft.setTextSize(1);
tft.setCursor(15, 220);
tft.print("http://forum.arduino.cc/index.php?topic=303135.0");
//-------------- END TFT ----------------------------
}
void moistureSampling (){
SoilSensor = analogRead(A15); // Leemos sensor de suelo, entrada analogica pin A15:
SoilSensor = map (SoilSensor, 0, 1023, 100, 0); // Mapeamos el valor del sensor de 0 a 100.
if (SoilSensor < 85) // Si el sensor detecta menos de 80%H
{
digitalWrite(RELAY_1, RELAY_ON); // Activamos rele y bomba
}
if (SoilSensor > (85 + hysterisis))// Si detecta mas de 90%H
{
digitalWrite(RELAY_1, RELAY_OFF); // Apaga el rele y la bomba
}
// SOILSENSOR:
tft.setTextColor(ILI9341_CYAN, ILI9341_BLACK);
tft.setCursor(0, 170);
tft.setTextSize(2);
tft.print("Agua en Sustrato:");
sprintf(textBuff, "%3d", SoilSensor);
tft.print(textBuff);
tft.println("%");
tft.println(" ");
}