Occupancy Sensor

From Unallocated Space
Jump to: navigation, search

Is the space open?

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 #9999
        echo "Override on"
else
        curl "http://-redacted-/axis-cgi/com/ptz.cgi?gotoserverpresetname=ocs2&camera=1"
        sleep 2
        curl "http://-redacted-/axis-cgi/com/ptz.cgi?camera=1&irisbar=185&alignment=horisontal&barcoord=80,0"
        sleep 8
        wget http://-redacted-/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='false' #for testing
cs=${cs:0:1}

if [ $level -gt 690 ] #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]" >> /uas/ocs/log
        echo "Attempting to update webflag, this should work"
        echo "Pushing to IRC bot"
        echo $nstatus ` date +"%T %F"` > /uas/irc/irc
        python /uas/statustweet/statustweet.py "$ns$nstatus ` date `"

ftp -n -redacted- <<END_SCRIPT
        quote -redacted-
        quote -redacted-
        ascii
        put /tmp/status -redacted-
quit
END_SCRIPT

#exit 0

        if [ "$ns" == "+" ]
        then
                #php /uas/asterisk/calladmin.php
                cp /etc/asterisk/uas/open.wav /etc/asterisk/uas/status.wav
                mplayer /uas/hal9001/nowopen.wav

                if [ $level -lt 9999 ]
                then
                         echo "Updating the wall."
                        /uas/ocs/thewall.sh
                fi
        else
                cp /etc/asterisk/uas/closed.wav /etc/asterisk/uas/status.wav
                echo -n "" > /uas/checkin/ciusers
                mplayer /uas/hal9001/nowclosed.wav
        fi
else
        echo "The status has not changed, the flag is still set to $ns"
fi


#debug
vlevel2=`convert /tmp/ocs.jpg -threshold 99% -format "%[fx:100*image.mean]" info:`
vlevel3=`convert /tmp/ocs.jpg -colors 3 -depth 8 -format %c histogram:info:-`

echo "` date`    [$ns]          $vlevel - $vlevel2 -- $vlevel3" >> /uas/ocs/logv
#debug
exit 0