0

I am fairly new to bash script even though I have some experience.

I am looking for my Raspberry Pi to detect my phone when it becomes available on the network, when it does so to play an audio clip, I have managed do this via the script below.

I have a problem, however, when my phone becomes available on the network, I do not want the audio to keep looping; I need it to play once and then stop playing the audio clip once it has already played. However, I do want the script to keep on running so it can detect the next time my phone becomes available on the network.

Maybe there is a better way of doing it, if there is I would love to hear your suggestions.

#!/bin/sh

if ping -c 10 192.168.1.4 &> /dev/null
then
    kodi-send --action="PlayMedia(/storage/music/welcome.mp3)"
    ping 192.168.1.4 &> /dev/null
else
    ./checkforerikphone.sh
fi
Pang
  • 9,564
  • 146
  • 81
  • 122

2 Answers2

3

try this

#!/bin/bash

while : ; do
    if ping -c 10 192.168.1.4 &> /dev/null ; then
       kodi-send --action="PlayMedia(/storage/music/welcome.mp3)"
    fi
    sleep 600  
done

This solution runs forever while :. and every 10 minutes, checks if your phone is active. So this significantly reduces the noise in your life, but it also lets you know that your phone is still connected.

You could change sleep 600 to sleep 300 and check every 5 minutes, or of course you can change 600 to any number of seconds you comforatable with.

Not a perfect solution per your spec, but managing lock files can be complicated. Get comfortable with this solution and then think about adding something like

 if ping ... ; then
   if ! [[ -e /tmp/phoneOnLine ]] ; then
     kodi-send ...
     echo "Found phone at $(date)" > /tmp/phoneOnLine
   fi
else
    echo "no phone found"
    /bin/rm -f /tmp/phoneOnLine
fi

You will definitely find corner cases where this doesn't work, so be prepared to debug the code. I would add an echo msg inside of each logic path (if/else/...). to understand how the code is working.

Also to prevent script for being faked out, I would delete the file at startup.

So a possible complete solution is

#!/bin/bash

#file may not exist, ignore error msg with "2> /dev/null"
/bin/rm -f /tmp/phoneOnLine 2> /dev/null

#loop forever   
while : ; do
    # check for phone
    if ping -c 10 192.168.1.4 &> /dev/null ; then
        # check for "lock" file
        if ! [[ -e /tmp/phoneOnLine ]] ; then
           kodi-send --action="PlayMedia(/storage/music/welcome.mp3)"                              
           echo "Found phone at $(date)" > /tmp/phoneOnLine
        else
           echo "Phone already found" 
        fi   # !! -e /tmp/ph
    else     # no ping
         echo "no phone found"
         /bin/rm -f /tmp/phoneOnLine 2>/dev/null
    fi       # ping
    sleep 600  
done
shellter
  • 36,525
  • 7
  • 83
  • 90
2

Try following:

#!/bin/bash

#when result of ping $? is 0, the phone is detected
ACTIVE=0

#default startup as NOT ACTIVE(not detected) => !0
pre_available=1

# loop forever
while :
do
    #ping and store outcome in "available"
    ping -c 10 192.168.0.8 > /dev/null
    available=$?    
    #if phone was not active and just got active (detected)
    if [[ "$pre_available" != "$ACTIVE"  && "$available" == "$ACTIVE" ]]
    then
        #do your stuff
        kodi-send --action="PlayMedia(/storage/music/welcome.mp3)"              
    fi
    #store availability for next iteration
    pre_available=$available
done
VK Kashyap
  • 168
  • 1
  • 10