////////////////////////////////////////////////////////////////////////////////
// Name:       FloraPropagatorUno-01                                          //
// Platform:   Arduino UNO Rev 3                                              //
// Created by: HARB, Januar 2017, GPL copyrights                              //
// http://robotigs.com/robotigs/includes/bots_header.php?idbot=17             //
// Connect your Arduino to your PC and open the serial monitor.               //
////////////////////////////////////////////////////////////////////////////////

//Set system targets -----------------------------------------------------------
float propatemp  = 18.0;                   //Temperature in the propagator +-1dC
float propahours = 14.0; //Number of hours around noon to switch on LED lighting
float propahumid = 00.0;            //Percentage to signal water spraying needed
//Propagating Strawberry Hybrid:      propatemp=24, propahours=16, propahumid=90
//Propagating Strawberry Hybrid Phase2:         Temp=18, Hours=14, propahumid=00
// SET PRECOMPILER OPTIONS *****************************************************

//Define the needed header files for the precompiler, no charge if not used ----
#include <stdio.h>                //http://quadpoint.org/projects/arduino-ds1302
#include <DS1302.h>                            //Copyright (c) 2009, Matt Sparks
#include <DHT.h>                     //DHT22/11 Temperature and humidity sensors

// Define precompiler variables ------------------------------------------------
#define DHTPIN 7                        //What digital pin is DHTXX connected to
#define DHTPIN1 12                      //What digital pin is DHTXX connected to
#define DHTPIN2 A0                      //What digital pin is DHTXX connected to

//#define DHTTYPE DHT11            //DHT 11 Uncomment whatever type you're using
//#define DHTTYPE DHT21                                        //DHT 21 (AM2301)
#define DHTTYPE DHT22                                 //DHT 22  (AM2302), AM2321

DHT dht(DHTPIN, DHTTYPE);                               //Initialize DHT sensor1
DHT dht1(DHTPIN1, DHTTYPE);                             //Initialize DHT sensor2
DHT dht2(DHTPIN2, DHTTYPE);                             //Initialize DHT sensor3

//Declare variables ------------------------------------------------------------
bool ledBinVal   = LOW;             //Choose HIGH=on or LOW=off for on-board LED
bool lightSwitch = HIGH;       //Choose HIGH=off or LOW=on for LED strip, Relay1
bool airSwitch   = HIGH;      //Choose HIGH=off or LOW=on for Air heater, Relay2
bool soilSwitch  = HIGH;  //Set HIGH=off or LOW=on for Propagator heater, Relay3
bool waterSwitch = HIGH;       //Choose HIGH=off or LOW=on for Waterpump, Relay4
int val;                                          //Define numeric variables val
word light;                                            //Read LDR value (0-1023)
word moist;
word water;
float airtemp0;
float humidity0;
float hic0;
float dew0;
float airtemp1;
float humidity1;
float hic1;
float dew1;
float airtemp2;
float humidity2;
float hic2;
float dew2;
char buf[50];
int starthours = 12 - (propahours / 2);         //Used to switch clock switch ON
int finishhours = 12 + (propahours / 2);       //Used to switch clock switch OFF
int currenthour;      //As in timetxt to compare with starthours and finishhours
String timetxt;  //Contains a stamp format without time-zone as defined by PgSql
const int kSclkPin = 2;                       //DS1302  Serial Clock, Yellow CLK
const int kIoPin   = 3;                          //DS1302 Input/Output, Blue DAT
const int kCePin   = 4;                          //DS1302 Chip Enable, Green RST
DS1302 rtc(kCePin, kIoPin, kSclkPin);                   //Create a DS1302 object
//END OF PRECOMPILER OPTIONS ---------------------------------------------------


void setup() { //Setup runs once ***********************************************
  Serial.begin(9600);   //Nothing more needed for the Serial Monitor to function
  pinMode(LED_BUILTIN, OUTPUT);          //Arduino boards contain an onboard LED
  digitalWrite(LED_BUILTIN, LOW);               //Arduino onboard LED switch OFF
  pinMode(2, OUTPUT);                                        //Funny colours LED
  pinMode(5, OUTPUT);                               //Relay1 = LEDstrip lighting
  digitalWrite(5, HIGH);                              //Relay1 LEDstrip HIGH=off
  pinMode(6, OUTPUT);                                //Relay2 = 1000W Air Heater
  digitalWrite(6, HIGH);                    //Relay2 = 1000W Air Heater HIGH=off
  pinMode(8, OUTPUT);                                              //Beepmachine
  pinMode(9, OUTPUT);                              //Tri color LED red, lighting
  pinMode(10, OUTPUT);                           //Tri color LED green, humidity
  pinMode(11, OUTPUT);                             //Tri color LED blue, heating
  pinMode (A1, INPUT);                                 //Define LDR pin as input
  pinMode (A2, OUTPUT);                     //Define Relay3 propagator as output
  digitalWrite(A2, HIGH);                //Relay3 = 35W propagator heater LOW=on
  pinMode (A3, OUTPUT);                      //Define Relay4 waterpump as output
  digitalWrite(A3, HIGH);                          //Relay4 = waterpump HIGH=off
  pinMode (A4, INPUT);                            //Define moisture pin as input
  pinMode (A5, INPUT);                               //Define water pin as input
  analogReference(DEFAULT);           //Option may be different for other boards
  dht.begin();                                                //Start DHT sensor
  dht1.begin();                                               //Start DHT sensor
  dht2.begin();                                               //Start DHT sensor
  rtc.writeProtect(false);                                              //DS1302
  rtc.halt(false);                                                      //DS1302
  //Time t(2017, 01, 19, 21, 13, 20, Time::kThursday);   //UNCOMMENT TO SET TIME
  //rtc.time(t); //Set the time and date on the chip   UNCOMMENT TO SET THE TIME
}//--(end setup )---------------------------------------------------------------


void loop() { //KEEP ON RUNNING THIS LOOP FOREVER ******************************

// 1) FIRST STEP IS TO READ ALL SENSORS ----------------------------------------
  light  = analogRead(A1);                             //Read LDR value (0-1023)
  moist  = analogRead(A4);                                          //Moisture
  water  = analogRead(A5);                                        //Waterlevel
  humidity0 = dht.readHumidity();        //Reading sensor takes 250 milliseconds
  airtemp0 = dht.readTemperature();                //Read temperature as Celsius
  humidity1 = dht1.readHumidity();       //Reading sensor takes 250 milliseconds
  airtemp1 = dht1.readTemperature();                    //Read temperature in °C
  humidity2 = dht2.readHumidity();       //Reading sensor takes 250 milliseconds
  airtemp2 = dht2.readTemperature();                   //Read temperature  in °C
  hic2 = dht2.computeHeatIndex(airtemp2, humidity2, false);   //Heat index in °C
  readTime();        //DS1302 Reading the time and format results into variables
  hic0 = dht.computeHeatIndex(airtemp0, humidity0, false);    //Heat index in °C
  hic1 = dht.computeHeatIndex(airtemp1, humidity1, false);    //Heat index in °C
  hic2 = dht.computeHeatIndex(airtemp2, humidity2, false);    //Heat index in °C
  dew0 = dewPoint(airtemp0, humidity0);               //Calculate dewpoint in °C
  dew1 = dewPoint(airtemp1, humidity1);               //Calculate dewpoint in °C
  dew2 = dewPoint(airtemp2, humidity2);               //Calculate dewpoint in °C

// 2) SECOND STEP IS TO CALCULATE THE DESIRED SETTING OF THE SWITCHES ----------
  calculate_lightswitch();  //Calculate lightSwitch ON or OFF according to clock
  calculate_airheater();                     //Calculate the airSwitch on or off
  calculate_soilheater();                   //Calculate the soilSwitch ON or OFF
  calculate_waterpump();                   //Calculate the waterSwitch on or off

// 3) THIRD STEP IS TO SET THE SWITCHES AS DESIRED -----------------------------
  switch_light();   //Switch the LED lighting on or off according to lightSwitch
  switch_airheater();    //Switch the airheater on or off according to airSwitch
  switch_soil();          //Switch the propagator heater according to soilSwitch
  switch_waterpump();  //Switch the waterpump on or off according to waterSwitch

// 4) LAST STEP IS TO COMMUNICATE TO THE SERIAL PORT AND FINISH LOOP -----------

  //COMMUNICATE TO THE SERIAL PORT ON THE PC -----------------------------------
  Serial.print(light);              //Tese values are caputered by a PHP program
  Serial.print(" ");               //This space is to easily break up the string
  Serial.print(moist);                      //Shows soil moisture (0-1023) 0=dry
  Serial.print(" ");               //This space is to easily break up the string
  Serial.print(water);                      //Shows water level (0-1023) 0=empty
  Serial.print(" ");               //This space is to easily break up the string
  Serial.print(airtemp0, 1);
  Serial.print(" ");               //This space is to easily break up the string
  Serial.print(humidity0, 0);
  Serial.print(" ");               //This space is to easily break up the string
  Serial.print(hic0, 1);
  Serial.print(" ");               //This space is to easily break up the string
  Serial.print(airtemp1, 1);
  Serial.print(" ");               //This space is to easily break up the string
  Serial.print(humidity1, 0);
  Serial.print(" ");               //This space is to easily break up the string
  Serial.print(hic1, 1);
  Serial.print(" ");               //This space is to easily break up the string
  Serial.print(airtemp2, 1);
  Serial.print(" ");               //This space is to easily break up the string
  Serial.print(humidity2, 0);
  Serial.print(" ");               //This space is to easily break up the string
  Serial.print(hic2, 1);
  Serial.print(" ");               //This space is to easily break up the string
  Serial.print(timetxt);
  Serial.print(" ");               //This space is to easily break up the string
  Serial.print(airSwitch);
  Serial.print(" ");               //This space is to easily break up the string
  Serial.print(lightSwitch);
  Serial.print(" ");               //This space is to easily break up the string
  Serial.print(waterSwitch);
  Serial.print(" ");               //This space is to easily break up the string
  Serial.print(soilSwitch);
  Serial.print(" ");               //This space is to easily break up the string
  Serial.print(dew0, 1);
  Serial.print(" ");               //This space is to easily break up the string
  Serial.print(dew1, 1);
  Serial.print(" ");               //This space is to easily break up the string
  Serial.print(dew2, 1);
  Serial.println(" X");                                             //Terminator
  toggle_ledBin();                  //Toggles the default on-board LED on or off
  delay(5000);  //DHT22 No more than 0.5 Hz sampling rate (once every 2 seconds)
} //End of void loop()                       //KEEP ON RUNNING THIS LOOP FOREVER
//345678911234567892123456789312345678941234567895123456789612345678971234567898


