Dear pimatics
(sorry for any typos and the fact some parts show in large fonts, no idea how to solve this)
I was looking for a reliable way to have my pimatic for check the presence of my iphone in my network. The idea behind is to use this in an alarm system.
The pimatic-ping solution does not work due to the fact the iphones are going into deep sleep after a short while and then are not reachable via standard ping.
There are other location based solutions using your icloud account or having an app push the location to your pimatic. These solutions are quite complex and to include the location in rules is much more difficult.
If you are owning a Fritzbox you may have noticed that the Fritzbox still detects the iphone as present in the network even if the iphone refuses a ping from your PC.
In this article https://forum.pimatic.org/topic/2052/fritzbox-wlan-script-someoneathome pimatic expert Swen has presented a script which connects to the Fritzbox and checks for the presence of a device. The Feedback from this script can be used to create a Device in your pimatic which shows reliably if a network-device is present in your network.
Unfortunately there was no guidance on how to implement solution this for dummies like myself so the installation for me was quite an Odyssey. Thanx to three users (Swen, Wuschel, SaxnPaule) which had a tremendous amount of patience with me I was finally able to get it running.
To allow the usage of this ingenious little tool for a broader audience I would like to explain which steps I took:
This was tested with Fritzbox 7490 wirh Fritz OS Version 06.93 and an iphone 6 with ios 11.2.5
Let’s start:
Step 1: Install PHP7.0 which is needed for this script
sudo apt-get install php7.0
Remark: Unfortunately this step also installs Apache2 automatically which is not needed and which makes your pimatic gui inaccessible. There is probably a better way to install PHP only but as mentioned I am a Dummy and not at all familar with Linux however uninstalling Apache2 afterwards has solved this issue for me
Step 2: Uninstall Apache2
sudo apt remove apache2
Step 3: Install PHP Soapclient, not sure what it is but it is needed and not installed automatically alongside PHP7.0
sudo apt install PHP7.0-soap
Step 4: restart your raspberry
sudo reboot now
(check if your pimatic gui is still accessible after restart)
Step 5: Create the script
sudo nano
copy and paste the following into the editor:
---------------------------------------Copy start--------------------------------------
#!/usr/bin/php7.0
<?php
if(empty($argv[1])){
die("MAC address missing");
}else{
$mac = $argv[1];
}
if(empty($argv[2])){
die("BOX IP or IPs missing");
}else{
$ipList = explode(":",$argv[2]);
}
function checkDevice($ip,$mac){
$result = "";
$uri = "urn:dslforum-org:service:Hosts:1";
$location = "http://".$ip.":49000/upnp/control/hosts";
$client = new SoapClient(
null,
array(
'location' => $location,
'uri' => $uri,
'noroot' => True,
'login' => "",
'password' => "",
'connection_timeout' => 5
)
);
try{
$query = $client->GetSpecificHostEntry(new SoapParam($mac,'NewMACAddress'));
$result = $query['NewActive'];
}catch(SoapFault $fault){
$result = 0;
}
return $result;
}
function checkAllDevices($ipList,$mac){
$result = 0;
foreach($ipList as $ip){
$resultCheck = checkDevice($ip,$mac);
if($resultCheck == 1){
$result = 1;
}
}
return $result;
}
echo checkAllDevices($ipList,$mac);
?>
-----------------------------------------Copy end----------------------------------------
Hit Shift & X to exit the Editor, it will ask you if you want to save the changes, confirm with Yes (Hit Enter)
Enter a name for the script i.e. yourfile.php (I will use this name in the subsequent description, please change if you use a different name)
Step 6: Make the script executable
sudo chmod +x yourfile.php
Step 7: test the script
The script needs two so called arguments which are the Mac Adress of the device you would like to check plus the IP Adress of the Fritzbox.
7.1. Default IP Adress of the Fritzbox is 192.168.178.1 please check in your Fritzbox under “Heimnetz” (~Home Network) / “Netzwerk” (~Network) the top entry in the list
7.2 The Mac Adress is a unique identifier that each network chip has. You can find this information in your Fritzbox.
Goto “Heimnetz” (~Home Network) / “Netzwerk” (~Network) find your device and click the change button on the right side which will bring you to the details.
Make sure that the box for “always assign same IPv4 Adress to this device” is checked.
The Mac Adress can be found next to “Geräteinformation” (~Device Information) it will look something like this: DG:40:6R:Z0:DT:98 (just an example please replace with your info)
Now you have all necessary information to test the script
sudo ./yourfile.php DG:40:6R:Z0:DT:98 192.168.178.1
The result should bring a 1 if the device is present and a 0 if not. The information is shown in the next line as the first digit. Set your iphone to flight mode and check if both works properly.
The script allows the check for devices on several Fritzboxes, probably in case you have a large area to cover and you use several boxes as repeaters. You can enter mutliple IP Adresses as argument 2 like:
./yourfile.php MACSearch IPBOX1:IPBOX2:IPBOX3
Step 8: Implementation into pimatic
Create a new device of type ShellPresenceSensor
Give it a name to your liking
check the box "command"
and enter the following:
php /home/pi/yourfile.php DG:40:6R:Z0:DT:98 192.168.178.1
Leave everything else unchanged and save the device.
If all has worked then there should not be any errors during creation and you should now have a new device which shows the presence of your iphone in your network.
The status is updated roughly every 1 minute (see value in miliseconds under “interval”)
You can now use this to setup rules like put your system into alarm mode or start / stop sonos
Greetings from Cologne
Christian
P.S.: I will not be able to provide any support if you have problems but I am sure one of the experts will be able to help.