#include "etherShield.h"
#include "ETHER_28J60.h"
//#include "OneWire.h"
#include "DHT.h"
#include "Wire.h"
#include "LiquidCrystal.h"
#define DHTPIN1 A0
//#define DHTPIN2 A1
#define DHTTYPE DHT11
DHT dht1(DHTPIN1, DHTTYPE);
//DHT dht2(DHTPIN2, DHTTYPE);
const int fanPin = 2;
const int valvePin = 3;
const int ventPin = 4;
const int WindowMotorSource1 = 5;
const int WindowMotorSource2 = 6;
const int WindowMotorSink1 = 7;
const int WindowMotorSink2 = 8;
//const int DS18S20_Pin = 9;
const int ethernet_cs = 10;
//const int MOSI = 11;
//const int MISO = 12
//const int SCK = 13;
//DHTPIN A0
const int photocellPin = A3;
int photocellReading;
long interval = 3000;
long previousMillis = 0;
long mistTime = 7000;
long ventTime = 30000;
long fanTime = 300000;
long previousMistTime = 0;
long previousFanTime = 0;
long previousMotorRuntime = 0;
long previousVentTime = 0;
byte originalState = 0;
byte Misted = 1;
byte notMisted = 0;
byte notAirCirculated = 0;
byte AirCirculated = 1;
byte Vented = 1;
byte notVented = 1;
byte windowMotorOpeningState = 0;
byte windowMotorClosingState = 0;
byte windowState = 0;
int repetitionCounter = 0;
int Repetitions = 100; // 3 sec rep. cycle * X = time between mistings
const int DaylightThreshold= 100;
LiquidCrystal lcd(0);
static uint8_t mac[6] = {
0x54, 0x55, 0x58, 0x10, 0xA0, 0x24}; // this just needs to be unique for your network,
static uint8_t ip[4] = {
192, 168, 1, 15}; // IP address for the webserver
static uint16_t port = 80; // Use port 80 - the standard for HTTP
//OneWire ds(DS18S20_Pin);
ETHER_28J60 e;
void setup()
{
Serial.begin(9600);
Serial.println(F("arduino greenhouse"));
dht1.begin();
lcd.begin(16, 2);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
//pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
e.setup(mac, ip, port);
}
void loop()
{
processClient();
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
checkHumidty();
checkTemp();
printConditions();
repetitionCounter++;
}
checkMistingTime();
checkFanTime();
checkVentingTime();
checkWindowMotorRuntime();
checkWindowVentState();
//Serial.println(repetitionCounter);
}
void processClient()
{
digitalWrite(10, LOW);
char* params;
if (params = e.serviceRequest())
{
e.print("<h1>Greenhouse</h1>");
e.print("
");
e.print("<h3><a href='/?valve=off'>Web Remote</a></h1>");
e.print("
");
e.print("Brightness: ");
e.print(photocellReading);
float h = dht1.readHumidity();
float t = dht1.readTemperature();
if (isnan(t) || isnan(h)) {
e.print("Failed to read from DHT");
}
else {
e.print("
");
e.print("Humidity: ");
e.print(h);
e.print(" Temperature: ");
e.print(t);
e.print("
");
e.print("Time since last misting cycle: ");
int timeSince = 3000 * repetitionCounter;
e.print(timeSince);
e.print("
");
if (windowState == 1)
{
e.print("Window vent is OPEN.");
}
else
{
e.print("Window vent is closed.");
}
}
e.print("
");
e.print("<button type='button' onclick=location.href='?valve=off'>VALVE</button>");
e.print("<button type='button' onclick=location.href='?fan=off'>FAN</button>");
if (strcmp(params, "?valve=on") == 0)
{
digitalWrite(valvePin, HIGH);
e.print("<a href='?valve=off'><button style='border: 1px solid #ff0000; border-left: 10px solid #ff0000' type='button'>VALVE IS ON</button></a>");
}
else if (strcmp(params, "?valve=off") == 0)
{
digitalWrite(valvePin, LOW);
e.print("<a href='?valve=on'><button style='border: 1px solid #000; border-left: 10px solid #000' type='button'>VALVE IS OFF</button></a>");
}
else if (strcmp(params, "?fan=on") == 0)
{
digitalWrite(fanPin, HIGH);
e.print("<a href='?fan=off'><button style='border: 1px solid #ff0000; border-left: 10px solid #ff0000' type='button'>FAN IS ON</button></a>");
}
else if (strcmp(params, "?fan=off") == 0)
{
digitalWrite(fanPin, LOW);
e.print("<a href='?fan=on'><button style='border: 1px solid #000; border-left: 10px solid #000' type='button'>FAN IS OFF</button></a>");
}
e.respond();
}
}
void checkHumidty()
{ Serial.println(F("humidity check"));
photocellReading = analogRead(photocellPin);
Serial.println(photocellReading);
if(photocellReading > DaylightThreshold)
{
if (repetitionCounter > Repetitions)
{
float h = dht1.readHumidity();
Serial.println(h);
if (h < 50)
{
digitalWrite (fanPin, HIGH);
digitalWrite (valvePin, HIGH);
//Serial.println ("humidifying");
notAirCirculated++;
notMisted++;
previousMistTime = millis();
previousFanTime = millis();
repetitionCounter = 0;
}
}
else
{
// Serial.println("not enough repetitions");
}
}
else
{
// Serial.println("not daytime");
}
}
void checkTemp()
{
Serial.println(F("Temp check"));
float t = dht1.readTemperature();
Serial.println(t);
if (t > 27)
{
digitalWrite (valvePin, HIGH);
digitalWrite (fanPin, HIGH);
notAirCirculated = 1;
notMisted = 1;
}
if (t > 33 && windowState == 0)
{
digitalWrite (WindowMotorSource1, HIGH);
digitalWrite (WindowMotorSink1, HIGH);
unsigned long currentMotorRuntime = millis();
windowMotorOpeningState = 1;
windowState = 1;
digitalWrite (ventPin, HIGH);
notVented = 1;
}
if (t > 33 && windowState == 1)
{
if (notVented == 0)
{
digitalWrite (ventPin, HIGH);
notVented = 1;
}
}
}
void printConditions()
{
lcd.clear();
float h = dht1.readHumidity();
float t = dht1.readTemperature();
if (isnan(t) || isnan(h))
{
//Serial.println("Failed to read from DHT");
}
else
{
lcd.print(F("Hum: "));
lcd.print(h);
lcd.setCursor(0, 1);
lcd.print(F("Temp: "));
lcd.print(t);
}
}
void checkMistingTime()
{
if (notMisted == Misted)
{
unsigned long currentMistTime = millis();
if (currentMistTime - previousMistTime > mistTime )
{
previousMistTime = currentMistTime;
digitalWrite (valvePin, LOW);
notMisted = originalState;
}
}
}
void checkFanTime()
{
if (notAirCirculated == AirCirculated)
{
unsigned long currentFanTime = millis();
if(currentFanTime - previousFanTime > fanTime)
{
previousFanTime = currentFanTime;
digitalWrite (fanPin, LOW);
notAirCirculated = originalState;
}
}
}
void checkVentingTime()
{
if (notVented == Vented)
{
unsigned long currentVentTime = millis();
if (currentVentTime - previousVentTime > ventTime)
{
previousVentTime = currentVentTime;
digitalWrite (ventPin, LOW);
notVented == originalState;
}
}
}
void checkWindowMotorRuntime()
{
unsigned long currentMotorRuntime = millis();
if (windowMotorOpeningState == 1)
{
if(currentMotorRuntime - previousMotorRuntime > interval)
{
previousMotorRuntime = currentMotorRuntime;
digitalWrite (WindowMotorSource1, LOW);
digitalWrite (WindowMotorSink1, LOW);
windowMotorOpeningState = originalState;
}
}
if (windowMotorClosingState == 1)
{
if(currentMotorRuntime - previousMotorRuntime > interval)
{
previousMotorRuntime = currentMotorRuntime;
digitalWrite (WindowMotorSource2, LOW);
digitalWrite (WindowMotorSink2, LOW);
windowMotorClosingState = originalState;
}
}
}
void checkWindowVentState()
{
if (windowState == 1)
{
long timeSince = repetitionCounter * 3000;
if (timeSince > fanTime)
{
float t = dht1.readTemperature();
if (t < 33)
{
digitalWrite (WindowMotorSource2, HIGH);
digitalWrite (WindowMotorSink2, HIGH);
windowState = originalState;
windowMotorClosingState == 1;
}
}
}
}