Commit 4a242b1b authored by Charles Luzzato's avatar Charles Luzzato
Browse files

Added relay.sh script

parents
#!/bin/sh
# Version: 0.01 2014-11-28 GE
# Original.
#================================================= ==========================
# This script polls LMS at a regular interval to determine the current
# status of the Squeezelite process on piCorePlayer. i.e. play or stop.
#
# A GPIO is then set/reset depending on the result of this status check.
#
# There is an adjustable relay off delay that can be used as an inactivity
# timing.
#---------------------------------------------------------------------------
#================================================= ==========================
# Possible issues
#---------------------------------------------------------------------------
# 1. Initial relay status during booting. It is probably better to start
# this script before starting Squeezelite. Also, a pull-up or pull-down
# resistor may be needed to ensure initial state of GPIO.
# 2. If setting MAC address manually, pay close attention to the physical,
# wireless and fake MAC addresses.
# 3. This script hasn't been tested on synced players.
#---------------------------------------------------------------------------
#================================================= ==========================
# Test procedure
#---------------------------------------------------------------------------
# sudo killall relay.sh
# sudo sh -c 'echo "18" > /sys/class/gpio/unexport'
# sudo ./relay.sh > /dev/null &
#---------------------------------------------------------------------------
#set -x
. /home/tc/www/cgi-bin/pcp-functions
pcp_variables
#================================================= ==========================
# Set the following according to your setup
#---------------------------------------------------------------------------
MAC_ADDR=80:1f:02:ed:f9:be # Raspberry Pi MAC address
#MAC_ADDR=$(pcp_controls_mac_address) # Set by pCP
LMS_IP=192.168.49.7 # LMS IP address
#LMS_IP=$(pcp_lmsip) # Set by pCP
INTERVAL=0.5 # Set Poll interval
GPIO=18 # Set GPIO
COMMAND="status 0 0" # LMS player status command
DELAYOFF=20 # Delay in no. of intervals
COUNT=0
DEBUG=0
TURNED_ON=0
#---------------------------------------------------------------------------
if [ $DEBUG = 1 ]; then
echo
echo "MAC_ADDR : "$MAC_ADDR
echo "LMS_IP : "$LMS_IP
echo "INTERVAL : "$INTERVAL
echo "GPIO : "$GPIO
echo "COMMAND : "$COMMAND
echo "DELAYOFF : "$DELAYOFF
echo
fi
get_mode() {
RESULT=`( echo "$MAC_ADDR $COMMAND"; echo exit ) | nc $LMS_IP 9090`
echo $RESULT | grep "mode%3Aplay" > /dev/null 2>&1
#echo $RESULT | grep "power%3A1" > /dev/null 2>&1
if [ $? == 0 ]; then
echo "Playing. Count: $COUNT"
COUNT=0
if [ $TURNED_ON == 0 ]; then
turn_on
fi
else
if [ $COUNT -ge $DELAYOFF ]; then
#echo "Stopped. Count: $COUNT"
if [ $TURNED_ON == 1 ]; then
turn_off
fi
COUNT=0
else
#COUNT=`expr $COUNT + 1` #or
COUNT=$(($COUNT + 1))
echo "Stopped. Count: $COUNT"
fi
fi
}
turn_on() {
echo "1" > /sys/class/gpio/gpio$GPIO/value
sleep .5
echo "0" > /sys/class/gpio/gpio$GPIO/value
TURNED_ON=1
}
turn_off() {
echo "1" > /sys/class/gpio/gpio$GPIO/value
sleep .5
echo "0" > /sys/class/gpio/gpio$GPIO/value
TURNED_ON=0
}
#================================================= ==========================
# Initial GPIO setup
#---------------------------------------------------------------------------
sudo sh -c 'echo '"$GPIO"' > /sys/class/gpio/export'
# My relay is active low, so this reverses the logic
sudo sh -c 'echo "1" > /sys/class/gpio/gpio'"$GPIO"'/active_low'
sudo sh -c 'echo "out" > /sys/class/gpio/gpio'"$GPIO"'/direction'
echo "0" > /sys/class/gpio/gpio$GPIO/value
#---------------------------------------------------------------------------
#================================================= ==========================
# Loop forever. This uses less the 1% CPU, so it should be OK.
#---------------------------------------------------------------------------
while true
do
get_mode
sleep $INTERVAL
done
#---------------------------------------------------------------------------
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment