RTClib, write only the date, only the day of the week Post

Good afternoon, gentlemen! Tell me please, I am using the excellent RTClib library for my project.
I have little difficulties, for example, I want to store only the day of the week in the watch's memory (I don't want other data to change), how can I do this?
Construction rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0)); changes all data at once.
I need

  1. only the day of the week
  2. only year, month, day
  3. only hour, minutes, seconds

You have already been answered in the national forum.
You can't change only part of the time.
First, read the current data from the clock, change what you need, fill it back.

DateTime old_time = rtc.now();
//change the day and month to 1st July, preserving the time of day and the year
DateTine new_time(old_time.year(), 
                              7,               // month = July
                              1,              // day of month
                 old_time.hour(), old_time.minute() , old_time.second());
rtc.adjust(new_time);

thank you for your reply and the example

in their own country, for some reason, many mercantile and evil :slight_smile:

Please write in English in the main forum category.

an attempt to declare structure
DateTime dt = rtc.now();
outside the loop causes a panic and restart esp32
otherwise the subroutine void AfterN () does not know what "dt" is
what am I doing wrong?

#include "WiFi.h"
const char* ssid = "24";
const char* password = "123"; 
#include <Wire.h>
#include "RTClib.h"       //to show time
RTC_DS3231 rtc;

byte d_old   =    7;
.
.
.
float dMin = 1000;

DateTime dt = rtc.now();
//===============================
void setup() {
  Serial.begin(115200);
  SerialNXT.begin(9600); 
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("login to WiFi");
  }
  Serial.println("OK");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP()); 

  ntp.begin(); 
  rtc.begin();  
}

void loop() {
.
.
.
} 


void AfterN () { 
.
      SerialNXT.print("n0.val=" + String(dt.hour())); ff(); 
      SerialNXT.print("n1.val=" + String(dt.minute())); ff(); 
      SerialNXT.print("n2.val=" + String(dt.second())); ff(); 
.
.
}

If you have an ESP32 and WiFi access, why are you using an external RTC at all? ESP32 has built in RTC that can automatically sync to internet time servers using NTP.

You can declare dt there, but can't use library methods outside the any function.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.