Difference between revisions of "Install RPi relay board"

From xPL
Jump to: navigation, search
(Debian with SystemD)
(Debian with SystemD)
 
(8 intermediate revisions by one user not shown)
Line 9: Line 9:
 
= Access the switches =
 
= Access the switches =
  
Check the [http://www.waveshare.com/wiki/Libraries_Installation_for_RPi#Install_WiringPi_Library download link]
+
Install [https://projects.drogon.net/raspberry-pi/wiringpi/download-and-install/ WiringPi]:
for the following <code>wget</code> command and install the library:
+
 
  su
 
  su
 +
apt install -y git-core
 
  cd /tmp/
 
  cd /tmp/
  wget http://www.waveshare.com/w/upload/f/f3/WiringPi.tar.gz
+
  git clone git://git.drogon.net/wiringPi
tar -xvzf WiringPi.tar.gz
+
 
  cd wiringPi/
 
  cd wiringPi/
  chmod 777 build
+
  git pull origin
 
  ./build
 
  ./build
  
 
Check the installation:
 
Check the installation:
gpio -v
 
 
  which gpio
 
  which gpio
 +
gpio -v
 
  gpio readall
 
  gpio readall
  
Line 28: Line 27:
 
  gpio mode $PIN output ; gpio write $PIN 0 ; sleep 1 ; gpio write $PIN 1
 
  gpio mode $PIN output ; gpio write $PIN 0 ; sleep 1 ; gpio write $PIN 1
 
   
 
   
 +
# for PIN in `seq 1 30`; do
 
  for PIN in 25 28 29; do
 
  for PIN in 25 28 29; do
 
   echo driving pin $PIN
 
   echo driving pin $PIN
Line 36: Line 36:
 
   sleep 1
 
   sleep 1
 
  done
 
  done
 +
 +
= Access the buttons =
 +
 +
Install [https://projects.drogon.net/raspberry-pi/wiringpi/download-and-install/ WiringPi]:
 +
su
 +
apt install -y git-core
 +
cd /tmp/
 +
git clone git://git.drogon.net/wiringPi
 +
cd wiringPi/
 +
./build
 +
 +
Check the installation:
 +
which gpio
 +
gpio -v
 +
gpio readall
 +
 +
Test the button:
 +
PIN=4
 +
gpio mode $PIN up ; gpio read $PIN
 +
 +
Tester the button diode:
 +
PIN=1
 +
gpio mode $PIN output ; gpio write $PIN 1 ; sleep 1 ; gpio write $PIN 0
  
 
= Install xPL switches controller =
 
= Install xPL switches controller =
  
Download the [http://www.dspc.ch/xPL/Downloads/xpl-piSwitch.pl switches controller] script.
+
Download the [http://www.dspc.ch/xPL/Downloads/xpl-piSwitches-gpio.pl switches controller] script.
  
 
Move the script to xPL scripts directory:
 
Move the script to xPL scripts directory:
 
  SCRIPTS_BASE_DIR=/home/control/Documents/Controls
 
  SCRIPTS_BASE_DIR=/home/control/Documents/Controls
  mv xpl-piSwitch.pl $SCRIPTS_BASE_DIR
+
  mv xpl-piSwitches-gpio.pl $SCRIPTS_BASE_DIR/xpl-piSwitches.pl
 
  chown control:users $SCRIPTS_BASE_DIR/*.pl
 
  chown control:users $SCRIPTS_BASE_DIR/*.pl
 
  chmod 775 $SCRIPTS_BASE_DIR/*.pl
 
  chmod 775 $SCRIPTS_BASE_DIR/*.pl
Line 52: Line 75:
 
  su control
 
  su control
 
  SCRIPTS_BASE_DIR=/home/control/Documents/Controls
 
  SCRIPTS_BASE_DIR=/home/control/Documents/Controls
  $SCRIPTS_BASE_DIR/xpl-piSwitch.pl -vn loungeAmps
+
  $SCRIPTS_BASE_DIR/xpl-piSwitches.pl -vn loungeAmps
  
 
Open a 2nd terminal window (on any machine with xPL installed) and monitor the xPL protocol:
 
Open a 2nd terminal window (on any machine with xPL installed) and monitor the xPL protocol:
Line 72: Line 95:
 
the xPL switches controller starts-up once <code>xpl-hub_started</code> has been emitted.
 
the xPL switches controller starts-up once <code>xpl-hub_started</code> has been emitted.
  
Edit <code>/etc/init/xpl-piSwitch.conf</code>:
+
Edit <code>/etc/init/xpl-piSwitches.conf</code>:
 
  ################################################################################
 
  ################################################################################
  # xPL Raspberry Pi switch, controls 3 relays
+
  # xPL Raspberry Pi switches, controls 3 relays
 
  #
 
  #
  description "xPL RPi switch"
+
  description "xPL RPi switches"
 
  version    "1.0"
 
  version    "1.0"
 
  author      "Francois Corthay"
 
  author      "Francois Corthay"
Line 84: Line 107:
 
  #
 
  #
 
  env SCRIPTS_DIR='/home/control/Documents/Controls'
 
  env SCRIPTS_DIR='/home/control/Documents/Controls'
  env SCRIPT_NAME='xpl-piSwitch.pl'
+
  env SCRIPT_NAME='xpl-piSwitches.pl'
 
  env PARAMETERS='-n loungeAmps'
 
  env PARAMETERS='-n loungeAmps'
 
   
 
   
Line 103: Line 126:
 
Start the device and test it:
 
Start the device and test it:
 
  su root
 
  su root
  service xpl-piSwitch start
+
  service xpl-piSwitches start
  service xpl-piSwitch status
+
  service xpl-piSwitches status
 
  initctl list | grep xpl | sort
 
  initctl list | grep xpl | sort
 
  ps aux | grep -i xpl | grep -v grep | sed 's/.*\/Controls\///'
 
  ps aux | grep -i xpl | grep -v grep | sed 's/.*\/Controls\///'
Line 113: Line 136:
 
the scripts can be defined as services.
 
the scripts can be defined as services.
  
Edit <code>/lib/systemd/system/xpl-piSwitch.service</code>:
+
Edit <code>/lib/systemd/system/xpl-piSwitches.service</code>:
 
  [Unit]
 
  [Unit]
  Description=RPi 3-line switch xPL controller
+
  Description=RPi 3-line switches xPL controller
 
  After=network.target
 
  After=network.target
 
   
 
   
 
  [Service]
 
  [Service]
 
  Type=simple
 
  Type=simple
  ExecStart=/home/control/Documents/Controls/xpl-piSwitch.pl -n loungeAmps -w 2
+
  ExecStart=/home/control/Documents/Controls/xpl-piSwitches.pl -n loungeAmps -w 2
 
  Restart=always
 
  Restart=always
 
   
 
   
Line 128: Line 151:
 
Activate the service:
 
Activate the service:
 
  su
 
  su
  systemctl enable xpl-piSwitch.service
+
  systemctl enable xpl-piSwitches.service
  service xpl-piSwitch start
+
  service xpl-piSwitches start
  
 
Reboot and check:
 
Reboot and check:
 
  ps ax | grep -v grep | grep -i xpl
 
  ps ax | grep -v grep | grep -i xpl
  service xpl-piSwitch status
+
  service xpl-piSwitches status
  
 
= Install xPL buttons monitor =
 
= Install xPL buttons monitor =
Line 182: Line 205:
 
  su
 
  su
 
  systemctl enable xpl-piButtons.service
 
  systemctl enable xpl-piButtons.service
 +
service xpl-piButtons start
  
 
Reboot and check:
 
Reboot and check:

Latest revision as of 11:57, 9 March 2018

The Waveshare RPi Relay Board houses 3 relays controlled by Raspberry Pi General-Purpose Input/Outputs (GPIOs).

They can be controlled by:

Access the switches

Install WiringPi:

su
apt install -y git-core
cd /tmp/
git clone git://git.drogon.net/wiringPi
cd wiringPi/
git pull origin
./build

Check the installation:

which gpio
gpio -v
gpio readall

Test the relays:

PIN=25
gpio mode $PIN output ; gpio write $PIN 0 ; sleep 1 ; gpio write $PIN 1

# for PIN in `seq 1 30`; do
for PIN in 25 28 29; do
  echo driving pin $PIN
  gpio mode $PIN output
  gpio write $PIN 0
  sleep 1
  gpio write $PIN 1
  sleep 1
done

Access the buttons

Install WiringPi:

su
apt install -y git-core
cd /tmp/
git clone git://git.drogon.net/wiringPi
cd wiringPi/
./build

Check the installation:

which gpio
gpio -v
gpio readall

Test the button:

PIN=4
gpio mode $PIN up ; gpio read $PIN

Tester the button diode:

PIN=1
gpio mode $PIN output ; gpio write $PIN 1 ; sleep 1 ; gpio write $PIN 0

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-gpio.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 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

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 3 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 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-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 3-line switches xPL controller
After=network.target

[Service]
Type=simple
ExecStart=/home/control/Documents/Controls/xpl-piSwitches.pl -n loungeAmps -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 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

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 loungeAmps -w 2
Restart=always

[Install]
WantedBy=multi-user.target

Activate the service:

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

Reboot and check:

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