Turning My Roku Into an Alarm

Table of Contents

Waking up is always hard with that morning drowsy feeling, so I figured to replace it with some heart racing action movies!

I use a Ubiquti EdgeRouter X as my router which conveniently provides SSH access. Any device on the same network as the Roku device would suffice, though this post primarily focuses on Linux devices.

The EdgeRouter X has a router IP of 192.168.1.1 with a default SSH port of 22. The default username and password is ubnt; however, should be changed upon initial configuration. Creating a new user is also advisable through the web interface at https://192.168.1.1.

You can SSH into a device using the following command, replacing <username> and the IP address as needed:

ssh <username>@192.168.1.1

Next, create a new script to house the commands toward the Roku device. For the purposes of this, I use Vim:

vi alarm.sh

To edit in Vim, press the i key.

Paste the following into the terminal:

#/bin/bash

# Enter Roku IP Address Below
ROKU_IP_ADDRESS='192.168.1.2'

# Enter Buttons to Execute Them or Numbers to Sleep
actions=('Home' '3' 'Right' '1' 'Select' '9')

for action in ${actions[@]}; do
    if [[ $action =~ ^[0-9]+$ ]]; then
        sleep $action
    else
        curl -d '' "http://$ROKU_IP_ADDRESS:8060/keypress/$action"
    fi
done

Use the up and down arrows to change lines and adjust the value of ROKU_IP_ADDRESS to reflect the IP address of your Roku device. This bash script accepts keypress key values and integers with integers denoting sleep times. For instance, if you wanted to execute the left button press, add 'Left' after a space. If a sleep/wait time is needed, add an integer afterwards encased in quotes. Sleep times are needed in case the device does not load quick enough, such as a channel loading.

Once done, press the Escape key followed by :wq' and the Enter` key to save.

Set the file permissions to allow execution of the script:

chmod +x alarm.sh

Now, lets register a cronjob to automatically run the script. First, determine the timezone of your machine by running the following command:

date

If the date does not match your current time, consider changing your device’s timezone. This will help keep the script running at the correct time regardless of a time shift.

I personally enjoy using crontab.guru to verify my cron schedule expressions are correct. For example, 30 7 * * 1-5 will run the script at 7:30 on weekdays.

Get the path to your current directory and save it:

pwd

Once you are set on the execution time, open the crontab editor:

crontab -e

Add a new line at the bottom and prefix it with your cron schedule expression. And suffix it with a command to execute your script, for example (replacing <current directory path>):

30 7 * * 1-5 /bin/bash <current directory path>/alarm.sh

And the button presses will now automatically be executed! Good luck waking up…