Auto-Wake a Pi5 without a Pijuice! (Mothbox)

Motivation

Replicate the functionality of a Pijuice with just a Pi5 and a $5 battery!

Like many folks here, we use a Pijuice on the Raspberry pi on the mothbox to be able to schedule automatic wakeup times, and be able to shutdown the pi to save energy when not in use.

Pijuice-s are a nice piece of equipment and also help function as a UPS too.

BUT they are pretty pricey ($75-$100), and they are often out of stock. 

Spurred on when we tried to order some more for more mothboxes and they told us they won't have any until late june, I started looking at other options.

 

I remembered that the Pi5 now has a built in Real-time-clock (RTC), and sought to investigate if we could replicate the power scheduling behavior of a Pi with a Pijuice.

 

Despite this big change in hardware 6 months ago, I was surprised to find VERY little information about the auto-wake up capabilities of a pI5. There were a handful of blog posts online that just said things like, "yeah theoretically now you can write to the auto-wake a specific time and it will wake up!" But there was barely any extra info online about particulars like "how could i schedule multiple wake-up times?"

So I started messing around with it myself, and I got some advice from nice folks on stackoverflow, and figured out a script that totally works! 

 

Auto-Wake Pi5 Scheduling

So here's how to do it!

I wrote a little script: (Sorry it's messy and has some other mothbox stuff, probably not pertinent to other projects).

https://github.com/Digital-Naturalism-Laboratories/Mothbox/blob/main/Software/Scheduler_Pi5.py

but the key parts I'll post below. 

How Wakealarm Works in General

The thing to know is that the /sys/class/rtc/rtc0/wakealarm file is the thing that tells the pi when the next time to wakeup is. And all it is is a long number written in epoch time (seconds since 1970). If this number is 10 minutes in the future, when the RTC clock reaches that number, it will tell the Pi 5 to turn itself on (if the pi5 is off). If the pi5 was on during this time nothing happens, but then if the pi turns OFF and sees that its wakealarm is a time in the PAST, it will automatically turn on. (This might not be true for all linux systems). 

Having just one variable means it can be kinda tricky for lay-persons to create an easy schedule for their raspberry pi.

When one uses a pijuice, you can just give it a cron-like command listing which minute, hour, day, month it should auto-wakeup. So I decided a build a system like this!

 

Pi5 Cron-like Wakeup Scripts

The script I linked above has several different functions. First it reads a schedule from a csv file.

#do the scheduling
settings = load_settings("/home/pi/Desktop/Mothbox/schedule_settings.csv")
if "runtime" in settings:
    del settings["runtime"]
if "utc_off" in settings:
    del settings["utc_off"]

 then it converts this expressions into a cron-like expression

cron_expression = "10 15 * * *"  # Every day at 10:15 AM
print(cron_expression)
#build cron-like expression from schedule
# Loop through each key-value pair in the dictionary
for key, value in settings.items():
  # Check if the value is a string and contains semicolons
  if isinstance(value, str) and ';' in value:
    # Replace semicolons with commas
    settings[key] = value.replace(';', ',')
#print(settings)
#note cron has no seconds
cron_expression = str(settings['minute'])+" "+str(settings['hour'])+" "+"*"+" "+"*"+" "+str(settings['weekday'])

Next you need to calculate the next event that should trigger a wakeup, and convert this time into an epoch time. That's the primary useful part of this code. You also need to CLEAR any existing wakeup alarms that had been set, or else you will run into an error.

next_epoch_time = calculate_next_event(cron_expression)
# Clear existing wakeup alarm (assuming sudo access)
clear_wakeup_alarm()

Finally you just run the 

  set_wakeup_alarm(next_epoch_time)

and your system is set!

 

To have your Pi5 system automatically follow this wakeup schedule, you just need to add an entry in your crontab like this

@reboot /usr/bin/python3 /home/pi/Desktop/Mothbox/Schedulerpi5.py

 

and everytime your system starts up, it will calculate and set the next time it should start up!

 

 

 

 

 

 




How much current does the Pi 5 draw when it is sleeping? 

I did an experiment recently and hand built one of the circuits from "Power switching RP2040 for low
standby current applications." It would be possible to design a little board/hat which combined that with an RTC to schedule complete shutdown more or less any Raspberry Pi. The RTC would draw only a tiny current.

Alex Rood
@alexrood  | she/her
WILDLABS
World Wide Fund for Nature/ World Wildlife Fund (WWF)
I'm the WILDLABS Communications and Community Management Associate Specialist at WWF-US
WILDLABS Team
Involvement level 2
WILDLABS Quiz Master
Inventory Beta Tester
Poster level 3

Hey Andrew, this is really awesome. I'm sure there are loads of people who ran into the same problems as you and will find your script super useful.

As you might know, we launched The Inventory last month--it's a wiki-style database for all things conservation technology. Based off this post, it sounds like you have some experience using Raspberry Pi in your work. It would be a huge help to the community if you could take some time and leave a review on Raspberry Pi's product page on The Inventory! Your review will help others in the community immensely. Let me know if you have any questions!  

 

Robin Sandfort
@capreolus  | he/him
Capreolus e.U.
wildlife biologist with capreolus.at
WILDLABS Event Speaker
Variety Hour Regular
Involvement level 2
Inventory Beta Tester
Reactor level 3

Hi Andrew,
this is great! 
Did you consider the Witty Pi 4 L3V7 Real Time Clock (RTC) und Power Management Modul für Raspberry Pi?
(This one has no included battery) or the one with the capacitor?

Greetings from Austria,
Robin