RTC Issue

jakorten

Well-known member
I suspect a problem with the RTC function of the Teensy 3.x with TeensyRTC.h (updated library?)
[Solution: Don't know what the exact problem is, but it has to do with Synching between System time and Teensy3Clock, workaround in #4]

On my Teensy 3.1 doing a Teensy3Clock.set(1427018955); (e.g.) and then asking for the time results in a: 0:01:20 1-1-1970 that seems to be more the elapsed time since system startup. I'm using the 1.6.0 IDE and Teensyduino 1.2.1

After setting the clock I call this method:

Code:
void digitalClockDisplay() {
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(" ");
  Serial.print(day());
  Serial.print("-");
  Serial.print(month());
  Serial.print("-");
  Serial.print(year()); 
  Serial.println(); 
}

First I was suspecting the backup battery being empty but after testing and trying to synch the Teensy from my iOS device that just sends the UNIX time over iOS I don't see the time being updated so...


Here is how I do the updating of the Time from iOS:

Time synch message
1427021121
0:00:23 1-1-1970

and that is the output of:
Code:
if ((inputbuf[0] == 't') && (maxbp == 11)) {
    Serial.println("Time synch message");
    time_t newTime = 0;
    for (int t = 1; t <= 10; t++) {
        newTime = newTime*10 + (inputbuf[t]-'0');
    }
    Serial.println(newTime);
    Teensy3Clock.set(newTime);
    digitalClockDisplay();
}
 
Last edited:
On my Teensy 3.1 doing a Teensy3Clock.set(1427018955); (e.g.) and then asking for the time results in a: 0:01:20 1-1-1970 that seems to be more the elapsed time since system startup.

Please post a complete program that reproduces the problem, with need of an Android phone. (eg, the "forum rule").

If there's really something going wrong on the Teensy side, I can investigate. But I do need you to post a complete program that can be copied into Arduino and uploaded to a Teensy here.

I don't have an Android phone, so I can't help with that side.
 
I'll make a test program that is much more simple and post that ok.

Update: hmm that is interesting, the TeensyRTC example does give the correct values. Sometimes one is too much focussed I guess.

B.t.w. I try to synch using just Serial2 input, that is not very complicated and should work independent of a device (being BLE / Xbee / whatever).

Ok here is the code. I deliberately set the time to 11/18/2008 and then try to update the time using Serial2 or Serial input:

Code:
/*
 * TeensyRTCExample
 *
 *  Created on: Nov 13, 2014
 *      Author: Konstantin Gredeskoul
 *        Code: https://github.com/kigster
 *
 *  (c) 2014 All rights reserved, MIT License.
 *  Adapted by Johan Korten 2015 for testing purposes
 *  tUNIXTIMESTAMP; should update the system with current time
 */

#include <Time.h>
#include <TeensyRTC.h>

TeensyRTC rtc;

char stringBuffer[20];
char inputbuf[100];
byte bufpointer = 0;
unsigned long logTimer = 0;

time_t getTeensy3Time() {
  return Teensy3Clock.get();
}

void logTime() {
  rtc.formatTime(stringBuffer, rtc.currentTime());
  Serial.print("Current Time: ");
  Serial.println(stringBuffer);
}

void setup() {

  setSyncProvider(getTeensy3Time);
  Teensy3Clock.set(1227018863);
  Serial.begin(115200);
  Serial2.begin(9600);
  Serial.println("Starting sketch TeensyRTCExample");

  rtc.begin();
}

void loop() {
  if (millis() - logTimer > 1000) {
    logTime();
    logTimer = millis();
  }
  processSerial();
}

void updateTime(unsigned long newTime) {

}

void processSerial() {
  while (Serial.available() > 0) {
    inputbuf[bufpointer] = Serial.read();
    //Serial.print(inputbuf[bufpointer]);
    if (inputbuf[bufpointer] == ';') {
      //Serial.println();
      processBuffer(bufpointer);
    } else {
      bufpointer++;
    }
  }
  while (Serial2.available() > 0) {
    inputbuf[bufpointer] = Serial2.read();
    //Serial.print(inputbuf[bufpointer]);
    if (inputbuf[bufpointer] == ';') {
      //Serial.println();
      processBuffer(bufpointer);
    } else {
      bufpointer++;
    }
  }
}

void processBuffer(int maxbp) {
  Serial.print("No Incoming chars: ");
  Serial.println(maxbp);
  if (maxbp >= 4) {
    if ((inputbuf[0] == 't') && (maxbp == 11)) {
      Serial.println("Time synch message");
      unsigned long newTime = 0;
      for (int t = 1; t <= 10; t++) {
        newTime = newTime * 10 + (inputbuf[t] - '0');
      }
      Serial.println(newTime);
      Teensy3Clock.set(newTime);
      logTime();
    }
  }
  bufpointer = 0;
}

I tried e.g.: t1427026408; using the normal Serial via the Arduino terminal (to not make things more complicated), later the attached phone/tabled could send the same kind of message of course using Serial2.

Output is still something in 2008 even if I update it with 1427026408. But if I replace the timestamp in the setup with 1427026408 it does change it to some time today!
 
Last edited:
Ok I did a trick and that trick works, but still not sure why it doesn't work in the above code!

Code:
/*
 * TeensyRTCExample
 *
 *  Created on: Nov 13, 2014
 *      Author: Konstantin Gredeskoul
 *        Code: https://github.com/kigster
 *
 *  (c) 2014 All rights reserved, MIT License.
 *  Adapted by Johan Korten 2015 for testing purposes
 *  tUNIXTIMESTAMP; should update the system with current time
 */

#include <Time.h>
#include <TeensyRTC.h>

TeensyRTC rtc;

char stringBuffer[20];
char inputbuf[100];
byte bufpointer = 0;
unsigned long logTimer = 0;

time_t getTeensy3Time() {
  return Teensy3Clock.get();
}

void logTime() {
  rtc.formatTime(stringBuffer, rtc.currentTime());
  Serial.print("Current Time: ");
  Serial.println(stringBuffer);
}

void setup() {
  setSyncProvider(getTeensy3Time);
  Teensy3Clock.set(1227018863);
  Serial.begin(115200);
  Serial2.begin(9600);
  Serial.println("Starting sketch TeensyRTCExample");

  rtc.begin();
}

void loop() {
  if (millis() - logTimer > 1000) {
    logTime();
    logTimer = millis();
  }
  processSerial();
}

void processSerial() {
  while (Serial.available() > 0) {
    inputbuf[bufpointer] = Serial.read();
    //Serial.print(inputbuf[bufpointer]);
    if (inputbuf[bufpointer] == ';') {
      //Serial.println();
      processBuffer(bufpointer);
    } else {
      bufpointer++;
    }
  }
  while (Serial2.available() > 0) {
    inputbuf[bufpointer] = Serial2.read();
    //Serial.print(inputbuf[bufpointer]);
    if (inputbuf[bufpointer] == ';') {
      //Serial.println();
      processBuffer(bufpointer);
    } else {
      bufpointer++;
    }
  }
}

void processBuffer(int maxbp) {
  Serial.print("No Incoming chars: ");
  Serial.println(maxbp);
  if (maxbp >= 4) {
    if ((inputbuf[0] == 't') && (maxbp == 11)) {
      Serial.println("Time synch message");
      unsigned long newTime = 0;
      for (int t = 1; t <= 10; t++) {
        newTime = newTime * 10 + (inputbuf[t] - '0');
      }
      [B]Serial.println(newTime);
      setTime(newTime);
      Teensy3Clock.set(now());
      logTime();[/B]
    }
  }
  bufpointer = 0;
}

So I ended up updating the system time from the Serial and then synching the Teensy3Clock with the system time. That works very well (also from the iPad). Critical lines in bold, last method.
 
Last edited:
Back
Top