For those of us letting our DD-WRT router handle presence detection we are likely aware of the following website:
http://tinsley.io/2015/03/openhab-presence-detection-with-dd-wrt/
This script modification/adaptation from ISY to openHAB was done by Aaron Tinsley (Thanks Aaron!).
Aaron’s script extracts MAC addresses from the ASSOCLIST at the AP level. While it is a good/working script. It has limited usefulness as it reviews connect/disconnect MAC details only for a specified Wi-Fi Radio. Also, the script does nothing to monitor hard wired LAN devices.
Given most current router hardware has 2 if not 3 radios these days, and a desire to monitor hard wired clients led me to the “arp” command. This command will monitor all of br0 MAC addresses i.e. dual band radios (both 2.4 GHz and 5GHz) as well as hard wired network clients.
Credit for the arp idea goes to user “scastano” on the universal devices forum (linked from Aaron’s website).
openHAB Items:
//DD-WRT Presence
String DDWRTDeviceCount “DD-WRT Devices Connected [%s]” //Total Number of Monitored Devices Connected
Switch JoePhone
Switch WifePhone
Switch SamsungTV
Switch KidIPAD
openHAB Sitemap Example:
Frame label=“DD-WRT Presence” {
//DD-WRT Switches
Text item=DDWRTDeviceCount {
Switch item=JoePhone
Switch item=WifePhone
Switch item=SamsungTV
Switch item=KidIPAD
}
}
*I would have used a number datatype in openHAB for the DDWRT Device Count, but curl wants to pass the MIME type “text/plain”, so using a string was my shortcut to get it working.
…Example below, working on my Netgear R6300v2 (same chipset as R7000) with DD-WRT 3.0 “snapshot” build 29300…
DD-WRT Startup Script:
To install… just navigate to “Administration > Commands” cut/paste your modified version of the script into the text box, then click the “save startup” button. Then reboot the router.
#!/bin/sh ############################################################### # Proximity detection # # A script designed to run on a router running DD-WRT to detect certain devices connected to the router. # It runs at startup and runs continually, checking for a specific list of devices (phones/laptop, etc) # that connect wirelessly to the router. Once a device is connected, the OpenHAB status will # be updated with either an ON or OFF. Make sure you set up a switch item in OpenHAB for each device # you want to track. # # The searching frequency can be adjusted to be slower/faster depending on your requirements. Searching too fast # could burden your router. Too slow might not update the status as necessary for the application. # # Make changes below # MAC address of each device to watch. Don't leave blank. # For security purposes, if your router requires a password, even if someone could clone the MAC of your # phone, they would still require the password of your network to link to your router. # arp command is case sensitive! letters need to be lower case # run "arp -i br0" from CLI to check MAC Addresses macdevice1="ec:00:00:00:f0:09" #Joe Nexus 6 macdevice2="00:00:00:47:f8:0a" #Wife Nexus 5 macdevice3="00:00:00:00:c0:0b" #Samsung TV Hard Wired macdevice4="f0:00:47:00:00:0c" #Kid iPad Wi-Fi Radio #OpenHAB username, password, and IP Address #username="OPENHAB_USERNAME" #password="OPENHAB_PASSWORD" #IPAddr="OPENHAB_IP_ADDRESS" #port="OPENHAB_PORT" # OpenHAB switch items to be updated for each tracked MAC item1="JoePhone" item2="WifePhone" item3="SamsungTV" item4="KidIPAD" #String Item in openHAB to display total devices connected item99="DDWRTDeviceCount" # Occupied and unoccupied delay in seconds to check status # Adjust for shorter/longer wait times. For instance, when one device is already # connected, you might want to check less frequently. This could also delay the # notification of a disconnect. delay_occupied=4 delay_unoccupied=2 # initial testing loop count - uncomment the counter near the bottom of the script for testing only. limit=120 ############################################### # do not change below here ############################################### sleep #initialize internal variables # status of each MAC. 0=disconnected. 1=connected. -1 initially forces openHAB update first loop macconnected1=-1 macconnected2=-1 macconnected3=-1 macconnected4=-1 connected=-1 # total number of currently conencted devices. currentconnected=0 counter=1 # Initial testing loop. Will run continually after testing is complete while [ $counter -lt $limit ]; do #reset current status. Two variables are used for each device. The past known status and the current # status. Only a change is reported to openHAB. Otherwise, it would constantly be updating openHAB with # the current status creating unnecessary traffic for both the router and openHAB maccurrent1=0; maccurrent2=0; maccurrent3=0; maccurrent4=0; ## Old Case Section Replaced # compare each device that is currently connected to the MAC devices we want to watch. # changed the following to chek for each MAC arpout=$(arp -i br0) maccurrent1=$(echo $arpout | grep -c $macdevice1) if [ $maccurrent1 -gt 0 ]; then maccurrent1=1 fi maccurrent2=$(echo $arpout | grep -c $macdevice2) if [ $maccurrent2 -gt 0 ]; then maccurrent2=1 fi maccurrent3=$(echo $arpout | grep -c $macdevice3) if [ $maccurrent3 -gt 0 ]; then maccurrent3=1 fi maccurrent4=$(echo $arpout | grep -c $macdevice4) if [ $maccurrent4 -gt 0 ]; then maccurrent4=1 fi # Look for a change in status from the old known to the current status. # If it changed, update openHAB. Otherwise it leaves it as is. if [ $macconnected1 -ne $maccurrent1 ]; then if [ $maccurrent1 -eq 1 ]; then macstatus1="ON"; else macstatus1="OFF"; fi curl -X POST -d $macstatus1 -H "Content-Type: text/plain" -i http://$username:$password@$IPAddr:$port/rest/items/$item1 fi if [ $macconnected2 -ne $maccurrent2 ]; then if [ $maccurrent2 -eq 1 ]; then macstatus2="ON"; else macstatus2="OFF"; fi curl -X POST -d $macstatus2 -H "Content-Type: text/plain" -i http://$username:$password@$IPAddr:$port/rest/items/$item2 fi if [ $macconnected3 -ne $maccurrent3 ]; then if [ $maccurrent3 -eq 1 ]; then macstatus3="ON"; else macstatus3="OFF"; fi curl -X POST -d $macstatus3 -H "Content-Type: text/plain" -i http://$username:$password@$IPAddr:$port/rest/items/$item3 fi if [ $macconnected4 -ne $maccurrent4 ]; then if [ $maccurrent4 -eq 1 ]; then macstatus4="ON"; else macstatus4="OFF"; fi curl -X POST -d $macstatus4 -H "Content-Type: text/plain" -i http://$username:$password@$IPAddr:$port/rest/items/$item4 fi # Update the known status from the current. Ready for the next loop. macconnected1=$maccurrent1; macconnected2=$maccurrent2; macconnected3=$maccurrent3; macconnected4=$maccurrent4; # Total up the number of devices connected. let currentconnected=$macconnected1+$macconnected2+$macconnected3+$macconnected4 # Look for a change, and update openHAB. if [ $connected -ne $currentconnected ]; then curl -X POST -d $currentconnected -H "Content-Type: text/plain" -i http://$username:$password@$IPAddr:$port/rest/items/$item99 fi connected=$currentconnected # Delay (sleep) depending on the connection status. # No devices connected could delay less. Once a device is connected, it could delay longer. if [ $connected -gt 0 ]; then sleep $delay_occupied else sleep $delay_occupied fi #for testing only - uncomment to have the looping stop at X loops defined in variable: limit. #let counter=$counter+1 done
Note: I’ve also been playing around with the concept of running the mac addresses and openHAB items into arrays. then looping through the tests/commands through the array, but not everyone has Bash/BusyBox on their DD-WRT instance. So this example is not coded as “concisely” as possible. That said, it should be “#!/bin/sh” on DD-WRT friendly (assuming your device can run arp & curl). As such, it will likely work with older router hardware/firmware builds.
Note2: assuming you have persistence sorted out… Leveraging the example above you could easily modifying the periodic presence detection rules based on a ‘gMobiles’ group as illustrated on the openHAB wiki “Samples-Tricks” page. Link here: