Controlling occupancy from your router (Open firmware: Tomato/DD-WRT/OpenWRT)

[quote=“silencery, post:20, topic:180260”]Yup, absolutely. If you turn on SSH you can use a program like putty (if you’re on windows) and execute

wget -qs “http://192.168.1.210:3480/data_request?id=lu_action&output_format=xml&DeviceNum=76&serviceId=urn:upnp-org:serviceId:VSwitch1&action=SetTarget&newTargetValue=1

directly from the command line in your access point. That would be a good place to start to check if the issue is on the vera side or your access point.[/quote]

Yea I think this is getting in over my head. I turned on SSH in the router, but I was still un able to get putty to SSH in. Probley a setting, but I don’t really understand it much of it so…

I’ve updated my script above to also ping some hosts that are wired to the LAN.

Any other settings I need to change, or any ideas on my error? Maybe some instructions?

Any other settings I need to change, or any ideas on my error? Maybe some instructions?[/quote]

[quote=“integlikewhoa, post:19, topic:180260”]Nothing happens, Is there any other settings in the router needed? Should I be able to run just a simple command from inside the router to see if its even getting out? Such as this?

wget -qs “http://192.168.1.210:3480/data_request?id=lu_action&output_format=xml&DeviceNum=76&serviceId=urn:upnp-org:serviceId:VSwitch1&action=SetTarget&newTargetValue=1

I see my phone and MAC on the active list. I ahve disabled my other AP to ensure I’m connected to the main. Iv disconnected and reconnected many times.[/quote]

Maybe You’ve accidentally removed a “fi” in the code…

Try go over the code again. Maybe start from the beginning. You could also try to post your modified script here …

Anyway as silencery suggests you can benefit by using Putty to connect to your router (with telnet or SSH, whatever your router is configured to accept) and test the commands at the command line.

You can also create a file (/tmp/test), edit it and paste the entire code into it, chmod the file to 775 and run it from the command line. It will be easier that way to find out what’s wrong with the code.

It sounds like you were already able to successfully get into the router? In case you’re having trouble, here are the basic instructions to log into your router and test the commands.

[ul][li]Enable SSH access in your access point/router by logging in to it and doing the following (from the dd-wrt wiki. Again, I don’t have dd-wrt so i can’t verify these instructions):[/li]
[list]
[li]Using the Web Interface, go to the Administration tab. (in v24 use Services tab)[/li]
[li]Under the Services sub-tab, Enable SSHd in the Secure Shell section. If new options don’t appear, Save Settings[/li]
[li]Enable Password Login[/li]
[li]Save and Apply Settings[/li]
[li]After this you may login as user “root” with the password you set for the webinterface[/li]
[/list]
[li]Download putty.exe[/li]
[li]From your windows computer, run putty.exe
[list]
[li]In the putty window, enter your router’s IP address, make sure port 22 is selected (and matches the settings in your router web administration pages) and select the ‘SSH’ option. [/li]
[li]Hit enter and if successful, you’ll be prompted for a username (usually root for dd-wrt). [/li]
[li]Enter the login info from the first step above.[/li]
[/list]
[/li]
[li]If you’ve successfully logged into your router through SSH, you’ll see a command prompt that looks somewhat similar to this image.[/li][/ul]

Once you’re in, you can start by running a wget command in putty to ensure your router is communicating properly with your vera switches such as

wget -qs "http://192.168.1.210:3480/data_request?id=lu_action&output_format=xml&DeviceNum=76&serviceId=urn:upnp-org:serviceId:VSwitch1&action=SetTarget&newTargetValue=1"

If it’s working, this command will change virtual switch to “on” assuming you have the plugin installed, the device ID is 76, and it’s currently off.

After verifying that works, the next step would be to triple check the script is setup properly. As farang has mentioned, I would bet it’s just a small syntax issue such as a missing line or a space in the wrong place. Shell scripts are fairly unforgiving with typos.

Wow, thanks for the help. I was able to telnet in but SHH I was getting a “Server unexpectedly closed network connection” and couldnt get in with putty. But Reading what you posted I see a few things I need to try.

I see it says login in as “root” before you download putty? My login is not root to get into the web UI on the router is that ok?
Also in Putty I never found a place to enter user or password. Is that because I didn’t get that far or is it done before the connection?

I did have SSHd enabled and password login enabled (did try disabled to tho) and ports in router and putty set to 22. When I hit enter tho I got a connection error rather then a login screen.

I’ll try again when I get home.

[quote=“integlikewhoa, post:26, topic:180260”]Wow, thanks for the help. I was able to telnet in but SHH I was getting a “Server unexpectedly closed network connection” and couldnt get in with putty. But Reading what you posted I see a few things I need to try.

I see it says login in as “root” before you download putty? My login is not root to get into the web UI on the router is that ok?
Also in Putty I never found a place to enter user or password. Is that because I didn’t get that far or is it done before the connection?

I did have SSHd enabled and password login enabled (did try disabled to tho) and ports in router and putty set to 22. When I hit enter tho I got a connection error rather then a login screen.

I’ll try again when I get home.[/quote]

Sorry, i haven’t used dd-wrt in a while so i’m not sure if it still grants SSH access under the root username if you change the web login. My guess (and hope) is yes?

Yeah, if you were never prompted for a login, that means you didn’t make it that far. Login prompt for SSH only happens after the connection is made. You’ll see a black screen asking you for the username (login as:).

If you’re certain everything on the web interface is configured properly, the SSH service on your particular dd-wrt install might be having problems and isn’t running or is actively being blocked elsewhere in your router settings. I’ve run into similar problems before on really old builds of dd-wrt.

You can test if SSH is accessible from a windows machine by running:

Start → CMD → telnet 192.168.1.1 22

Where 192.168.1.1 is your router’s address and 22 is the SSH port you’re trying to use. If your router’s SSH is working properly, you’ll see a black window that says “SSH blah blah blah.” Otherwise, you’ll see a message saying connect failed.

The telnet client is available by default for windows machines, but for anything newer than Vista, you may need to turn it on by going to Control Panel → Programs and Features → Turn Windows features on or off

I can telnet in on port 23 but not on 22

I take that back. I just tried it from work using my home IP address and I got

SSH-2.0-dropbear_2013.56

Should I also be able to putty remotely? I’m still getting the same error from here. Maybe a putty setting I’m not doing? I’m only putting in IP making sure it port 22, and SSH (which is default) and clicking connect.

Yup, that’s all you gotta do. You got the right response (SSH-2.0-dropbear_2013.56). Crazy. I don’t know why why you wouldn’t be able to putty into the router unless dd-wrt was doing something strange with the authentication. I’ll have to think about it…

BTW, it’s a good idea to shut down remote SSH access after you’re done testing everything (access from LAN is ok) for better security.

Well I’m home and been playing with it no luck. I and Telnet in no problem, but not SSH. Tried changing ports and read up on it. Looks like a DDWRT problem. Somewent to tomato and said no problem. I tried one of my other AP with ddwrt and same.

you don’t really have to run SSH, just telnet and run the command you wanted to run…

I got it work sort of. I gave up on SSH on the main router, I got SSH to work on the atheros Ap router I have. I ended up Getting his new code to work in the Main router once. I don’t get an error anymore. But most of the time it just don’t work.

When it first started to work my phone was working ok, longer delay when leaving then I’d like but worked. Connecting seems fairly fast. But my wifes phone wasn’t showing. So to firgure out if it was the mac or the script I decided to switch hers with mine (first mac and second) But then neither worked and I only changed MAC. Then I swapped it back and neither has worked since. I get the internet button to work. It comes on after some time if I turn it off. Which the ping delay so I see why. But I tried to input my desktop wired computer IP in one of the other spots and didn’t get a response. I’m sure it’s just my lack of knowledge but copy and paste and change MAC, vera device#(multiswitch) and vera local IP shouldn’t be hard to get the code in right.

I sure wish someone could help me get this going. Only thing that seems to work is the internet button #8.

It’s always on and if I turn it off 10 min or so later I check its back on.

Anyone know if this will work with ASUS routers?

It’s really not the brand of router you have. Its the chip that certain model has and the software you have installed on it. Theoretically most if not all routers that can run DD-wrt or similar aftermarket software should be able to work. The code linked on here was specific to one that runs a Broadcom chip in it, which many different brands and alot of routers use. But don’t ask me to many questions because I still haven’t been able to get it working right even with the same exact model of router that the OP had and copy and pasting the code.

Thank you for this! I spent an entire evening flashing my router, putting all the DHCP reservations/port forwards back, adding devices back to the network… 100% worth it! Finally getting home occupancy status that is fast, low (no) battery, and reliable. Thank you!

I did make a very slight change to the script. It trades some speed on leaving the network for a little more accuracy. I added a counter for when a phone is no longer found. I have it set to count 6 misses before it finally sets the person as “away”. If it detects the phone at home, it still updates that as soon as the loop executes. You can probably add something similar for the ping examples, if you wanted to.

I’m not much of a coder, so if anyone spots anything wrong, please let me know. That said, it has been working well for a full day now.

The startup script is the same:

while [ ! -e "/tmp/custom.sh" ]; do sleep 1 # wait till /tmp/custom.sh gets generated done /tmp/custom.sh &

Here’s my modifications to the custom script:

[code]#!/bin/sh
WATCHDOG_SLEEP_SEC=10
MAC_ADDRESS_1=“00:XX:XX:XX:XX:XX” # Phone 1 MAC
c1_last_state=“0”
MAC_ADDRESS_2=“00:XX:XX:XX:XX:XX” # Phone 2 MAC
c2_last_state=“0”
HOST_1=“htpc-lan.XXXXX.local”
h1_last_state=“x”
HOST_2=“htpc-wlan.XXXXX.local”
h2_last_state=“x”
HOST_3=“alfa-usb-wlan.XXXXX.local”
h3_last_state=“x”
HOST_4=“alfa-spoofed.XXXXX.local”
h4_last_state=“x”
HOST_5=“mackentorsken.XXXXX.local”
h5_last_state=“x”
HOST_10=“www.sunet.se”
h10_last_state=“x”
VERAURL=“http://xxx.xxx.xxx.xxx:3480/data_request?id=variableset&DeviceNum=xxx&serviceId=urn:dcineco-com:serviceId:MSwitch1&Variable=
x=0
y=0
offcount1=0
offcount2=0

while sleep $WATCHDOG_SLEEP_SEC
do
if [ “$x” == 180 ]; then
# Every 30 minutes or so we do them all again, just in case Vera missed something
x=0
c1_last_state=“0”
c2_last_state=“0”
h1_last_state=“x”
h2_last_state=“x”
h3_last_state=“x”
h4_last_state=“x”
h10_last_state=“x”
fi
x=$(( $x + 1 ))

c1_new_state=`wl assoclist | grep $MAC_ADDRESS_1`
if [ "$c1_new_state" == "$c1_last_state" ] ; then
	sleep 0
else
	if [ "$c1_new_state" == "assoclist $MAC_ADDRESS_1" ]; then
		c1_last_state="$c1_new_state"
		wget -qs "${VERAURL}Status1&Value=1"
		offcount1=0
	else
		offcount1=$(( $offcount1 + 1 ))
	fi
fi

c2_new_state=`wl assoclist | grep $MAC_ADDRESS_2`
if [ "$c2_new_state" == "$c2_last_state" ] ; then
	sleep 0
else
	if [ "$c2_new_state" == "assoclist $MAC_ADDRESS_2" ]; then
		c2_last_state="$c2_new_state"
		wget -qs "${VERAURL}Status2&Value=1"
		offcount2=0
	else
		offcount2=$(( $offcount2 + 1 ))
	fi
fi

if [ $offcount1 -lt 6 ]; then
	# give not responding devices 1 minute to respond
	sleep 0
else
	c1_last_state="$c1_new_state"
	wget -qs "${VERAURL}Status1&Value=0"
	offcount1=0
fi

if [ $offcount2 -lt 6 ]; then
	#give not responding devices 1 minute to respond
	sleep 0
else
	c2_last_state="$c2_new_state"
	wget -qs "${VERAURL}Status2&Value=0"
	offcount2=0
fi


# Ping LAN hosts
h1_new_state=`ping -w 1 -c 1 $HOST_1 | grep 'received' | awk -F',' '{ print $2}' | awk '{ print $1}'`
if [ "$h1_new_state" == "$h1_last_state" ] ; then
	sleep 0
else
	h1_last_state="$h1_new_state"
	if [ "$h1_new_state" == 1 ]; then
		wget -qs "${VERAURL}Status4&Value=1"
	else
		wget -qs "${VERAURL}Status4&Value=0"
	fi
fi

h3_new_state=`ping -w 1 -c 1 $HOST_3 | grep 'received' | awk -F',' '{ print $2}' | awk '{ print $1}'`
if [ "$h3_new_state" == "$h3_last_state" ] ; then
	sleep 0
else
	h3_last_state="$h3_new_state"
	if [ "$h3_new_state" == "1" ]; then
		wget -qs "${VERAURL}Status6&Value=1"
	else
		wget -qs "${VERAURL}Status6&Value=0"
	fi
fi

h4_new_state=`ping -w 1 -c 1 $HOST_4 | grep 'received' | awk -F',' '{ print $2}' | awk '{ print $1}'`
if [ "$h4_new_state" == "$h4_last_state" ] ; then
	sleep 0
else
	h4_last_state="$h4_new_state"
	if [ "$h4_new_state" == "1" ]; then
		wget -qs "${VERAURL}Status5&Value=1"
	else
		wget -qs "${VERAURL}Status5&Value=0"
	fi
