Controlling a PC via Apple HomeKit: part 3

In the first 2 parts we installed the infrastructure and the tools to control the PCs. In this last part of the tutorial, we will create the scripts that will be run when the actions are triggered.

Create the Power On script on the Raspberry Pi

Create the directory where the script will be located. The directory structure is $HOME/.smart-pc-control/<topic>. The directory should contain a file named script.sh that receives the action data as a parameter.

The power on script uses Wake On Lan to start the remote PC. To install Wake On Lan on the raspbian:

sudo apt-get install wakeonlan

A remote PC can be woken up by just running the command wakeonlan <mac address>. For example:

wakeonlan 54:b2:03:09:09:c4

Then create a directory for the script:

mkdir -p $HOME/.smart-pc-control/pc/nuc/state/on/

Edit the script file

nano -w $HOME/.smart-pc-control/pc/nuc/state/on/script.sh
# !/bin/bash

if [[ $1 == "true" ]] ; then
echo "[INFO] Waking up pluto"
wakeonlan <mac address>
fi

Make the script executable:

chmod +x $HOME/.smart-pc-control/pc/nuc/state/on/script.sh

Create the Power Off script on the PC

The Power Off script needs the same directories as the power on one. So, to create them:

mkdir -p $HOME/.smart-pc-control/pc/nuc/state/on/

Then, edit the file:

nano -w $HOME/.smart-pc-control/pc/nuc/state/on/script.sh

The contents should look like this:

 #! /bin/bash

if [[ $1 == "false" ]] ; then
echo "[INFO] About to shutdown the PC"
nohup sudo shutdown -h now &
else
echo "[INFO] Nothing to do … the flag is on"
fi

Make it executable:

chmod +x $HOME/.smart-pc-control/pc/nuc/state/on/script.sh

Launch the Home App on your Apple device, select the device and turn it on/off to test.

Published by

Otavio Piske

Just another nerd

Leave a Reply

Your email address will not be published. Required fields are marked *