Using Bluetooth for Presence Detection

28 replies [Last post]
bobonline19
Offline
Joined: 4 Apr 2013

Just wondering if anyone has experimented with Bluetooth presence detection on the HAH? I searched the forums and found that although the LiveBox may have Bluetooth it cannot be used due to lack of drivers?

Could an external Bluetooth dongle be added via the USB host port (and a USB hub as this port is already used for CurrentCost and JeeNodes)?

Another process would need to be setup with a corresponding ini file that has a list of known Bluetooth MAC addresses. This process could then use the hcitool to poll for the list of Bluetooth MACs. Each address in the list would become a xap endpoint with on/off for present and value for ping time (which gives an indicator of proximity).

xAP-header
{
v=12
hop=1
uid=xxxxxxxx
class=xAPBSC.event
source=dbzoo.livebox.BluetoothPresence:Entry.1

}
input.state
{
state=on
text=120.4

displaytext='Charlie' present ping=120.4ms

}

With this in place it would be possible to create LUA scripts that react to the number of and even who is present in the house.

- Fire a RF relay to unlock a gate

- Turn on some lights as you arrive home

- Tweet that somebody is home

- Start playing your playlist on your squeezeserver

http://www.dreamgreenhouse.com/projects/2012/rpibt/index.php

http://www.raspberrypi.org/phpBB3/viewtopic.php?f=37&t=47059&p=415030

allanayr
Offline
Ayr, United Kingdom
Joined: 25 Sep 2011
Sound interesting

Would the iPhone geofencing attribute be able to be used instead?

garrydwilms
Offline
United Kingdom
Joined: 31 Mar 2011
Interesting idea. I have

Interesting idea. I have often thought presence detection would be useful but not thought of how to implement it. Must admit though Bluetooth is over my head at the moment but I would love to see it implemented. 

Is this something your capable of testing? Would be willing to beta test for you :)

sure Brett will have some thoughts on the feasability of this. 

 

garry

garrydwilms
Offline
United Kingdom
Joined: 31 Mar 2011
Another option

How about putting this project on a jeenode.

http://www.fritz-hut.com/2012/08/26/bluetooth-presence-detection/

This will allow really simple integration via the existing hardware. Would just need a bluenode applet writing. 

this might be my Xmas project I think!

There are cheap bluetooth serial devices available too 

http://www.ebay.co.uk/itm/1x-Wireless-Bluetooth-Serial-Transceiver-Modul...

cheers

G

brett
Offline
Providence, United States
Joined: 9 Jan 2010
A bluetooth node would be the

A bluetooth node would be the way to go.   This way it keeps the hardware out of the HAH system, and makes it more independent of the control head.

Much like the serial relay board I coded up recently.  Its a complely self contains xAPBSC compliant node.
http://www.dbzoo.com/livebox/ethrelay

I would do the same sort of thing with bluetooth.    A bluetooth unit mounted onto top of some motherboard carrier which recieves the signal and pumps out some xAP packet to let you know when another bluetooth device is in range.   That xAP packet would be pushed straight down an ethernet cable, or out via RF if you wish so you could HahNode enable it.

After that its just LUA scripting on the HAH to decide what to do next.

Brett

bobonline19
Offline
Joined: 4 Apr 2013
iPhone geofence

To allanayr. I have not looked in to this but believe this is a function that runs on the phone. A app can launch or be sent a message on arrival or leaving a defined zone. This would require an iPhone app to perform an action (e.g. Connect via iServer and post a message in?). The advantage of the Bluetooth address solution is it should work with any Bluetooth device you carry.

bobonline19
Offline
Joined: 4 Apr 2013
Hi Garry, Brett, Thank you

Hi Garry, Brett,

 

Thank you both for your input. Sounds like a Bluetooth node may be the way to go. I'm going to check whether a HC-05 is required as opposed to HC-06 (fuller AT command set). The link I posted uses l2ping to check if a known Bluetooth address is in range rather than just performing a Bluetooth discovery. Devices respond to the ping even if the device is not discoverable, you don't need to leave your phone/device discoverable.

Presumably you can connect a basic I2C EEPROM or similar to a JeeNode to persist the list of Bluetooth addresses to poll/report or alternatively just use the list of paired devices from the Bluetooth device?

I'll post again if I get anywhere with this over Christmas, I still have to build my HAHNode and room node and get them running first.

