Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 5 of 5

Thread: Teensy 3.6 RTC not continues time in hibernation

  1. #1

    Teensy 3.6 RTC not continues time in hibernation

    Hello everyone,

    I'm having problems with the T3.6 RTC in combination with the Snooze library. The RTC works fine with the "TimeTeensy3" sketch and keeps the time even when unplugged from power using the battery.

    But when I'm trying to retain the time when the Teensy is waking up from hibernation, it continues with the time it went to sleep (see screenshot).

    What am I missing?

    My final goals is to put the Teensy into sleep over night and wake him up in the morning. For this I'm using the TimeAlarm-Lib for setting a repeating alarm and the "alarm.setRtcTimer" function from the Snooze library to set the sleeping time.

    In the example code you'll find the current test settings as well as the settings that will be used in production mode.

    Thank you for your help,
    Martin

    Code:
    #include <Snooze.h>
    #include <TimeLib.h>
    #include <TimeAlarms.h>
    // Load drivers
    SnoozeAlarm alarm;
    SnoozeTimer timer;
    
    // install drivers to a SnoozeBlock
    SnoozeBlock config_teensy36(alarm, timer);
    
    void setup() {
    
    // set the Time library to use Teensy 3.0's RTC to keep time
      setSyncProvider(getTeensy3Time);
    
      Serial.begin(115200);
      while (!Serial);  // Wait for Arduino Serial Monitor to open
      delay(100);
      if (timeStatus()!= timeSet) {
        Serial.println("Unable to sync with the RTC");
      } else {
        Serial.println("RTC has set the system time");
      }
      
    // Set timer for tes mode
    Alarm.timerRepeat(30,GoToSleep);
    // Set timer For productive mode
    //Alarm.alarmRepeat(22,00,00,GoToSleep);
    
    }
    
    void loop() {  
    
      if (Serial.available()) {
        time_t t = processSyncMessage();
        if (t != 0) {
          Teensy3Clock.set(t); // set the RTC
          setTime(t);
        }
      }
        
      digitalClockDisplay();  
      Alarm.delay(1000);
      
    }
    
    void GoToSleep(){
    
      // Set RtcTimer for test mode (Teensy should wake up in one minute)
      alarm.setRtcTimer(0, 1, 0);// hour, min, sec
      // Set RtcTimer for productive mode (Teensy should wake up at 7:00am )
      //alarm.setRtcTimer(9, 0, 0);// hour, min, sec
      
      Snooze.hibernate( config_teensy36 );
      
    }
    
    time_t getTeensy3Time()
    {
      return Teensy3Clock.get();
    }
    
    void digitalClockDisplay() {
      // digital clock display of the time
      Serial.print(hour());
      printDigits(minute());
      printDigits(second());
      Serial.print(" ");
      Serial.print(year()); 
      Serial.print("-");
      Serial.print(month());
      Serial.print("-");
      Serial.print(day());
      
      Serial.println(); 
    }
    
    
    
    /*  code to process time sync messages from the serial port   */
    #define TIME_HEADER  "T"   // Header tag for serial time sync message
    
    unsigned long processSyncMessage() {
      unsigned long pctime = 0L;
      const unsigned long DEFAULT_TIME = 1357041600; // Jan 1 2013 
    
      if(Serial.find(TIME_HEADER)) {
         pctime = Serial.parseInt();
         return pctime;
         if( pctime < DEFAULT_TIME) { // check the value is a valid time (greater than Jan 1 2013)
           pctime = 0L; // return 0 to indicate that the time is not valid
         }
      }
      return pctime;
    }
    
    void printDigits(int digits){
      // utility function for digital clock display: prints preceding colon and leading 0
      Serial.print(":");
      if(digits < 10)
        Serial.print('0');
      Serial.print(digits);
    }
    Attached Thumbnails Attached Thumbnails Click image for larger version. 

Name:	2020-04-30_165916.jpg 
Views:	9 
Size:	57.0 KB 
ID:	19910  
    Last edited by ToniPropeller; 04-30-2020 at 03:24 PM.

  2. #2
    Senior Member
    Join Date
    Oct 2015
    Location
    Roma (IT, EU)
    Posts
    339
    Never used alarms so I'm not sure this suggestion works; anyway, you could try resyncing the time library with the Teensy 3.6 RTC every time you wake up from hibernation:

    Code:
    setSyncProvider(Teensy3Clock.get())

  3. #3
    Thank you for your tip. I thought about this too, but I'm missing a function, that is called when the Teensy wakes up. I already figured out, that the setup part is skipped and the program is continuing in the loop. But calling "setSyncProvider(Teensy3Clock.get())" here isn't making any difference...

  4. #4
    Senior Member duff's Avatar
    Join Date
    Jan 2013
    Location
    Las Vegas
    Posts
    1,019
    A couple of things that stand out is that you do not need the Timer driver just the Alarm driver. In the included sketch I also use the USBSerial driver to aid with printing to the Arduino Serial Monitor. You have to call setSyncProvider(getTeensy3Time) after waking. I tested it and it works, please download and use the latest beta Snooze library (either clone it or download the zip file) so we are on the same page, the latest is still beta but should work better than older releases but I'm waiting for any bugs to be reported before making an official release update. What I see in this sketch is that the the rtc updates it's time accounting for the the time sleeping also that is printed to the Serial Monitor.

    Code:
    #include <Snooze.h>
    #include <TimeLib.h>
    #include <TimeAlarms.h>
    //---------------------------------------------------------------------------
    // Load drivers
    SnoozeAlarm alarm;
    // SnoozeUSBSerial driver does what Serial does like "print", -
    // "println", "prinf", etc and also handles the effects of using -
    // the Arduino Serial monitor and sleeping. Use it when you -
    // want to print to serial monitor for sleeping applications.
    SnoozeUSBSerial usb;
    //---------------------------------------------------------------------------
    // install drivers to a SnoozeBlock
    SnoozeBlock config_teensy36(usb, alarm);
    //---------------------------------------------------------------------------
    void setup() {
      pinMode(LED_BUILTIN, OUTPUT);
      digitalWriteFast(LED_BUILTIN, HIGH);
      // Set RtcTimer for test mode (Teensy should wake up in one minute)
      alarm.setRtcTimer(0, 0, 10);// hour, min, sec
      // set the Time library to use Teensy 3.6's RTC to keep time
      setSyncProvider(getTeensy3Time);
      while (!usb);  // Wait for Arduino Serial Monitor to open
      delay(100);
      if (timeStatus() != timeSet) {
        usb.println("Unable to sync with the RTC");
      } else {
        usb.println("RTC has set the system time");
      }
      // Set timer for test mode
      Alarm.timerRepeat(30, GoToSleep);
      // Set timer For productive mode
      //Alarm.alarmRepeat(22,00,00,GoToSleep);
    }
    //---------------------------------------------------------------------------
    void loop() {
      if (usb.available()) {
        time_t t = processSyncMessage();
        if (t != 0) {
          Teensy3Clock.set(t); // set the RTC
          setTime(t);
        }
      }
      digitalClockDisplay();
      Alarm.delay(1000);
    }
    //---------------------------------------------------------------------------
    void GoToSleep() {
      // Set RtcTimer for productive mode (Teensy should wake up at 7:00am )
      //alarm.setRtcTimer(9, 0, 0);// hour, min, sec
      usb.println("Going to hibernate Sleep now");
      delay(5);
      digitalWriteFast(LED_BUILTIN, LOW);
      Snooze.hibernate( config_teensy36 );
      digitalWriteFast(LED_BUILTIN, HIGH);
      setSyncProvider(getTeensy3Time);
    }
    //---------------------------------------------------------------------------
    time_t getTeensy3Time() {
      return Teensy3Clock.get();
    }
    //---------------------------------------------------------------------------
    void digitalClockDisplay() {
      // digital clock display of the time
      Serial.print(hour());
      printDigits(minute());
      printDigits(second());
      Serial.print(" ");
      Serial.print(year());
      Serial.print("-");
      Serial.print(month());
      Serial.print("-");
      Serial.print(day());
      Serial.println();
    }
    //---------------------------------------------------------------------------
    /*  code to process time sync messages from the serial port   */
    #define TIME_HEADER  "T"   // Header tag for serial time sync message
    unsigned long processSyncMessage() {
      unsigned long pctime = 0L;
      const unsigned long DEFAULT_TIME = 1357041600; // Jan 1 2013
      if (Serial.find(TIME_HEADER)) {
        pctime = usb.parseInt();
        return pctime;
        // check the value is a valid time (greater than Jan 1 2013)
        if ( pctime < DEFAULT_TIME) {
          // return 0 to indicate that the time is not valid
          pctime = 0L; 
        }
      }
      return pctime;
    }
    //---------------------------------------------------------------------------
    void printDigits(int digits) {
      // utility function for digital clock display: prints preceding colon and leading 0
      usb.print(":");
      if (digits < 10)
        usb.print('0');
      usb.print(digits);
    }

  5. #5
    Yeah brilliant, thank you very much, this works for me and helps me a lot for the upcoming todos. The timer driver was a leftover, but thanks for the hint ;-) I missed the point, that the program is continuing right after the Snooze.hibernate call. Nice one!

    Have a nice day and take care,
    Martin

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •