Install MSNswitch

From xPL
Jump to: navigation, search

The MSN switch consists of a pair of mains switches which can be controlled via Ethernet. In the given example, the switches control the power amplifiers of my lounge's speakers, hence the name of the device.

Before all, make sure you have installed an xPL hub.

Access the switches

Configure the switches

The switch can be discovered with the help of a web browser at URL: http://MSNswitch.

The web intergace allows to define a new user in Account, and to specify a fixed IP address in Network.

Switches command line control

Get switches state:

curl -u control: http://thanatos.cofnet/outlet_status.xml 2>/dev/null | perl -pe 's/(<[^\/])/\n$1/g' | perl -pe 's/(<\/request)/\n$1/g' && echo

Turn switch on and off again

curl -u control: http://thanatos.cofnet/control.cgi -d "outlet=1&command=1" && echo
sleep 1
curl -u control: http://thanatos.cofnet/control.cgi -d "outlet=1&command=0" && echo

Install xPL switches controller

Install HTTP::Tiny:

sudo apt-get install libhttp-tiny-perl

Alternatively, you can do it the CPAN way:

su
perl -MFile::Find=find -MFile::Spec::Functions -Tlw -e 'find { wanted => sub { print canonpath $_ if /\.pm\z/ }, no_chdir => 1 }, @INC' 2> /dev/null | grep -i http
perl -MCPAN -e "install 'HTTP::Tiny'"
exit

Download the switches controller script.

Move the script to xPL scripts directory:

SCRIPTS_BASE_DIR=/home/control/Documents/Controls
mv xpl-msnSwitch.pl $SCRIPTS_BASE_DIR
chown control:users $SCRIPTS_BASE_DIR/*.pl
chmod 775 $SCRIPTS_BASE_DIR/*.pl

Test xPL switches controller

Test it on a BeagleBone:

su control
SCRIPTS_BASE_DIR=/home/control/Documents/Controls
$SCRIPTS_BASE_DIR/xpl-msnSwitch.pl -vn loungeAmps

Open a 2nd terminal window (on any machine with xPL installed) and monitor the xPL protocol:

SCRIPTS_BASE_DIR=/home/control/Documents/Controls
$SCRIPTS_BASE_DIR/xpl-monitor.pl -vf

Open a 3rd terminal window (on any machine with xPL installed) and control the switches:

SCRIPTS_BASE_DIR=/home/control/Documents/Controls
$SCRIPTS_BASE_DIR/xpl-send.pl -v -d dspc-switch.loungeAmps -c control.request command=request
$SCRIPTS_BASE_DIR/xpl-send.pl -v -d dspc-switch.loungeAmps -c control.basic outlet=1 command=on
$SCRIPTS_BASE_DIR/xpl-send.pl -v -d dspc-switch.loungeAmps -c control.basic outlet=1 command=off

The first xpl-send command asks for the status of the switches. The answer is seen in the xPL traffic monitor.

Launch the xPL switches controller at startup

The xPL switches controller is to be launched after the xPL hub.

Ubuntu

On Ubuntu, the xPL switches controller starts-up once xpl-hub_started has been emitted.

Edit /etc/init/xpl-msnSwitch.conf:

################################################################################
# xPL MSN switch, controls dual mains switch
#
description "xPL MSN switch"
version     "1.0"
author      "Francois Corthay"

#-------------------------------------------------------------------------------
# Configuration variables
#
env SCRIPTS_DIR='/home/control/Documents/Controls'
env SCRIPT_NAME='xpl-msnSwitch.pl'
env PARAMETERS='-n loungeAmps'

#-------------------------------------------------------------------------------
# Start and stop conditions
#
start on xpl-hub_started
stop on shutdown
respawn
setuid control
setgid users

#-------------------------------------------------------------------------------
# Start daemon
#
exec $SCRIPTS_DIR/$SCRIPT_NAME $PARAMETERS

Start the device and test it:

su root
service xpl-msnSwitch start
service xpl-msnSwitch status
initctl list | grep xpl
ps aux | grep -i xpl | grep -v grep | sed 's/.*\/Controls\///'

Mac OS

On Mac OS, the xPL switches controller starts-up later than the hub.

Edit /Library/LaunchDaemons/xpl-msnSwitch.plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <key>Label</key>
    <string>xPL MSN switch controller</string>
    <key>OnDemand</key>
    <false/>
    <key>UserName</key>
    <string>control</string>
    <key>ProgramArguments</key>
    <array>
      <string>/Users/control/Documents/Controls/xpl-msnSwitch.pl</string>
      <string>-n</string>
      <string>loungeAmps</string>
      <string>-w</string>
      <string>25</string>
    </array>
  </dict>
</plist>

Load the xPL client:

launchctl load /Library/LaunchDaemons/xpl-msnSwitch.plist
launchctl list | grep -i xpl 
ps ax | grep -v grep | grep -i xpl