Page 3 of 3
Re: Presence detection based on IP and Bluetooth
Posted: Wednesday 11 April 2018 15:11
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.
Re: Presence detection based on IP and Bluetooth
Posted: Wednesday 11 April 2018 17:08
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
Presence detection based on IP and Bluetooth
Posted: Tuesday 02 March 2021 21:09
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
Re: Presence detection based on IP and Bluetooth
Posted: Tuesday 02 March 2021 21:47
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
Re: Presence detection based on IP and Bluetooth
Posted: Wednesday 03 March 2021 7:28
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
Re: Presence detection based on IP and Bluetooth
Posted: Thursday 04 March 2021 8:16
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.