Presence Detection using Bluetooth and WiFi

Home automation often relies on the ability to detect whether someone is present within the house. This information can then be used to perform different action as appropriate. Most people have a mobile phone on them so we can use the presence of, or lack of,  these devices to relatively accurately determine whether someone is home or not.

Bluetooth Presence Detection Script

The devices dictionary holds a set of key:value pairs. In this case the key is the name of the switch set up in OpenHab to hold the status and the value is the Bluetooth device address. Additional key:value pairs will allow you to include more devices as necessary. Provided that Bluetooth on the device is switched on (it doesn’t have to be set to discoverable) and the device is within range, the script should be able to detect it.

#!/usr/bin/env python

import bluetooth
import time
import requests

devices = {
    'Person1BT': '00:00:00:00:00:00',
    'Person2BT': '00:00:00:00:00:00',
    'Person3BT': '00:00:00:00:00:00',
    'Person4BT': '00:00:00:00:00:00'
    }

while True:
        print "Checking Bluetooth Devices"

        for (switch, deviceaddress) in devices.items():
                print "Checking " + switch + "("+ deviceaddress +")"
                try:
                        current = requests.get("http://localhost:8080/rest/items/"+switch+"/state")
                        result = bluetooth.lookup_name(deviceaddress, timeout=5)
                        print "Located Devices: " + str(result)
                        if (result != None and current.text =="OFF"):
                                r = requests.put("http://localhost:8080/rest/items/"+switch+"/state",data='ON')
                        elif (result == None and current.text =="ON"):
                                r = requests.put("http://localhost:8080/rest/items/"+switch+"/state",data='OFF')
                        else:
                                print "Nothing Needs Changing"
                except:
                        print "Couldn't Connect"
                time.sleep(5)

WiFi Presence Detection Script

The devices dictionary holds a set of key:value pairs. In this case the key is the name of the switch set up in OpenHab to hold the status and the value is the IP address assigned to the device. Additional key:value pairs will allow you to include more devices as necessary. This script assumes that a device will always have the same IP address so your router should be set up accordingly. This will send one ping packet (-c1 ) silently (-q ) to the device, waiting a maximum of one second (-W 1 ) for a response. Whether or not the device can be located is determined by looking at the ping process’ exit code:

Success: 0 
No reply: 1
Other error: 2 
#!/usr/bin/env python

import subprocess
import time
import requests

devices = {
    'Person1WIFI': '192.168.1.1',
    'Person2WIFI': '192.168.1.2',
    'Person3WIFI': '192.168.1.3',
    'Person4WIFI': '192.168.1.4'
    }

while True:
        print "Checking WiFi Devices"

        for (switch, ipaddress) in devices.items():
                print "Checking " + switch + "("+ ipaddress +")"
                try:
                        current = requests.get("http://localhost:8080/rest/items/"+switch+"/state")
                        result = subprocess.call('ping -q -c1 -W 1 '+ipaddress+' > /dev/null', shell=True)
                        print "New Value: (0 = present, 1= not):" + str(result)
                        if (result == 0 and current.text =="OFF"):
                                r = requests.put("http://localhost:8080/rest/items/"+switch+"/state",data='ON')
                        elif (result != 0 and current.text =="ON"):
                                r = requests.put("http://localhost:8080/rest/items/"+switch+"/state",data='OFF')
                        else:
                                print "Nothing Needs Changing"
                except:
                        print "Couldn't Connect"
                time.sleep(5)

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *