Home of the Squeezebox™ & Transporter® network music players.
Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 29
  1. #11
    Senior Member
    Join Date
    Feb 2006
    Posts
    138

    Play state to IFTTT (or similar)

    I like the idea of tracking play state. I have several pi core players using HiFi Berry Pro XLR. These are connected to powered monitors around my home. The Pi is powered over Ethernet, so always powered on.

    I'd like to use the play state of the Pi to trigger a remote controlled outlet to turn on the speakers. I already have the Amazon outlets that works with Alexa. So, an IFTTT action should be able to do this.

    Sadly, I am not a developer. I'll need some help.
    Michael Graves

    House: RPi3 + HifiBerry Pro XLR, M-Audio BX5As
    Home Office: RPi3 + HifiBerry Pro XLR, M-Audio BX5A D2, Berhringer B2092A subwoofer
    Workshop: RPi3 + HifiBerry Pro XLR, Alesis M520 Active
    Back Yard: HiFiBerryOS on Pi3, Dayton Amp, Definitive Tech AW6500s
    Front Yard: SB3, SMSL Q5 Amp, Definitive Tech AW5500s
    LMS via PiCorePlayer on Pi4

    email: mgraves <at> mstvp.com
    blog: http://www.mgraves.org

  2. #12
    Senior Member
    Join Date
    Sep 2005
    Posts
    2,853
    Quote Originally Posted by mgraves View Post
    Sadly, I am not a developer. I'll need some help.
    Make some script like amp_power.sh with something that powers the wifi-plug.

    eg: (made for a Tasmota driven device without using mqtt "for this example" -in real life its using mqtt instead of curl)
    Code:
    #!/bin/bash
    port=9090
    server=lms.server.ip.address
    muzzigbox=ma:ca:dd:re:ss from the used player
    amp=IP.of.your.wifiswitch
    now=$(date +%H:%M)
    up=$(ps aux |grep squeezelite |grep -v grep|awk '{print $9}')
    if [ ! "$now" == "$up"  ]
       then
       sleep 2
       status=$(printf "$muzzigbox mode ?\nexit\n" | nc $server $port | cut -d ' ' -
       if [ $status = play ]
             then
             curl http://$amp/cm?cmnd=Power%20ON
       else
             curl http://$amp/cm?cmnd=Power%20OFF
       fi
    else
            echo detect startup
            printf "$muzzigbox power 0 \nexit\n"|nc $server $port
            curl http://$amp/cm?cmnd=Power%20OFF
    fi
    Use this script or similar for your -S option in squeezelite

    Take a look at this Thread

    The first script poweres the amp on / off if you press the powerbutton on the lms gui/ power the squeezelite player in any way.
    The 2.nd script that should run each xx minutes as a cronjob "unpowers the squeezeliteplayer if its not playing anymore" and that power cycle starts the poweroff for your Amp.

  3. #13
    Senior Member
    Join Date
    Feb 2006
    Posts
    138
    Quote Originally Posted by DJanGo View Post
    Take a look at this Thread

    The first script poweres the amp on / off if you press the powerbutton on the lms gui/ power the squeezelite player in any way.
    The 2.nd script that should run each xx minutes as a cronjob "unpowers the squeezeliteplayer if its not playing anymore" and that power cycle starts the poweroff for your Amp.
    Thanks for the info!

    I'm not concerned about powering off the plugin. That can be set up occur automatically on a scheduled basis. That is, turn it off every night at 11pm.
    Michael Graves

    House: RPi3 + HifiBerry Pro XLR, M-Audio BX5As
    Home Office: RPi3 + HifiBerry Pro XLR, M-Audio BX5A D2, Berhringer B2092A subwoofer
    Workshop: RPi3 + HifiBerry Pro XLR, Alesis M520 Active
    Back Yard: HiFiBerryOS on Pi3, Dayton Amp, Definitive Tech AW6500s
    Front Yard: SB3, SMSL Q5 Amp, Definitive Tech AW5500s
    LMS via PiCorePlayer on Pi4

    email: mgraves <at> mstvp.com
    blog: http://www.mgraves.org

  4. #14
    Junior Member
    Join Date
    Jul 2020
    Posts
    29
    Quote Originally Posted by Greg Erskine View Post
    hi Axel,

    Although not a good as an interrupt solution a simple loop inquiry of LMS may be OK.

    The command "pcp mode" returns "play" or "stop". Do the inquiry every second and set relay appropriate. This only create a 1% load on CPU and little network traffic, not enough to cause problems.

    regards
    Greg
    Do you happen to know why "pcp mode" does not distinguish "stop" and "power off" state? It returns "stop" in either state.

    Code:
    tc@Konyha:~$ pcp play
    tc@Konyha:~$ pcp mode
    play
    tc@Konyha:~$ pcp pause
    tc@Konyha:~$ pcp mode
    pause
    tc@Konyha:~$ pcp stop
    tc@Konyha:~$ pcp mode
    stop
    tc@Konyha:~$ pcp play
    tc@Konyha:~$ pcp mode
    play
    tc@Konyha:~$ pcp power off
    tc@Konyha:~$ pcp mode
    stop
    tc@Konyha:~$
    Last edited by sakos; 2020-08-07 at 09:47. Reason: add output

  5. #15
    Senior Member Greg Erskine's Avatar
    Join Date
    Sep 2006
    Location
    Sydney, Australia
    Posts
    1,917
    hi sakos,

    Generally speaking the "pcp commands" are only a simple interface into LMS commands. They can only respond as LMS responds.

    Having said that, reading the documentation we should get an "undefined" value. Maybe we have coded a default value of stop.

    Code:
    <playerid> mode ?
    
    The "mode" command allows to query the player state and returns one of "play", "stop" or "pause". If the player is off, "mode ?" returned value is undefined.
    
    Example:
    
    Request: "04:20:00:12:23:45 mode ?<LF>"
    Response: "04:20:00:12:23:45 mode stop<LF>"
    I have added your post to the list of things todo.

    regards
    Greg

  6. #16
    Ok, my first (b)ash script, actually my first work on linux For newbees like me (working with Windows normaly):

    - use MobaXTerm to ssh to your piCorePlayer, create a new file in /home/tc/ps2gpio.sh
    - double click ps2gpio.sh in the MobaXTerm explorer view to edit the file
    - copy and paste the script below, save and accept request with "all"
    - In terminal Window - provided you will use GPIO4 set to 3V when squeezelite ist playing:

    chmod +xX ps2gpio.sh
    sudo ./ps2gpio.sh -g 4


    If you think thats fine, you can put it to Autostart:
    - Web browser to your picoreplayer
    - Tweaks - User commands:
    sudo /home/tc/ps2gpio.sh -g 4 -d

    Sorry for the noisy explenations. Here is the script:
    ------------------------------------------------------------------------------------------
    Code:
    #!/bin/sh
    
    BASE_GPIO_PATH=/sys/class/gpio
    
    
    function Usage () {
      echo ""
      echo "PlayState to GPIO (ps2gpio) sets a GPIO when squeezelite is playing."
      echo "--------------------------------------------------------------------"
      echo "Usage: sudo $0 -g GPIO [-s n] [-d] [-h] [-v]"
      echo ""
      echo "-g : GPIO-number"
      echo "-s : Set value for \"play\" state: 0,1. Default=1 sets GPIO to 3V when playing"
      echo "-d : Daemonize: Start running in backgrund and exit"
      echo "-v : Verbose output when daemonized" 
      echo "-h : Shows this help"
      echo ""
      echo "Examples:"
      echo "---------"
      echo "  sudo $0 -g 27           : Sets GPIO27 to 3V when playing, stop running with Ctrl-c"  
      echo "  sudo $0 -g 4 -s 0 -d    : Sets GPIO4  to 0V when playing, running in background"
      echo "  sudo $0 -g 4 -s 1 -d -v : Sets GPIO4  to 3V when playing, running in background with status output"
      echo ""
    exit 1
    } # function Usage
    
    
    function err () {
      echo "$1" >&2; Usage 
    } # function err
    
    
    # Parameter
    
    
    # Defaults
    Daemonize=false
    PlayVal=1
    Verbose=false
    
    
    if [ ! $1 ]; then Usage; fi
    
    
    while getopts g:s:dhv opt 
    do 
      case $opt in
        g ) # Check for valid GPIO
            if [ "$OPTARG" -ge 2 ] && [ "$OPTARG" -le 27 ]; then
              myGPIO=$OPTARG
            else
              err "\"$OPTARG\" is not a valid GPIO, please use 2 .. 27"  
            fi # Check for valid GPIO
          ;;
        s ) # Check parameter for setting GPIO to 0 or 1 when playing
            if [ $OPTARG ]; then # Set GPIO to 0 or 1 when playing
              if [ "$OPTARG" -eq 0 ] || [ "$OPTARG" -eq 1 ]; then     
                PlayVal=$OPTARG 
              else
                err "\"$OPTARG\" is not a valid GPIO state, please use 0 or 1"
              fi
            else # no parameter
              PlayVal=1 # default
            fi # Check parameter for setting GPIO to 0 or 1 when playing
          ;;
        d ) Daemonize=true ;;
        v ) Verbose=true;;      
        h ) Usage;;
      esac
    done
    if [ ! $myGPIO ]; then err "Mandatory parameter -g \"GPIO\" is missing"; fi
    
    
    echo "$0 -g $myGPIO -s $PlayVal -d $Daemonize -v $Verbose"
    
    
    # Init
    if [ ! -e $BASE_GPIO_PATH/gpio$myGPIO ]; then # Init GPIO
      echo "Export GPIO $myGPIO to userspace"
      echo "$myGPIO" > $BASE_GPIO_PATH/export
    fi # Init GPIO
    
    
    echo Set GPIO $myGPIO as an output
    echo "out" > $BASE_GPIO_PATH/gpio$myGPIO/direction
    
    
    # wait for squeeze to come online
    wt=0
    until pids=$(pidof squeezelite squeezelite-dsd)
    do
      sleep 1; wt=$(($wt+1))
      echo "Waited $wt seconds for for squeezelite ..." 
      if [ "$wt" -ge 60 ]; then err "Waited too long for squeezelite"; fi
    done
    
    
    # read actual GPIO value - gives 0 ?
    # oldGPIOval=`cat /sys/class/gpio/gpio$myGPIO/value`
    # echo "oldGPIOval is $oldGPIOval"
    
    
    function Loop () { 
      while true
      do  
        STATE=`(pcp mode)`
        if [ ! "$STATE" = "$OldState" ]; then # STATE change
          echo pcp State is $STATE
          if [ "$STATE" = "play" ]; then GPIOval=$PlayVal; else GPIOval=$((!$PlayVal)); fi # Set GPIO value
          if [ ! "$GPIOval" = "$oldGPIOval" ]; then
            echo Set GPIO $myGPIO to $GPIOval
            echo "$GPIOval" > $BASE_GPIO_PATH/gpio$myGPIO/value
            oldGPIOval=$GPIOval
          fi # GPIO val change
          OldState=$STATE 
        fi # STATE change
        sleep 0.5
      done # while true
    } # function Loop
    
    
    if $Daemonize; then # daemonize
      if $Verbose; then
        echo "Daemonizing with output." 
        Loop &
      else  
        echo "Daemonizing." 
        Loop > /dev/null 2>&1 &
      fi
    else # run in foreground:
      Loop
    fi 
    exit 0
    ------------------------------------------------------------------------------------------
    Not very well tested (On a fresh pcp 6.1 ok). Should work nevertheless.

    have fun
    Aki

  7. #17
    P.S. Dont forget to backup :-)

  8. #18
    Senior Member Greg Erskine's Avatar
    Join Date
    Sep 2006
    Location
    Sydney, Australia
    Posts
    1,917
    I wish my first shell script looked that good!

  9. #19
    Thanks Greg :-) Was a long way of learning from early Fortan (Punch cards) to Powershell nowadays.
    I am motivated to start with C on piCorePlayer. Is there any helping starting point on piCore for newbees?
    Aki

  10. #20
    Senior Member paul-'s Avatar
    Join Date
    Jan 2013
    Posts
    3,364
    piCorePlayer a small player for the Raspberry Pi in RAM.
    Homepage: https://www.picoreplayer.org

    Please donate if you like the piCorePlayer

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •