Python Plugin: MAC address presence Topic is solved

Python and python framework

Moderator: leecollings

Post Reply
Xorfor

Python Plugin: MAC address presence

Post by Xorfor »

Great these Python plugins!!! Will the Domoticz team move all currently supported devices to python plugins?

I created a plugin to detect whether a mobile phone is in the Domoticz network, identified by MAC address. So, no fixed ip addresses are required. Still some work to do.

Please look at: https://github.com/Xorfor/Domoticz/tree ... acpresence
Logread
Posts: 228
Joined: Sunday 28 August 2016 7:48
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: France
Contact:

Re: Python Plugin: MAC address presence

Post by Logread »

I wrote a similar plugin - with wake-on-lan and remote shutdown options - some time ago but did not yet find time to polish it for a public release, however I noted that a simple « arp-scan » did not reliably detect some mobile devices.

I had to force a repeat option for the scan and also move the scanning process to a shell script called by a cron, in order to avoid locking the python thread during a scan.

What is your experience with reliability of detection for mobile devices ?
Xorfor

Re: Python Plugin: MAC address presence

Post by Xorfor »

@Logread: You are right, the reliability of detection of devices is not very good. That is one of the reasons that I added a timeout of 10 minutes (by default). An other reason for the timeout is that you will not switch off the heating immediately because you leave your home for a minute.
Arp-scan and also arp on windows need some retries to find the devices.
I use a lua script (like this python version) for months and that is working very good.
Rodio
Posts: 39
Joined: Thursday 16 April 2015 10:18
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Python Plugin: MAC address presence

Post by Rodio »

Hi Xorfor,

I'm really interested in this solution, so I installed your plugin and get NO errors ;)
But the device status is not updated either :(
Running v3.8153 on a RPI
Triple checked the MAC adress, phone is pingable.


Logfile during startup (MACadress is deleted by me, same as the attachment) :

2017-12-07 20:16:25.551 (MacDetect) Debug log level set to: 'true'.
2017-12-07 20:16:25.551 (MacDetect) Number of mac items: 1
2017-12-07 20:16:25.551 (MacDetect) 'Mode1':'iPhone,xx:xx:xx:xx:xx:xx'
2017-12-07 20:16:25.551 (MacDetect) 'HardwareID':'9'
2017-12-07 20:16:25.551 (MacDetect) 'Port':'0'
2017-12-07 20:16:25.551 (MacDetect) 'Mode2':'Single'
2017-12-07 20:16:25.551 (MacDetect) 'Author':'Xorfor'
2017-12-07 20:16:25.551 (MacDetect) 'Mode3':'1'
2017-12-07 20:16:25.551 (MacDetect) 'Name':'MacDetect'
2017-12-07 20:16:25.551 (MacDetect) 'HomeFolder':'/home/pi/domoticz/plugins/MacDetect/'
2017-12-07 20:16:25.551 (MacDetect) 'Mode6':'Debug'
2017-12-07 20:16:25.551 (MacDetect) 'Version':'1.0.0'
2017-12-07 20:16:25.551 (MacDetect) 'Mode4':'10'
2017-12-07 20:16:25.551 (MacDetect) 'Key':'macpresence'
2017-12-07 20:16:25.551 (MacDetect) Device count: 1
2017-12-07 20:16:25.551 (MacDetect) Device: 1 - ID: 70, Name: 'iPhone', nValue: 0, sValue: 'Off'
2017-12-07 20:16:25.551 (MacDetect) Device ID: '70'
2017-12-07 20:16:25.551 (MacDetect) Device Name: 'iPhone'
2017-12-07 20:16:25.551 (MacDetect) Device nValue: 0
2017-12-07 20:16:25.551 (MacDetect) Device sValue: 'Off'
2017-12-07 20:16:25.551 (MacDetect) Device LastLevel: 0
2017-12-07 20:16:25.551 (MacDetect) Heartbeat interval set to: 60.

Logfile during 'normal use' :
2017-12-07 20:38:34.734 (MacDetect) Calling message handler 'onHeartbeat'.
2017-12-07 20:38:34.734 (MacDetect) onHeartbeat called
2017-12-07 20:38:34.780 (MacDetect) num: 1
2017-12-07 20:38:34.780 (MacDetect) Name: iPhone
2017-12-07 20:38:34.780 (MacDetect) No addresses found
Device screenshot
Device screenshot
Schermafbeelding 2017-12-07 om 21.09.54.png (20.31 KiB) Viewed 5648 times
Hardware screenshot
Hardware screenshot
HardwareInfo.jpg (150.17 KiB) Viewed 5648 times
Xorfor

Re: Python Plugin: MAC address presence

Post by Xorfor »

Did you install arp-scan???

I forgot to mention it in the readme. I just did an update.

The installation can be done (on Raspberry Pi) by:

sudo arpt-get arp-scan -y

Hope this helps. Be aware that arp-scan is not a bullet proof solution to scan mac addresses. But with a timeout of 10 minutes, I didn't have any problem with 3 mac addresses.
Rodio
Posts: 39
Joined: Thursday 16 April 2015 10:18
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Python Plugin: MAC address presence

Post by Rodio »

Hi,

installed arp-scan now ;)
Rebooted several time, because of error MacDetect ended abruptly (i think)
after 3rd reboot the errors disappeared .
Now I get this messages, but both phones are active at this moment (AccesPoints (apple airport) see them both.

2017-12-09 18:51:49.985 (MacDetect) Calling message handler 'onHeartbeat'.
2017-12-09 18:51:49.985 (MacDetect) onHeartbeat called
2017-12-09 18:51:53.231 (MacDetect) num: 1
2017-12-09 18:51:53.231 (MacDetect) Name: iPhone
2017-12-09 18:51:53.231 (MacDetect) address: 98:CA:......... not found
2017-12-09 18:51:53.231 (MacDetect) num: 2
2017-12-09 18:51:53.231 (MacDetect) Name: S6
2017-12-09 18:51:53.231 (MacDetect) address: E8:50:.......... not found

edit: typo
Xorfor

Re: Python Plugin: MAC address presence

Post by Xorfor »

@Rodio

Did you try on console: sudo arp-scan -lq? This is actual what the plugin is executing. Sometimes it will take a while before a device gets listed.
User avatar
EdwinK
Posts: 1820
Joined: Sunday 22 January 2017 21:46
Target OS: Raspberry Pi / ODroid
Domoticz version: BETA
Location: Rhoon
Contact:

Re: Python Plugin: MAC address presence

Post by EdwinK »

Could this (and your other script) also be used stand-alone. I (and others) seem to have problems running plugins from the RPI.
Running latest BETA on a Pi-3 | Toon® Thermostat (rooted) | Hue | Tuya | IKEA tradfri | Dashticz V3 on Lenovo Huawei Tablet | Conbee
Xorfor

Re: Python Plugin: MAC address presence

Post by Xorfor »

@EdwinK: This is the 'old' LUA code I used:

Code: Select all

-- List of smartphones with the mac address
local smartphones={} 
smartphones['Name1']='xx:xx:xx:xx:xx:xx'
smartphones['Name2']='xx:xx:xx:xx:xx:xx'
smartphones['Name3']='xx:xx:xx:xx:xx:xx'

function timedifference(s)
  year = string.sub(s, 1, 4)
  month = string.sub(s, 6, 7)
  day = string.sub(s, 9, 10)
  hour = string.sub(s, 12, 13)
  minutes = string.sub(s, 15, 16)
  seconds = string.sub(s, 18, 19)
  t1 = os.time()
  t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
  difference = os.difftime (t1, t2)
  return difference
end

commandArray = {}
-- save list of connected devices
os.execute("sudo arp-scan -l -q > /tmp/scan-arp.txt") 

for key, mac in pairs(smartphones) do
  -- search for the mac address in the temporary file
  local mac_success=os.execute("sudo grep -e " .. mac .. " /tmp/scan-arp.txt") 

  if mac_success then
    -- device available, or wait 5 minutes
    if ( 
        otherdevices[key] ~= 'On' 
        or timedifference(otherdevices_lastupdate[key]) > 300 
       ) 
    then
      print("MAC address: " .. key .. " (" .. mac .. ") in LAN")
      commandArray[key] = 'On'
    end
  else
    -- device not detected after 10 minutes, then switch off
    if ( 
        (otherdevices[key] ~= 'Off' and timedifference(otherdevices_lastupdate[key]) > 600) 
        or timedifference(otherdevices_lastupdate[key]) > 1800 
       ) 
    then
      print("MAC address: " .. key .. " (" .. mac .. ") not in LAN")
      commandArray[key] = 'Off'
    end
  end
end

return commandArray
Create a dummy device (this is not working with 3.8775, so wait for a fix for that) with a switch for each mac address (mobile phone). Create in Events a new LUA/Time event with above code. Replace Name1 ... Name3 (or more) with the names of the just created switches and fill in the corresponding mac addresses.
User avatar
EdwinK
Posts: 1820
Joined: Sunday 22 January 2017 21:46
Target OS: Raspberry Pi / ODroid
Domoticz version: BETA
Location: Rhoon
Contact:

Re: Python Plugin: MAC address presence

Post by EdwinK »

Thanks. Looking into this one
Running latest BETA on a Pi-3 | Toon® Thermostat (rooted) | Hue | Tuya | IKEA tradfri | Dashticz V3 on Lenovo Huawei Tablet | Conbee
Rodio
Posts: 39
Joined: Thursday 16 April 2015 10:18
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Python Plugin: MAC address presence

Post by Rodio »

Hi Xorfor,

I got an answer, including the MACs of the 2 phones, but still Domoticz doesn't seem to know.
What I noticed is that the arp-scan returns lower-caps, so I changed one of the devices in lowercaps.
Did not help :-(


Thx
R
Xorfor

Re: Python Plugin: MAC address presence

Post by Xorfor »

@Rodio: I made some improvements and added extra debugging. Can you please try the latest version.
Rodio
Posts: 39
Joined: Thursday 16 April 2015 10:18
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Python Plugin: MAC address presence

Post by Rodio »

:oops: :oops: :oops: :oops: :oops: :oops: :oops: :oops: :oops:

ehhhhh

:oops:

sooooooooo heeeeeee

stupid me, the lowercase was the solution.
But I didn't notice the extra space I put behind the devicename.
I entered 'S6, xx:xx:xx:xx' instead of S6,xx:xx:xx:xx.

So now both devices work :D

so my solution is 'S6,12:a3:bc:d4:56:ef'

I'm happy and gonna test some more.

@Xorfor, thx for the help.
I really appreciate it
Rodio
Posts: 39
Joined: Thursday 16 April 2015 10:18
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Python Plugin: MAC address presence

Post by Rodio »

The new version logfile:

I disabled the WiFi of the S6, just to test.

2017-12-11 22:54:17.969 (DetectMac) Debug log level set to: 'true'.
2017-12-11 22:54:18.170 (DetectMac) Number of mac items: 2
2017-12-11 22:54:18.170 (DetectMac) num: 1
2017-12-11 22:54:18.170 (DetectMac) self.timeouts[num]: 10
2017-12-11 22:54:18.170 (DetectMac) Creating device 'iPhone7'.
2017-12-11 22:54:18.172 (DetectMac) num: 2
2017-12-11 22:54:18.172 (DetectMac) self.timeouts[num]: 10
2017-12-11 22:54:18.172 (DetectMac) Creating device 'S6'.
2017-12-11 22:54:18.173 (DetectMac) 'Author':'Xorfor'
2017-12-11 22:54:18.173 (DetectMac) 'Port':'0'
2017-12-11 22:54:18.173 (DetectMac) 'Version':'1.2.0'
2017-12-11 22:54:18.173 (DetectMac) 'Mode6':'Debug'
2017-12-11 22:54:18.173 (DetectMac) 'Mode4':'10'
2017-12-11 22:54:18.173 (DetectMac) 'Mode1':'iPhone7,xx:xx:xx:xx:xx:xx|S6,xx:xx:xx:xx:xx:xx'
2017-12-11 22:54:18.173 (DetectMac) 'HomeFolder':'/home/pi/domoticz/plugins/MacDetect/'
2017-12-11 22:54:18.173 (DetectMac) 'Name':'DetectMac'
2017-12-11 22:54:18.173 (DetectMac) 'Mode2':'Multiple'
2017-12-11 22:54:18.173 (DetectMac) 'HardwareID':'10'
2017-12-11 22:54:18.173 (DetectMac) 'Key':'macpresence'
2017-12-11 22:54:18.173 (DetectMac) 'Mode3':'1'
2017-12-11 22:54:18.173 (DetectMac) Device count: 2
2017-12-11 22:54:18.173 (DetectMac) Device: 1 - ID: 116, Name: 'DetectMac - iPhone7', nValue: 0, sValue: ''
2017-12-11 22:54:18.173 (DetectMac) Device ID: '116'
2017-12-11 22:54:18.173 (DetectMac) Device Name: 'DetectMac - iPhone7'
2017-12-11 22:54:18.173 (DetectMac) Device nValue: 0
2017-12-11 22:54:18.173 (DetectMac) Device sValue: ''
2017-12-11 22:54:18.173 (DetectMac) Device LastLevel: 0
2017-12-11 22:54:18.173 (DetectMac) Device: 2 - ID: 117, Name: 'DetectMac - S6', nValue: 0, sValue: ''
2017-12-11 22:54:18.173 (DetectMac) Device ID: '117'
2017-12-11 22:54:18.173 (DetectMac) Device Name: 'DetectMac - S6'
2017-12-11 22:54:18.174 (DetectMac) Device nValue: 0
2017-12-11 22:54:18.174 (DetectMac) Device sValue: ''
2017-12-11 22:54:18.174 (DetectMac) Device LastLevel: 0
2017-12-11 22:54:18.224 (DetectMac) Heartbeat interval set to: 60.
2017-12-11 22:54:20.127 (DetectMac) Calling message handler 'onHeartbeat'.
2017-12-11 22:54:20.127 (DetectMac) onHeartbeat called
2017-12-11 22:54:23.528 (DetectMac) num: 1
2017-12-11 22:54:23.528 (DetectMac) Name: iPhone7
2017-12-11 22:54:23.528 (DetectMac) pos: 454
2017-12-11 22:54:23.528 (DetectMac) address: xx:xx:xx:xx:xx:xx found
2017-12-11 22:54:23.528 (DetectMac) num: 2
2017-12-11 22:54:23.528 (DetectMac) Name: S6
2017-12-11 22:54:23.528 (DetectMac) pos: -1
2017-12-11 22:54:25.532 (DetectMac) Calling message handler 'onStop'.
Rodio
Posts: 39
Joined: Thursday 16 April 2015 10:18
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Python Plugin: MAC address presence

Post by Rodio »

And turned the Wifi ON

2017-12-11 22:58:27.538 (DetectMac) onHeartbeat called
2017-12-11 22:58:30.857 (DetectMac) num: 1
2017-12-11 22:58:30.857 (DetectMac) Name: iPhone7
2017-12-11 22:58:30.858 (DetectMac) pos: 485
2017-12-11 22:58:30.858 (DetectMac) address: xx:xx:xx:xx:xx:xx found
2017-12-11 22:58:30.858 (DetectMac) num: 2
2017-12-11 22:58:30.858 (DetectMac) Name: S6
2017-12-11 22:58:30.858 (DetectMac) pos: 423
2017-12-11 22:58:30.858 (DetectMac) address: xx:xx:xx:xx:xx:xx found
2017-12-11 22:58:30.858 (DetectMac - S6) Updating device from 0:'' to have values 1:'On'.
2017-12-11 22:58:30.874 (DetectMac) Update 1:'On' (DetectMac - S6)
Xorfor

Re: Python Plugin: MAC address presence

Post by Xorfor »

Thnx for the test!!! I will put this on the todo list, to remove white spaces.
Rodio
Posts: 39
Joined: Thursday 16 April 2015 10:18
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Python Plugin: MAC address presence

Post by Rodio »

Top

Cheers
R
refthoom
Posts: 2
Joined: Sunday 24 September 2017 9:33
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Python Plugin: MAC address presence

Post by refthoom »

Hi Xorfor, do you think it's possible to have a version without arp-scan? That would make it usable in more situations.

I'm no programmer of any sorts but I tried this in a shell:

Code: Select all

#flush cache to get rid of old stuff
sudo ip -s -s neigh flush all
#look for the mac on the network
arp -a | grep -v incomplete | grep -i "xx:xx:xx:xx:xx:xx" > phone
This gives me a file named 'phone' with mac and IP address on one line. So no need for arp-scan :)
Is this something you could use in your script as an alternative?
Bolten88
Posts: 6
Joined: Tuesday 16 June 2020 5:14
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Python Plugin: MAC address presence

Post by Bolten88 »

Is this plugin still working?
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests