Install RPi relay board 4

From xPL
Revision as of 10:39, 9 March 2018 by Fcorthay (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

The Seeed RPi Relay Board houses 4 relays controlled by the I2C bus.

Access the switches

Enable I2C:

su
apt install -y i2c-tools
raspi-config

Enable access to the device for the control user:

I2C_USER='control'
usermod -aG i2c $I2C_USER

Check the installation:

su $I2C_USER
ls -l /dev/i2c*
i2cdetect -y 1

Test the relays:

CHIP_ADDRESS='0x20'
REGISTER_ADDRESS='0x06'
i2cdump -y 1 $CHIP_ADDRESS
i2cget -y 1 $CHIP_ADDRESS $REGISTER_ADDRESS
i2cset -y 1 $CHIP_ADDRESS $REGISTER_ADDRESS 0xF0
i2cset -y 1 $CHIP_ADDRESS $REGISTER_ADDRESS 0xFF

Install xPL switches controller

Download the switches controller script.

Move the script to xPL scripts directory:

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

Test xPL switches controller

Test it:

su control
SCRIPTS_BASE_DIR=/home/control/Documents/Controls
$SCRIPTS_BASE_DIR/xpl-piSwitches.pl -vn ampSwitch

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.ampSwitch -c control.request command=request
$SCRIPTS_BASE_DIR/xpl-send.pl -v -d dspc-switch.ampSwitch -c control.basic outlet=1 command=on
$SCRIPTS_BASE_DIR/xpl-send.pl -v -d dspc-switch.ampSwitch -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

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

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

################################################################################
# xPL Raspberry Pi switches, controls 4 relays
#
description "xPL RPi switches"
version     "1.0"
author      "Francois Corthay"

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

#-------------------------------------------------------------------------------
# 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-piSwitches start
service xpl-piSwitches status
initctl list | grep xpl | sort
ps aux | grep -i xpl | grep -v grep | sed 's/.*\/Controls\///'

Debian with SystemD

On Debian with systemd (such as https://www.raspbian.org/), the scripts can be defined as services.

Edit /lib/systemd/system/xpl-piSwitches.service:

[Unit]
Description=RPi 4-line switches xPL controller
After=network.target

[Service]
Type=simple
ExecStart=/home/control/Documents/Controls/xpl-piSwitches.pl -n ampSwitch -w 2
Restart=always

[Install]
WantedBy=multi-user.target

Activate the service:

su
systemctl enable xpl-piSwitches.service
service xpl-piSwitches start

Reboot and check:

ps ax | grep -v grep | grep -i xpl
service xpl-piSwitches status

Install xPL buttons monitor

Download the buttons monitor script.

Move the script to xPL scripts directory:

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

Test xPL buttons monitor

Test it:

su control
SCRIPTS_BASE_DIR=/home/control/Documents/Controls
$SCRIPTS_BASE_DIR/xpl-piButtons.pl -vn ampSwitch

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

Press on a button. The corresponding message is seen in the xPL traffic monitor.

Launch the xPL buttons monitor at startup

Debian with SystemD

On Debian with systemd (such as Raspbian), the scripts can be defined as services.

Edit /lib/systemd/system/xpl-piButtons.service:

[Unit]
Description=RPi buttons xPL monitor
After=network.target

[Service]
Type=simple
ExecStart=/home/control/Documents/Controls/xpl-piButtons.pl -n ampSwitch -w 2
Restart=always

[Install]
WantedBy=multi-user.target

Activate the service:

su
systemctl enable xpl-piButtons.service

Reboot and check:

ps ax | grep -v grep | grep -i xpl
service xpl-piButtons status