derek
Offline
Glasgow, United Kingdom
Joined: 26 Oct 2009
EEPROM in jeeNodes

The AVR 328 chip has 1K bytes of EEPROM - a few of these are used by the roomNode code but loads of space left.

It would be easy enough to modify the hardware on the etherRelay to hold a bluetooth device, there are spare I/O pins exposed on unpopulated connector pads.

Cheers,
Derek.

allanayr
Offline
Ayr, United Kingdom
Joined: 25 Sep 2011
You're quite right that

You're quite right that bluetooth is probably more flexible and reliable.

This project:

http://www.automatedhome.co.uk/new-products/hack-your-heating-using-an-i...

is something that I found which uses an interesting take on geofencing to control heating.

As my heating is already controlled by the HAH the hardware extension would be unnecessary and therfore just a bit of lua scripting would probably achieve the same result. (OK I admit it, I don't really do hardware so I'm always looking for a software only solution:-)

AndrewJ
Offline
United Kingdom
Joined: 22 Nov 2012
Great idea using BT for

Great idea using BT for general presence detection, if you wanted to step it up a gear and have more refined presence detection you could also look into NFC

technology, I plan to use this at home to direct music streaming to specific bluetooth speakers using my Galaxy S4 which is NFC enabled.

NFC module

Cheers

Andrew

bobonline19
Offline
Joined: 4 Apr 2013
Allan,I guess if the app

Allan,

I guess if the app could send a tweet on geo change you have everything you need as the xap twitter can get you to lua. IFTTT may convert an email to a tweet if not?

 

Derek,

Thanks for the info. I've ordered a HC-05. I'll experiment with the AT commands first and then if that works go for a relay module to convert to xap.

 

Andrew,

Could you just program an NFC tag at each speaaker? You can program them to perform Bluetooth pairing or to visit a URL when scanned by your phone?

AndrewJ
Offline
United Kingdom
Joined: 22 Nov 2012
Hi Bob, yep exactly the idea.

Hi Bob, yep exactly the idea.

garrydwilms
Offline
United Kingdom
Joined: 31 Mar 2011
Your are quite right, hc05

Your are quite right, hc05 required rather than hc06.

i now have one on the way from china, in the meantime I'll have a go at coding a jeenode and lua applet up based on the data sheet and test via dummy serial commands from a PC.  Due to existing hardware being present on my system I am opting to integrate via jeenode rf back to the HAHcentral/serial route.

I'll post if I have any success.

Garry

brett
Offline
Providence, United States
Joined: 9 Jan 2010
These hc05's look fun I'll

These hc05's look fun I'll buy one as well and we can all have a hack fest on it.

Brett

mark_baldwin
Offline
Blackburn, United Kingdom
Joined: 19 May 2012
ordered :) let the hack fest

ordered :) let the hack fest begin 

AndrewJ
Offline
United Kingdom
Joined: 22 Nov 2012
Ordered - will pass the time

Ordered - will pass the time over the holidays when im eating Turkey :)

...maybe i should have chose a UK seller...

allanayr
Offline
Ayr, United Kingdom
Joined: 25 Sep 2011
Whew! What a big subject

Whew! What a big subject presence detection is. I (and Mr Google) have had a look at a few things over the last
couple of days and there's a lot of stuff about how difficult it is to make sense of.

I'm sure that you hardware guys will have a good time hacking the HC-05 and that may be the best route, but perhaps
an alternative is to use a Raspberry Pi. I have one sitting next to my HAH anyway so I've had a look at doing stuff
with a blue tooth dongle inserted in it.

Here's a few observations:


This site:
http://www.perceptiveautomation.com/userforum/viewtopic.php?f=4&t=4894&s...

uses a cron setting and a couple of bash scripts to detect phones using wifi

cron:
# detect iPhone on network
*/1 * * * * ~/bin/iphonecron &> /dev/null

iphonecron
#!/bin/bash

for((i=1; $i<=12; i=$(($i+1)))); do
~/bin/iphonecheck
sleep 4
done

iphonecheck
#!/bin/bash
IPHONELASTONLINE=`cat /tmp/iphonestate 2>/dev/null`
ISONLINE="false"
/sbin/ping -t 1 -c 1 192.168.1.117 &>/dev/null && ISONLINE="true" && sleep 1

