Kuna Motion Detection with DD-WRT

Inspired by presence detection using DD-WRT, I’ve developed a DD-WRT script to monitor traffic on my router for upload data from my Kuna Security Lamp. Once the script detects upload traffic (ie, the Kuna detected motion and is recording video to the cloud) from a particular MAC address, it triggers a virtual motion sensor. In my setup, I have the Kuna light set to always on and then I have a GE Link bulb which I can control through Smartthings. I can now trigger the on/off/brightness based on Kuna motion detection. I imagine this script could also be extended to other uses - perhaps have the lights dim in my living room when it detects high download bandwidth from my Roku (watching Netflix).

The prerequisites and setup are identical to the presence detection - you’ll need:

  • Router running DD-WRT (I also had to install curl)
  • Virtual motion sensor (@Mike_Maxwell’s uDTH is perfect for this)
  • Endpoint URL for the virtual motion sensor (I used this example using ThingLayer)

Finally, you’ll need to go to the DD-WRT administration page and setup up the following two scripts:

Startup Script:

#! /bin/sh
sleep 10 # wait for router to fully boot
while [ ! -e "/tmp/custom.sh" ]; do
   sleep 1 # wait till /tmp/custom.sh gets generated
done
/tmp/custom.sh &

Custom Script:

#!/bin/sh
WATCHDOG_SLEEP_SEC=2

### SETTINGS ###
kuna_last_state="OFF"
thresh="90" # Threshold - number of packets per 2 seconds
kunaMAC="00:00:00:00:00:00"
kunaoffcount=0
kunaON="https://graph.api.smartthings.com/..."
kunaOFF="https://graph.api.smartthings.com/..."

### SETUP IPTABLES ###
# Taken from https://code.google.com/archive/p/wrtbwmon
# Create the RRDIPT CHAIN (it doesn't matter if it already exists).
iptables -N RRDIPT 2> /dev/null

# Add the RRDIPT CHAIN to the FORWARD chain (if non existing).
iptables -L FORWARD --line-numbers -n | grep "RRDIPT" | grep "1" > /dev/null
if [ $? -ne 0 ]; then
 iptables -L FORWARD -n | grep "RRDIPT" > /dev/null
 if [ $? -eq 0 ]; then
    echo "DEBUG : iptables chain misplaced, recreating it..."
    iptables -D FORWARD -j RRDIPT
 fi
 iptables -I FORWARD -j RRDIPT
fi

# For each host in the ARP table
LAN_IFACE=$(nvram get lan_ifname)
grep ${LAN_IFACE} /proc/net/arp | while read IP TYPE FLAGS MAC MASK IFACE
do
   # Add iptable rules (if non existing).
 iptables -nL RRDIPT | grep "${IP} " > /dev/null
 if [ $? -ne 0 ]; then
    iptables -I RRDIPT -d ${IP} -j RETURN
    iptables -I RRDIPT -s ${IP} -j RETURN
 fi
done

### MONITOR TRAFFIC ###
kunaIP=`grep -v "0x0" /proc/net/arp | grep "$kunaMAC" | cut -c0-17 | tr -d " "`
while sleep $WATCHDOG_SLEEP_SEC
do
   # Get packets sent since last loop; in sed command: 2d for upload, 1d for download
   kunaPKTS=`iptables -L RRDIPT -vnxZ | grep "$kunaIP" | cut -c1-8 | sed -e 2d | tr -d " "`
   if [[ "$kunaPKTS" -gt "$thresh" ]] ; then
        kuna_new_state="ON"
   else
        kuna_new_state="OFF"
   fi

   if [ "$kuna_new_state" == "$kuna_last_state" ]; then
        sleep 0
   else
        if [ "$kuna_new_state" == "ON" ]; then
                kuna_last_state="$kuna_new_state"
                curl "${kunaON}" -k
                kunaoffcount=0
        else
                kunaoffcount=$(( $kunaoffcount + 1 ))
        fi
   fi

   # Wait for 5 consecutive off counts
   if [ $kunaoffcount -lt 5 ]; then
        sleep 0
   else
        kuna_last_state="$kuna_new_state"
        curl "${kunaOFF}" -k
        kunaoffcount=0
   fi
done

Reboot your router and you should begin to see motion detected on your virtual sensor as the Kuna detects motion.

6 Likes