fi
h5_new_state=`ping -w 1 -c 1 $HOST_5 | grep 'received' | awk -F',' '{ print $2}' | awk '{ print $1}'`
if [ "$h5_new_state" == "$h5_last_state" ] ; then
	sleep 0
else
	h5_last_state="$h5_new_state"
	if [ "$h5_new_state" == "1" ]; then
		wget -qs "${VERAURL}Status3&Value=1"
	else
		wget -qs "${VERAURL}Status3&Value=0"
	fi
fi

if [ "$y" == 11 ]; then
	# We dont want to ping an external site so often, might be considered as abuse
	y=0
fi
y=$(( $y + 1 ))

if [ "$y" == 1 ] ; then
	h10_new_state=`ping -w 5 -c 1 $HOST_10 | grep 'received' | awk -F',' '{ print $2}' | awk '{ print $1}'`
	if [ "$h10_new_state" == "$h10_last_state" ] ; then
		sleep 0
	else
		h10_last_state="$h10_new_state"
		if [ "$h10_new_state" == "1" ]; then
			wget -qs "${VERAURL}Status8&Value=1"
		else
			wget -qs "${VERAURL}Status8&Value=0"
		fi
	fi
fi

done[/code]

[quote=“ibn4n, post:37, topic:180260”]I did make a very slight change to the script. It trades some speed on leaving the network for a little more accuracy. I added a counter for when a phone is no longer found. I have it set to count 6 misses before it finally sets the person as “away”. If it detects the phone at home, it still updates that as soon as the loop executes. You can probably add something similar for the ping examples, if you wanted to.

I’m not much of a coder, so if anyone spots anything wrong, please let me know. That said, it has been working well for a full day now.[/quote]

Thanks, I have been trying to get this working for weeks with the supplied code and no go. But copy and paste of yours works out of the gate.

[quote=“integlikewhoa, post:38, topic:180260”][quote=“ibn4n, post:37, topic:180260”]I did make a very slight change to the script. It trades some speed on leaving the network for a little more accuracy. I added a counter for when a phone is no longer found. I have it set to count 6 misses before it finally sets the person as “away”. If it detects the phone at home, it still updates that as soon as the loop executes. You can probably add something similar for the ping examples, if you wanted to.

I’m not much of a coder, so if anyone spots anything wrong, please let me know. That said, it has been working well for a full day now.[/quote]

Thanks, I have been trying to get this working for weeks with the supplied code and no go. But copy and paste of yours works out of the gate.[/quote]

No problem! I’m honestly excited that it was helpful!

I suspect you may have been having the same problem as I was. This wasn’t returning anything for me:

`wl -a wl0.1 assoclist | grep $MAC_ADDRESS_1`

After reading a few dd-wrt wiki pages and messing around with the command on the router, I changed that to:

`wl assoclist | grep $MAC_ADDRESS_1`

So I guess if folks like the original script (sans-counters) better, but can’t seem to get it to work, that may be a place to look.

[quote=“ibn4n, post:39, topic:180260”]No problem! I’m honestly excited that it was helpful!

I suspect you may have been having the same problem as I was. This wasn’t returning anything for me:

`wl -a wl0.1 assoclist | grep $MAC_ADDRESS_1`

After reading a few dd-wrt wiki pages and messing around with the command on the router, I changed that to:

`wl assoclist | grep $MAC_ADDRESS_1`

So I guess if folks like the original script (sans-counters) better, but can’t seem to get it to work, that may be a place to look.[/quote]

Thanks again,

I’m not sure if there was alot of false triggers for you (as I haven’t had it long enough to test) but I know right now leaving it takes almost a mile or 3-5 min. from home to trigger. Unless I have false triggers I’m thinking about going back to 1 or 2 retry’s and less time between retry’s to find a happy med.

The 30 min. recheck.
So If if misses when I come home or I manually flip off my virtual switch it’s going to take 30 mins to recheck and fix it right? Unless its pulling alot of resources I’m also thinking I would rather have this 5 min or so tops. Really the sooner the better, but I’m not sure what the down side is.

I haven’t been able to get the LAN computer to work. I’m assuming I’m putting in the wrong info. If I have a static local IP of 192.168.1.00 on a computer.

How does this line get filled out?

HOST_1=“htpc-lan.XXXXX.local”

It does go by IP address right? Does everything between the “” get erased and IP entered? I have tried it more then one way and never switches. But neither did the MAC’s before.

Last one. I have 4 routers around the property all Same SSID and Password on different channels running DDWRT. Ofcourse as I roam now this doesn’t work when I drop off the main router (one I’m playing with now). Hopefully someone can help extend this code with the variables to other AP’s.