//==========================================================================================================
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
//
// Logger Description Arduino
// D1 SCL A5 (remember to use 4.7K pullup resistor)
// D2 SDA A4 (remember to use 4.7K pullup resistor)
// Also required: GND, 5V, and V(bat)
// V(bat): 2.0 - 3.5V (this is the CR1220 that goes on the back of the RTC/Logger board; "+" side out)
//
// This sketch is based on the one found in this thread: https://forum.arduino.cc/index.php?topic=128928.0...
// with minor modifications by me.
//
// NOTES:
// - When I tried to replace the backup battery with 3.3V from the arduino it didn't work.
// But when I gave it 3V from my external power supply, it did.
// - The module I have, "Wifi D1 Mini" or "Wemos D1 Mini", has a diode and resistor added to the
// circuit to protect against reverse polarity if the battery is inserted the wrong way.
// But that increases the battery drain tremendously. So I removed those components and plan
// to be careful when inserting the battery (positive side outward).
// The problem and the solution are described here: https://sharedinventions.com/?p=663
//
//==========================================================================================================
#include <Arduino.h>
#include "RTClib.h" // Adafruit RTClib version 1.12.5 (and its dependencies). Install this through the library manager in this IDE
#include <SPI.h>
#include <SD.h>
RTC_DS1307 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
int loop_counter;
File dataFile;
void setup ()
{
Serial.begin(57600);
pinMode(10, OUTPUT);
digitalWrite(10, HIGH); //-3- I may have to set Arduino Pin 10 HIGH here to keep the logger selected (slave-select)
Serial.println("Looking for Real Time Clock...");
if (! rtc.begin())
{
Serial.println("Could NOT find RTC");
Serial.flush();
abort();
}
else
{
Serial.println("Found RTC");
}
if (! rtc.isrunning())
{
Serial.println("\nReal Time Clock is NOT running, Setting the time now...");
// When time needs to be set on a new device, or after a power loss, the
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
Serial.println("Time has been set");
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
else
{
Serial.println("\nReal Time Clock is already running - NOT setting the time now.");
}
// See if the card is present and can be initialized
Serial.print("Initializing SD card...");
while(1)
{
if (SD.begin()) break;
Serial.println("Failed to initialize, or card not present. Trying again...");
delay(1000);
}
Serial.println("card initialized.");
loop_counter = 0;
// Open the file. Note that only one file can be open at a time.
// To close this file: dataFile.close();
// FILE_READ: open the file for reading, starting at the BEGINNING of the file.
// FILE_WRITE: open the file for reading and writing, starting at the END of the file.
// The SD card library uses 8.3 format filenames and is case-insensitive.
// So "datalog.txt" is the same as "DATALOG.txt"
Serial.println("Chceck if the file exists ");
if(SD.exists("datalog.txt")){
Serial.println("Of coure, it exists");
}
else{
SD.mkdir("datalog.txt");
Serial.println("The datalog has been created");
}
// Serial.println("Opening datalog.txt...");
// while(1)
// {
// dataFile = SD.open("datalog.txt", FILE_WRITE);
// if (dataFile) break;
// Serial.println("Failed to open datalog.txt. Trying again...");
// delay(1000);
// }
// Serial.println("Opened datalog.txt");
// }
}
void loop (){
// Serial.println("Checking time...");
Serial.flush();
dataFile.flush();
DateTime now = rtc.now();
//Serial.println("Got the time");
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" | ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
dataFile.print(now.year(), DEC);
dataFile.print('/');
dataFile.print(now.month(), DEC);
dataFile.print('/');
dataFile.print(now.day(), DEC);
dataFile.print(" | ");
dataFile.print(now.hour(), DEC);
dataFile.print(':');
dataFile.print(now.minute(), DEC);
dataFile.print(':');
dataFile.print(now.second(), DEC);
dataFile.println();
// Serial.print(" since midnight 1/1/1970 = ");
// Serial.print(now.unixtime());
// Serial.print("s = ");
// Serial.print(now.unixtime() / 86400L);
// Serial.println("d");
// // calculate a date which is 7 days, 12 hours, 30 minutes, and 6 seconds into the future
// DateTime future (now + TimeSpan(7,12,30,6));
// Serial.print(" now + 7d + 12h + 30m + 6s: ");
// Serial.print(future.year(), DEC);
// Serial.print('/');
// Serial.print(future.month(), DEC);
// Serial.print('/');
// Serial.print(future.day(), DEC);
// Serial.print(' ');
// Serial.print(future.hour(), DEC);
// Serial.print(':');
// Serial.print(future.minute(), DEC);
// Serial.print(':');
// Serial.print(future.second(), DEC);
// Serial.println();
// Serial.println();
// char my_string[100];
// String dataString = ""; // NOTE: dataFile.println() can take char * or String; Strings might be easier to format.
//sprintf(my_string, "My Test %d", loop_counter);
// If the log file is available, write to it:
if (!dataFile)
{
// If the file isn't open, print an error, and try again to open the file.
Serial.println(" Error opening datalog.txt");
dataFile = SD.open("datalog.txt", FILE_WRITE);
}
loop_counter++;
delay(1000);
}