//built on open source code. All correct actions are theirs, all mistakes are mine.
#define moisture_input 0
#define divider_top 2
#define divider_bottom 3
#include <SD.h>
#include <dht11.h>
#include <Time.h>
#include <Wire.h>
#include <DS1307RTC.h> // a basic DS1307 library that returns time as a time_t
int moisture; // analogical value obtained from the experiment
int SoilMoisture(){
int reading;
// set driver pins to outputs
pinMode(divider_top,OUTPUT);
pinMode(divider_bottom,OUTPUT);
// drive a current through the divider in one direction
digitalWrite(divider_top,LOW);
digitalWrite(divider_bottom,HIGH);
// wait a moment for capacitance effects to settle
delay(1000);
// take a reading
reading=analogRead(moisture_input);
// reverse the current
digitalWrite(divider_top,HIGH);
digitalWrite(divider_bottom,LOW);
// give as much time in 'reverse' as in 'forward'
delay(1000);
// stop the current
digitalWrite(divider_bottom,LOW);
return reading;
}
dht11 DHT11;
const int POW_PIN = 8;
const int CS = 10;
const int LED = 9;
const int chipSelect = 10;
void write_header()
{
// This function creates the GPS.LOG file and writes out the CSV header to it. This is only done if the file does not exist.
// Write out the GPS.LOG file header.
File dataFile = SD.open("widget.csv", FILE_WRITE);
if(dataFile)
{
// Flash the SD card LED three times
for (int x = 0; x < 3; x++)
{
digitalWrite(LED, HIGH);
delay(50);
digitalWrite(LED, LOW);
delay(50);
} // end for loop
Serial.println("Writing data file header");
// Write the CSV header
dataFile.println("Day,Month,Day,Time,Year,Soil,Humidity,Temp(C),Temp(F),Temp(K),Dew,Dew(fast)");
dataFile.close(); // Close the file
} // end if
} // end function write_header
void setup_card()
{
// This function initializes the connection to the SD card. If there is an error it writes it to the console.
Serial.begin(115200); // Setup the serial monitoring interface for debugging
Serial.println("Initializing Card...");
//Connect to the SD card
if (!SD.begin(CS)) // If there is an error connecting to the SD card display an error and terminate the program
{
Serial.println("Card failure");
return;
} // end if
// Check to see if the GPS.LOG file exists. If it does not then create it and write out the CSV header
if (!SD.exists("widget.csv"))
{
write_header();
} // end if
} // end function setup_card
void setup()
{
Serial.begin(115200);
setSyncProvider(RTC.get); // the function to get the time from the RTC
if(timeStatus()!= timeSet)
Serial.println("Unable to sync with the RTC");
else
Serial.println("RTC has set the system time");
DHT11.attach(5);
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);
// 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:
return;
}
Serial.println("card initialized.");
Serial.println("Time,Day,Month,Year,Soil,Humidity,Temp(C),Temp(F),Temp(K),Dew,Dew(fast)");
}
void loop()
{
moisture=SoilMoisture(); // assign the result of SoilMoisture() to the global variable 'moisture'
// Serial.print("Soil moisture: ");
// Serial.print(moisture); // print the analogical measurement of the experiment
// later i will improve here a calculation for derive Soil Moisture in %
Serial.println();
delay(1000);
digitalClockDisplay();
delay(1000);
int chk = DHT11.read();
String dataString = "";
// read three sensors and append to the string:
for (int analogPin = 0; analogPin < 1; analogPin++) {
int sensor = analogRead(analogPin);
dataString += String(sensor);
if (analogPin < 1) {
dataString += ",";
}
}
File dataFile = SD.open("widget.csv", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
// Write the CSV header
dataFile.println("Time,Soil,Humidity,Temp(F),Temp(C)");
dataFile.print(" ");
dataFile.print((hour()));
dataFile.print(":");
dataFile.print(minute());
dataFile.print(":");
dataFile.print(second());
dataFile.print(",");
dataFile.print(month());
dataFile.print("/");
dataFile.print(day());
dataFile.print("/");
dataFile.print(year());
dataFile.print(",");
dataFile.print(moisture);
dataFile.print(",");
dataFile.print((float)DHT11.humidity, DEC);
dataFile.print(",");
dataFile.print(DHT11.fahrenheit(), DEC);
dataFile.print(",");
dataFile.print((float)DHT11.temperature, DEC);
dataFile.println("");
dataFile.close();
// print to the serial port too:
//Serial.println("Soil,Humidity,Temp F,Temp C");
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.print(",");
Serial.print(day());
Serial.print(",");
Serial.print(month());
Serial.print(",");
Serial.print(year());
Serial.print(",");
Serial.print(moisture);
Serial.print(",");
Serial.print((float)DHT11.humidity, DEC);
Serial.print(",");
Serial.print((float)DHT11.temperature, DEC);
Serial.print(",");
Serial.print(DHT11.fahrenheit(), DEC);
Serial.print(",");
Serial.print(DHT11.kelvin(), DEC);
Serial.print(",");
Serial.print(DHT11.dewPoint(), DEC);
Serial.print(",");
Serial.print(DHT11.dewPointFast(), DEC);
Serial.println("");
delay(10000);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening widgets.csv");
} // assign the result of SoilMoisture() to the global variable 'moisture'
// moisture1 = SoilMoisture1();
// print the number of seconds since reset:
delay(1000);
}
void digitalClockDisplay(){
// digital clock display of the time
//Serial.print(hour());
// printDigits(minute());
// printDigits(second());
// Serial.print(" ");
// Serial.print(day());
// Serial.print(" ");
// Serial.print(month());
// Serial.print(" ");
// Serial.print(year());
// Serial.println();
}
void printDigits(int digits){
// utility function for digital clock display: prints preceding colon and leading 0
Serial.print(":");
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}