Using Bluetooth for Presence Detection
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
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
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
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
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.
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:-)
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.
Cheers
Andrew
Hi Bob, yep exactly the idea.
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
These hc05's look fun I'll buy one as well and we can all have a hack fest on it.
Brett
ordered :) let the hack fest begin
Ordered - will pass the time over the holidays when im eating Turkey :)
...maybe i should have chose a UK seller...
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.
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.
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.
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.
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.
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!
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.
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.
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!
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...
Would the iPhone geofencing attribute be able to be used instead?