[code]
#include <EEPROM.h>
#include <DFRobot_PH.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <SimpleDHT.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <SD.h>
#include <SPI.h>
#include "GravityTDS.h"
#include <Ethernet.h>
#include <ThingSpeak.h>
#define ONE_WIRE_BUS 3//////////////////////////////////// Temp sensor D3
#define TdsSensorPin A1 ////////////////////////////////// EC Sensor
LiquidCrystal_I2C lcd(0x27, 20, 4);
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
GravityTDS gravityTds;
int pinDHT11 = 2; //////////////////////////////////////// Temp & Humidity Sensor
SimpleDHT11 dht11(pinDHT11);
float temperature = 25, tdsValue = 0;
float envtemp;
float envhum;
float ctemp;
float CEC;
float CPH;
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 0, 177);
IPAddress myDns(192, 168, 0, 1);
EthernetClient client;
unsigned long myChannelNumber = 1284020;
const char * myWriteAPIKey = "EN78TLXBN2XZCGNT";
///////// Set These Variables ////////////////
float PH_SET = 9.3; // Desired PH Level
float PH_Range = .5; // Allowable Deviation From Desired PH
float EC_SET = 1.1; // Desired EC Level
float EC_Range = .250; // Allowable Deviation From Desired EC
int PH_Up_Dose_Time = 2; /// PH Up Pump Run Time in seconds
int PH_Down_Dose_Time = 2; // PH Down Pump Run Time in seconds
float Ph_Wait_Time = 2; //// Time Delay After PH Adjustment in seconds
float PH_Pump_Wait = 2; // Delay after PH Adjustment
int EC_Dose_Time = 2; // set EC pump dose Run times in seconds
int EC_Wait_time = 2; // set time to wait between dosing
int EC_Pump_Wait = 2; // Time after dosing to allow for mixing
float ectrig = EC_SET - EC_Range;
float uptrig = PH_SET - PH_Range;
float downtrig = PH_SET + PH_Range;
int PHUP = 22;
int PHDOWN = 24;
int EC_A = 7;
int EC_B = 6;
// Code for video 4 (pH sensing - Aquaponics and Hydroponics)
int sensorPin = A3; /////////////////////////////////////////////////////// select the analog input pin for the pH sensor
int sensorValue = 0; // variable to store the value coming from the sensor
float ad7 = 282.0; // change this value to the one on serial monitor when in pH7 buffer
float ad4 = 395.0; // change this value to the one on serial monitor when in pH4 buffer
int Nut_Add;
int Up_Add;
int Down_Add;
///////////////////////////////////////////////////////////////////////////////////////
void setup(void)
{
// start serial port
Serial.begin(9600);
lcd.begin();
Serial.println("Dallas Temperature IC Control Library Demo");
sensors.begin();
// Ethernet.init(10);
// start the Ethernet connection:
Serial.println("Initialize Ethernet with DHCP:");
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
while (true) {
delay(1); // do nothing, no point running without Ethernet hardware
}
}
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable is not connected.");
}
// try to congifure using IP address instead of DHCP:
Ethernet.begin(mac, ip, myDns);
} else {
Serial.print(" DHCP assigned IP ");
Serial.println(Ethernet.localIP());
}
// give the Ethernet shield a second to initialize:
delay(1000);
ThingSpeak.begin(client); // Initialize ThingSpeak
gravityTds.setPin(TdsSensorPin);
gravityTds.setAref(5.0); //reference voltage on ADC, default 5.0V on Arduino UNO
gravityTds.setAdcRange(1024); //1024 for 10bit ADC;4096 for 12bit ADC
gravityTds.begin(); //initialization
pinMode(PHUP, OUTPUT);
pinMode(PHDOWN, OUTPUT);
pinMode(EC_A, OUTPUT);
pinMode(EC_B, OUTPUT);
lcd.setCursor(0, 0);
lcd.print("PH =");
lcd.setCursor(9, 0);
lcd.print("Temp=");
lcd.setCursor(0, 1);
lcd.print("EC =");
lcd.setCursor(0, 2);
lcd.print("Pump ");
lcd.setCursor(5, 2);
lcd.setCursor(0, 3);
lcd.print("Env T=");
lcd.setCursor(6, 3);
lcd.setCursor(13, 3);
lcd.print("H= ");
lcd.setCursor(14, 3);
}
/////////////////////////////////////////////////////////////////////////////////////////
void loop()
{
if (Nut_Add != 0) {
Nut_Add = 0;
}
if (Up_Add != 0) {
Up_Add = 0;
}
if (Down_Add != 0) {
Down_Add = 0;
}
Serial.print(" Add Val ");
Serial.print(Nut_Add); Serial.print(Up_Add); Serial.println(Down_Add);
Get_Temp();
lcd.setCursor(15, 0);
lcd.print(ctemp, 1);
Get_EC();
lcd.setCursor(4, 1);
lcd.print(CEC, 3);
Get_pH();
lcd.setCursor(4, 0);
lcd.print(CPH, 2);
// ph.calibration(phvoltage, phtemperature); // calibration process by Serail CMD
Get_EnvTH();
lcd.setCursor(7, 3);
lcd.print(envtemp, 1);
lcd.setCursor(16, 3);
lcd.print(envhum, 0);
Serial.print("Main Loop Temp ");
Serial.println(ctemp);
Serial.print("Main Loop EC ");
Serial.println(CEC);
Serial.print("Main Loop PH = ");
Serial.println(CPH);
Serial.print("Main Loop Enviroment ");
Serial.print(envtemp); Serial.print(" *F, ");
Serial.print(envhum, 0); Serial.println(" H ");
// if ((CEC <= ectrig))
// {
Nut_Add = 1;
lcd.setCursor(6, 2);
lcd.print("Part A");
Serial.println ("Nut A on");
digitalWrite(EC_A, HIGH);
delay(EC_Dose_Time * 1000);
digitalWrite(EC_A, LOW);
lcd.setCursor(6, 2);
lcd.print("Off ");
Serial.println ("Nut A off");
delay(EC_Wait_time * 1000);
Serial.println("Nut B On ");
digitalWrite(EC_B, HIGH);
lcd.setCursor(6, 2);
lcd.print("Part B");
delay(EC_Dose_Time * 1000);
digitalWrite(EC_B, LOW);
lcd.setCursor(6, 2);
lcd.print("Off ");
Serial.println("Nut B Off ");
delay(EC_Wait_time * 1000);
delay(EC_Pump_Wait * 1000);
// }
//if (CEC >= ectrig) {
// if ((CPH <= uptrig))
// {
Serial.println("Up Pump On ");
Up_Add = 1;
lcd.setCursor(6, 2);
lcd.print("UP ");
digitalWrite(PHUP, HIGH);
delay(PH_Up_Dose_Time * 1000);
digitalWrite(PHUP, LOW);
lcd.setCursor(6, 2);
lcd.print("Off ");
Serial.println("Up Pump Off ");
delay(PH_Pump_Wait * 1000);
// }
// if ((CPH >= downtrig ) )
// {
Serial.println("Down Pump On");
Down_Add = 1;
lcd.setCursor(6, 2);
lcd.print("Down");
digitalWrite(PHDOWN, HIGH);
delay(PH_Down_Dose_Time * 1000);
digitalWrite(PHDOWN, LOW);
lcd.setCursor(6, 2);
lcd.print("Off ");
Serial.println("Down Pump Off ");
delay(PH_Pump_Wait * 1000);
// }
// }
[/code]