use a Foscam ipcam as motion & sound detector

Preface

I wanted virtual motion and sound detectors in Domoticz to be able to script with.

Created a script which reads out the motion and sound flags (my cam has motion and sound detection capabilities on board and offers them trough the web-interface).

  1. no external packages needed;
  2. note: usually the default port on foscams is 88!
  3. chmod +x the hell out of it before use (I keep forgetting that)
  4. schedule it every minute (logic is in the script)

Code

#!/bin/bash
# Script to use the sound detection features of your Foscam (e.g. C1) and use these as virtual sensors in Domoticz
# Schedule this every minute with crontab -e, for example: */1 * * * * /path/to/script/foscam.sh
# variables to change so your setup
#
FOSCAMIP="xxx.xxx.xxx.xxx"
FOSCAM_PORT="xx"
FOSCAM_USER="xxx"
FOSCAM_PASS="xxx"
#
DOMO_IP="xxx.xxx.xxx.xxx"
DOMO_PORT="xxx"
DOMO_USER="xxx"
DOMO_PASS="xxx"
#
FOSCAM_SOUND_IDX="xxx"
FOSCAM_MOTION_IDX="xxx"
#
# no need to edit after this point ...
# foscam sound detection loop is 5s by default. 5s * 12 = 1 minute (for cron can only schedule every minute)
for i in {1..12}
do
# if cam is pingable continue, else don't do anything.
if ping -c 1 $FOSCAMIP &> /dev/null
then
curl "http://$FOSCAMIP:$FOSCAM_PORT/cgi-bin/CGIProxy.fcgi?cmd=getDevState&usr=$FOSCAM_USER&pwd=$FOSCAM_PASS" > Foscam.txt
# check motionDetectAlarm and soundAlarm flags
FOSCAMSound=`grep -oP '(?<=).*(?=<\/soundAlarm>)' Foscam.txt`
FOSCAMMotion=`grep -oP '(?<=).*(?=<\/motionDetectAlarm>)' Foscam.txt`
#check for sounddetection & flip FOSCAM_SOUND_IDX switch if so
if [ $FOSCAMSound = "2" ];
then
curl -s -i -H "Accept: application/json" "http://$DOMO_USER:$DOMO_PASS@$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=switchlight&idx=$FOSCAM_SOUND_IDX&switchcmd=On&level=0"
fi
#check for motiondetection & flip FOSCAM_MOTION_IDXswitch if so
if [ $FOSCAMMotion = "2" ];
then
curl -s -i -H "Accept: application/json" "http://$DOMO_USER:$DOMO_PASS@$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=switchlight&idx=$FOSCAM_MOTION_IDX&switchcmd=On&level=0"
fi
fi
# sleep 5s and go back to loop (12x for 1 minute)
sleep 5
done

Result

screen-shot-2016-10-22-at-18-34-06

3 thoughts on “use a Foscam ipcam as motion & sound detector

  1. Jeroen

    Code
    #!/bin/bash
    # Script to use the sound detection features of your Foscam (e.g. C1) and use these as virtual sensors in Domoticz
    # Schedule this every minute with crontab -e, for example: */1 * * * * /path/to/script/foscam.sh
    # variables to change so your setup
    #
    FOSCAMIP=”xxx.xxx.xxx.xxx”
    FOSCAM_PORT=”xx”
    FOSCAM_USER=”xxx”
    FOSCAM_PASS=”xxx”
    #
    DOMO_IP=”xxx.xxx.xxx.xxx”
    DOMO_PORT=”xxx”
    DOMO_USER=”xxx”
    DOMO_PASS=”xxx”
    #
    FOSCAM_SOUND_IDX=”xxx”
    FOSCAM_MOTION_IDX=”xxx”
    #
    # no need to edit after this point …
    # foscam sound detection loop is 5s by default. 5s * 12 = 1 minute (for cron can only schedule every minute)
    for i in {1..12}
    do
    # if cam is pingable continue, else don’t do anything.
    if ping -c 1 $FOSCAMIP &> /dev/null
    then
    curl “http://$FOSCAMIP:$FOSCAM_PORT/cgi-bin/CGIProxy.fcgi?cmd=getDevState&usr=$FOSCAM_USER&pwd=$FOSCAM_PASS” > Foscam.txt
    # check motionDetectAlarm and soundAlarm flags
    FOSCAMSound=`grep -oP ‘(?<=).*(?=)’ Foscam.txt`
    FOSCAMMotion=`grep -oP ‘(?<=).*(?=)’ Foscam.txt`

    till here it seems te work fine. I can see the results in Foscam.txt but it doens’t change te switch in domoticz. When I copy/past the link i the browser it does change te switch state.

    #check for sounddetection & flip FOSCAM_SOUND_IDX switch if so
    if [ $FOSCAMSound = “2” ];
    then
    curl -s -i -H “Accept: application/json” “http://$DOMO_USER:$DOMO_PASS@$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=switchlight&idx=$FOSCAM_SOUND_IDX&switchcmd=On&level=0”
    fi
    #check for motiondetection & flip FOSCAM_MOTION_IDXswitch if so
    if [ $FOSCAMMotion = “2” ];
    then
    curl -s -i -H “Accept: application/json” “http://$DOMO_USER:$DOMO_PASS@$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=switchlight&idx=$FOSCAM_MOTION_IDX&switchcmd=On&level=0”
    fi
    fi
    # sleep 5s and go back to loop (12x for 1 minute)
    sleep 5
    done

  2. safi Post author

    What happens if you try to start curl from the command line?

Leave a Reply

Your email address will not be published. Required fields are marked *