Let your iMac/MBP detect a burglar whilst idle...

Hi folks...
(Apologies for typos.)

I have been working on a very simple MacBook_Pro/iMac centric project. I have called it Spy .
If you just want to test for fun you only need ./Spy and this defaults to a 5 second video with Medium sensitivity from the working directory whre Spy is.
(Just light talking is all that is required to get to video recording mode.)
It does NOT require anything other than a current virgin Apple OSX install, the version as of 9th May 2018 is OSX 10.13.4, High Sierra.
Although developed on my MBP it is best used on a fixed iMac station.
In the code below I decided on creating script files on the fly instead of using functions that are saved in the /tmp/ directory.
The reason being that those that want the HW access code snippets don't have to edit, cut and paste the 'bash' script as they are already saved.

What does it do?
It is a _hidden_ camera that records a video when a sound signal is detected. It is to video burglars that enter the home during daylight hours.
Night time detection is possible and a trigger circuit is shown to latch a remote relay to turn on lights if you want this facility.
The only modification to your computer is a piece of black electrical tape to cover the light that comes on next to the camera on the computer.

How does it work?
Once the program is started the screen near immediately goes blank and the machine standby is disabled.
Several files are created and access rights set up on all of them.
The overall sleep disabling is done by creating a second terminal and calling pmset .
The program then goes into a loop that uses QuickTime Player to listen for audio that is in the room being monitored.
The sound levels are tested and once user set levels are exceeded the loop is exited and the video starts for the time allocated.

The resulting video is saved to the Desktop but the screen should remain black until the keyboard is hit, Ctrl-C.
The screen will reappear and the TWO terminals will have to be manually shut down.

If there has been no detection then Ctrl-C both terminals will stop the program.

Read the code for more info.

Have fun and enjoy...

EDIT:
Minor change to commented out auto-lighting latch code; position inside the code change only and still commented out.

#!/bin/bash
# Spy
# Usage: ./Spy [Timer_in_seconds] [H, M, L or V]
# Example: ./Spy 30 H<CR>
#
# Development machine MacBook Pro:
# OSX 10.13.4, High Sierra, default bash terminal.
#
# GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin17)
# Copyright (C) 2007 Free Software Foundation, Inc.
#
# Applescript version 2.7.
#
# Uploaded 10th May 2018 to https://www.unix.com

# Variables.
TIMER=$1
HML=$2
HIGH=192
LOW=64
LEVEL=192
ARRAY=()
SUBSCRIPT=0
CHAR=0

# Create the required blank files.
: > /tmp/noidle
chmod 755 /tmp/noidle
: > /tmp/NewCLI
chmod 755 /tmp/NewCLI
: > /tmp/Spy.scpt
chmod 755 /tmp/Spy.scpt
: > "$HOME"/Desktop/Spy.mov
chmod 644 "$HOME"/Desktop/Spy.mov
: > /tmp/Untitled.m4a
chmod 666 /tmp/Untitled.m4a
: > /tmp/Untitled.wav
chmod 666 /tmp/Untitled.wav
: > /tmp/audio.scpt
chmod 755 /tmp/audio.scpt

# Error checks, upper limit set to one hour!
case $TIMER in
	''|*[!0-9]*)	TIMER=6 ;;
esac
if [ "$TIMER" = "" ] || [ "$TIMER" -le 6 ] || [ "$TIMER" -gt 3601 ]
then
	TIMER=6
fi
# Sensitivity level, default is [M]edium and can be omitted, [V, L, M, H].
if [ "$HML" = "H" ] || [ "$HML" = "h" ]
then
	HIGH=144
	LOW=112
fi
if [ "$HML" = "L" ] || [ "$HML" = "l" ]
then
	HIGH=240
	LOW=16
fi
if [ "$HML" = "V" ] || [ "$HML" = "v" ]
then
	HIGH=255
	LOW=0
fi

