IP Camera motion detection

Moderator: leecollings

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

Re: IP Camera motion detection

Post by bran2000 »

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: 60
Joined: Saturday 20 June 2015 10:16
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: IP Camera motion detection

Post by bran2000 »

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 :
pi@DomoticzSam ~/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
safi78
Posts: 204
Joined: Thursday 05 September 2013 15:39
Target OS: Linux
Domoticz version: LastBeta
Contact:

Re: IP Camera motion detection

Post by safi78 »

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 ;)
homebridge, rfxcom, zwave, nest, applamp, hue, debian, apple, mysensors, netatmo, fibaro, synology, foscam, otherz
bran2000
Posts: 60
Joined: Saturday 20 June 2015 10:16
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: IP Camera motion detection

Post by bran2000 »

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 :
pi@DomoticzSam ~/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: 24
Joined: Thursday 07 January 2016 7:49
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: IP Camera motion detection

Post by sjefk »

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 ?
safi78
Posts: 204
Joined: Thursday 05 September 2013 15:39
Target OS: Linux
Domoticz version: LastBeta
Contact:

Re: IP Camera motion detection

Post by safi78 »

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
homebridge, rfxcom, zwave, nest, applamp, hue, debian, apple, mysensors, netatmo, fibaro, synology, foscam, otherz
sjefk
Posts: 24
Joined: Thursday 07 January 2016 7:49
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: IP Camera motion detection

Post by sjefk »

Thnx ! Will give it a try :)
DennisD
Posts: 51
Joined: Friday 18 September 2015 21:46
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: IP Camera motion detection

Post by DennisD »

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 11:38
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: IP Camera motion detection

Post by mehtadone »

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 ... 10#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 11:38
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: IP Camera motion detection

Post by mehtadone »

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

Derik
Posts: 1601
Joined: Friday 18 October 2013 23:33
Target OS: Raspberry Pi / ODroid
Domoticz version: BETA
Location: Arnhem/Nijmegen Nederland
Contact:

Re: IP Camera motion detection

Post by Derik »

Dear All..

Try to get motion working..
Looks it is running:

Code: Select all

linaro@odroidxu4:~$ sudo /etc/init.d/motion status
● motion.service - LSB: Start Motion detection
   Loaded: loaded (/etc/init.d/motion; bad; vendor preset: enabled)
   Active: active (exited) since Sun 2017-06-11 16:02:19 CEST; 3h 21min ago
     Docs: man:systemd-sysv-generator(8)

Jun 11 16:03:05 odroidxu4 systemd[1]: Started LSB: Start Motion detection.
Jun 11 18:16:17 odroidxu4 systemd[1]: Started LSB: Start Motion detection.
Jun 11 19:16:35 odroidxu4 systemd[1]: Started LSB: Start Motion detection.
Warning: Journal has been rotated since unit was started. Log output is incomplete or unavailable.
linaro@odroidxu4:~$
did set a ip cam in my lan.

Code: Select all

minimum_frame_time 0

# URL to use if you are using a network camera, size will be autodetected (incl http:// ftp:// mjpg:// rstp:// or file:///)
# Must be a URL that returns single jpeg pictures or a raw mjpeg stream. Default: Not defined
netcam_url http://192.168.5.234:7444/video 


# Username and password for network camera (only if required). Default: not defined
# Syntax is user:password
; netcam_userpass value

# The setting for keep-alive of network socket, should improve performance on compatible net cameras.
# off:   The historical implementation using HTTP/1.0, closing the socket after each http request.
# force: Use HTTP/1.0 requests with keep alive header to reuse the same connection.
# on:    Use HTTP/1.1 requests that support keep alive as default.
# Default: off
; netcam_keepalive on
Will try to follow the wiki only this part i do not understand: Setup a new dummy motion-sensor in Domoticz

What do i do exactly?
Do i need a script?
Do i place the lines in the on and off from the Dummy?
What when i do not use a pass?

Is there a way to test motion? [ so i can see the camera's? ]
Or is there a webui for motion??

How can i set the Camera storage outside the rpi [ odriod / domoticz storage ]

Thanks allready
Xu4: Beta Extreme antenna RFXcomE,WU Fi Ping ip P1 Gen5 PVOutput Harmony HUE SolarmanPv OTG Winddelen Alive ESP Buienradar MySensors WOL Winddelen counting RPi: Beta SMAspot RFlinkTest Domoticz ...Different backups
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests