SD Card & RTC

hello all,

i am building a data logger to save the operation time and still working on the basics features to save time with the RTC DS1302 and to write in a file in the SD Card.

my problem is after tracing the code i once i call

SD.Begin();

the time clock is return time as 2165-165-165 27:165:85

while before that it’s working fine and returning correct time.

I am Using WeMos D1 for this project in order to send data through WiFi and to log it in case no connection.

#include <SD.h>
#include <SPI.h> 

#include <ESP8266WiFi.h>

#include <stdio.h>
//#include <DS1302.h>
#include "RTClib.h"

const char* ssid = "123456789"
const char* password = "123456789"

int ledPin = 5;
WiFiServer server(80);


 
char servername[]="hidadacclrep.com";  // remote server we will connect to

WiFiClient client;

String result;
int CS_PIN = 15; 

File file;


 
const int kCePin   = 0;  //5 Chip Enable
const int kIoPin   = 2;  //6 Input/Output
const int kSclkPin = 14;  //7 Serial Clock

// Create a DS1302 object.
DS1302 rtc(kCePin, kIoPin, kSclkPin);
  
String dayAsString(const Time::Day day) {
  switch (day) {
    case Time::kSunday: return "Sunday";
    case Time::kMonday: return "Monday";
    case Time::kTuesday: return "Tuesday";
    case Time::kWednesday: return "Wednesday";
    case Time::kThursday: return "Thursday";
    case Time::kFriday: return "Friday";
    case Time::kSaturday: return "Saturday";
  }
  return "(unknown day)";
}

void printTime() {
  // Get the current time and date from the chip. 
  


  Time t = rtc.time();

  // Name the day of the week.
  const String day = dayAsString(t.day);

  // Format the time and date and insert into the temporary buffer.
  char buf[50];
  snprintf(buf, sizeof(buf), "%04d-%02d-%02d %02d:%02d:%02d",
           
           t.yr, t.mon, t.date,
           t.hr, t.min, t.sec);

  // Print the formatted string to serial so we can see the time.
  Serial.println(buf);
}

char* getTime() {
  //Get the current time and date from the chip. 
  Time t = rtc.time();

  // Name the day of the week.
  const String day = dayAsString(t.day);

  // Format the time and date and insert into the temporary buffer.
  char buf[50];
  snprintf(buf, sizeof(buf), "%04d-%02d-%02d %02d:%02d:%02d",
           
           t.yr, t.mon, t.date,
           t.hr, t.min, t.sec);

  // Print the formatted string to serial so we can see the time.
  return buf;
}

//}  // namespace


void setup()
{

  //Serial.begin(9600);
  Serial.begin(115200);

 rtc.halt(false);
 rtc.writeProtect(true);

  delay(2000);
  initializeWiFi();
}


void loop() { 

  result="";
  //sendGET();
  //printTime();
  delay(1000);
  
  
   
  initializeSD();
  
  printTime();
  delay(1000);
  createFile("test1.txt"); 
  
  printTime();
  delay(1000);
// file.print(v);
 
    file.println("This is sample text!");
  
    file.println(ESP.getChipId());
     

    file.println(getTime());   file.println(getTime());

    Serial.println(ESP.getChipId());
    

 // delay(1000);  
  Serial.println("hello"); 
  Serial.println(file.read()); 
  

  
  closeFile();
 // closeFile();


  printTime();
  delay(1000);
 
}



void deleteHttpHeader()
{
if(result.endsWith("Content-Type: text/plain"))
{
  result="";
}

}

void sendGET() //client function to send/receive GET request data.
{
  if (client.connect(servername, 80)) {  //starts client connection, checks for connection
    Serial.println("connected");
    client.println("GET /datalog.aspx?Deviceid=" +  String(ESP.getChipId()) + " HTTP/1.1"); //download text
    client.println("Host: hidadacclrep.com");
    client.println("Connection: close");  //close 1.1 persistent connection  
    client.println(); //end of get request
  } 
  else {
    Serial.println("connection failed"); //error message if no client connect
    Serial.println();
  }

  while(client.connected() && !client.available()) delay(1); //waits for data
  while (client.connected() || client.available()) { //connected or data available
    char c = client.read(); //gets byte from ethernet buffer
    result = result+c;
    deleteHttpHeader();
    Serial.println(c);
  }

  Serial.println(result);
 

char servername[]="hidadacclrep.com";  // remote server we will connect to

WiFiClient client;

String result;
  client.stop(); //stop client

}



void initializeWiFi()
{
  Serial.begin(115200);
  delay(10);
 
 
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
 
  Serial.print(F("Setting static ip to : "));
//  Serial.println(ip);
 
  // Connect to WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  //IPAddress subnet(255, 255, 255, 0); // set subnet mask to match your network
  //WiFi.config(ip, gateway, subnet); 
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");

  
 
  // Start the server
  server.begin();
  Serial.println("Server started");
 
  // Print the IP address
  Serial.print("Use this URL : ");
  Serial.print("http://");
  Serial.print(WiFi.localIP());
  Serial.println("/");
 
  digitalWrite(ledPin, HIGH);
  delay(500);
  digitalWrite(ledPin, LOW);
  delay(500);
  digitalWrite(ledPin, HIGH);
  delay(500);
  digitalWrite(ledPin, LOW);
  delay(500);
  digitalWrite(ledPin, HIGH);
  delay(500);
  digitalWrite(ledPin, LOW); 
}

void initializeSD()
{
  
  printTime();
  delay(1000);
  Serial.println("Initializing SD card...");
  
  printTime();
  delay(1000);
  //pinMode(CS_PIN, OUTPUT);

  printTime();
  delay(1000);
  if (SD.begin())
  {
    Serial.println("SD card is ready to use.");
  } 
  else
  {
    Serial.println("SD card initialization failed");
    return;
  }
  
  printTime();
  delay(1000);
}

int createFile(char filename[])
{
  file = SD.open(filename, FILE_WRITE);

  if (file)
  {
    Serial.println("File created successfully.");
    
    digitalWrite(ledPin, HIGH);
    delay(500);
    digitalWrite(ledPin, LOW);
    
    return 1;
    
  } else
  {
    Serial.println("Error while creating file.");
    
    digitalWrite(ledPin, HIGH);
    delay(100);
    digitalWrite(ledPin, LOW);
    delay(100);
    digitalWrite(ledPin, HIGH);
    delay(100);
    digitalWrite(ledPin, LOW);
    delay(100);
    digitalWrite(ledPin, HIGH);
    delay(100);
    digitalWrite(ledPin, LOW); 
  
    return 0;
  }
}

int writeToFile(char text[])
{
  if (file)
  {
    file.println(text);
    Serial.println("Writing to file: ");
    Serial.println(text);
    return 1;
  } else
  {
    Serial.println("Couldn't write to file");
    return 0;
  }
}

void closeFile()
{
  if (file)
  {
    file.close();
    Serial.println("File closed");
  }
}

int openFile(char filename[])
{
  file = SD.open(filename);
  if (file)
  {
    Serial.println("File opened with success!");
    return 1;
  } else
  {
    Serial.println("Error opening file...");
    return 0;
  }
}

String readLine()
{
  String received = "";
  char ch;
  while (file.available())
  {
    ch = file.read();
    if (ch == '\n')
    {
      return String(received);
    }
    else
    {
      received += ch;
    }
  }
  return "";
}

That could because of inadequate power. Use a proper 1 amp wall wart.

thanks for your reply,

but could you please clarify more what do you mean, i am new to the arduino

I'm afraid I can't put that more simply. Are you using a proper 9v 1 amp wall power supply? I suspect whatever you are using is inadequate to power your gear. The SD command is the straw that breaks the camel's back, hence the dodgey clock readout.