void toggle_ledBin(void) { //Toggles the default on-board LED on or off ********
  ledBinVal = !ledBinVal;                                         //Toggle value
  digitalWrite(LED_BUILTIN, ledBinVal);         //Set Arduino boards onboard LED
} //Exit toggle_ledBin ---------------------------------------------------------


void calculate_lightswitch() { //Calculate lightSwitch according to clock ******
  if (currenthour < starthours){
      lightSwitch = HIGH;            //Switch the grow LEDs off, HIGH=off LOW=on
   }
   if (currenthour > finishhours){
      lightSwitch = HIGH;            //Switch the grow LEDs off, HIGH=off LOW=on
   }
   if (currenthour > (starthours-1) && currenthour <(finishhours)){
      lightSwitch = LOW;             //Switch the  grow LEDs on, HIGH=off LOW=on
   }
} //Exit calculate_lightswitch -------------------------------------------------


void calculate_airheater() { //Calculate the airSwitch on or off ***************
   if (airtemp0 < propatemp-2){            //Measurement shows it is way to cold
      if (airtemp0 > 1){   //Meaning temperature sensor is functioning all right
         airSwitch = LOW;                 //Switch the heater on HIGH=off LOW=on
      }else{                 //Airtemp <=1 is assumed to be an measurement error
         airSwitch = HIGH;               //Switch the heater off HIGH=off LOW=on
      }                 //If sensor seems faulty then the heater is switched off
   }                                          //End of if (airtemp0 < propatemp)
   if (airtemp0 > propatemp){             //The heather has reached propatemp +1
      airSwitch = HIGH;                  //Switch the heater off HIGH=off LOW=on
   }                                          //End of if (airtemp0 > propatemp)
} //Exit calculate_airheater ---------------------------------------------------


void calculate_soilheater() { //Calculate the soilSwitch ON or OFF *************
   if (airtemp0 < propatemp){                  //Measurement shows it is to cold
      if (airtemp0 > 1){   //Meaning temperature sensor is functioning all right
         soilSwitch = LOW;                //Switch the heater on HIGH=off LOW=on
      }else{                 //Airtemp <=1 is assumed to be an measurement error
         soilSwitch = HIGH;              //Switch the heater off HIGH=off LOW=on
      }                 //If sensor seems faulty then the heater is switched off
   }                                          //End of if (airtemp0 < propatemp)
   if (airtemp0 > (propatemp)){           //The heather has reached propatemp +1
      soilSwitch = HIGH;                 //Switch the heater off HIGH=off LOW=on
   }                                          //End of if (airtemp0 > propatemp)
} //Exit calculate_soilheater --------------------------------------------------


