#include <SHT1x.h> //fan library
#include <math.h>
//general define
char ID = 0;
String CMD = "";
int subID = 0;
bool split = false;
//light
int percent = 0;
int relay1 = 22;
int relay2 = 23;
int relay3 = 24;
int pinBright = 2;
//
//fan - Specify data and clock connections and instantiate SHT1x object
#define dataPin 10
#define clockPin 11
SHT1x sht1x(dataPin, clockPin);
//Inital value
float Temperature = 0.0;
float Humidity = 0.0;
bool fanauto = false;
int relay4 = 25;
int relay5 = 26;
int relay6 = 27;
int relay7 = 28;
//
//water level
bool waterlevelauto = false;
const int trigPin = 6;
const int echoPin = 7;
long duration;
int distance;
int water_max = 15;
int water_level = 0;
int inputWL = 0;
int relay8 = 29;
int relay9 = 30;
//
//air pump
int relay10 = 31;
//
// nutrient pump
bool circularauto = false;
//
//data collection (to monitor)
float temp = 0.0;
float humi = 0.0;
String pH = "";
//
void setup()
{
Serial.begin (9600);
Serial1.begin (9600);
Serial2.begin (9600);
Serial2.begin (9600);
CMD.reserve(20);
pinMode (relay1, OUTPUT);
pinMode (relay2, OUTPUT);
pinMode (relay3, OUTPUT);
pinMode (pinBright, OUTPUT);
pinMode (relay4, OUTPUT);
pinMode (relay5, OUTPUT);
pinMode (relay6, OUTPUT);
pinMode (relay7, OUTPUT);
pinMode (relay8, OUTPUT);
pinMode (relay9, OUTPUT);
pinMode (relay10, OUTPUT);
pinMode (trigPin, OUTPUT);
pinMode (echoPin, INPUT);
}
void loop()
{
temp = sht1x.readTemperatureC();
humi = sht1x.readHumidity();
Round_Temperature(temp);
Serial2.print ('R');
Serial2.print ('\r');
delay (1000);
pH = Serial2.readStringUntil(13);
Serial2.readStringUntil(13);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10); // Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH); // Reads the echoPin, returns the sound wave travel time in microseconds
distance = duration*0.034/2; // Calculating the distance
water_level = water_max-distance;
Serial.print ("Temperature: ");
Serial.print (temp);
Serial.print ("C || ");
Serial.print ("Humidity: ");
Serial.print (humi);
Serial.print ("% || ");
Serial.print ("pH: ");
Serial.print (pH);
Serial.print (" || ");
Serial.print ("Water Level: ");
Serial.print (water_level);
Serial.println ("cm ");
delay (3000);
if (fanauto == true)
{
Temperature = sht1x.readTemperatureC();
Humidity = sht1x.readHumidity();
Round_Temperature(Temperature);
if (Temperature >= 28)
{
digitalWrite (relay7, HIGH);
}
if (Temperature < 28)
{
digitalWrite (relay7, LOW);
}
}
if (waterlevelauto == true)
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10); // Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH); // Reads the echoPin, returns the sound wave travel time in microseconds
distance = duration*0.034/2; // Calculating the distance
water_level = water_max-distance;
if (water_level < inputWL)
{
digitalWrite (relay8, HIGH);
digitalWrite (relay9, LOW);
}
if (water_level > inputWL)
{
digitalWrite (relay8, LOW);
digitalWrite (relay9, HIGH);
}
}
if (circularauto == true)
{
digitalWrite (relay8, HIGH);
digitalWrite (relay9, HIGH);
}
if (circularauto == false)
{
digitalWrite (relay8, LOW);
digitalWrite (relay9, LOW);
}
}
void Round_Temperature(float input_temp)
{
input_temp = input_temp*100;
input_temp = round(input_temp);
input_temp = input_temp*0.01;
}
void serial3Event()
{
while (Serial3.available())
{
char inChar = (char)Serial3.read();//receive one byte
if(inChar==',')
{
split = true;
}
if(split==false)//split char before ','
{
ID = inChar;
}
if(split==true)//split char after ','
{
if(inChar!=',' & inChar!='.')
{
CMD += inChar;
}
}
if (inChar == '.')
{
switch(ID)
{
case 'L': //lights
{
if (CMD == "A") //auto mode
{
digitalWrite (relay1, LOW);
digitalWrite (relay2, LOW);
digitalWrite (relay3, LOW);
}
if (CMD == "S") //switch mode
{
digitalWrite (relay1, HIGH);
digitalWrite (relay2, HIGH);
digitalWrite (relay3, LOW);
}
if (CMD == "RO") //remote mode
{
digitalWrite (relay1, HIGH);
digitalWrite (relay2, LOW);
digitalWrite (relay3, HIGH);
}
if (CMD == "RF")
{
digitalWrite (relay1, HIGH);
digitalWrite (relay2, LOW);
digitalWrite (relay3, LOW);
}
}
break;
case 'B': //brightness
{
percent = CMD.toInt() ;
percent = (percent * 2.25);
analogWrite (pinBright, percent);
}
break;
case 'F': //fan
{
if (CMD == "A")
{
fanauto = true;
}
if (CMD == "S") //Switch Mode
{
digitalWrite (relay4, HIGH);
digitalWrite (relay5, HIGH);
digitalWrite (relay6, LOW);
fanauto = false;
}
if (CMD == "RO")
{
digitalWrite (relay4, HIGH);
digitalWrite (relay5, LOW);
digitalWrite (relay6, HIGH);
fanauto = false;
}
if (CMD == "RF")
{
digitalWrite (relay4, HIGH);
digitalWrite (relay5, LOW);
digitalWrite (relay6, LOW);
fanauto = false;
}
}
break;
case 'N':
{
circularauto = true;
Serial1.print ("D," + CMD);
Serial1.print ('\r');
waterlevelauto = false;
if (CMD == "S")
{
circularauto = false;
}
}
break;
case 'W': //water level
{
inputWL = CMD.toInt ();
if (inputWL >= 5 & inputWL <= 14)
{
waterlevelauto = true;
}
if (inputWL < 5)
{
Serial.println ("Invalid water level: must be between 5 and 14 cm");
digitalWrite (relay8, HIGH);
digitalWrite (relay9, LOW);
}
if (inputWL > 14)
{
Serial.println ("Invalid water level: must be between 5 and 14 cm");
digitalWrite (relay8, LOW);
digitalWrite (relay9, HIGH);
}
}
break;
case 'P': //water pump
{
if (CMD == "O")
{
digitalWrite (relay9, HIGH);
}
if (CMD == "F")
{
digitalWrite (relay9, LOW);
}
}
break;
case 'V': //valve
{
if (CMD == "O")
{
digitalWrite (relay8, HIGH);
}
if (CMD == "F")
{
digitalWrite (relay8, LOW);
}
}
break;
case 'A': //airpump
{
if (CMD == "O")
{
digitalWrite (relay10, HIGH);
}
if (CMD == "F")
{
digitalWrite (relay10, LOW);
}
}
break;
default:
break;
}
split = false; //reset value
ID = 0; //reset value
CMD = ""; //reset value
}
}
}
Comments