How long M5stack-paper can sleep for?



  • I have programmed M5stack-Paper in a way that it connects to the WiFi, read data from Firebase and sleep for provided seconds using "M5.shutdown(Secs)". read documentation here

    It is working fine if the sleep duration is less than 4 hours.
    If the sleep duration is more than 4 hours then the M5 paper is waking up properly according to the given sleep duration but waking up randomly after 2 - 3 hours .
    I also tried "M5.shutdown( const rtc_date_t &RTC_DateStruct, const rtc_time_t &RTC_TimeStruct)" but it didn't work as well.

    1. Is there any limitation regarding the sleep duration for M5stack-paper?
    2. Is there any other shutdown function which can be used to wake it up for longer durations?


  • Hello @ScheduleDisplay

    M5.shutdown(secs) uses the timer function of the RTC. According to the documentation the timer is 8 bits and it slowest clock source is 1/60 Hz. So you get about 255 x 60 s = 15300 s which is roughly 4 h (= 14400 s).

    Re 1.) So yes, there is a limitation of the sleep duration when using the timer function of the RTC.

    If you need longer period than 4 hours you need to look into the alarm functionality of the RTC. E.g. you first need to read the RTC time, add the amount you want to sleep to it and then set the alarm accordingly.

    M5.shutdown(const rtc_date_t &RTC_DateStruct, const rtc_time_t &RTC_TimeStruct) can be used to set the alarm, but it only works when you first calculate the alarm time in relation the actual time in the RTC.

    Note: the actual time in the RTC resets when there is no USB and the battery has run out of juice. The actual time is not automatically the time on you wall clock (except when you actually set it in the RTC). But for the alarm function to be used it doesn't have to be synced with the wall clock - it's only important that you know the time the RTC so you can calculate the difference and set the alarm.

    Re 2.) so no other shutdown function is needed.

    Thanks
    Felix



  • Hello Felix,
    Thank you very much for your reply.

    I updated the code according to your suggestion.

    1. I know the actual time so I set the actual date and time of RTC.
      rtc_date_t RTCDate;
      rtc_time_t RTCtime;
    
      RTCDate.year = year();
      RTCDate.mon  = month();
      RTCDate.day  = day();
      M5.RTC.setDate(&RTCDate);
    
      RTCtime.hour = hour();
      RTCtime.min  = minute();
      RTCtime.sec  = second();
      M5.RTC.setTime(&RTCtime);
    
      Serial.print("\n Actual RTC date and time  =>" + String(RTCDate.day) + "-" + String(RTCDate.mon) + "-" + String(RTCDate.year) + " " + String(RTCtime.hour) + ":" + String(RTCtime.min) + ":" + String(RTCtime.sec) );
    
    
    1. I also know the sleep duration in secs. for example 30 minutes (1800 secs). So I added these secs into the current time to get the wakeup time in the future.
    int nextRereshTimeInSecs = now() + 1800;
    setTime(nextRereshTimeInSecs);
    
    1. Then I set the alarm using setAlarmIRQ(const rtc_date_t &date, const rtc_time_t &time) and shut down the M5stack-paper.
        rtc_date_t wakeupDate;
        rtc_time_t wakeupTime;
    
        int currentYear = year();
        int8_t int8_year = currentYear % 100;
        int8_t int8_month = month();
        int8_t int8_day = day(); 
        
        // Populate rtc_date_t structure
        wakeupDate.year = int8_year ;
        wakeupDate.mon = int8_month ;
        wakeupDate.day = int8_day;
    
        int8_t int8_hour = hour();
        int8_t int8_minute = minute();
        int8_t int8_second = second(); 
        // Populate rtc_time_t structure
        wakeupTime.hour = int8_hour;
        wakeupTime.min = int8_minute;
        wakeupTime.sec = int8_second;
    
        Serial.print("\n Future RTC date and time  =>" + String(wakeupDate.day) + "-" + String(wakeupDate.mon) + "-" + String(wakeupDate.year) + " " + String(wakeupTime.hour) + ":" + String(wakeupTime.min) + ":" + String(wakeupTime.sec) );
    
        delay(300);
        M5.RTC.clearIRQ();
        M5.RTC.setAlarmIRQ(wakeupDate, wakeupTime);
    
        delay(10);
        M5.shutdown();
    

    Currently, I am testing it with the sleep duration of 30 minutes and it looks like working. I will test it for more than 4 hours tonight.

    The question is that, do I have to use
    M5.shutdown( wakeupDate, wakeupTime)

    OR

    is it OK what I am currently using
    M5.RTC.setAlarmIRQ(wakeupDate, wakeupTime);
    M5.shutdown();



  • Hello @ScheduleDisplay

    it doesn't really matter as `M5.shutdown( wakeupDate, wakeupTime) essentially does the same. See here.

    Thanks
    Felix



  • @felmue I tried longer sleep duration of 9 hours and unfortunately the device didn't wakeup.
    Is it possible that M5.RTC.setAlarmIRQ(wakeupDate, wakeupTime); also has a limitaiton of max 4 hours of sleep duration?



  • Hello @ScheduleDisplay

    no, I don't think there is a limitation. This is a regular alarm function of the RTC which should allow an alarm even years from now.

    But I noticed that the code in the library adds a small delay(10) to make sure the I2C command to set the alarm time has finished before powering off.

    The only other reason I could think of that the M5Paper doesn't wake up anymore would be a flat battery.

    Thanks
    Felix



  • @felmue Hello Felix.

    1. I also already had delay (10) before M5.shutdown();
      Do I2CEnable need to be TRUE in M5 initialization? to use I2C commands?
      I have it False in M5 initialization call M5.begin(true, false, true, true, false);, could this be a problem?
      However, it is working fine if the sleep duration is less than 4 hours

    2. M5Paper has 100% battery, I can restart it manually by pressing the side button. The battery is showing as 100%



  • Hello @ScheduleDisplay

    No, if the touch is enabled (first true) then that already is done there. See here.

    Ok - battery - I am afraid I am not sure why it would fail then.

    Thanks
    Felix



  • @felmue Hello Felix.
    Is it possible to provide me the working code snippet with longer than 4 hours sleep duration?

    Or

    Can you please check if there is any problem with the code snippet I posted earlier?

    Thanks



  • Hello @ScheduleDisplay

    please find an example here which (when turned on by power button) shuts down and wakes up from RTC about 7 hours later.

    Note: only using the hour field gives you a maximum of 23 hours. If you need more you'll need to also use the day and maybe year fields.

    Thanks
    Felix



  • @felmue Hello Felix.
    Thank you very much for your help.
    I get it working using the hour field with some little changes.