I am building an automatic plant waterer and data logger, however when I view the data from the .CSV file recorded on a sd card, I get nothing. I believe something is wrong with my code. Any help is greatly appreciated.
#include "DHT.h"
#include <SPI.h>
#include <Wire.h>
#include "RTClib.h"
#include "SD.h"
#include <OneWire.h>
#include <DallasTemperature.h>
#define DHTTYPE DHT11
#define ECHO_TO_SERIAL 1 //Sends datalogging to serial if 1, nothing if 0
#define LOG_INTERVAL 30 //milliseconds between entries (2.5 min)
const int dhtPin = 9;
const int chipSelect = 10;
const int enablePin = 7;
const int in1Pin = 8;
const int in2Pin = 6;
const int wateringTime = 300; //Set the watering time (4 min for a start)
const float wateringThreshold = 1000; //Value Above which the garden gets watered
OneWire oneWire(A2);
DallasTemperature sensors(&oneWire);
DHT dht(dhtPin, DHTTYPE);
RTC_DS1307 rtc;
int soilTemp = 0; //Scaled value of soil temp (degrees F)
int soilMoisture = 0; //Scaled value of volumetric water content in soil (percent)
int humidity = 0; //Relative humidity (%)
int airTemp = 0; //Air temp (degrees F)
int heatIndex = 0; //Heat index (degrees F)
int sunlight = 0 ; //Sunlight illumination in lux
bool watering = false;
bool wateredToday = false;
DateTime now;
File logfile;
void error(char *str)
{
Serial.print("error: ");
Serial.println(str);
while(1);
}
void setup() {
//Initialize serial connection
Serial.begin(9600); //Just for testing
Serial.println("Initializing SD card...");
pinMode(chipSelect, OUTPUT); //Pin for writing to SD card
pinMode(in1Pin, OUTPUT);
pinMode(in2Pin, OUTPUT);
pinMode(enablePin, OUTPUT);
digitalWrite(enablePin, LOW);
digitalWrite(in1Pin, LOW);
digitalWrite(in2Pin, LOW);
//Establish connection with DHT sensor
dht.begin();
//Establish connection with Soil Temp sensor
sensors.begin();
//Establish connection with real time clock
Wire.begin();
if (!rtc.begin()) {
logfile.println("RTC failed");
#if ECHO_TO_SERIAL
Serial.println("RTC failed");
#endif //ECHO_TO_SERIAL
}
//Set the time and date on the real time clock if necessary
if (! rtc.isrunning()) {
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
//Check if SD card is present and can be initialized
if (!SD.begin(chipSelect)) {
error("Card failed, or not present");
}
Serial.println("Card initialized.");
// create a new file
char filename[] = "LOGGER00.CSV";
for (uint8_t i = 0; i < 100; i++) {
filename[6] = i/10 + '0';
filename[7] = i%10 + '0';
if (! SD.exists(filename)) {
// only open a new file if it doesn't exist
logfile = SD.open(filename, FILE_WRITE);
break; // leave the loop!
}
}
if (! logfile) {
error("couldnt create file");
}
Serial.print("Logging to: ");
Serial.println(filename);
logfile.println("Date, Time, Soil Temp(C), Air Temp(F), Soil Moisture Content(%), Relative Humidity(%), Heat Index(F) ,Sunlight Illumination, Watering?"); //HEADER
#if ECHO_TO_SERIAL
Serial.println("Date, Time, Soil Temp(C), Air Temp(F), Soil Moisture Content(%), Relative Humidity(%), Heat Index(F) ,Sunlight Illumination, Watering?");
#endif ECHO_TO_SERIAL// attempt to write out the header to the file
now = rtc.now();
}
void loop() {
//delay software
delay((LOG_INTERVAL -1) - (millis() % LOG_INTERVAL));
if (!(now.day()==rtc.now().day())) {
wateredToday = false;
}
now = rtc.now();
logfile.print(now.year(), DEC);
logfile.print("/");
logfile.print(now.month(), DEC);
logfile.print("/");
logfile.print(now.day(), DEC);
logfile.print(",");
logfile.print(" ");
logfile.print(now.hour(), DEC);
logfile.print(":");
logfile.print(now.minute(), DEC);
logfile.print(":");
logfile.print(now.second(), DEC);
logfile.print(",");
logfile.print(" ");
#if ECHO_TO_SERIAL
Serial.print(now.year(), DEC);
Serial.print("/");
Serial.print(now.month(), DEC);
Serial.print("/");
Serial.print(now.day(), DEC);
Serial.print(",");
Serial.print(" ");
Serial.print(now.hour(), DEC);
Serial.print(":");
Serial.print(now.minute(), DEC);
Serial.print(":");
Serial.print(now.second(), DEC);
Serial.print(",");
Serial.print(" ");
#endif
//Collect Variables
sensors.requestTemperatures();
float soilTemp;
soilTemp = sensors.getTempCByIndex(0);
delay(20);
soilMoisture = analogRead(A1);
delay(20);
humidity = dht.readHumidity();
delay(20);
airTemp = dht.readTemperature(true);
delay(20);
heatIndex = dht.computeHeatIndex(airTemp,humidity);
delay(20);
sunlight = analogRead(A0);
delay(20);
//Log variables
logfile.print(soilTemp,2);
logfile.print(",");
logfile.print(airTemp);
logfile.print(",");
logfile.print(soilMoisture);
logfile.print(",");
logfile.print(humidity);
logfile.print(",");
logfile.print(heatIndex);
logfile.print(",");
logfile.print(sunlight);
logfile.print(",");
#if ECHO_TO_SERIAL
Serial.print(soilTemp,2);
Serial.print(",");
Serial.print(airTemp);
Serial.print(",");
Serial.print(soilMoisture);
Serial.print(",");
Serial.print(humidity);
Serial.print(",");
Serial.print(heatIndex);
Serial.print(",");
Serial.print(sunlight);
Serial.print(",");
#endif
if (soilMoisture > wateringThreshold && wateredToday == false) {
//water the garden
digitalWrite(enablePin, HIGH);
digitalWrite(in1Pin, HIGH);
digitalWrite(in2Pin, LOW);
delay(wateringTime);
digitalWrite(in1Pin, LOW);
digitalWrite(in2Pin, LOW);
digitalWrite(enablePin, HIGH);
//record that we're watering
logfile.print("TRUE");
#if ECHO_TO_SERIAL
Serial.print("TRUE");
#endif
wateredToday = true;
}
else {
logfile.print("FALSE");
#if ECHO_TO_SERIAL
Serial.print("FALSE");
#endif
}
logfile.println();
#if ECHO_TO_SERIAL
Serial.println();
#endif
delay(50);
//Write to SD card
logfile.flush();
delay(5000);
}