Ich habe dummerweise die Batterie auf dem Board rausgenommen und damit die Uhrzeit zurückgesetzt. Ich habe mich nach Lösungen zum Stellen der RTC umgesehen und dabei diese Seite gefunden: RTC with Arduino | Adafruit Adalogger FeatherWing | Adafruit Learning System
Der Anleitung folgend habe ich den Beispiel-Sketch aufs Board geladen und im Monitor überprüft, ob ich eine Ausgabe bekomme. Man soll ja die rtc.adjust Zeile ent-kommentieren, was bei mir schon der Fall war. Angeblich soll dieser Befehl die Uhr auf den Zeitpunkt des Kompilens zurücksetzen, was allerdings nicht passiert. Auch das Setzen auf eine bestimmte, festgelegte Zeit hat keinerlei Auswirkungen. Mache ich etwas falsch? Habe ich vielleicht einen wichtigen Schritt vergessen?
// Date and time functions using a PCF8523 RTC connected via I2C and Wire lib
#include "RTClib.h"
RTC_PCF8523 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
void setup () {
Serial.begin(57600);
#ifndef ESP8266
while (!Serial); // wait for serial port to connect. Needed for native USB
#endif
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
abort();
}
if (! rtc.initialized() || rtc.lostPower()) {
Serial.println("RTC is NOT initialized, let's set the time!");
// 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__)));
// 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));
//
// Note: allow 2 seconds after inserting battery or applying external power
// without battery before calling adjust(). This gives the PCF8523's
// crystal oscillator time to stabilize. If you call adjust() very quickly
// after the RTC is powered, lostPower() may still return true.
}
// Date and time functions using a PCF8523 RTC connected via I2C and Wire lib
#include "RTClib.h"
RTC_PCF8523 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
void setup () {
Serial.begin(57600);
#ifndef ESP8266
while (!Serial); // wait for serial port to connect. Needed for native USB
#endif
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
abort();
}
if (! rtc.initialized() || rtc.lostPower()) {
Serial.println("RTC is NOT initialized, let's set the time!");
// 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__)));
// 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));
//
// Note: allow 2 seconds after inserting battery or applying external power
// without battery before calling adjust(). This gives the PCF8523's
// crystal oscillator time to stabilize. If you call adjust() very quickly
// after the RTC is powered, lostPower() may still return true.
}
Habe es gerade durch Zufall teilweise gelöst. Anders als in der Anleitung gezeigt war die adjust Zeile in der if Anweisung nicht die richtige, sondern eine ähnliche Zeile weiter unten.
Die Uhrzeit wurde richtig gesetzt, ist jetzt aber stehen geblieben...
Du kannst die Zeit auch über USB mittels eines Strings über das Terminal der IDE schicken.
Ich verwende eine Bibliothek für den DS3231; weiß jetzt nicht den Link. Der Sketch muß an die verwendete Bibliothek und RTC angeglichen werden.
Der Sketch ist nicht kontrolliert.
Quellen: Beispiel der Bibliothek und Sommerzeitberechnung von Jurs, hier im Forum pubbliziert.
#include "ds3231.h"
#define BUFF_MAX 128
uint8_t time[8];
char recv[BUFF_MAX];
char in;
unsigned int recv_size = 0;
void setup() {
Wire.begin();
Serial.begin(9600);
DS3231_init(DS3231_INTCN);
memset(recv, 0, BUFF_MAX);
// print time and date end update instruction.
Serial.println(F("Actual time and date:"));
DS3231_get(&t);
Serial.print(t.hour);
Serial.print(F(":"));
Serial.print(t.min);
Serial.print(F(":"));
Serial.print(t.sec);
Serial.print(F(" "));
Serial.print(t.mday);
Serial.print(F("."));
Serial.print(t.mon);
Serial.print(F("."));
Serial.print(t.year);
Serial.println(F(""));
STUsummertime = summertime_EU(t.year, t.mon, t.mday, t.hour, 1); // check if is summertime.
Serial.println(F("For change please insert UTC+1 time and date in the format:[T]hhmmssWTTMMYYYY"));
Serial.println(F("T ... command for Time seting"));
Serial.println(F("hh ... hour"));
Serial.println(F("mm ... minute"));
Serial.println(F("ss ... seconds"));
Serial.println(F("W ... day of the Week. Start with sunday = 0"));
Serial.println(F("TT ... day"));
Serial.println(F("MM ... month"));
Serial.println(F("YYYY ... year"));
}
void loop() {
DS3231_get(&t);
// String einlesen und auswerten wenn String mit /CR/NL abgeschlossen.
if (Serial.available() > 0) {
in = Serial.read();
if ((in == 10 || in == 13) && (recv_size > 0))
{
parse_cmd(recv, recv_size);
recv_size = 0;
recv[0] = 0;
}
else if (in < 48 || in > 122)
{
; // ignore ~[0-9A-Za-z]
}
else if (recv_size > BUFF_MAX - 2)
{ // drop lines that are too long
// drop
recv_size = 0;
recv[0] = 0;
}
else if (recv_size < BUFF_MAX - 2)
{
recv[recv_size] = in;
recv[recv_size + 1] = 0;
recv_size += 1;
}
}
....
}
byte summertime_EU(int year, byte month, byte day, byte hour, byte tzHours)
// European Daylight Savings Time calculation by "jurs" for German Arduino Forum
// input parameters: "normal time" for year, month, day, hour and tzHours (0=UTC, 1=MEZ)
// return value: returns 1 during Daylight Saving Time, 0 otherwise
{
if (month < 3 || month > 10) return false; // keine Sommerzeit in Jan, Feb, Nov, Dez
if (month > 3 && month < 10) return true; // Sommerzeit in Apr, Mai, Jun, Jul, Aug, Sep
if (month == 3 && (hour + 24 * day) >= (1 + tzHours + 24 * (31 - (5 * year / 4 + 4) % 7)) || month == 10 && (hour + 24 * day) < (1 + tzHours + 24 * (31 - (5 * year / 4 + 1) % 7)))
return 1;
else
return 0;
}
// Auswertung String
void parse_cmd(char *cmd, int cmdsize)
{
uint8_t i;
uint8_t reg_val;
char buff[BUFF_MAX];
struct ts t;
// TssmmhhWDDMMYYYY aka set time
if (cmd[0] == 84 && cmdsize == 16) {
//T355720619112011
t.sec = inp2toi(cmd, 1);
t.min = inp2toi(cmd, 3);
t.hour = inp2toi(cmd, 5);
t.wday = cmd[7] - 48;
t.mday = inp2toi(cmd, 8);
t.mon = inp2toi(cmd, 10);
t.year = inp2toi(cmd, 12) * 100 + inp2toi(cmd, 14);
DS3231_set(t);
Serial.println(F("Time and date seted.")); // Print actual Time after upodate
Serial.println(F("Actual time and date:"));
DS3231_get(&t);
Serial.print(t.hour);
Serial.print(F(":"));
Serial.print(t.min);
Serial.print(F(":"));
Serial.print(t.sec);
Serial.print(F(" "));
Serial.print(t.mday);
Serial.print(F("."));
Serial.print(t.mon);
Serial.print(F("."));
Serial.print(t.year);
Serial.println(F(""));
} else {
Serial.print(F("unknow comand: "));
Serial.print(cmd[0]);
Serial.print(F(" "));
Serial.println(cmd[0], DEC);
}
}