if [[ "$ISONLINE" != "$IPHONELASTONLINE" ]]; then
osascript -e "tell application \"IndigoServer\" to set value of variable \"PresenceiPhone\" to \"$ISONLINE\""
fi

echo $ISONLINE > /tmp/iphonestate

I don't think that wifi really works but the ping can be changed to l2ping and the bluetooth MAC address and it seems
to work OK. Also with bluetooth it's not required to do the check every 5 seconds because bluetooth will get a response
more reliably than wifi. That said, not all bluetooth pings are reliable, iPhone's don't respond to l2ping when asleep
but my old motorola seems fairly reliable.

Obviously I don't do the "Indigoserver" stuff above. I'm working on a python script which just reads /tmp/iphonestate
and serves it up to a socket which the HAH could listen on.

The python script will probably be something like this:

from subprocess import *

import os

import socket





# new style - subprocess



phonecheck = Popen("cat /tmp/iphonestate",shell=True, stdout=PIPE).stdout


while 1:

line = phonecheck.readline()

if not line: break

lastword = line


clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

clientsocket.connect(('192.168.0.2', 8089))

clientsocket.send(lastword)




After that it would be just a lua script on the HAH to decide what to do with the information.

garrydwilms
Offline
United Kingdom
Joined: 31 Mar 2011
Another equally valid method

Another equally valid method if you have the pi and bluetooth dongle available.

 Bit concerned to hear iphone won't respond to bluetooth ping when asleep though. What about a device scan? I'll have to have a play, as device scanning was the basis of the hc05 integration.

 

allanayr
Offline
Ayr, United Kingdom
Joined: 25 Sep 2011
Not 100% sure, so other views

Not 100% sure, so other views would be welcome, but neither my iPhone 3GS nor the wife's 4S respond to a scan or a ping when asleep. Perhaps I'm doing something wrong and (like you) I'd kinda hoped that the iPhone could be the basis of the presence detection.

When I switch bluetooth on on the iphones and they are discoverable they show up on a scan and also respond to a ping but when they are asleep I can't seem to get any response.

I haven't tried yet but it looks like it might be possible to setup the iPhone "Find My Friends" app to generate an email notification which could then be converted to a tweet by IFTTT. An issue with that would be the reliability of the app and the time lag between the notification and the tweet actually doing anything useful on the HAH. The site I mentioned above does discuss the possibility of forcing the iphone to wake up by sending some kind of push notification to it, but at that point I feel it's becoming to cumbersome to be useful. Having said that, if it's a case of adjusting a thermostat when the house is empty then even a 20 minute delay wouldn't really be an issue.

 

Look forward to hearing anyone else's experience of pinging sleeping iPhones.

 

Just found this on the Dreamgreenhouse site:

iPhones don't enable Bluetooth unless they are paired to one or more devices. The iPhone 3GS doesn't respond to pings and always appears off-line though.

