Mosquitto MQTT presence notifications from an Asus RT-AC68U Router / AsusWRT firmware

Searched high and low for something that would publish presence notification from my particular router to the MQTT broker on a RPi. Found several scripts, but none of them really seemed to work as intended, or was just too far off what I was trying to do, it just made sense to write my own.

AsusWRT firmware is not a particularly user friendly platform to write for, and the only other real contender OpenWRT has wifi issues on 2.4GHz. So I spent an obscene amount of time trying to get meaningful code to work.

I also didn’t want code that was going to announce the presence state to the MQTT broker every 5 seconds, so I wrote the code to do cycle checks every 15 seconds (changeable to whatever you want) but only publish a notification if there was a change of state i.e. connect or disconnect.

I hope you find what I have written useful. So without further ado… the code:

#!/bin/sh

 # Asus2MQTT V1.0 - coded by Paul Murray - 2019. Credit to the numerous Linux websites I looked at to get code examples (unfortunately very few examples actually worked as expected on AsusWRT)
 # Props also go out to ColinTaylor on the SNB Forums, for code snippets, and steering me back onto the right path several times along the journey of writing this code
 # This code is the result of well over 100 hours of trial and error. This piddly bit of code could have been done in a fraction of the time had it been written in something else
 # I would have really liked to have put the discovered MAC addresses into some kind of an array, instead of outputting them to a file, but getting this basic code to work was hard enough
 # I hope you find this software beneficial. The software is provided without warranty and should be used at your own discretion, I take no responsibility for it working
 # [SCOPE OF SOFTWARE] This code is designed to be run on an Asus RT-AC68U router (may work on similar Asus routers) running AsusWRT firmware (384.10 has nano support)
 # To receive messages at the other end you will need something like a Raspberry Pi running Mosquitto MQTT broker with the following command: mosquitto_sub -h localhost -d -v -t /Presence/#
 # All going well you should only see notifications at the Mosquitto MQTT broker when you join or leave the wifi network (no constant are we there yet?, are we there yet?, are we there yet?)

 hisprevstate=0
 herprevstate=0
 ipaddr=xxx.xxx.xxx.xxx               # Put your MQTT Mosquitto broker IP address here
 newlist=/jffs/scripts/maclist.new
 oldlist=/jffs/scripts/maclist.old
 touch $oldlist

while sleep 15; do                      # Set the cycle time to whatever floats your boat

 for iface in $(nvram get wl_ifnames); do

        wl -i $iface assoclist >> $newlist
        sed -i "s/assoclist//g" $newlist
 done
        mv $newlist $oldlist

 hisphone(){

        if [ $hiscurstate = 1 ]; then
                mosquitto_pub -h $ipaddr -t /Presence -m "He Is Home"
        else
                mosquitto_pub -h $ipaddr -t /Presence -m "He Is Not Home"
        fi
        hisprevstate=`expr $hiscurstate`
 }

 herphone(){

        if [ $hercurstate = 1 ]; then
                mosquitto_pub -h $ipaddr -t /Presence -m "She Is Home"
        else
                mosquitto_pub -h $ipaddr -t /Presence -m "She Is Not Home"
        fi
        herprevstate=`expr $hercurstate`
 }

 hismac=0
 hermac=0

 while read line; do

        case $line in
        AA:BB:CC:DD:EE:FF)  # Put the mac address of the device you want to monitor here
        hismac=1
        ;;
        BB:CC:DD:EE:FF:AA)  # Put the mac address of the device you want to monitor here
        hermac=1
        ;;
        esac
 done < $oldlist

 if [ $hismac = 1 ]; then
        hiscurstate=1
 else
        hiscurstate=0
 fi

 if [[ $hiscurstate != $hisprevstate ]]; then hisphone
 fi

 if [ $hermac = 1 ]; then
        hercurstate=1
 else
        hercurstate=0
 fi

 if [[ $hercurstate != $herprevstate ]]; then herphone
 fi

done
1 Like

@HeadScratcher

Please consider to edit your posting and add code fences.

Tried several times because it didn’t post as I copied it. Is there some kind of code option I couldn’t find?

Well, it’s a bit hidden (there are invisible icons in the editor).

Just type three backticks before and after the code
```

your code goes here

```

Thanks that’s a ton better :smiley:

:+1:

Probably should link to where this all began… It was quite a struggle to get it to work, so if anyone wants to try it out and gets stuck, you can always follow my trail of breadcrumbs to see how I arrived at what I did.