You will have to start working on the individual pieces. The 165 in your RTC results indicates a problem. So write a sketch to only work with the RTC and get proper dates and times. You can search the forum to find if there is a solution for RTC 165, it did come along in the past; from memory it's a hardware issue (faulty RTC, bad wiring, bad power).
The below code is tested on a Mega and will work on an Uno/Nano/Pro Mini under the condition that the wiring is correct.
// format to set time" <Tyyyy-MM-dd HH:mm:ss>
// e.g. <T2024/05/01 11:54:00>
#include <Wire.h>
#include "RTClib.h" //library file for DS3231 RTC Module
RTC_DS3231 rtc;
const char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
const byte numChars = 32;
char receivedChars[numChars];
boolean newData = false;
void setup()
{
Serial.begin(115200);
if (! rtc.begin())
{
Serial.println("Couldn't find RTC");
Serial.flush();
while (1);
}
DateTime now = rtc.now();
printDT(now);
}
void loop()
{
static uint8_t prevSeconds = 0;
DateTime now = rtc.now();
if (now.second() != prevSeconds)
{
prevSeconds = now.second();
printDT(now);
}
recvWithStartEndMarkers();
if (newData == true)
{
newData = false;
switch (receivedChars[0])
{
case 'T':
case 't':
setDT();
break;
}
}
}
void printDT(DateTime now)
{
Serial.print(now.year(), DEC);
Serial.print('/');
if (now.month() < 10) Serial.print("0");
Serial.print(now.month(), DEC);
Serial.print('/');
if (now.day() < 10) Serial.print("0");
Serial.print(now.day(), DEC);
Serial.print(" (");
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
Serial.print(") ");
if (now.hour() < 10) Serial.print("0");
Serial.print(now.hour(), DEC);
Serial.print(':');
if (now.minute() < 10) Serial.print("0");
Serial.print(now.minute(), DEC);
Serial.print(':');
if (now.second() < 10) Serial.print("0");
Serial.print(now.second(), DEC);
Serial.println();
Serial.print(" since midnight 1/1/1970 = ");
Serial.print(now.unixtime());
Serial.print("s = ");
Serial.print(now.unixtime() / 86400L);
Serial.println("d");
}
//==========================
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (Serial.available() > 0 && newData == false)
{
rc = Serial.read();
if (recvInProgress == true)
{
if (rc != endMarker)
{
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars)
{
ndx = numChars - 1;
}
}
else
{
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker)
{
recvInProgress = true;
}
}
}
void setDT()
{
Serial.println(F("Setting time"));
char *strtokIndex;
strtokIndex = strtok(&receivedChars[1], "/");
if (strtokIndex == nullptr)
{
Serial.println(F("Error retrieving year"));
return;
}
int year = atoi(strtokIndex);
Serial.println(year);
strtokIndex = strtok(nullptr, "/");
if (strtokIndex == nullptr)
{
Serial.println(F("Error retrieving month"));
return;
}
int month = atoi(strtokIndex);
Serial.println(month);
strtokIndex = strtok(nullptr, " ");
if (strtokIndex == nullptr)
{
Serial.println(F("Error retrieving day"));
return;
}
int day = atoi(strtokIndex);
Serial.println(day);
strtokIndex = strtok(nullptr, ":");
if (strtokIndex == nullptr)
{
Serial.println(F("Error retrieving hour"));
return;
}
int hours = atoi(strtokIndex);
Serial.println(hours);
strtokIndex = strtok(nullptr, ":");
if (strtokIndex == nullptr)
{
Serial.println(F("Error retrieving minutes"));
return;
}
int minutes = atoi(strtokIndex);
Serial.println(minutes);
strtokIndex = strtok(nullptr, ":");
if (strtokIndex == nullptr)
{
Serial.println(F("Error retrieving seconds"));
return;
}
int seconds = atoi(strtokIndex);
Serial.println(seconds);
rtc.adjust(DateTime(year, month, day, hours, minutes, seconds));
Serial.println(F("Setting time done"));
}
It's a mix of an example of the RTClib library and Robin's updated Serial Input Basics tutorial; the latter is only used to set the time using the serial monitor.