Bluetooth proximity sensor

Discussions about RaZberry - Z-Wave board for Raspberry computer
Post Reply
gsaw
Posts: 78
Joined: 22 Aug 2016 00:26

Bluetooth proximity sensor

Post by gsaw »

Hi,

i'm wearing a smart watch. Pebble Watch. Newly i thought, may be it possible to use my pebble as a proximity sensor. I tested it and it seems to be working. It can also used together with a smartphone.

Namely it is possible to establish a bluetooth connection to a device if you know mac address of the device. And you do not need to pair device. Once you connected to the device, you can obtain a RSSI value (Received signal strength indication). The closer is device to doungle the lower is the RSSI.

I have installed banana pi with razzberry in my workroom. I connected a bluetooth dongle to banana pi and installed bluetooth stuff.

Such i wrote a bash script like this

Code: Select all

hcitool cc $1 2>/dev/null;
PRESENT=$(hcitool rssi $1 2>/dev/null | sed -e "s/RSSI return value: -*//")
hcitool dc $1 2>/dev/null


if [ -z $PRESENT ]
then
        echo "250"
        exit 250
else
        echo "$PRESENT"
        exit $PRESENT
fi
I saved the script as

Code: Select all

/usr/bin/btping
The script gets as paraemter mac address and returns back the RSSI value.

Code: Select all

root@bananapi:/opt/z-way-server/automation# btping B0:B4:48:02:1B:8E
1
Add btping to /opt/z-way-server/automation/.syscommands

Code: Select all

root@bananapi:/opt/z-way-server/automation# cat .syscommands 
btping
now you can create CodeDevice (sensorMultilevel) based on btping.

Code: Select all

parseInt(system('btping B0:B4:48:02:1B:8E')[1])
Now i can create logical rules based on this proximity sensor. For example turn on light if rssi is < 3 and turn off if rssi >3. It works perfectly. Or i can "track" my phone if i lost it again somewhere. :lol:

I have polling interval 10 seconds. I do not know, how much it stress the battery of my pebble. But i hope not very significant.

In order to find out mac address of pebble, smartphone or other device you have to put it in discoverable mode and start hcitool scan in command line on banana pi

Code: Select all

root@bananapi:/opt/z-way-server/automation# hcitool scan
Scanning ...
	B0:B4:48:02:1B:8E	Pebble 1B8E
	C4:46:19:E2:6A:EE	ubuntu-0
For example pebble goes in discoverable mode if you go to bluetooth setting menu. An android smartphone is also in discoverable mode if you open bluetooth settings.

greetings
gsaw
Posts: 78
Joined: 22 Aug 2016 00:26

Re: Bluetooth proximity sensor

Post by gsaw »

I just discovered my pebble was not connected as usual to my smartphone. This is because it worked with pebble. Once i connected my pebble to smartphone, it was not possible to connect banana pi to pebble. :evil: Pebble can be connected to only one device at time.

It work also only with smartphone which can manage more then one connection. I am so sad. :cry:

There are some bluetooth locator key tags. I whould try it.
vh342
Posts: 14
Joined: 19 Jan 2016 23:20

Re: Bluetooth proximity sensor

Post by vh342 »

You are on the right way!

The problem is, you will need a completed bluetooth connection between your Pebble and Pi for reading out live RSSI values. If you want to look up presence status only, the paired connection is not necessary! Just do a bluetooth name lookup with "hcitool name $MAC". If your Pebble/Phone/BTGadget is in range, the name will be showed. It works, if the BT devices have been paired once before.

Here is my BT presence script:

Code: Select all

bt_scanned=$(sudo hcitool name $1 2>/dev/null)
exitcode=$? 
n=0

if [ $? -eq 0 ]; then
 if [ "$bt_scanned" != '' ]; then 
    echo "on"
 #  "MAC Found!"
 else
  for((i=0; i<3; i++))
   do
    sleep 10; 
    bt_scanned=$(sudo hcitool name $1 2>/dev/null);  
     if [ $? -eq 0 ]; then 
       if [ "$bt_scanned" == '' ]; then 
         n=n+1;
       fi;
     fi; 
  done	 

  if [ $n==3 ]; then
	echo "off" 
 # "MAC not found, 3 times"
  fi;
 fi;
fi; 

exit $exitcode

I hope that is saving the money for your key tags ;)
gsaw
Posts: 78
Joined: 22 Aug 2016 00:26

Re: Bluetooth proximity sensor

Post by gsaw »

vh342 wrote:You are on the right way!

The problem is, you will need a completed bluetooth connection between your Pebble and Pi for reading out live RSSI values. If you want to look up presence status only, the paired connection is not necessary! Just do a bluetooth name lookup with "hcitool name $MAC".
Hm, no, actually this does not work.

Code: Select all

root@bananapi:/home/georg# hcitool name B0:B4:48:02:1B:8E; echo $?
0
root@bananapi:/home/georg# hcitool name B0:B4:48:02:1B:8E; echo $?
Pebble 1B8E
0
root@bananapi:/home/georg# hcitool name B0:B4:48:02:1B:8E; echo $?
0
1: paired pebble
2: pebble is not paired
3: pebble's bluetooth is switched off

It always return 0. :cry: If Pebble is paired with phone it does not accept any connections, even name requests. And just detection of presence is not enough. It detects my pebble even if I'm in another one room, 8 meters away. In my flat it is to vague. I played with pebble and it is very nice thing. If i enter my workroom it turns light on, for me, for nobody else :) And light is turned off right I leave the room. Much better then motion detection and complex rules. I already ordered tags by amazon. Tomorrow or rather today these should be delivered. :lol: I will try it, may be it makes fun too.
Post Reply