Ultimate Wifi and Bluetooth Presence Detection

All kinds of 'OS' scripts
ben53252642
Posts: 392
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Ultimate Wifi and Bluetooth Presence Detection

Post by ben53252642 » Saturday 02 July 2016 20:06

Script attempts to ping target device 4 times in order:
  • Wifi
    Bluetooth Mac
    Wifi
    Bluetooth Mac
If all attempts to ping fail it notifies domoticz that the device is "Off".

requirements: apt-get install fping

The great thing about this Bluetooth detection method is that it does not require the target device to be paired, to get a target devices mac address simply put it in discoverable mode and run: hcitool scan

Once you have the mac address, as long as Bluetooth is turned on, on the device you can ping it (again you do not need to pair the device and it only need to be in discoverable mode to initially get it's mac address) making it highly ideal for reliable presence detection.

Bash Script:

Code: Select all

#!/bin/bash
while [ 1 ]
do

# Set Parameters
Name='Bens Mobile Phone'
domoticzserverip='192.168.0.5'
IDX='108'
IP='192.168.0.80'
bluetoothmac='04:9C:02:57:23:F5'

# First network ping attempt
fping -c1 -b 32 -t1000 $IP 2>/dev/null 1>/dev/null
if [ "$?" = 0 ] ; then
device=$(echo "On")
technology="Wifi 1st attempt"
success="yes"
sleep 1
else
success="no"
technology=''
fi

# First bluetooth ping attempt
if [[ $success != 'yes' ]]; then
bt1=$(l2ping -c1 -s32 -t1 "$bluetoothmac" > /dev/null && echo "On" || echo "Off")
if [[ $bt1 == 'On' ]]; then
device=$(echo "On")
technology="Bluetooth 1st attempt"
success="yes"
fi
fi

# Second network ping attempt
if [[ $success != 'yes' ]]; then
fping -c1 -b 32 -t1000 $IP 2>/dev/null 1>/dev/null
if [ "$?" = 0 ] ; then
device=$(echo "On")
technology="Wifi 2nd attempt"
success="yes"
fi
fi

# Second bluetooth ping attempt
if [[ $success != 'yes' ]]; then
bt1=$(l2ping -c1 -s32 -t1 "$bluetoothmac" > /dev/null && echo "On" || echo "Off")
if [[ $bt1 == 'On' ]]; then
device=$(echo "On")
technology="Bluetooth 2nd attempt"
success="yes"
fi
fi

# If the device is still offline, declare it for processing
if [[ $success != 'yes' ]]; then
device=$(echo "Off")
fi

# Check Online / Offline state of Domoticz device
domoticzstatus=$(curl -s "http://"$domoticzserverip"/json.htm?type=devices&rid="$IDX"" | grep '"Data" :' | awk '{ print $3 }' | sed 's/[!@#\$%",^&*()]//g')
# Compare ping result to Domoticz device status
if [ "$device" = "$domoticzstatus" ] ; then
echo "Status in sync $technology"
else
echo "Status out of sync, correcting..."
if [ "$device" = On ] ; then
echo "$Name" "Online"
curl -s "http://"$domoticzserverip"/json.htm?type=command&param=switchlight&idx="$IDX"&switchcmd=On" 2>/dev/null 1>/dev/null
else
echo "$Name" "Offline"
curl -s "http://"$domoticzserverip"/json.htm?type=command&param=switchlight&idx="$IDX"&switchcmd=Off" 2>/dev/null 1>/dev/null
fi
fi

done
screen10.png
screen10.png (30.78 KiB) Viewed 14346 times

Holland
Posts: 329
Joined: Friday 12 July 2013 13:53
Target OS: Raspberry Pi
Domoticz version: Beta Ch
Location: The Netherlands
Contact:

Re: Ultimate Wifi and Bluetooth Presence Detection

Post by Holland » Sunday 17 July 2016 15:02

Hi,

Just tried your script. Works well for a iPhone 6, but not as expected with a 6s.

With a 6s the bluetooth is rather unreliable when the phone is locked. For testing purposed, when i disable wifi, it sometimes fails to ping via bluetooth. The solution is to disable and re-enable bluetooth again. Just to be sure, same thing happens when I use l2ping via the command.


Just wondering if you experienced the same issue during testing?

p.s Using the Pi 3 as the BT host

ben53252642
Posts: 392
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Re: Ultimate Wifi and Bluetooth Presence Detection

Post by ben53252642 » Sunday 17 July 2016 15:29

I use a 6s, had it reliably detected via Bluetooth for more than 48hrs at a time.

The first Bluetooth Ping attempt does fail sometimes which is why I added the second attempt before the script marks it as being offline.

I use an external CSR 4.0 bluetooth dongle however the Pi's onboard Bluetooth shouldn't be a problem. If it "works" but not always my suggestion would be to add some additional pings into the script before marking it as offline.

Eg maybe make it run 4 bluetooth pings before marking it as offline.

I have a PHP version of this script which I'll be making public shortly as well. :)

mcmikev
Posts: 147
Joined: Tuesday 26 May 2015 8:11
Target OS: Raspberry Pi
Domoticz version: beta
Location: right here
Contact:

Re: Ultimate Wifi and Bluetooth Presence Detection

Post by mcmikev » Sunday 17 July 2016 15:36

Hi,

Really liked the presence detecion style like this :-)