void calculate_waterpump() { //Calculate the waterSwitch on or off *************
   if (humidity0 < propahumid) {                   //When the air becomes to dry
      waterSwitch = LOW;                   //Switch the pump ON, HIGH=off LOW=on
   }else{                                //Otherwise the soil seems moist enough
      waterSwitch = HIGH;                 //Switch the pump OFF, HIGH=off LOW=on
   }                                           //End of (humidity1 < propahumid)
} //Exit calculate_waterpump ---------------------------------------------------


void switch_waterpump() { //Switch the waterpump according to waterSwitch ******
   if (waterSwitch == LOW){                           //SWITCH THE WATER PUMP ON
      digitalWrite(A3, LOW);        //Switch the pump ON, Relay4 HIGH=off LOW=on
   }else{                                            //SWITCH THE WATER PUMP OFF
      digitalWrite(A3, HIGH);      //Switch the pump OFF, Relay4 HIGH=off LOW=on
   }                              //End of waterSwitch == LOW, set the waterpump
} //Exit switch_waterpump ------------------------------------------------------

void switch_airheater() { //Switch the airheater according to airSwitch ********
  if (airSwitch == LOW){                                  //SWITCH THE HEATER ON
     digitalWrite(5, LOW);        //Switch the heater on, Relay2 HIGH=off LOW=on
  }else{                                                 //SWITCH THE HEATER OFF
     digitalWrite(5, HIGH);          //So switch the heater off, Relay1 HIGH=off
  }                                 //End of airSwitch == LOW, set the airheater
} //Exit switch_airheater ------------------------------------------------------

void switch_light() { //Switch the LED lighting according to lightSwitch *******
    if (lightSwitch == LOW){                   //SWITCH THE LED STRIP GROW LIGHT
      digitalWrite(6, LOW);   //So turn the lighting on, Relay1 HIGH=off, LOW=on
   }else{
      digitalWrite(6, HIGH);       //So switch the lighting off, Relay2 HIGH=off
   } //End of TIME SWITCH, calculate and set the lightSwitch -------------------
} //Exit switch_light ----------------------------------------------------------

void switch_soil() { //Switch the propagator heater according to soilSwitch ****
    if (soilSwitch == LOW){                       //SWITCH THE PROPAGATOR HEATER
      digitalWrite(A2, LOW);//So turn the propagator ON, Relay3 HIGH=off, LOW=on
   }else{
      digitalWrite(A2, HIGH);    //So switch the propagator OFF, Relay3 HIGH=off
   } //End of SOIL SWITCH, Switch the propagator heater ------------------------
} //Exit switch_soil -----------------------------------------------------------




void readTime(){ //DS1302 Reading the time and format results into variables ***
   Time t = rtc.time();            //Get the current time and date from the chip
   snprintf(buf, sizeof(buf), "%04d-%02d-%02d %02d:%02d:%02d",       //Format as
           t.yr, t.mon, t.date,       //Desired by the Pgsql timestamp data type
           t.hr, t.min, t.sec);
   currenthour = t.hr;            //Used to calculate the grow light time switch
   timetxt = buf ;                              //Used to export to the database
} //End of DS1302 Reading the time and format results into variables -----------


double dewPoint(double celsius, double humidity) { //Calculate dewpoint ********
//John Main added dewpoint code from: http://playground.arduino.cc/main/DHT11Lib
//Also added DegC output for Heat Index.
//dewPoint function NOAA
//ref1: http://wahiduddin.net/calc/density_algorithms.htm
//ref2: http://www.colorado.edu/geography/weather_station/Geog_site/about.htm

  // (1) Saturation Vapor Pressure = ESGG(T)
  double RATIO = 373.15 / (273.15 + celsius);
  double RHS = -7.90298 * (RATIO - 1);
  RHS += 5.02808 * log10(RATIO);
  RHS += -1.3816e-7 * (pow(10, (11.344 * (1 - 1 / RATIO ))) - 1) ;
  RHS += 8.1328e-3 * (pow(10, (-3.49149 * (RATIO - 1))) - 1) ;
  RHS += log10(1013.246);
  // factor -3 is to adjust units - Vapor Pressure SVP * humidity
  double VP = pow(10, RHS - 3) * humidity;
  // (2) DEWPOINT = F(Vapor Pressure)
  double T = log(VP / 0.61078);                                      //Temp var
  return (241.88 * T) / (17.558 - T);
} //End of double dewPoint(double celsius, double humidity) --------------------

////////////////////////////////////////////////////////////////////////////////
// PIN ALLOCATIONS TABLE ARDUINO UNO                                          //
// Board -Atmel- PIN - IDE - Function          - Connection               ALT //
//                                                                            //
// CONNECTIONS RAILS TOP LEFT: DIGITAL PWM<~> ******************************* //
// SCL   -  28 - PC5 -19/A5- ADC5/SCL/PCINT13  - White cable to GY-521    TWI //
// SDA   -  27 - PC4 -18/A4- ADC4/SDA/PCINT12  - Yellow cable to GY-521   TWI //
// AREF  -  21 - REF -     - AREF              -                              //
// GND   -  22 - GND -     - GND               -                              //
// 13    -  19 - PB5 -  13 - SCK/PCINT5        - LED_BUILTIN Arduino      SPI //
// 12    -  18 - PB4 -  12 - MISO/PCINT4       - DHT22 Sensor Checkbox1   SPI //
// ~11   -  17 - PB3 -  11 - MOSI/OC2A/PCINT3  - LED 3-color blue         PWM //
// ~10   -  16 - PB2 -  10 - SS/OC1B/PCINT2    - LED 3-color green        PWM //
// ~9    -  15 - PB1 -   9 - OC1A/PCINT1       - LED 3-color red          PWM //
// 8     -  14 - PB0 -   8 - PCINT0/CLK0/ICP1  - Active buzzer            DIO //
//                                                                            //
// CONNECTIONS RAILS TOP RIGHT: DIGITAL PWM<~> ****************************** //
// 7     -  13 - PD7 -   7 - PCINT23/AIN1      - DHT22 Sensor0 SWITCHBOX  DIO //
// ~6    -  12 - PD6 -   6 - PCINT22/OCA0/AIN0 - Relay2 Airheater (Yellow)PWM //
// ~5    -  11 - PD5 -   5 - PCINT21/OC0B/T1   - Relay1 Light (Green)     PWM //
// ~4    -   6 - PD4 -   4 - PCINT20/XCK/T0    - DS1320 RST Green         PWM //
// ~3    -   5 - PD3 -   3 - PCINT19/OC2B/INT1 - DS1320 DAT Blue          INT //
// ~2    -   4 - PD2 -   2 - PCINT18/INT0      - DS1320 CLK Yellow        INT //
// TX->1 -   3 - PD1 -   1 - PCINT17/TXD       - Serial monitor           TXD //
// RX<-0 -   2 - PD0 -   0 - PCINT16/RCD       - Serial Monitor           RCD //
//                                                                            //
// CONNECTIONS RAILS BOTTOM LEFT: POWER ************************************* //
// 5V    -   7 - VCC -      - VCC              - Output to breadboard     VCC //
// RES   -   1 - RES -      - PCINT14/RESET    -                          RES //
// 3.3V  -     -     -     -                   -                              //
// 5V    -     -     -     -                   -                              //
// GND   -     -     -     -                   -                              //
// GND   -     -     -     -                   -                              //
// Vin   -     -     -     -                   -                              //
//                                                                            //
// CONNECTIONS RAILS BOTTOM RIGHT: ANALOG IN ******************************** //
// A0    -  23 - PC0 -A0/14- ADC0/PCINT8       - DHT22 Sensor Checkbox2   ADC //
// A1    -  24 - PC1 -A1/15- ADC1/PCINT9       - LDR                      ADC //
// A2    -  25 - PC2 -A2/16- ADC2/PCINT10      - Relay3 Soilheater (Red)  ADC //
// A3    -  26 - PC3 -A3/17- ADC3/PCINT12      - Relay4 Waterpump (Blank) ADC //
// A4    -  27 - PC4 -A4/18- ADC4/SDA/PCINT12  - Moisture                 TWI //
// A5    -  28 - PC5 -A5/19- ADC5/SCL/PCINT13  - Water level              TWI //
////////////////////////////////////////////////////////////////////////////////
// EEPROM MEMORY MAP:                                                         //
// Start End  Number Description                                              //
// 0000  0000      1 Never use this memory location to be AVR compatible      //
////////////////////////////////////////////////////////////////////////////////
//345678911234567892123456789312345678941234567895123456789612345678971234567898