// COMBINED CODE SETS
/-------------------------------------/
/----------- FILE INFO ---------------/
/-------------------------------------/
// File_Name_No_5_Greenhouse_DUAL_TEMP_HUMIDITY_10232017
// Datalogger with the Arduino Ethernet Shield on MEGA Board
// Reads sensors and writes reading to SD card
/-------------------------------------/
/------ LIBRARIES & DEFINITIONS ------/
/-------------------------------------/
// ----- INCLUDED LIBRARIES -----//
// Include libraries
#include <SD.h>
#include <SPI.h>
#include <DallasTemperature.h>
#include <OneWire.h>
#include <DHT.h>
#include <Adafruit_Sensor.h>
//*********************** FROM CLOCK CODE *************************
//READ REAL TIME CLOCK - RUNS ON UNO AND MEGA BOARD
#include <Wire.h>
#define DS3231_I2C_ADDRESS 0x68
// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
return( (val/1016) + (val%10) );
}
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
return( (val/1610) + (val%16) );
}
//*********************** END FROM CLOCK *********************
//-----DEFINE LOOP INTERVALS -----//
// Systematic Loop Interval - 2 Seconds
const int LOOPINTERVAL = 6000;
// Log Interval - 30 Minutes
// const int LOGINTERVAL = 108000;
// — ONE WIRE SENSORS — //
//Data wire is plugged into pin 3 on the Arduino
#define ONE_WIRE_BUS 3
//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);
//Assign the addresses of your 1-Wire temp sensors.
//See the tutorial on how to obtain these addresses:
//http://www.hacktronics.com/Tutorials/arduino-1-wire-address-finder.html
DeviceAddress insideThermometer = {0x10, 0xC8, 0x5E, 0x21, 0x02, 0x08, 0x00, 0x34 };
DeviceAddress outsideThermometer = { 0x10, 0xB7, 0x6A, 0x21, 0x02, 0x08, 0x00, 0x13 };
// — DHT11 SENSORS — //
#define DHTPIN1 6
#define DHTPIN2 7
#define DHTTYPE DHT11
DHT dhtSensor1(DHTPIN1, DHTTYPE);
DHT dhtSensor2(DHTPIN2, DHTTYPE);
// — GlOBAL VARS — //
float tempC_inside;
float tempF_inside;
float tempC_outside;
float tempF_outside;
float fHumidity1;
float fHumidity2;
/------- from SD Test Code ---------/
const int chipSelect = 4;
File dataFile;
/------- from SD Test Code ---------/
/-----------------------------------/
/--------------SETUP----------------/
/-----------------------------------/
void setup(void)
//*********************** FROM CLOCK CODE *************************
{
Wire.begin();
Serial.begin(9600);
}
void readDS3231time(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
Wire.beginTransmission(DS3231_I2C_ADDRESS);
Wire.write(0); // set DS3231 register pointer to 00h
Wire.endTransmission();
Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
// request seven bytes of data from DS3231 starting from register 00h
*second = bcdToDec(Wire.read() & 0x7f);
*minute = bcdToDec(Wire.read());
*hour = bcdToDec(Wire.read() & 0x3f);
*dayOfWeek = bcdToDec(Wire.read());
*dayOfMonth = bcdToDec(Wire.read());
*month = bcdToDec(Wire.read());
*year = bcdToDec(Wire.read());
}
void displayTime()
{
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
// retrieve data from DS3231
readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,
&year);
Serial.println();
// send it to the serial monitor
Serial.print(hour, DEC);
// convert the byte variable to a decimal number when displayed
Serial.print(":");
if (minute<10)
{
Serial.print(“0”);
}
Serial.print(minute, DEC);
Serial.print(":");
if (second<10)
{
Serial.print(“0”);
}
Serial.print(second, DEC);
Serial.print(" “);
//Serial.print(dayOfMonth, DEC);
Serial.print(month, DEC);
Serial.print(”/");
//Serial.print(month, DEC);
Serial.print(dayOfMonth, DEC);
Serial.print("/");
Serial.print(year, DEC);
Serial.print(" Day of week: ");
switch(dayOfWeek){
case 1:
Serial.println(“Sunday”);
break;
case 2:
Serial.println(“Monday”);
break;
case 3:
Serial.println(“Tuesday”);
break;
case 4:
Serial.println(“Wednesday”);
break;
case 5:
Serial.println(“Thursday”);
break;
case 6:
Serial.println(“Friday”);
break;
case 7:
Serial.println(“Saturday”);
break;
}
}
//*********************** END FROM CLOCK *********************
{
Serial.begin(9600);
delay(250);
Serial.println(“Greenhouse V5”);
// Initialize DHT sensor
dhtSensor1.begin();
dhtSensor2.begin();
/------- from SD Test Code ---------/
{
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.print(“Initializing SD card…”);
// make sure that the default chip select pin is set to
// output, even if you don’t use it:
pinMode(10, OUTPUT);
digitalWrite(10, HIGH);
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println(“Card failed, or not present”);
// don’t do anything more:
while (1) ;
}
Serial.println(“card initialized.”);
// Open up the file we’re going to log to!
dataFile = SD.open(“datalog.txt”, FILE_WRITE);
if (! dataFile) {
Serial.println(“error opening datalog.txt”);
// Wait forever since we cant write data
while (1) ;
}
}
/------- from SD Test Code ---------/
// Start up the library
sensors.begin();
// set the resolution to 10 bit (good enough?)
sensors.setResolution(insideThermometer, 10);
sensors.setResolution(outsideThermometer, 10);
Serial.println(“Setup has Finished”);
}
/-----------------------------------/
/---------------LOOP----------------/
/-----------------------------------/
void loop(void)
{
delay(LOOPINTERVAL);
sensors.requestTemperatures();
// OneWire Sensor - Inside Greenhouse
tempC_inside = sensors.getTempC(insideThermometer);
tempF_inside = DallasTemperature::toFahrenheit(tempC_inside);
if (tempC_inside == -127.00)
{
Serial.print(“Error getting temperature: Inside OneWire”);
}
else
{
Serial.print("Inside Greenhouse temperature is: ");
//Serial.print("C: “);
// Serial.print(tempC_inside);
Serial.print(tempF_inside);
Serial.println(” F ");
}
// OneWire Sensor - Outside Greenhouse
tempC_outside = sensors.getTempC(outsideThermometer);
tempF_outside = DallasTemperature::toFahrenheit(tempC_outside);
if (tempC_outside == -127.00)
{
Serial.print(“Error getting temperature: Outside OneWire”);
}
else
{
Serial.print("Outside Greenhouse temperature is: ");
//Serial.print(“C: “);
//Serial.print(tempC_outside);
Serial.print(tempF_outside);
Serial.println(” F”);
}
// GET DHT SENSOR HUMIDITY READINGS
fHumidity1 = dhtSensor1.readHumidity();
fHumidity2 = dhtSensor2.readHumidity();
Serial.print(“Inside Greehouse Humidity is: “);
Serial.print(fHumidity1);
Serial.print(”%”);
Serial.println();
Serial.print(“Outside Greenhoue Humidity is: “);
Serial.print(fHumidity2);
Serial.print(”%”);
Serial.println();
Serial.println();
/* Write readings to file /
dataFile.print(tempF_inside);
dataFile.print(",");
dataFile.print(tempF_outside);
dataFile.print(",");
dataFile.print(fHumidity1);
dataFile.print(",");
dataFile.println(fHumidity2);
dataFile.flush();
delay(1000);
//****************************** FROM CLOCK CODE ***********************
{
displayTime(); // display the real-time clock data on the Serial Monitor,
delay(1000); // every second
}
//******************************* END FROM CLOCK CODE *******************
}