But when i tried to run the script I get errors like this:
The status is not changing in domoticz. Ip address is correct of domoticz server and iphone.
BT address is also correct.

What can this be? any pointers?

Code: Select all

pi@domo:~/domoticz/scripts$ sh iphone_mike.sh
iphone_mike.sh: 25: iphone_mike.sh: [[: not found
iphone_mike.sh: 35: iphone_mike.sh: [[: not found
iphone_mike.sh: 45: iphone_mike.sh: [[: not found
iphone_mike.sh: 55: iphone_mike.sh: [[: not found
Status out of sync, correcting...

ben53252642
Posts: 392
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Re: Ultimate Wifi and Bluetooth Presence Detection

Post by ben53252642 » Sunday 17 July 2016 15:37

yep, use ./iphone_mike.sh to run it

don't use sh iphone_mike.sh (using this the error you mentioned will occur).

let me know :D

mcmikev
Posts: 147
Joined: Tuesday 26 May 2015 8:11
Target OS: Raspberry Pi
Domoticz version: beta
Location: right here
Contact:

Re: Ultimate Wifi and Bluetooth Presence Detection

Post by mcmikev » Sunday 17 July 2016 15:47

Working better now :-)

but still an issue. Not sure what this is. I have placed the port of domoticz behind the ip otherwise it does not work here.

Permissions are fine. Excluded for the pi that runs this scripts.

Code: Select all

Status out of sync, correcting...
Mikes iPhone Online
Can't create socket: Operation not permitted
Status out of sync, correcting...
Mikes iPhone Online
Status out of sync, correcting...
Mikes iPhone Online
Can't create socket: Operation not permitted
Status out of sync, correcting...
Mikes iPhone Online
Can't create socket: Operation not permitted
Can't create socket: Operation not permitted

ben53252642
Posts: 392
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Re: Ultimate Wifi and Bluetooth Presence Detection

Post by ben53252642 » Sunday 17 July 2016 15:56

hmm are you running this as a non root user?

Try sudo ./mikes_iphone.sh

Also reboot the pi, this appears to be an issue with Bluetooth, can you manually l2ping the mac address?

mcmikev
Posts: 147
Joined: Tuesday 26 May 2015 8:11
Target OS: Raspberry Pi
Domoticz version: beta
Location: right here
Contact:

Re: Ultimate Wifi and Bluetooth Presence Detection

Post by mcmikev » Sunday 17 July 2016 16:00

no root user indeed.

Sudo does work :-)

ben53252642
Posts: 392
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Re: Ultimate Wifi and Bluetooth Presence Detection

Post by ben53252642 » Sunday 17 July 2016 16:09

ok If you want to run this on startup do this:

create startup.sh in the same folder as mikes_iphone.sh

the contents of which should be (edit as needed):

Code: Select all

cd /root/domoticz/benscripts/ping
# Start Bens Mobile Ping Monitor
#/usr/bin/screen -S PingBensMobile -d -m ./bensmobile.sh
Then create in /etc/init.d a script called ping (again edit as needed).

Code: Select all

#!/bin/sh
### BEGIN INIT INFO
# Provides: Startscripts
# Required-Start: $network
# Required-Stop: $network
# Default-Start: 2 3 5
# Default-Stop:
# Description: 
### END INIT INFO

case "$1" in
'start')
        sudo sh /root/domoticz/benscripts/startup.sh
        ;;
'stop')
        ;;
*)
        echo "Usage: $0 { start | stop }"
        ;;
esac
exit 0
Don't forget to chmod 755 both scripts, then if you have it go into webmin and set the ping script to start on boot.

chrispazz
Posts: 60
Joined: Friday 08 July 2016 10:38
Target OS: -
Domoticz version:
Contact:

Re: Ultimate Wifi and Bluetooth Presence Detection

Post by chrispazz » Tuesday 19 July 2016 14:50

Is it possibile to translate it to LUA timed script?

ben53252642
Posts: 392
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Re: Ultimate Wifi and Bluetooth Presence Detection

Post by ben53252642 » Tuesday 19 July 2016 15:26

chrispazz - negative, my understanding is that Lua scripts in Domoticz are checked either every minute or each time a device state changes (eg a bed lamp turns on).

In this case running it every say 2 seconds would not be possible.

chrispazz
Posts: 60
Joined: Friday 08 July 2016 10:38
Target OS: -
Domoticz version:
Contact:

Re: Ultimate Wifi and Bluetooth Presence Detection

Post by chrispazz » Tuesday 19 July 2016 20:40

I added your version and it is working ok! Thank you

FritsK
Posts: 3
Joined: Wednesday 13 July 2016 16:07
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Ultimate Wifi and Bluetooth Presence Detection

Post by FritsK » Sunday 24 July 2016 13:32

Great script. Does this script has to be run manually? Or will it automatically start pinging if a device shows up in the network?

ben53252642
Posts: 392
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Re: Ultimate Wifi and Bluetooth Presence Detection

Post by ben53252642 » Sunday 24 July 2016 13:43

The script runs in a loop looking for the target device, if you want it to run "headless", when you are not logged into the linux machine then you need to use "screen". See the 9th post from the top of this thread for that script (which runs the main script headless).

I'd recommend learning about "screen" and how it works, reading / YouTube etc...

User avatar
Derik
Posts: 2099
Joined: Friday 18 October 2013 23:33
Target OS: Raspberry Pi
Domoticz version: BETA
Location: Arnhem/Nijmegen Nederland
Contact:

Re: Ultimate Wifi and Bluetooth Presence Detection

Post by Derik » Sunday 24 July 2016 15:21

mmm what is the range of the Bluetooth ??
Is there a different between Bt Radio's???

My Windows Phone WiFi is not alive always so this option looks cool to me...
To switch my dummy home or not
Xu4: Beta Extreme antenna RFXcomE,WU Fi Ping ip P1 Gen5 PVOutput Harmony HUE SolarmanPv OTG Winddelen Alive ESP Buienradar MySensors WOL Winddelen counting RPi: Beta SMAspot RFlinkTest Domoticz ...Different backups

ben53252642
Posts: 392
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Re: Ultimate Wifi and Bluetooth Presence Detection

Post by ben53252642 » Sunday 24 July 2016 15:27

The range of the Bluetooth is about 10 - 15m using a CSR 4.0 USB dongle, Raspberry Pi 3 onboard Bluetooth is about the same.

Many smart phone brands switch off the wifi to sleep and conserve power which is why this script is good cause it takes a multi technology approach to detecting the devices.

I have a php version of this which writes to a MySQL database and is "multi node", as in I have Raspberry Pi's around the house with Bluetooth dongles on them and they each report to the database if they can see the target device, this was my approach for extending the range of the presence detection.

I'll be making the code for the multi-node setup available shortly. :D

User avatar
Derik
Posts: 2099
Joined: Friday 18 October 2013 23:33
Target OS: Raspberry Pi
Domoticz version: BETA
Location: Arnhem/Nijmegen Nederland
Contact:

Re: Ultimate Wifi and Bluetooth Presence Detection

Post by Derik » Sunday 24 July 2016 15:48

I have Raspberry Pi's around the house with Bluetooth
All with domoticz...
So they are slaves???

Or do you have a other setup.
Xu4: Beta Extreme antenna RFXcomE,WU Fi Ping ip P1 Gen5 PVOutput Harmony HUE SolarmanPv OTG Winddelen Alive ESP Buienradar MySensors WOL Winddelen counting RPi: Beta SMAspot RFlinkTest Domoticz ...Different backups

ben53252642
Posts: 392
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Re: Ultimate Wifi and Bluetooth Presence Detection

Post by ben53252642 » Sunday 24 July 2016 15:57

The Pi's just run a php script in a loop looking for the Bluetooth mac address, if they spot it they report back to the MySQL DB.

The main Domoticz Pi runs a few scripts, one of which checks the MySQL DB to see which Pi's spotted the target device bluetooth mac and if the time they spotted the device was less than 5 seconds difference between that time and the system time then it marks the device as online.

If all Pi's can't see the target then the device is marked as offline.

Time to pickup a device if it was offline and just came online via Bluetooth is about 1.5 seconds.

Additional Pi's don't run Domoticz, they only need to run the php script looking for the target and update MySQL on the main Pi if they spot it.

I have an "additional Pi" at the main entrance to my house which picks up my iPhone 6s before I reach the door.

As far as hardware for "additional Pi's" is concerned, Raspberry Pi3 is pretty much perfect, got wifi and bluetooth onboard and you can get very small cases for them to place around the house.

User avatar
remb0
Posts: 956
Joined: Thursday 11 July 2013 22:21
Target OS: Raspberry Pi
Domoticz version: Beta
Location: The Netherlands
Contact:

Re: Ultimate Wifi and Bluetooth Presence Detection

Post by remb0 » Monday 25 July 2016 19:53

i got

Code: Select all

Status out of sync, correcting...
remco Mobile Phone Online
Status out of sync, correcting...
remco Mobile Phone Online
Can't connect: No route to host
Can't connect: No route to host
Status out of sync, correcting...
remco Mobile Phone Offline
Can't connect: No route to host
Can't connect: No route to host
Status out of sync, correcting...
remco Mobile Phone Offline
Status out of sync, correcting...
remco Mobile Phone Online
I think my bleutooth (onboard pi3) isn't enabled.

systemctl status bluetooth
gave me:

Code: Select all

● bluetooth.service - Bluetooth service
   Loaded: loaded (/lib/systemd/system/bluetooth.service; enabled)
   Active: active (running) since Mon 2016-07-25 14:08:37 CEST; 5h 43min ago
     Docs: man:bluetoothd(8)
 Main PID: 1013 (bluetoothd)
   Status: "Running"
   CGroup: /system.slice/bluetooth.service
           └─1013 /usr/lib/bluetooth/bluetoothd
[code]

but when I check:
sudo bluetoothctl
agent on
default-agent
scan on

i got: [b]no default controller available[/b]

who got a golden tip for me?

ben53252642
Posts: 392
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Re: Ultimate Wifi and Bluetooth Presence Detection

Post by ben53252642 » Monday 25 July 2016 19:56

are you using a Z-Wave card on the gpio? If so remember the onboard Bluetooth has to be disabled for that (I use an external USB CSR 4.0 dongle).

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest