Detect human presence with DomotiGa

After a few days using DomotiGa I found I had the need for the system to know who is home and who is not. There is already a built in bluetooth interface in DomotiGa, but I never had much luck with it, so I disabled it. At my home we are usually 2 persons present at any one time. "The names have been changed to protect the innocent" Wink

How do I know who's home?

Well, I had to use bluetooth somehow to determine who those persons are. To detect those persons, I have to rely on bluetooth on their smart phones. And of course, you will need a bluetooth dongle on your DomotiGa server. The drawback is that they must have bluetooth enabled at all times when at home. 

My approach is to use the shell interface and the hcitool.  This is the same approach used by DomotiGa's bluetooth interface. First, I need the hcitool installed (you might already have it installed), but here's how to install it:

$ sudo apt-get install bluez-utils

Then you need to get the addresses of the "allowed" mobile phones. Enable bluetooth on all phones, and make sure they are "always visible". This is important. It will not work if they are set to be visible just for a few minutes, as they usually are by default. Now it's time to see who's phones are present. Use hcitool to do a scan:

$ hcitool scan

This will show a list of all phones present:

Scanning ...
        E3:40:E2:01:04:2D       Peters phone
        B2:37:F2:5F:06:5D       Amandas phone

Those are the addresses we need. Of course you can add as many or as few as you like here. You will have to modify the script to reflect the numbers of persons. See comments in the script below.

Then the script that will do the actual work for us:

#!/bin/bash
#
# Use with DomotiGa shell interface
#
# Written by Ronny Oretap 2015 - Free to use at your own risk. ;-)
#
#
# Will return the names of those present, one on each line:
#
#            Present
#            Peter
#            Amanda
#    or
#            Away
#            -
#            -
# or
#            Present
#            -
#            Amanda
# or
#            Present
#            Peter
#            -
#
#

# Set all as away first - Add as many as you like
###########################################
name1="-"
name2="-"

# The bluetooth addresses allowed - Add as many as you like
##################################################
address1="0C:71:5D:74:1A:83"
address2="E4:40:E2:00:06:2D"

# We use a file to determine the status of the previous scan
################################################
previous=`cat proximity.prev`

# Do the scan and put the result in $ispresent
################################################
ispresent=`hcitool scan | awk ' { print $0 }'`

# Check if name1 is present
##################################
if [[ $ispresent == *"$address1"* ]]
then
        # Insert the name of the person here
        name1="Peter"
fi

# Check if name2 is present
##################################
if [[ $ispresent == *"$address2"* ]]
then
        # Insert the name of the person here
        name2="Amanda"
fi

# Add more names here, if needed
###################################

#

#


# You may chain as many names as you like here
################################################
if [ "$name1" == "Peter" ] || [ "$name2" == "Amanda" ]; then
        echo "Present" > proximity.prev
        echo "Present"
        echo "$name1"
        echo "$name2"
        exit
fi

# None found and the previous scan did not find anyone either
# We can assume no one is home
##################################################
if [ "$previous" == "Away" ]; then
        echo "Away" > proximity.prev
        echo "Away"
        echo "-"
        echo "-"
        exit
fi

# None found (First scan) Pending until next away... 
# Sometimes the hcitool scan fails, even if someone is present
# This to avoid "false alarms"
##################################################
        echo "Away" > proximity.prev
        echo "Present"
        echo "?"
        echo "?"

# End of script

Now all you have to do is to create a new "Shell device", executing this script. The returned values will then be inserted into value1 (Present/Away), value2 (Peter/-) and value3 (Amanda/-)
 

Tags: