Auto-Wake a Pi5 without a Pijuice!

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).

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!

2 Likes

That’s cool! Is it a full bootup, or is it faster to just wake?

1 Like

i’m not sure what you mean. but basically you tell your pi, shutdown
and then it turns off, and later when it is time, it will boot itself up.

make sure to add these lines to make your pi actually turn itself off and not run at 2ma in a standby mode

sudo -E rpi-eeprom-config --edit Add the following two lines: POWER_OFF_ON_HALT=1 WAKE_ON_GPIO=0

(more details here: Programming ‐ Code a Mothbox from Scratch · Digital-Naturalism-Laboratories/Mothbox Wiki · GitHub )

1 Like