Sunday, 17 April 2022

Camera PIR Triggering

My RPiZ based backyard camera (192.168.3.18) is now triggered by Zigbee PIR sensors.

These send status messages to the Sonoff Zigbee Bridge (192.168.3.14 running Tasmota) which then publishes them to my MQTT Broker (running on a RPi4 192.168.3.6).

A Node-Red instance (running on a different RPi4 192.168.3.7) then parses these messages and if it is a motion ON (rather than OFF) message it causes a file to be created on its RPi4.

The RPiZ is checks for this file every second and takes a MOTION photo if it exists and after 5 minutes takes a TIMER photo anyway. It's using the following Bash script:

pi@tola-rpiz-noir-camera:~ $ vim ./camera.sh
pi@tola-rpiz-noir-camera:~ $
pi@tola-rpiz-noir-camera:~ $
pi@tola-rpiz-noir-camera:~ $ sudo systemctl restart cameraStillCapture.service
pi@tola-rpiz-noir-camera:~ $ sudo systemctl status cameraStillCapture.service
● cameraStillCapture.service - Camera Still Capture
   Loaded: loaded (/etc/systemd/system/cameraStillCapture.service; enabled; vendor preset: enabled)
   Active: active (running) since Sun 2022-04-17 10:21:14 BST; 2s ago
 Main PID: 7234 (camera.sh)
   CGroup: /system.slice/cameraStillCapture.service
           ├─7234 /bin/bash /home/pi/camera.sh
           └─7242 ssh pi@192.168.3.7 ls -al /home/pi/takePhoto.txt
Apr 17 10:21:14 tola-rpiz-noir-camera systemd[1]: Started Camera Still Capture.
Apr 17 10:21:15 tola-rpiz-noir-camera camera.sh[7234]: ls: cannot access '/home/pi/takePhoto.txt': No such file or
pi@tola-rpiz-noir-camera:~ $ cat camera.sh
#!/bin/bash
counter=500
RESP2="TIMER"
while :
do
  #echo $counter
  if [ $counter -gt 312 ]; then
    raspistill -rot 90 -o /home/pi/backdoorRAW.jpg
    convert /home/pi/backdoorRAW.jpg  -fill white  -undercolor '#00000080' -gravity North -pointsize 64 -annotate +5+5 $RESP2" $(date) " /home/pi/backdoor.jpg
    RESP2="TIMER"
    sudo cp /home/pi/backdoor*.jpg /var/www/html/
    scp /home/pi/backdoor.jpg pi@192.168.3.6:/home/pi
    counter=0
    #echo RESET $counter
  fi
  RESP=$(ssh pi@192.168.3.7 "ls -al /home/pi/takePhoto.txt")
  # If empty then file didn't exist and error stream message produced
  # i.e. ls: Cannot access xxxxx No such file...
  #echo $RESP
  SUB='takePhoto'
  #echo $SUB
  if [ $(grep -c "$SUB" <<< "$RESP") -eq 1 ]; then
          #echo "There"
          counter=1000
          RESP2=$(ssh pi@192.168.3.7 "cat /home/pi/takePhoto.txt")
          echo $RESP2
          RESP3=$(ssh pi@192.168.3.7 "rm /home/pi/takePhoto.txt")
  #else
          #echo "Not There"
  fi
  #sleep 312
  sleep 1
  counter=$((counter+1))
done
pi@tola-rpiz-noir-camera:~ $ date
Sun Apr 17 10:21:26 BST 2022
pi@tola-rpiz-noir-camera:~ $


I had to speed up the make movies script to catch the rapidly changin MOTION images (TIMER images only change every 5 mins)


pi@tola-smarthome-v3:~ $
pi@tola-smarthome-v3:~ $ cat makeMovies.sh
#!/bin/bash
echo "" > oldCottage.jpg
lastCottageSum=$(sum oldCottage.jpg   | cut -c1-6)
cottageCounter=0
echo "" > oldExtension.jpg
lastExtensionSum=$(sum oldExtension.jpg | cut -c1-6)
extensionCounter=0
echo "" > oldFrontgarden.jpg
lastFrontgardenSum=$(sum oldFrontgarden.jpg | cut -c1-6)
frontgardenCounter=0
echo "" > oldBackdoor.jpg
lastBackdoorSum=$(sum oldBackdoor.jpg | cut -c1-6)
backdoorCounter=0
lastDate=$(date -I)

while :
do
    cottageSum=$(sum cottage.jpg   | cut -c1-6)
    if [ $cottageSum -ne $lastCottageSum ]
    then
        echo $(date) cottage $cottageSum $lastCottageSum
        lastCottageSum=$cottageSum
        cottageCounter=$((cottageCounter+1))
        #cp cottage.jpg cottageM$cottageCounter.jpg
        cp cottage.jpg cottageM$(printf %04d $cottageCounter).jpg
    fi
    extensionSum=$(sum extension.jpg | cut -c1-6)
    if [ $extensionSum -ne $lastExtensionSum ]
    then
        echo $(date) extension $extensionSum $lastExtensionSum
        lastExtensionSum=$extensionSum
        extensionCounter=$((extensionCounter+1))
        #cp extension.jpg extensionM$extensionCounter.jpg
        cp extension.jpg extensionM$(printf %04d $extensionCounter).jpg
    fi
    frontgardenSum=$(sum frontgarden.jpg | cut -c1-6)
    if [ $frontgardenSum -ne $lastFrontgardenSum ]
    then
        echo $(date) frontgarden $frontgardenSum $lastFrontgardenSum
        lastFrontgardenSum=$frontgardenSum
        frontgardenCounter=$((frontgardenCounter+1))
        #cp frontgarden.jpg frontgardenM$frontgardenCounter.jpg
        cp frontgarden.jpg frontgardenM$(printf %04d $frontgardenCounter).jpg
    fi
    backdoorSum=$(sum backdoor.jpg | cut -c1-6)
    if [ $backdoorSum -ne $lastBackdoorSum ]
    then
        echo $(date) backdoor $backdoorSum $lastBackdoorSum
        lastBackdoorSum=$backdoorSum
        backdoorCounter=$((backdoorCounter+1))
        #cp backdoor.jpg backdoorM$backdoorCounter.jpg
        cp backdoor.jpg backdoorM$(printf %04d $backdoorCounter).jpg
    fi
    date=$(date -I)
    if [ $date != $lastDate ]
    then
        echo $(date) date $date $lastDate
        mkdir $lastDate
        mv frontroomM* $lastDate
        frontroomCounter=0
        mv cottageM* $lastDate
        cottageCounter=0
        mv frontgardenM* $lastDate
        frontgardenCounter=0
        mv backdoorM* $lastDate
        backdoorCounter=0
        lastDate=$date
    fi
    echo $(date) cottage $cottageSum $lastCottageSum
    echo $(date) extension $extensionSum $lastExtensionSum
    echo $(date) frontgarden $frontgardenSum $lastFrontgardenSum
    echo $(date) backdoor $backdoorSum $lastBackdoorSum
    # Need to be quicker to catch MOTION rather than TIMER images
    #sleep 60
    sleep 5
done
pi@tola-smarthome-v3:~ $ date
Sun 17 Apr 11:37:11 BST 2022
pi@tola-smarthome-v3:~ $ df -m .
Filesystem     1M-blocks  Used Available Use% Mounted on
/dev/root          14668 10114      3926  73% /


Above script is a work in progress :)












No comments:

Post a Comment