IP Camera motion detection

bran2000
Posts: 79
Joined: Saturday 20 June 2015 10:16
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: IP Camera motion detection

Postby bran2000 » Sunday 01 May 2016 21:45

safi78 wrote:Hi,

I recently bought the Foscam C1 which has SOUND detection as well, thought I'd modify your script a bit and share it here:

Code: Select all

#!/bin/bash
FOSCAMIP="xxx.xxx.xxx.xxx"
FOSCAM_PORT="88"
FOSCAM_USER="xxxx"
FOSCAM_PASS="xxxx"

DOMO_IP="xxx.xxx.xxx.xxx"
DOMO_PORT="8080"
DOMO_USER="xxxx"
DOMO_PASS="xxxx"

FOSCAM_STATUS_IDX="xxx"

#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 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

# use motionDetectAlarm instead of soundAlarm for motion and change foscam detection loop and sleep multiplier accordingly :)
FOSCAMStatus=`grep -oP '(?<=<soundAlarm>).*(?=<\/soundAlarm>)' Foscam.txt`

if [ $FOSCAMStatus = "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_STATUS_IDX&switchcmd=On&level=0"

fi

fi

sleep 5
done



I decided just to create a fake motion detector which turns off after 5s (don't need script for that ;) )

And, even more awesome, when there's SOUND detected at the cam it sends a snapshot to my Telegram! (Some additional scripting required of-course ;) )

Screen Shot 2015-12-09 at 01.14.33.png


Thanks for the script & inspiration, it works brilliantly!

Kind regards,

Sander


Hi,

i have a problem with your script, error on the line 25; it seems to here :
if [ $FOSCAMStatus = "2" ];

you explain about this line :
# use motionDetectAlarm instead of soundAlarm for motion and change foscam detection loop and sleep multiplier accordingly :)
FOSCAMStatus=`grep -oP '(?<=<soundAlarm>).*(?=<\/soundAlarm>)' Foscam.txt`

it creates the foscam.txt file, when i get inside there is lot of characters specials, i guess it's a original picture and the txt file is not the right format.

but i don't know why i have this error

Thanks

bran2000
Posts: 79
Joined: Saturday 20 June 2015 10:16
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: IP Camera motion detection

Postby bran2000 » Sunday 01 May 2016 23:06

bran2000 wrote:
safi78 wrote:Hi,

I recently bought the Foscam C1 which has SOUND detection as well, thought I'd modify your script a bit and share it here:

Code: Select all

#!/bin/bash
FOSCAMIP="xxx.xxx.xxx.xxx"
FOSCAM_PORT="88"
FOSCAM_USER="xxxx"
FOSCAM_PASS="xxxx"

DOMO_IP="xxx.xxx.xxx.xxx"
DOMO_PORT="8080"
DOMO_USER="xxxx"
DOMO_PASS="xxxx"

FOSCAM_STATUS_IDX="xxx"

#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 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

# use motionDetectAlarm instead of soundAlarm for motion and change foscam detection loop and sleep multiplier accordingly :)
FOSCAMStatus=`grep -oP '(?<=<soundAlarm>).*(?=<\/soundAlarm>)' Foscam.txt`

if [ $FOSCAMStatus = "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_STATUS_IDX&switchcmd=On&level=0"

fi

fi

sleep 5
done



I decided just to create a fake motion detector which turns off after 5s (don't need script for that ;) )

And, even more awesome, when there's SOUND detected at the cam it sends a snapshot to my Telegram! (Some additional scripting required of-course ;) )

Screen Shot 2015-12-09 at 01.14.33.png


Thanks for the script & inspiration, it works brilliantly!

Kind regards,

Sander


Hi,

i have a problem with your script, error on the line 25; it seems to here :
if [ $FOSCAMStatus = "2" ];

you explain about this line :
# use motionDetectAlarm instead of soundAlarm for motion and change foscam detection loop and sleep multiplier accordingly :)
FOSCAMStatus=`grep -oP '(?<=<soundAlarm>).*(?=<\/soundAlarm>)' Foscam.txt`

it creates the foscam.txt file, when i get inside there is lot of characters specials, i guess it's a original picture and the txt file is not the right format.

but i don't know why i have this error

Thanks


sorry this his the full message error :
[email protected] ~/domoticz/scripts $ ./motion_detect_cam_jardin.sh
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 13327 100 13327 0 0 53267 0 --:--:-- --:--:-- --:--:-- 65975
./motion_detect_cam_jardin.sh: ligne 25 : [: = : opérateur unaire attendu

User avatar
safi78
Posts: 451
Joined: Thursday 05 September 2013 15:39
Target OS: Linux
Domoticz version: LastBeta
Location: Alkmaar, NL
Contact:

Re: IP Camera motion detection

Postby safi78 » Tuesday 03 May 2016 15:43

The contents of your file is the output of the line:

Code: Select all

curl "http://$FOSCAMIP:$FOSCAM_PORT/cgi-bin/CGIProxy.fcgi?cmd=getDevState&usr=$FOSCAM_USER&pwd=$FOSCAM_PASS" > Foscam.txt


So, your problem is probably before the line you mentioned.

Could it be that the connection to your cam (ip, port, user, pass) just fails and produces wrong output to the file?

The output you are showing is the output from the CURL command, but it should put the result of the CURL command into the file ;)
I don't know if you noticed or not, but I'm an extremely arrogant man who tends to think all of his plans will work ...

https://safiweb.nl

homebridge, rfxcom, zwave, nest, applamp, hue, debian, apple, mysensors, netatmo, fibaro, synology, foscam, otherz

bran2000
Posts: 79
Joined: Saturday 20 June 2015 10:16
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: IP Camera motion detection

Postby bran2000 » Tuesday 10 May 2016 8:12

the script seems working as i said the foscam.txt is fully filled.it already put the result of the curl in the foscam.txt but i have this error when i execute the script :
[email protected] ~/domoticz/scripts $ ./motion_detect_cam_jardin.sh
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 13327 100 13327 0 0 53267 0 --:--:-- --:--:-- --:--:-- 65975
./motion_detect_cam_jardin.sh: ligne 25 : [: = : opérateur unaire attendu

i checked the ip, port,user and pass, they are all ok.

sjefk
Posts: 27
Joined: Thursday 07 January 2016 8:49
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: IP Camera motion detection

Postby sjefk » Tuesday 09 August 2016 15:11

safi78 wrote:Hi,

I recently bought the Foscam C1 which has SOUND detection as well, thought I'd modify your script a bit and share it here:

Code: Select all

#!/bin/bash
FOSCAMIP="xxx.xxx.xxx.xxx"
FOSCAM_PORT="88"
FOSCAM_USER="xxxx"
FOSCAM_PASS="xxxx"

DOMO_IP="xxx.xxx.xxx.xxx"
DOMO_PORT="8080"
DOMO_USER="xxxx"
DOMO_PASS="xxxx"

FOSCAM_STATUS_IDX="xxx"

#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 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

# use motionDetectAlarm instead of soundAlarm for motion and change foscam detection loop and sleep multiplier accordingly :)
FOSCAMStatus=`grep -oP '(?<=<soundAlarm>).*(?=<\/soundAlarm>)' Foscam.txt`

if [ $FOSCAMStatus = "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_STATUS_IDX&switchcmd=On&level=0"

fi

fi

sleep 5
done



I decided just to create a fake motion detector which turns off after 5s (don't need script for that ;) )

And, even more awesome, when there's SOUND detected at the cam it sends a snapshot to my Telegram! (Some additional scripting required of-course ;) )

Screen Shot 2015-12-09 at 01.14.33.png

Thanks for the script & inspiration, it works brilliantly!

Kind regards,

Sander



Hi Sander,

Could you post the whole script.. including the telegram expansion ?

User avatar
safi78
Posts: 451
Joined: Thursday 05 September 2013 15:39
Target OS: Linux
Domoticz version: LastBeta
Location: Alkmaar, NL
Contact:

Re: IP Camera motion detection

Postby safi78 » Tuesday 09 August 2016 15:13

This is probably more usefull, because there's a lot of specific stuff in each set-up.

I just followed this, and added the notifications to my script: https://www.domoticz.com/wiki/Telegram_Bot
I don't know if you noticed or not, but I'm an extremely arrogant man who tends to think all of his plans will work ...

https://safiweb.nl

homebridge, rfxcom, zwave, nest, applamp, hue, debian, apple, mysensors, netatmo, fibaro, synology, foscam, otherz

sjefk
Posts: 27
Joined: Thursday 07 January 2016 8:49
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: IP Camera motion detection

Postby sjefk » Tuesday 09 August 2016 15:30

Thnx ! Will give it a try :)

DennisD
Posts: 29
Joined: Friday 18 September 2015 21:46
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: IP Camera motion detection

Postby DennisD » Monday 09 January 2017 21:28

Hi, I modified safi's code a little bit to include telegram notifications with snapshot. I know it's a little thread resurrection but i hope somebody find this useful too use.

Code: Select all

#!/bin/bash
# All credits go to Safi, i just added Telegram bot notifications with snapshot
# 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. Be sure to put this file in the same directory as the Foscam.txt (most likely in /home/pi/)
# Telegram bot is needed for this. Description can be found here: https://www.domoticz.com/wiki/Telegram_Bot

FOSCAMIP=""                                  # FOSCAM IP Address
FOSCAM_PORT=""                               # FOSCAM PORT   
FOSCAM_USER=""                               # FOSCAM USERNAME
FOSCAM_PASS=""                               # FOSCAM PASSWORD
#
DOMO_IP=""                                   # DOMOTICZ IP Address
DOMO_PORT=""                                 # DOMOTICZ PORT
DOMO_USER=""                                 # DOMOTICZ USERNAME
DOMO_PASS=""                                 # DOMOTICZ PASSWORD
#
FOSCAM_SOUND_IDX=""                          # FOSCAM SOUND IDX (IDX OF VIRTUAL SWITCH FOR SOUND)
FOSCAM_MOTION_IDX=""                         # FOSCAM MOTION ID (IDX OF VIRTUAL SWITCH FOR MOTION)
#
CHATID=""                                    #CHATID TELEGRAM
BOTID=""                                     #BOT TOKEN ID
#
# 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..5}
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>).*(?=<\/soundAlarm>)' Foscam.txt)
FOSCAMMotion=$(grep -oP '(?<=<motionDetectAlarm>).*(?=<\/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"
#-- get snapshot from camera
wget "http://$FOSCAMIP:$FOSCAM_PORT/cgi-bin/CGIProxy.fcgi?cmd=snapPicture2&usr=$FOSCAM_USER&pwd=$FOSCAM_PASS" -O /var/tmp/snapshotV.jpg
#-- send snapshot with Telegram
curl -s -X POST "https://api.telegram.org/$BOTID/sendPhoto" -F chat_id=$CHATID -F photo="@/var/tmp/snapshotV.jpg"
curl --data chat_id=$CHATID --data-urlencode "text=Sound alarm, sound is detected"  "https://api.telegram.org/$BOTID/sendMessage"

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"
#-- get snapshot from camera
wget "http://$FOSCAMIP:$FOSCAM_PORT/cgi-bin/CGIProxy.fcgi?cmd=snapPicture2&usr=$FOSCAM_USER&pwd=$FOSCAM_PASS" -O /var/tmp/snapshotV.jpg
#-- send snapshot with Telegram
curl -s -X POST "https://api.telegram.org/$BOTID/sendPhoto" -F chat_id=$CHATID -F photo="@/var/tmp/snapshotV.jpg"
curl --data chat_id=$CHATID --data-urlencode "text=Motion alarm, motion is detected"  "https://api.telegram.org/$BOTID/sendMessage"
fi
fi
# sleep 5s and go back to loop (12x for 1 minute)
sleep 5
done


make sure you have 2 virtual switches for both sound and motion alarm
Save and upload this code to /home/pi/
Chmod +x foscam.sh (or any other name you wish to give it)
contrab -e and add this line: */1 * * * * /home/pi/foscam.sh

mehtadone
Posts: 8
Joined: Sunday 15 January 2017 12:38
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: IP Camera motion detection

Postby mehtadone » Friday 27 January 2017 17:37

Thank you to everyone on here. Some amazing work!

I've updated the script to work with my setup. I have my Foscam saving video's and pictures to my raspberry pi on motion detection using FTP. I then run a Dropbox sync script that uploads all the pictures to dropbox and removes them from the pi every 15 mins. This is based on this https://github.com/andreafabrizi/Dropbox-Uploader.

Within Domo, I have two switches. One for presence detection, I use the one from here: http://www.domoticz.com/forum/viewtopic.php?f=23&t=15531&p=114910#p114910. This just lets domo know if I'm home or not. If I am at home, then the script below doesn't send me telegram messages. The other is the foscam motion switch as described in a previous post.

I have also edited the script so instead of querying the camera for the latest snapshot, it just takes the latest image from the directory the camera is saving to.

Code: Select all

#!/bin/bash
# All credits go to Safi, i just added Telegram bot notifications with snapshot
# 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. Be sure to put this file in the same directory as the Foscam.txt (most likely in /home/pi/)
# Telegram bot is needed for this. Description can be found here: https://www.domoticz.com/wiki/Telegram_Bot

FOSCAMIP=""             #FOSCAM IP Address
FOSCAM_PORT=""          #FOSCAM PORT
FOSCAM_USER=""          #FOSCAM USERNAME
FOSCAM_PASS=""          #FOSCAM PASSWORD
IMAGEDIRECTORY=""       #FTP Location of files saved from FOSCAM
#
DOMO_IP=""              #DOMOTICZ IP Address
DOMO_PORT=""            #DOMOTICZPORT
DOMO_USER=""            #DOMOTICZ USERNAME
DOMO_PASS=""            #DOMOTICZ PASSWORD
#
FOSCAM_MOTION_IDX=""    #FOSCAM MOTION ID (IDX OF VIRTUAL SWITCH FOR MOTION)
PRESENCEIDX=""          #PRESENCE DETECTION (IDX OF VIRTUAL SWITCH FOR PRESENCE DETECTION)
#
CHATID=""               #CHATID TELEGRAM
BOTID=""                #BOT TOKEN ID
#
DROPBOXURL=""           #DROPBOX URL FOR ALL PHOTOS AND VIDS

# no need to edit after this point ...

# Get the status if I am home or not from domo
function jsonValue() {
   KEY=$1
   num=$2
   awk -F"[,:}]" '{for(i=1;i<=NF;i++){if($i~/'$KEY'\042/){print $(i+1)}}}' | tr -d '"' | sed -n ${num}p
}

DOMOTICZ=`curl -s --connect-timeout 2 --max-time 5 "http://$DOMO_USER:$DOMO_PASS@$DOMO_IP:$DOMO_PORT/json.htm?type=devices&rid=$PRESENCEIDX"`

if [ -z "$DOMOTICZ" ]
then
   exit 1
else
   PRESENCE=$(echo $DOMOTICZ | jsonValue Status 1)
fi

# If I am at home, no need to carry on.
if [[ $PRESENCE == *'On'* ]];
then
    exit 0
fi

# foscam sound detection loop is 5s by default. 5s * 12 = 1 minute (for cron can only schedule every minute)
for i in {1..5}
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
FOSCAMMotion=$(grep -oP '(?<=<motionDetectAlarm>).*(?=<\/motionDetectAlarm>)' Foscam.txt)

fi
#check for motiondetection & flip FOSCAM_MOTION_IDXswitch if so
if [[ $FOSCAMMotion = "2" ]];
then

cd $IMAGEDIRECTORY || exit
fn=$(ls -t | head -n1)

#-- send message to telegram
curl --data chat_id=$CHATID --data-urlencode "text=Ooo motion detected. Check out $DROPBOXURL for more images & video"  "https://api.telegram.org/$BOTID/sendMessage"

#-- set domo switch to on
curl -s -i -H "Accept: application/json" "http://$DOMO_USER:$DOMO_PASS@$DOMO_IP:$DOMO_PORT/json.htm?type=devices&rid=$DOMOIDX"

#-- send snapshot with Telegram
curl -s -X POST "https://api.telegram.org/$BOTID/sendPhoto" -F chat_id=$CHATID -F photo=@$fn

#-- set domo switch back to off
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=Off&level=0"

fi

# sleep 5s and go back to loop (12x for 1 minute)
sleep 5
done
end




mehtadone
Posts: 8
Joined: Sunday 15 January 2017 12:38
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: IP Camera motion detection

Postby mehtadone » Sunday 19 February 2017 13:31

On a follow on, I found that my last script was a little unreliable. The polling of the foscam motion flag sometimes didn't trigger so I went another way.

I have my Foscam saving motion snapshots to a FTP folder on the pi. (which in turn is uploading to dropbox and deleting the file.) I set up a file watcher on this folder which then switches the domo switch.

I'll test and let you know but so far, it seems more reliable.

First you need to:
sudo apt-get install inotify-tools

Then just set this script to run on startup using /etc/local.rc

Code: Select all

#!/bin/bash
DOMO_IP=""                    #DOMOTICZ IP Address
DOMO_PORT=""               #DOMOTICZPORT
DOMO_USER=""               #DOMOTICZ USERNAME
DOMO_PASS=""              #DOMOTICZ PASSWORD
FOSCAM_MOTION_IDX=""    #FOSCAM MOTION ID (IDX OF VIRTUAL SWITCH FOR MOTION)

MONITORDIR=""         #Directory which FOSCAM save snapshots to

#no need to edit after this point ...
inotifywait -m -r -e create --format '%w%f' "${MONITORDIR}" | while read NEWFILE
do
      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"
done



Return to “Lights and Switches”

Who is online

Users browsing this forum: No registered users and 1 guest