I don't think either of the iPhones I've tried is paired to anything (is there actually anything that you can pair to that is worthwhile?? :-(.  So perhaps that is part of the issue.

 

 

bobonline19
Offline
Joined: 4 Apr 2013
Sounds like you've gone down
Sounds like you've gone down the root of my original post. Thanks for sharing the info. The dream green house link tested l2ping ok with iphone4. Which one are you using? Don't have one myself but my boss has his paired to his car immobiliser so don't think it has to be on. May be needs to be paired?could the pi send udp xap packets? Hah plugboard would just work then?glad there is so much interest in this should be great to get something working. Hopefully have a chance to play with a hc-05 myself soon  
allanayr
Offline
Ayr, United Kingdom
Joined: 25 Sep 2011
My iPhone is a 3GS on iOs 6

My iPhone is a 3GS on iOs 6 and my wife has a 4S on iOs 7. I've just dug out an old bluetooth headset to see if I can get it to pair with the 4S (it'll need a couple of hours charging first though). The pi can certainly send UDP data though whether it's possible to make them into xap packets I'm not sure. The HAH example lua scripts have a tcpRelayApplet which I thought could be used as the basis for an applet to pick up a tcp message from the pi and convert it to a xap message.

I'll see what a charged up bluetooth headset achieves tomorrow.

allanayr
Offline
Ayr, United Kingdom
Joined: 25 Sep 2011
Good news. Having paired the

Good news. Having paired the iPhone 4S with a bluetooth headset it looks as though it now responds to pings when asleep. Even better news is that the 3GS also appears to respond to pings when asleep having also been paired to the headset. They seem to have reasonable range though the ping time seems fairly meaningless, in my house at least, it can vary quite considerably even within the same room.

Slightly less encouragingly the Raspi seems to hang up if left for long periods pinging the phones every minute. This may be partly due to the cheapo bluetooth dongle and the fact that the USB ports on the pi are not that reliable. I'll have to see how it goes over the longer term.

allanayr
Offline
Ayr, United Kingdom
Joined: 25 Sep 2011
As a second option I now have

As a second option I now have a python script working on th pi that handles both the pinging
and transmitting to the HAH just in case the cron/bash script combo proves to be too flaky.

The python script pings the phones and then transmits the result as a UDP datagram. This is
picked up by the udp2xap.lua program running on the HAH and converted to an xap message which
in xfxviewer looks like this:

xap-header
{
uid=FF00CC00
source=dbzoo.lua.socket
hop=1
class=udp.data
v=12
}
body
{
port=41737
data=3GS away
ip=192.168.0.3
}

It looks like presence detection using bluetooth is very doable.
The next stage of actually deciding how to use the information might be more challenging!

bobonline19
Offline
Joined: 4 Apr 2013
Hi Allan,Sounds like you have

Hi Allan,

Sounds like you have been busy. Good news that the iPhone issue is resolved and you have communication to xap. Would the python library used here run on the PI http://www.dbzoo.com/livebox/hah_xap_python ?

allanayr
Offline
Ayr, United Kingdom
Joined: 25 Sep 2011
I'm sure that that's the

I'm sure that that's the intention, thought I have no experience of using Python on the HAH. Lua seems to be the scripting language of choice on the HAH because it runs natively and is probably as flexible as Python. I'm pretty sure that the simple Python script that I'm using on the pi will be easily translated into Lua. 

bobonline19
Offline
Joined: 4 Apr 2013
I thought the python xap
I thought the python xap library could run on the pi to generate the xap udp packets. The plugboard on the hah would then receive and process the xap packets from the pi?
allanayr
Offline
Ayr, United Kingdom
Joined: 25 Sep 2011
Sorry misunderstood

That might be worth a look although basically as long as the info gets to the HAH it can be processed there just as easily in whatever plugboard applet is going to be used to deal with it.

allanayr
Offline
Ayr, United Kingdom
Joined: 25 Sep 2011
A few observations

After a few days playing about with the Raspi/bluetooth dongle combination, here are a few things I've learnt:
My dongle (cheapo chinese I think) isn't all that reliable and sometimes completely hangs up the Pi. I've
reset the Pi's USB ports to operate in USB 1.1 mode only; and this seems to have helped a bit in the
Raspbian Wheezy config but in my Arch Linux config it's still very flaky. I'm sure that you'll probably find
that the HC05 module setup will be much more stable.

Having said that this setup does seem to detect and relay bluetooth ping to the HAH on a reasonably steady basis.
My old Motorola L6 is fairly good at responding to pings. On the iPhone front, the 4S is much more reliable than
the 3GS when in sleep mode.

I've also had a play around with WiFi detection. I installed arp-scan and use the following in the python script
to ping the phones using WiFi: "arp-scan 192.168.0.1-192.168.0.20 | grep 5c:95:ae:xx:xx:xx; echo command returned $?"
I'm not sure how long it takes to detect a newly arrived iPhone or how long the MAC stays in the arp table, I'll have
to spend a bit more time on it, but when asleep the iPhone 4S seems to reliably respond to arp-scan. On the other hand
when asleep the 3GS seems to go completely "off grid" sometimes.

Truth is that for long term presence monitoring (such as when the house has been unoccupied for several hours or even
several days) a combination of bluetooth and WiFi probably give a fuller picture. Though I've still not decided how
to use the information yet. I'll be interested to hear how people get on with their HC05's in due course.

Now back to monitoring those UDP packets!

garrydwilms
Offline
United Kingdom
Joined: 31 Mar 2011
HC05 and Jeenode example

I have added my initial attempt using the jeenode and HC05 in the example scripts forum.

 

Here:  http://www.homeautomationhub.com/content/bluetooth-presence-hc05-and-jee...

Hardware Info