DS1307 RTC breaks only when called from a thread? (ESP32 / Coding)
Posted: Thu Jun 09, 2022 8:02 pm
Hello,
I have a LilyGo T-display with a DS1307 I2C RTC module connected. The module is working great given the example sketches. It works as expected in the setup() of my sketch. But as soon as I try to call the RTC from one of two threads, the date and time will be wrong. It'll go from a proper date and time like: "06/09/22 14:12:00." To either "165/165/2165 37:165" or close but invalid values like "06/03/2022 12:35:36." The RTC is remembering the right time between boots despite the wrong value later in the sketch.
How do I use all of this in one of the threads of the ESP32? It feels like some sort of scope problem.
Thanks!
I have a LilyGo T-display with a DS1307 I2C RTC module connected. The module is working great given the example sketches. It works as expected in the setup() of my sketch. But as soon as I try to call the RTC from one of two threads, the date and time will be wrong. It'll go from a proper date and time like: "06/09/22 14:12:00." To either "165/165/2165 37:165" or close but invalid values like "06/03/2022 12:35:36." The RTC is remembering the right time between boots despite the wrong value later in the sketch.
How do I use all of this in one of the threads of the ESP32? It feels like some sort of scope problem.
Code: Select all
#include <Wire.h>
#include <RtcDS1307.h>
RtcDS1307<TwoWire> Rtc(Wire);
RtcDateTime now; // i've tried using this as global, or as a local variable, to the same result.
void setup() {
Serial.begin(115200);
while (!Serial) {
delay(100);
}
Wire.begin(21, 22);
Rtc.Begin();
//.. clutter
RtcDateTime now;
now = Rtc.GetDateTime(); // the rtc module reports the right time/date in here 06/09/22 14:12:00. Many times if given a loop.
xTaskCreate(loopOne, "loopOne", 2048, NULL, 1, NULL);
xTaskCreate(loopTwo, "loopTwo", 2048, NULL, 1, NULL);
}
void loopOne(void *arg) {
RtcDateTime now; // local
while ( 1 ) {
now = Rtc.GetDateTime(); //now would be 165/165/2165 37:165
//.. clutter. long delays etc.
// now = Rtc.GetDateTime(); //from global. same wrong value.
delay(5);
}
}
void loopTwo(void *arg) {
// OR here...
while ( 1 ) {
RtcDateTime now = Rtc.GetDateTime(); // or like this. same wrong value.
//.. clutter. long delays etc.
delay(5);
}
}
}
void loop {
}