# Create the necessary scripts to run.
# This is the file to prevent the MBP from sleeping.
echo '#!/bin/sh
/usr/bin/pmset noidle
exit 0' > /tmp/noidle

# This generates a separate terminal for the above script.
echo '#!/bin/sh
open -F -n -g -b com.apple.Terminal "$1"
exit 0' > /tmp/NewCLI

# Ensure the MBP does not go to sleep.
/tmp/NewCLI /tmp/noidle

# Create the movie grabber script.
echo 'set theFilePath to "/Users/amiga/Desktop/Spy.mov"
tell application "QuickTime Player"
	set newMovieRecording to new movie recording
	tell newMovieRecording
		start
		delay '$TIMER'
		pause
		save newMovieRecording in POSIX file theFilePath
		stop
		close newMovieRecording
		quit
	end tell
end tell' > /tmp/Spy.scpt

# Create the audio detection script.
echo 'tell application "QuickTime Player"
	activate
	set savePath to "Macintosh HD:tmp:Untitled.m4a"
	set recording to new audio recording
	set visible of front window to false
	delay 2
	start recording
	delay 3
	stop recording
	export document "Untitled" in file savePath using settings preset "Audio Only"
	close (every document whose name contains "Untitled") saving no
	tell application "System Events" to click menu item "Hide Export Progress" of menu "Window" of menu bar 1 of process "QuickTime Player"
		delay 1
		quit
end tell' > /tmp/audio.scpt

# #########################################################
# This is ONLY needed to work with the simple DC trigger and relay switching
# board. This is to give a DC pulse to enable the circuits at the end of this script.
trigger_pulse()
{
	CHAR=0
	: > /tmp/Untitled.wav
	printf "\122\111\106\106\144\037\000\000\127\101\126\105\146\155\164\040\020\000\000\000\001\000\001\000\100\037\000\000\100\037\000\000\001\000\010\000\144\141\164\141\100\037\000\000" >> /tmp/Untitled.wav
	while [ $CHAR -le 999 ]
	do
		printf "\377\377\000\000\377\337\000\000" >> /tmp/Untitled.wav
		CHAR=$(( $CHAR + 1 ))
	done
	afplay /tmp/Untitled.wav
}
# #########################################################

# Now start the program proper.
# Put the display and KB to sleep ONLY.
pmset displaysleepnow

# Start the audio detection system.
# MAIN.
while true
do
	osascript /tmp/audio.scpt
	# Use the default audio converter to convert to a mono 8 bit WAVE file.
	afconvert -f 'WAVE' -c 1 -d UI8@8000 /tmp/Untitled.m4a /tmp/Untitled.wav
	# Convert to a RAW file format for displaying, reuse '.m4a' extension although file is RAW.
	dd if=/tmp/Untitled.wav of=/tmp/Untitled.m4a skip=4096 bs=1 count=8000 > /dev/null 2>&1
	# Create an array of whitespace delimited decimal values...
	ARRAY=( $( hexdump -v -e '1/1 "%u "' /tmp/Untitled.m4a ) )
	# Test for limits in the array. If outside then break out of both loops.
	for SUBSCRIPT in {0..7999}
	do
		LEVEL=${ARRAY[$SUBSCRIPT]}
		if [ $LEVEL -ge $HIGH ] || [ $LEVEL -le $LOW ]
		then
			break 2
		fi
	done
done

# #########################################################
# Remove the comment and build the simple circuits at the end of this script.
#trigger_pulse
# #########################################################

# Once audio is detected record a single video for the number of seconds programmed.
osascript /tmp/Spy.scpt
exit 0

# #########################################################
# This is the DC output control board triggered from a squarewave signal.
# A very simple voltage doubler and passive filter for a controlled DC output.
# This is to give a DC voltage to trigger the board below. ONLY, if night time
# usage is wanted.
# (Connect DC OUT & _GND_ to a DC coupled oscilloscope to see it working.)
#
#           Headset O/P.    C1              |\|D2                 X
# Tip only. --> O--------o--||--o-------o---| +---o-------o-------O +VE DC OUT.
#                        |      |       |   |/| + |       |
# Barrel. ----> O        \      | +     \         |       \ 
#               |        /    --+--     /         | +     /
#               |     R1 \     / \ D1   \ R2     === C2   \ R3
#               |        /    '---'     /        -+-      /
#               |        \      |       \         |       \ 
#               |        /      |       /         |       /
#               |        |      |       |         |       |       Y
#               +--------o------o-------o---------o---o---o-------O -VE.
#                              Pseudo Ground. ----> __|__
#                                             _GND_ /////
# Parts List:-
# C1 ......... 1 uF, 50V.
# C2 ......... 10 uF, electrolytic, 10V.
# R1 ......... 33R, 1/8W, 5% tolerance resistor.
# R2 ......... 1M, 1/8W, 5% tolerance resistor.
# R3 ......... 100K, 1/8W, 5% tolerance resistor.
# D1, D2 ..... OA90 or any similar germanium diode.
# 3.2mm standard STEREO jack plug for headset socket.
# Coaxial connecting cable.
# Sundries as required, stripboard, etc, (similar to above).
# #########################################################
# This is a working relay switching board that can be latched hard on using one
# set of contacts of the relay. The other isolated contacts are used to switch
# on an external lamp.
#                                                                  +------o------+
#                                                                  |      |      |
#                                                                  \      |      |
#                                                                  /      |      |
#  C = Common.             +------O NO                          R3 \    + |      o
# NO = Normally Open.      v                                       /    --+--  S1 /
# NC = Normally closed. NO                                         \  D1 / \     o
#                          ^                                       /    '---'    |
#              C O---o----<o> RLA1 Isolated Contacts.              |      |      |
#                          v                                    +--+--+   |      |
#                       NC ^                     TP1 Optional.  | RLA |   |      | +
#                          +------O NC                 O        +--+--+   |    __|__
#                                                      |        c  |      |     ===
#                                                      |        o--o------o      |
#                        R1     * RV1 Optional.        |  b    /          |  BY1 |
# From DC Output O-----/\/\/\---o---/\/\/\---o------o--o--o__|/  Q1       |    __|__
# Control Board Above.          |    ___     |      |        |\         C o     ===
#                               |     |      |      \         _\|         |      |
#                               +-----+      | +    /           o--+      | RLA2 |
#                                        C1 ===  R2 \           e  |  o-><o> <---o
#                                           ---     /              | NC      NO  |
#                                            |      \              |             |
#                                            |      /              |             |
#                                            |      |              |             |
#          _GND_ O-------o-------------------o------o--------------o-------------+
# Pseudo Ground. ----> __|__
#                    /////
# PartsList:-
# R1 ......... 2K2, 1/8W, 5% tolerance resistor.
# R2 ......... 1M, 1/8W, 5% tolerance resistor.
# R3 ......... 47R, 1/8W, 5% tolerance resistor.
# RV1 ........ 22K preset variable resistor, (* optional).
# C1 ......... 220uF, 10V electrolytic capacitor.
# Q1 ......... BC549, or any small signal silicon NPN transistor.
# D1 ......... 1N4004 or any similar silicon rectifier diode.
# RLA ........ DPDT, 5 Volt coil, relay.
# S1 ......... Miniature ON/OFF switch.
# BY1 ........ 4 x AA batteries.
# 4 x AA battery holder, and connector if required.
# Link Terminals, as required, (TP1 Optional).
# Stripboard, (Veroboard), as required.
# Various coloured and tinned copper wire as required.
# Small cable ties, optional.
# Stick on cable clips, optional.
# #########################################################
1 Like

I forgot to mention one important part:
Change this line from echo 'set theFilePath to "/Users/amiga/Desktop/Spy.mov"
to echo 'set theFilePath to "/Users/your_user_name/Desktop/Spy.mov" ...
And away you go...

1 Like