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
2 Likes

@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.

Hi @HeadScratcher, thanks for sharing that script with the community, that’s exactly what I was looking for. Could you help me out a bit?

First: do I need Merlin’s firmware to get this script running? I managed to send it to my router with stock firmware through Telnet just fine and set it to run on reboots but I am not sure if it is running or not. That’s why I wanted to know if Merlin’s firmware is mandatory.

Second: this command “ mosquitto_sub -h localhost -d -v -t /Presence/#” doesn’t seem to be recognized on my raspberry. I tried replacing localhost with the broker IP address also.

I should mention that my mosquitto broker is working just fine with other stuff so I believe it is not the problem.

Thanks!

Hi Eduardo, thank you for taking the time to check out my little script. Like anything you invest a lot of time into, you hope that one day someone might find it useful.

Unfortunately I have had a sleep since I wrote this… and remember very little about what I was thinking at the time. Hopefully some of it comes back to me.

First thing I notice you are doing wrong is this… ```
mosquitto_pub -h $ipaddr -t /Presence -m “He Is Home”
You are subing in your query, this is pubing from the router to the MQTT.

With regards to does it need AsusWRT-Merlin firmware or not? Good question, I would like to put the original firmware back on the router as I have problems with my VPN and want to know if it is the firmware that is causing the issues or not.

So I have thought about trying to get it to work with the original Asus firmware. Although why would I have changed it if it would work with the original software? Sorry I can’t answer that one at this time, as it has been so long since I wrote that, but I would be keen to know if you manage to get it working with the Asus firmware.

If you have no success in getting it to work let me know and I can try play with it on my router.

Just reading through my own code notes at the top… mosquitto_sub -h localhost -d -v -t /Presence/# was my own suggestion for the MQTT machine.

Tied up at the moment writing some other code, but I would like to get back to this to understood what I did.