Hi everyone
This is my first post in this community after having taken advantage of many inspirations and solutions in here. Thank you everyone for taking the effort posting and helping others. Here’s my humble attempt to give something back.
I’ve been using the hping3/ARP based iPhone detection method dated back to 2016 for years now with more or less success. My iPhone’s kept showing offline randomly when their WIFI shut off and with the new iOS 15.5, they started to do that even more.
So, I decided to attempt a completely new approach based on my DHCP server’s DHCP logs - because the iPhone does seem to connect to the DHCP server with a DHCPrequest every 5 minutes or so (despite shutting off parts of its WIFI stack apparently i.e. not responding to PING requests). This method basically works in the following way:
- DHCP server script parses its logs and checks whether there was a DHCPrequest/acknowledgement to the respective iPhone recently enough (1500 secs/25mins seems enough to work)
- DHCP server script sends a POST command to the OpenHAB3 server switching the iPhone item ON/OFF (if the item doesn’t receive an ON/OFF signal 6mins or more, it switches itself to OFF)
This approach takes 3 simple elements:
Element 1: DHCP server script on the DHCP server (mine is running linux)
#!/bin/bash
#Enter your ip of your device here
DEVICES="8f:43:19:63:84:e2 3a:5f:dh:1e:63:25"
STATUS="OFF"
for i in `echo $DEVICES`; do
#Enter your ip of your device here
SCNDLASTLOGLINE=`grep $i /var/log/messages | grep DHCPACK | tail -n2 | head -n1`
LASTLOGLINE=`grep $i /var/log/messages | grep DHCPACK | tail -n1`
SCNDLASTTIMESTAMP=$(echo -n $SCNDLASTLOGLINE | awk '{print $1" "$2" "$3}')
SCNDLASTTIMESTAMPUNIX=$(date -d "${SCNDLASTTIMESTAMP}" +"%s")
LASTTIMESTAMP=$(echo -n $LASTLOGLINE | awk '{print $1" "$2" "$3}')
LASTTIMESTAMPUNIX=$(date -d "${LASTTIMESTAMP}" +"%s")
NOW=`date +%s`
if (( NOW - LASTTIMESTAMPUNIX <= 1500 )); then
# echo $i " seems home"
STATUS="ON"
else
# echo $i " longer offline than 1500secs / 25mins"
STATUS="OFF"
fi
echo `date` "(" $NOW ") Status of" $i "is" $STATUS >> /var/log/iphone_status
if [[ "$i" == "8f:43:19:63:84:e2" ]] && [ "$STATUS" == "ON" ] ; then
curl -X POST --header "Content-Type: text/plain" --header "Accept: application/json" -d $STATUS "http://192.168.0.111:8080/rest/items/Present_iPhone1"
elif [[ "$i" == "3a:5f:dh:1e:63:25" ]] && [ "$STATUS" == "ON" ] ; then
curl -X POST --header "Content-Type: text/plain" --header "Accept: application/json" -d $STATUS "http://192.168.0.111:8080/rest/items/Present_iPhone2"
fi
<<'###BLOCK-COMMENT'
echo "2ndlast log entry: " $SCNDLASTLOGLINE
echo "Last log entry: "$LASTLOGLINE
echo "2ndlast log timestamp: " $SCNDLASTTIMESTAMP
echo "2ndlast log timestamp (UNIX): " $SCNDLASTTIMESTAMPUNIX
echo "Last log entry timestamp: " $LASTTIMESTAMP
echo "Last log entry timestamp (UNIX): " $LASTTIMESTAMPUNIX
echo "Now is: " $NOW
###BLOCK-COMMENT
done
Element 2: Cronjob on the DHCP server running the above script
# Openhab3 iphone detection cronjob
*/5 * * * * /somewhereuseful/iphonedhcppresence.sh >/dev/null 2>&1
Element 3: OpenHAB items for each iPhone used (I have included 2 phones in my example)
Switch Present_iPhone1 "iPhone One" <girl_2> (gPhones, gPresent) { expire="6m,command=OFF" }
Switch Present_iPhone2 "iPhone Two" <boy_2> (gPhones, gPresent) { expire="6m,command=OFF" }
Element 4: Set the DHCP lease time on the DHCP server
Set the DHCP lease timeouts to default = 30mins / max = 45mins so that the iphone needs to get an address within the time interval defined above (longer intervals will result in offline gaps, 30mins DHCP timeout with 25min script timeout and 6min OpenHAB item timeout seem to work fine)
Let me know what you think
Regards
Al