Here is the code :
// Growroom controller
// 18 hr growing cycle (18hrs on 6hrs off)
// lights on at 13.00 lights off at 5.00
// Conditions wanting to achieve:
// 18 hrs on:
// Temperature = 21 - 27 C (Ideal 26 C) setPointonTemp = 26
// Humidity = 40 % setPointonHumid = 40
// Nutrient heat = 19 - 21 C (Ideal 20 C) setPointonHeat = 20
// 6 hrs off:
// Temperature = 18 - 22 C (Ideal 20 C) setPointoffTemp = 20
// Humidity = 40 % setPointoffHumid = 40
// Nutrient heat = 19 - 21 C (Ideal 20 C) setPointoffHeat = 20
// Grow light = digital pin 6
// Grow room heater = digital pin 7
// Grow room humidifier = digital pin 8
// Grow room dehumidifier = digital pin 9
// Grow room nutrient tank heater = digital pin 10
// Grow room intake and exhaust fans = digital pin 11
// Sensors used and pin :
// DHT-22 to read temp and humidity of grow room = digital pin 5
// 1xDS18b20 to read the the temp of nutrients = digital pin 3 (one wire bus) addresss { 0x28, 0xA0, 0xCF, 0xF8, 0x04, 0x00, 0x00, 0xFC }
// 1xds18b20 to read the outside temp of growroom = digital pin 3 (one wire bus) { 0x28, 0xE8, 0x74, 0xB3, 0x05, 0x00, 0x00, 0xC8 }
#include "DHT.h"
#include <SPI.h>
#include <Ethernet.h>
#include <HttpClient.h>
#include <Xively.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <RTClib.h>
#include <Wire.h>
#include <Time.h>
#include <TimeAlarms.h>
RTC_DS1307 RTC;
// Start Time
int sHour = 13; // start time of growing cycle
int sMinute = 00;
int sSecond = 0;
// End Time
int eHour = 05; // end time of growing cycle
int eMinute = 00;
int eSecond = 0;
const int lightRelay = 6;
const int heaterRelay = 7;
const int humidifierRelay = 8;
const int dehumidifierRelay = 9;
const int nutrientheaterRelay = 10;
const int fanRelay = 11;
#define DHTPIN 5 // PIN data of DHT22
#define DHTTYPE DHT22 // DHT 22 (AM2302)
#define ONE_WIRE_BUS 3
DHT dht(DHTPIN, DHTTYPE);
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
// The address of the DS18B20
DeviceAddress TANK_TEMP = { 0x28, 0xA0, 0xCF, 0xF8, 0x04, 0x00, 0x00, 0xFC };
DeviceAddress OUTSIDE_TEMP = { 0x28, 0xE8, 0x74, 0xB3, 0x05, 0x00, 0x00, 0xC8 };
// Set some setpoints via perfect Conditions to allow slight deviation when turning on relays
float setPointonTemp = 26;
float setPointonHumid = 40;
float setPointonHeat = 20;
float setPointoffTemp = 20;
float setPointoffHumid = 40;
float setPointoffHeat = 20;
void setup(void)
{
// Set the relay to off immediately
digitalWrite(lightRelay, HIGH);
// Start serial
Serial.begin(9600);
// Instantiate the RTC
Wire.begin();
RTC.begin();
// Set the pinmode
pinMode(lightRelay, OUTPUT);
pinMode(heaterRelay, OUTPUT);
pinMode(heaterRelay,OUTPUT);
pinMode(humidifierRelay,OUTPUT);
pinMode(dehumidifierRelay,OUTPUT);
pinMode(nutrientheaterRelay,OUTPUT);
pinMode(fanRelay,OUTPUT);
// Check if the RTC is running.
if (! RTC.isrunning()) {
Serial.println("RTC is NOT running");
}
// Get time from RTC
DateTime current = RTC.now();
DateTime compiled = DateTime(__DATE__, __TIME__);
if (current.unixtime() < compiled.unixtime()) {
Serial.println("RTC is older than compile time! Updating");
RTC.adjust(DateTime(__DATE__, __TIME__));
}
// Use RTC time to set the start time
setTime(sHour, sMinute, sSecond, current.day(), current.month(), current.year());
time_t s = now();
// Use RTC time to set the end time
setTime(eHour, eMinute, eSecond, current.day(), current.month(), current.year());
time_t e = now();
// Use RTC time to set the current time
setTime(current.hour(), current.minute(), current.second(), current.day(), current.month(), current.year());
time_t n = now();
// Test if grow light should be on
if (s <= n && n <= e) {
digitalWrite(lightRelay, LOW); // Sets the grow light "on"
}
Alarm.alarmRepeat(sHour, sMinute, sSecond, LightOn);
Alarm.alarmRepeat(eHour, eMinute, eSecond, LightOff);
// Start up the library
sensors.begin();
// Set the resolution to 10 bit (good enough?)
sensors.setResolution(TANK_TEMP, 10);
sensors.setResolution(OUTSIDE_TEMP, 10);
dht.begin();
Serial.println("Setup Complete.");
}
void printTemperature(DeviceAddress deviceAddress)
{
float tempC = sensors.getTempC(deviceAddress);
if (tempC == -127.00) {
Serial.print("Error getting temperature");
} else {
Serial.print("C: ");
Serial.print(tempC);
Serial.print(" F: ");
Serial.print(DallasTemperature::toFahrenheit(tempC));
}
}
void loop(void)
{
// Get the current time
DateTime now = RTC.now();
setTime(now.hour(), now.minute(), now.second(), now.day(), now.month(), now.year());
// Display the current time
Serial.print("Current time: ");
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();
float h = dht.readHumidity(); // Value of Temp
float t = dht.readTemperature(); // Value of Humidity
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" *C");
delay(2000);
Serial.print("Getting temperature...\n\r");
sensors.requestTemperatures();
Serial.print("TEMP OF TANK is: ");
printTemperature(TANK_TEMP);
Serial.print("\n\r");
delay(2000);
Serial.print("TEMP OUTSIDE is: ");
printTemperature(OUTSIDE_TEMP);
Serial.print("\n\r");
Alarm.delay(2000);
}
void LightOn() {
Serial.println("Turning Light On");
digitalWrite (lightRelay, HIGH);
float h = dht.readHumidity(); // Value of Temp
float t = dht.readTemperature(); // Value of Humidity
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" *C");
if (h < setPointonHumid)
{
digitalWrite(humidifierRelay,HIGH);
digitalWrite(fanRelay,LOW);
} else if (h == setPointonHumid +3) {
digitalWrite(humidifierRelay,LOW);
digitalWrite(fanRelay,HIGH);
if (t < setPointonTemp)
{
digitalWrite(heaterRelay,HIGH);
digitalWrite(fanRelay,LOW);
} else if (t == setPointonTemp +3) {
digitalWrite(heaterRelay,LOW);
digitalWrite(fanRelay,HIGH);
}
Serial.print("Getting temperature...\n\r");
sensors.requestTemperatures();
Serial.print("TEMP OF TANK is: ");
printTemperature(TANK_TEMP);
Serial.print("\n");
delay(2000);
Serial.print("TEMP OUTSIDE is: ");
printTemperature(OUTSIDE_TEMP);
Serial.print("\n");
digitalWrite(lightRelay, LOW);
Alarm.delay(2000);
}
}
void LightOff() {
Serial.println("Turning Light Off");
digitalWrite (lightRelay, LOW);
float h = dht.readHumidity(); // Value of Temp
float t = dht.readTemperature(); // Value of Humidity
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" *C");
if (h < setPointoffHumid)
{
digitalWrite(humidifierRelay,HIGH);
digitalWrite(fanRelay,LOW);
} else if (h == setPointoffHumid +3) {
digitalWrite(humidifierRelay,LOW);
digitalWrite(fanRelay,HIGH);
if (t < setPointoffTemp)
{
digitalWrite(heaterRelay,HIGH);
digitalWrite(fanRelay,LOW);
} else if (t == setPointoffTemp +3) {
digitalWrite(heaterRelay,LOW);
digitalWrite(fanRelay,HIGH);
}
Serial.print("Getting temperature...\n\r");
sensors.requestTemperatures();
Serial.print("TEMP OF TANK is: ");
printTemperature(TANK_TEMP);
Serial.print("\n");
delay(2000);
Serial.print("TEMP OUTSIDE is: ");
printTemperature(OUTSIDE_TEMP);
Serial.print("\n");
digitalWrite(lightRelay, LOW);
Alarm.delay(2000);
}
Serial.print("Getting temperature...\n\r");
sensors.requestTemperatures();
Serial.print("TEMP OF TANK is: ");
printTemperature(TANK_TEMP);
Serial.print("\n\r");
delay(2000);
Serial.print("TEMP OUTSIDE is: ");
printTemperature(OUTSIDE_TEMP);
Serial.print("\n\r");
Alarm.delay(2000);
digitalWrite(lightRelay, HIGH);
}