IP Camera motion detection

bran2000
Posts: 76
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: 76
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: 372
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: 76
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: 25
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: 372
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: 25
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: 11
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


Return to “Lights and Switches”

Who is online

Users browsing this forum: No registered users and 1 guest