Install REST
Controlling the home from distance can be made via standard web access methods such as the RESTful API.
Different options are available for the implementation of such a service:
- Installing a web server and writing CGI scipts
- Writing scripts serving a TCP socket and complying to the HTTP scheme
- Using a framework such as:
I have chosen Mojolicious Lite for its simplicity and the ease of development.
Before all, make sure you have installed an xPL hub together with the xPL state monitor.
Contents
Install Mojolicious Lite
Mojolicious::Lite can be installed with the standard CPAN method. For Debian and related distributions such as Ubuntu, a dedicated option is to use:
sudo apt install libmojolicious-perl
Test the REST server
Download the xPL home state REST interface script.
Start the server:
REST_PORT=13865 REST_SERVICE_DIRECTORY='/home/control/Documents/Controls' morbo -l http://*:$REST_PORT $REST_SERVICE_DIRECTORY/xpl-homeStateRest.pl
This starts an HTTP service. Each time the Perl script is updated, the service is updated too. Interpretor errors are displayed in the terminal in whick the morbo runs.
Test the service in another terminal window:
REST_PORT=13865 curl -X GET localhost:$REST_PORT/state/study/lights/ceiling && echo curl -X PUT localhost:$REST_PORT/state/study/lights/ceiling/on
Check the result in:
cat /tmp/xpl-homeStateRest.log
Install the REST server
Once the script works, stop it with ctrl-C
.
Launch it as a daemon:
$REST_SERVICE_DIRECTORY/xpl-homeStateRest.pl daemon -l http://*:$REST_PORT 2>/devull &
Stop the daemon:
pkill -f xpl-homeStateRest
Debian with SystemD
On Debian with systemd (such as Raspbian or Ubuntu), the scripts can be defined as services.
Edit /lib/systemd/system/xpl-homeStateRest.service
:
[Unit] Description=xPL home state REST interface After=network.target [Service] Type=simple User=control Group=users ExecStart=/home/control/Documents/Controls/xpl-homeStateRest.pl daemon -m production -l http://*:13865 Restart=always [Install] WantedBy=multi-user.target
Activate the service:
su systemctl enable xpl-homeStateRest.service service xpl-homeStateRest start
Reboot and check:
ps ax | grep -v grep | grep -i xpl ps aux | grep -i xpl | grep -v grep | sed 's/.*\/Controls\///' systemctl list-units --type=service --state=running | grep xpl service xpl-homeStateRest status