Categories
Coding Fedora Linux

Controlling a PC via Apple HomeKit: part 2

On the first part of the tutorial, I showed how to install the infrastructure software to allow Apple devices to control a PC via Homekit. Now I am going to show how to install the services that interface with the infrastructure to execute the actions triggered by the Apple devices.

Installing the power on service on the Raspberry PI

The first service to install is the service responsible for powering on the PC using WakeOnLan. This is done using this small project I wrote: Smart PC Control.

This software is not packaged for Raspbian and it needs to be compiled from source along with 2 of its dependencies. Luckily this is a simple process.

First, let’s start by installing the build dependencies:

sudo apt-get install  build-essential gcc make cmake libjson-c-dev uuid-dev liburiparser-dev

With the build dependencies installed, we can proceed to install the first dependency: GRU.

git clone https://github.com/orpiske/gru.git
cd gru
mkdir build && cd build
cmake ..
make
sudo make install

Now we need to install the Eclipse Paho C MQTT client:

git clone https://github.com/eclipse/paho.mqtt.c.git -b v1.3.1
cd paho.mqtt.c
mkdir build && cd build
cmake .. && make
sudo make install

And, finally, the Smart PC Control project:

git clone https://github.com/orpiske/smart-pc-control.git
mkdir build && cd build
cmake ..
make
sudo make install

With this project installed we can proceed to its configuration.

Configuring the Power On Service on the Raspberry Pi

The Smart PC Control project install SystemD unit files and its respective configuration.

First, let’s edit the service configuration

cp /etc/sysconfig/smart-pc-control-power.set-me-up /etc/sysconfig/smart-pc-control-power
sudo nano -w /etc/sysconfig/smart-pc-control-power

The configuration should look similar to this:

# The URL of the MQTT broker
MQTT_BROKER_URL=tcp://localhost:1883

# Replace changeme with the hostname where the system is running
CLIENT_ID="power-control-changeme"

# Uncomment for clients that don't store any state
DAEMON_OPTIONS="--stateless"

# Ensure to export library dir if not using a standard location
# LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib

Now, let’s edit the System D unit file: /usr/lib/systemd/system/smart-pc-control-power@.service.

The content should look like this:

[Unit]
Description=Smart PC Control - Power Daemon
After=syslog.target network.target

[Service]
User=%i
Type=forking
EnvironmentFile=-//etc/sysconfig/smart-pc-control-power
ExecStart=/usr/local/bin/smart-pc-control-power -d -b $MQTT_BROKER_URL -i $CLIENT_ID $DAEMON_OPTIONS
PrivateTmp=false
Restart=on-failure

[Install]
WantedBy=multi-user.target

Enable the service

To enable the service for the pi user, run:

sudo systemctl enable --now smart-pc-control-power@pi.service

Install the Power Off Service on the PC

This part assumes a PC running Fedora. For other distributions or OSes, you have to build the tool manually and install it.

The software is fully package for Fedora and available on the awesome Fedora COPR. To do so:

sudo dnf copr enable orpiske/orp-tools-testing
sudo dnf install -y smart-pc-control

With the services installed, we can proceed to configuring its daemon

sudo nano -w /etc/sysconfig/smart-pc-control-power

The contents of the file should look like this:

# The URL of the MQTT broker
MQTT_BROKER_URL=tcp://mimas:1883

# Replace changeme with the hostname where the system is running
CLIENT_ID="power-control-changeme"

# Uncomment for clients that don't store any state
# DAEMON_OPTIONS="--stateless"

# Ensure to export library dir if not using a standard location
# LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib64

Note that the DAEMON_OPTIONS is commented. The Power Off doesn’t need it.

To enable the service, run:

sudo systemctl enable --now smart-pc-control-power@$USER.service

Note: replace USER with your actual user

This should be all that is required to install and run the power daemons.

Go to part 3.

By Otavio Piske

Just another nerd

Leave a Reply

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