Occupancy Sensor
The occupancy sensor bash script (detailed below) is run via cron every 5 minutes.
The script pulls a static image from the Big Brother Axis IP camera, analyzes it for light levels, then determines if the space is open (TRUE) or closed (FALSE).
It compares the new status with the current status to determine if the state of the space has changed.
If the status has changed, it updates the website with a new TRUE/FALSE flag. If the status has not changed it does nothing.
The occupancy sensor is also responsible for:
- Calling The Wall script when the flag changes from FALSE to TRUE.
- Switching in open.wav or closed.wav to status.wav for our Asterisk phone responder. (changes will be made regarding this function)
Sensor Override
The Occupancy Sensor override is currently located hanging on the side of the gray cabinet. When enabled it will override the light level check, and report that the space is open. This is useful for parties, or watching movies, etc.
The switch is connected to the 5v line of a USB cable, when the circuit is complete, the override is on, and a USB dongle is connected to the computer running the occupancy sensor. The script then checks for the presence of this USB dongle with lsusb before checking the light levels.
Source Code for ocs.sh
#!/bin/bash override=`lsusb` if [[ $override == *0781:5530* ]] then level=9999 echo "Override on" else curl "http://***.***.***.***/axis-cgi/com/ptz.cgi?gotoserverpresetname=ocs2&camera=1" curl "http://***.***.***.***/axis-cgi/com/ptz.cgi?camera=1&irisbar=185&alignment=horisontal&barcoord=83,0" sleep 8 wget http://***.***.***.***/axis-cgi/jpg/image.cgi -q -O /tmp/ocs.jpg level=`convert /tmp/ocs.jpg -colorspace gray -format "%[fx:mean]" info:|cut -c3-5` vlevel=`convert /tmp/ocs.jpg -colorspace gray -format "%[fx:mean]" info:` echo $level echo $vlevel fi cs=`cat /tmp/status` cs=${cs:0:1} if [ $level -gt 690 ] then nstatus="The space has been open since " ns="+" else nstatus="The space has been closed since " ns="-" fi if [ "$cs" != "$ns" ] then echo "The status has changed, The flag is now set to $ns" echo -n $ns$nstatus ` date +"%T %F"` > /tmp/status if [ $level -eq 9999 ] then echo -n " (override engaged)" >> /tmp/status fi echo "`date` [$ns]" >> /home/unallocated/ocs/log echo "Attempting to update webflag, this should work" echo "Pushing to IRC bot" echo $nstatus ` date +"%T %F"` > /home/unallocated/irc/irc #code omitted #Pushes the status to the website #code omitted if [ "$ns" == "+" ] then cp /etc/asterisk/uas/open.wav /etc/asterisk/uas/status.wav if [ $level -lt 11 ] then echo "Updating the wall." /home/unallocated/ocs/thewall.sh fi else cp /etc/asterisk/uas/closed.wav /etc/asterisk/uas/status.wav fi else echo "The status has not changed, the flag is still set to $ns" fi #debug vlevel2=`convert /tmp/ocs.jpg -colorspace gray -format "%[max]" info:` vlevel3=`convert /tmp/ocs.jpg -format "%[max]" info:` vlevel4=`convert /tmp/ocs.jpg -colorspace gray -format "%[mean]" info:` echo "` date` [$ns] $vlevel - $vlevel2 -- $vlevel3 --- $vlevel4" >> /home/unallocated/ocs/logv #debug exit 0