Presence detection based on IP and Bluetooth

Moderator: leecollings

rlg6767
Posts: 8
Joined: Friday 30 March 2018 22:51
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Presence detection based on IP and Bluetooth

Post by rlg6767 »

When I initially run the script from the terminal I can see in the terminal it sees the mobile device from both ping and bluetooth. Then after that it just reports that it can see the device from ping.

If this is by design, then that is totally fine.
User avatar
jvdz
Posts: 2333
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Presence detection based on IP and Bluetooth

Post by jvdz »

Ok, but that wasn't the question. ;)
The script will only try BT when the IP PING fails, so the question is what happens when the IP Ping fails and the BT is tried?
This is just to ensure both methods work as that is used a safeguard since most cellphone shut down their WiFi for periods to preserve battery life.

Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
mwmaster
Posts: 5
Joined: Sunday 15 October 2017 21:22
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Presence detection based on IP and Bluetooth

Post by mwmaster »

I found a script online that doen's require additional software.
The script switches a dummy on or off by detecting my phones wifi or bluetooth connection.
bluetooth workes fine but it doesn't work with my wifi (IP=fixed btw)

can any one tell me what is wrong with my settings/script

https://www.domoticz.com/wiki/Event_scr ... ork_device

Code: Select all

-- Title: script_time_ping.lua
-- Date:  14-07-2015

commandArray={}

debug=false

prefixe="(PING) "

local ping={}
local ping_success
local bt_success

ping[1]={'192.168.1.1', 'Device 1', 'Teller 1', 'nil', 5}                 -- android
ping[2]={'192.168.1.2', 'Device 2', 'Teller 2', 'aa:bb:cc:dd:ee:ff', 5}   -- iphone
ping[3]={'192.168.1.3', 'Device 3', 'Teller 3', 'nil', 5}                 -- android

for ip = 1, #ping do
  if ping[ip][4] == 'nil' then
    bt_success=false
    ping_success=os.execute('ping -c 1 -w 1 '..ping[ip][1])
  else
    f = assert (io.popen ("hcitool names "..ping[ip][4]))
    bt = f:read()
    if bt==nil then
      bt_success=false
    else
      bt_success=true
    end
    f:close()
  end
  if ping_success or bt_success then
    if (debug==true) then
      print(prefixe.."ping success "..ping[ip][2])
    end
    if (otherdevices[ping[ip][2]]=='Off') then
      commandArray[ping[ip][2]]='On'
    end
    if (uservariables[ping[ip][3]]) ~= 1 then
      commandArray['Variable:'..ping[ip][3]]= tostring(1)
    end
  else
    if (debug==true) then
      print(prefixe.."ping fail "..ping[ip][2])
    end
    if (otherdevices[ping[ip][2]]=='On') then
      if (uservariables[ping[ip][3]])==ping[ip][5] then
        commandArray[ping[ip][2]]='Off'
      else
        commandArray['Variable:'..ping[ip][3]]= tostring((uservariables[ping[ip][3]]) + 1)
      end
    end
  end
end

return commandArray
User avatar
jvdz
Posts: 2333
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Presence detection based on IP and Bluetooth

Post by jvdz »

mwmaster wrote: Tuesday 02 March 2021 21:09 I found a script online that doen's require additional software.
-snip-
f = assert (io.popen ("hcitool names "..ping[ip][4]))
...and what do you think the used hcitool is? ;)
I would advice you to also do the BlueTooth hcltool check for android as that has the same issue as Apple these days were the Wifi goes is some sort of sleep mode and doesn't respond, so you need bluetooth as an alternative.

Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Presence detection based on IP and Bluetooth

Post by waaren »

mwmaster wrote: Tuesday 02 March 2021 21:09 can any one tell me what is wrong with my settings/script
this script use os.execute to execute the ping command. The Lua os.execute() function does return 3 values and the os resultcode (0 is OK, > 0 is NOK) is in the third one.
One way of solving this is to change

Code: Select all

ping_success=os.execute('ping -c 1 -w 1 '..ping[ip][1])
to

Code: Select all

local _, _, rc  = os.execute('ping -c 1 -w 1 '..ping[ip][1]) -- store resultcode of the ping command in rc
ping_success = rc == 0  -- ping was successful when rc is 0 
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
lost
Posts: 662
Joined: Thursday 10 November 2016 9:30
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Presence detection based on IP and Bluetooth

Post by lost »

jvdz wrote: Tuesday 02 March 2021 21:47 I would advice you to also do the BlueTooth hcltool check for android as that has the same issue as Apple these days were the Wifi goes is some sort of sleep mode and doesn't respond, so you need bluetooth as an alternative.
Agree: BT when activated never sleeps as this does not much affect battery life compared to wifi. No issue with random MAC addresses that becomes quite common for wifi as well, to avoid tracing users.
Issue may be with range, depending on home size and construction... and your habits: At home, mobile phone are not allowed to go upstairs in bedrooms, 1st because of lack of confidence with connected devices with mic+cam, 2nd because that's the only way to keep control when children have their own device.

To keep responsiveness while being able to reject short out of range periods and not affect phone battery too much with intensive "BT pings", I would advice switching to python instead of a bash script, as this may become a bit more complex. On my side, a phone BT seen "in-range" is only checked every 10mn while those "out-of-range" are every 20s for instance.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest