Detecting iPhone Wi-Fi connection

Posted on
Wed Feb 16, 2011 12:46 am
artpics offline
Posts: 232
Joined: Feb 24, 2009
Location: Calabasas CA

Re: Detecting iPhone Wi-Fi connection

Hamw.

i modified the script to use local (lan) addresses as you did my script works fine on both phones.

Posted on
Wed Feb 16, 2011 6:10 am
hamw offline
Posts: 1212
Joined: Mar 31, 2008

Re: Detecting iPhone Wi-Fi connection

Here's the log:

Code: Select all
Feb 16, 2011 7:07:41 AM
  Time/Date Action   Location Detection via WiFi
  Script             Marion's iPhone wasn't detected - will try to wake it up
  Script             Ham is Home
  Time/Date Action   Location Detection via WiFi
  Script             Marion is Home

Feb 16, 2011 7:08:13 AM
  Time/Date Action   Location Detection via WiFi
  Script             Marion is Home

Feb 16, 2011 7:08:37 AM
  Time/Date Action   Location Detection via WiFi
  Script             Marion is Home


When the first phone is not detected, it moves on and finds the other. However, the script stops if the first phone is found. Thoughts?

Posted on
Wed Feb 16, 2011 1:30 pm
hammer32 offline
Posts: 66
Joined: May 13, 2006
Location: Copperas Cove, TX

Re: Detecting iPhone Wi-Fi connection

I think I see the problem, I've got a "return" after each phone gets tested the first time around. If the first iPhone is detected then I think that return makes it jump out of the script. I'm away from home remoting into the system, so I expected my iPhone to be away and didn't catch that the script stops after the first phone is detected.

If the 'return's are removed I think a test might be needed right before the pushmail step... if the phones have been detected then exit the script... Given my Applescript skills I'll have to think about that for a bit :)

Posted on
Wed Feb 16, 2011 6:54 pm
downindm offline
Posts: 26
Joined: Nov 07, 2010

Re: Detecting iPhone Wi-Fi connection

This works great - and yes, you do need to remove the "return" statements if you want the script to run all the way through I think. I also added in an additional check to ping google.com. If that ping fails, it toggles the router off and back on to hopefully bring my connection back.

Code: Select all
with timeout of 10 seconds
   
   try
      (do shell script "/sbin/ping -o -t 4 -c 4 google.com")
      
   on error errStatement number errNum --errStatement holds text of error message, errNum the integer code described in either 'man ping' or sysexits.h
      
      tell application "IndigoServer"
         turn off device "Router"
         log "Internet Connection Lost - Rebooting Router"
         delay 5
         turn on device "Router"
         set value of variable "Router_Last_Reboot" to (current date) as string
      end tell
   end try
   
end timeout

Posted on
Wed Feb 16, 2011 10:34 pm
hamw offline
Posts: 1212
Joined: Mar 31, 2008

Re: Detecting iPhone Wi-Fi connection

Commented out the return statements and it runs thru, although not 100% accurate for some reason.

Code: Select all
16Feb, 2011 11:25:14 PM
  Time/Date Action    Location Detection via WiFi
  Script              Marion is Home
  Script              Ham is Home
  Script              Marion is Home
  Script              Ham is Away  --No, was at home with phone right next to me

16Feb, 2011 11:27:31 PM
  Script              Marion is Home
  Script              Ham's iPhone wasn't detected - will try to wake it up
  Script              Marion is Home
  Script              Ham is Home --Correct.


Is there a setting that that would keep pushmail from displaying the new mail notification?

Posted on
Fri Mar 04, 2011 9:59 pm
hamw offline
Posts: 1212
Joined: Mar 31, 2008

Re: Detecting iPhone Wi-Fi connection

Got the pushmail to quiet down and script works great. However, I'm getting mail send issues - server timeouts - that then cause email messages to accumulate. Any way of setting the smtp timeout on Mac Mail?

Posted on
Wed Apr 06, 2011 11:17 am
jameslitton offline
Posts: 5
Joined: Apr 06, 2011

Re: Detecting iPhone Wi-Fi connection

We have three iPhones in the house and like everyone else in this thread I wanted a way to determine who was home based on the presence of that person's phone on my WiFi network.

I wanted to accomplish this goal without having to make configuration changes to the phones and without having to resort to pushing mail to the devices to wake them up.

Each of the phones have various and sundry applications installed on them that regularly poll the internet for data. The most important of these applications is mail which is configured on all three phones for quarter-hour updates for IMAP accounts on a server that is external to my home network. As stated elsewhere in this thread the phones connect to the WiFi network, check mail and, unless there is new mail, disconnect about five seconds later.

As each phone connects to the internet it is forced to route traffic via my default gateway (for me a BSD based router) which creates an ARP entry for that device in the router's ARP table. The default TTL for new ARP entries is 1200 seconds (20 minutes) which works very well with our quarter-hour email updates. This means that, in theory, as long as our phones are turned on and are within range of my WiFi network then the device should have a valid ARP entry in the router's ARP table which gives me a reliable source for determining who is home and who isn't.

Now that I have my data source I needed a way to grab and parse the data then update the appropriate Indigo variables. I already have a host of scripts that run on my HA server so I wrote a very simple BASH script and configured it to run via cron at five minute intervals.


Code: Select all
#!/bin/bash
ssh root@192.168.xxx.xxx 'arp -a' >whoshome.tmp

if grep -q device1 whoshome.tmp; then
   curl -X PUT -d value=home http://127.0.0.1:8176/variables/homePerson1
else
   curl -X PUT -d value=away http://127.0.0.1:8176/variables/homePerson1
fi

if grep -q device2 whoshome.tmp; then
   curl -X PUT -d value=home http://127.0.0.1:8176/variables/homePerson2
else
   curl -X PUT -d value=away http://127.0.0.1:8176/variables/homePerson2
fi

if grep -q device3 whoshome.tmp; then
   curl -X PUT -d value=home http://127.0.0.1:8176/variables/homePerson3
else
   curl -X PUT -d value=away http://127.0.0.1:8176/variables/homePerson3
fi

rm whoshome.tmp


if you have your devices defined with static addresses via DHCP (as I do) then you can grep on the device name. If not, you can search on MAC address, either method works equally well.

Posted on
Fri Apr 15, 2011 10:30 pm
hamw offline
Posts: 1212
Joined: Mar 31, 2008

Re: Detecting iPhone Wi-Fi connection

James,

Could you advise how to implement this script via time date action? I tried putting the script as is into the action tab and it wouldn't compile.

thanks!

Posted on
Sat Apr 16, 2011 10:11 am
jameslitton offline
Posts: 5
Joined: Apr 06, 2011

Re: Detecting iPhone Wi-Fi connection

hamw,

This script functions outside of Indigo so you would not set it up using Indigo Triggers.

I have mine set up to run at five minute intervals via cron. I run all of my HA cron jobs as root so I don't have to worry about permission issues. To do this open up a Terminal session and type:

Code: Select all
sudo crontab -e

Once in the vi editor you will need to add the following line (adjusting for your environment of course) to the bottom of your file.

Code: Select all
*/5 * * * * /Users/user123/scripts/whoshome.sh >/dev/null

Don't forget to make the shell script executable. To do this, open a Terminal session, navigate to the same directory where you have stored your script and type:

Code: Select all
chmod +x whoshome.sh

If you haven't already done so, you will also need to set up "passwordless logins" to your router by generating a key on your HA server and then installing that key on your router. A quick Google search will produce numerous step-by-step guides for doing this.

Hope that helps ... good luck!

Thanks!

Posted on
Sat Apr 16, 2011 4:22 pm
hamw offline
Posts: 1212
Joined: Mar 31, 2008

Re: Detecting iPhone Wi-Fi connection

Would AppleScript's "do shell script" syntax help? Is it possible to use your same concept to poll an AP Extreme to find out who's on it?

Posted on
Sat Apr 16, 2011 4:51 pm
dstrickler offline
User avatar
Posts: 340
Joined: Oct 08, 2010
Location: Boston, MA

Re: Detecting iPhone Wi-Fi connection

jameslitton,

That's an excellent suggestion on the arp table and a sure fire passive way to check. What I am looking for this elusive "Who Is Arriving/Leaving" check.

The problem I have been dealing with is knowing within 60 seconds (or faster) of my entering, or leaving the house. This becomes trickier. With your code, and others I have seen, there is an inherent delay in getting valid positioning data.

Some examples of issues detecting when you walk in the door
* Arp tables: the phone could be asleep, and not instantly connected via WiFI.
* GPS checks: Polling the GPS in an iPhone will chew up the battery, and the resulting data on a single lookup is not always accurate
* Bluetooth: You need to be close to a computer/receiver for it to work. You'll need to place them at all the doors at your house. And sometimes, bluetooth won't connect as you pass quickly by (it needs a few seconds to get a lock)

And I've even investigated facial recognition with a camera at the door (too slow at 5-10 seconds for a lock), and RFDI pads (you need to hold you keyring up to it - awkward when you have groceries in your hand).

The trick seems to be (or at least is working 90% for me) is a number of checks in sequence:
1. My doors have sensors that trigger Insteon when opened. The Insteon trigger from the door says do some checks.
2 Was there motion (via an Insteon sensor) before the door was opened? If so, someone is entering. If not someone is leaving.
3 Count how many people/phones is knows about in the house right now.
4. If #3 detected leaving, right now, in 60 seconds, and 190 seconds, do a GPS check on the phone. This way I can get a confirmed "direction" that the person is moving. If away from the house, mark them as out of the house. If they haven't moved, leave them as marked in the house.

You can see how #4 could do the same sort of thing in reverse for a person arriving.

Flaws in my system
* If I open the front door and pick up the postal mail, the system thinks someone is leaving the house. Same with letting the dog out.
* If I leave the house just as my wife is arriving the logic gets a little confusing, but you can code around it.
* GPS checks are not always accurate. A good rain/snow storm in Boston (where I live) throw it off by 1,000 feet, and I need an accuracy of less than 100 feet to know if I am in the house.
* It's slow to know if I am "Arriving". Too slow to do anything specific to me. The door/motion checks are quick to say "someone is arriving", but they don't know the elusive "who".

If anyone has any ideas on how to make my system better (without a boatload of work/expense), I'm all ears. It's a problem I, and a lot of others would love to solve.

Dave

Posted on
Sat Apr 16, 2011 8:46 pm
jameslitton offline
Posts: 5
Joined: Apr 06, 2011

Re: Detecting iPhone Wi-Fi connection

Hamw,

You could certainly use AppleScript's "do shell script" as a mechanism to execute the script. For my purposes, the scheduled approach via cron makes more sense but the Applescript method would allow you to run the script in response to a trigger such as the garage door opening, etc.

I'm fairly certain that the AirPort devices do not support command line access thus no ssh or arp commands. In my case, I run a home built BSD router. You might want to look at OpenSource router projects such as m00nwall or pfSense as alternatives. They would certainly support the methods I have deployed in my script. That said, the AirPort devices DO appear to support Syslog. You could set you HA server up as a Syslog server and then point your AirPort to that box then modify the script to look for entries in the log file. While not the same method that I have employed, this approach should get you the same results.

dstickler,

Your desired result is much trickier to achieve but one method that could work would be to use your iPhones as your garage door opener (assuming you do the majority of your entering and exiting via the garage door as we do). As you pull up to the house and open the Indigo App on your phone you will (or should) force a connection to your WiFi network which will be seen by your router. Change the script execution frequency to 60 seconds and you should achieve the results you're looking for. You could achieve the same result with a MorningLinc and a Morning Industry device on the doors that you and your family use to enter and exit the house. Ultimately, however, this only deals with the "presence" portion of your scenario and does not answer the question of "direction". This solution also has weaknesses such as you and your wife arrive together; only one of you will use your phone to open the door so the system will not recognize the entry of the second individual if their phone is "asleep" and thus will not be registered as home until that person's phone wakes up. This approach also assumes that the phone would be used to lock/close the door each time the person leaves the house which may not actually occur depending on the hardware you have deployed and the practices currently in place.

I would deal with the "direction" element of your scenario by setting up a second WiFi Network in my garage. I would run that WiFi Access Point on a Linksys box running something like DD-WRT and would ensure that the signal was very weak so that it would not be picked up in the house. As long as the phone was awake as you entered/exited you SHOULD be able to establish direction by looking at the TTL info on the ARP entries (shorter TTL on the house network and a longer TTL on the garage network, for example, SHOULD indicate that you left.).

This approach does not fit into the "without a boatload of work/expense" category and, as I pointed out in my text, still has inherent flaws. This is the first solution that came to mind after reading your post, but I will think about this some more and see if I can come up with an alternative approach.

Thanks!

Posted on
Sun Apr 17, 2011 12:48 pm
berkinet offline
User avatar
Posts: 3289
Joined: Nov 18, 2008
Location: Berkeley, CA, USA & Mougins, France

Re: Detecting iPhone Wi-Fi connection

I have been following this thread with an eye to doing something like auto-presence detection at some point. I have observed a few themes that seem to make the problem space a little clearer. It seems one major problem is making sure the iPhone, or other device, is awake and ready to respond, a separate issue seems to be determining which device is present, there also seems to be a problem in timing the whole communications protocol, and, finally, there seems to be an issue about how to code a solution.

In the hope of furthering this discussion, I'd like to offer a few thoughts on these areas:
1) sending an email to the phone seems to work for the purpose of waking the phone, but has the side effect of creating a lot of nuisance messages and notifications. Perhaps it would be possible to use another push notification, like a calendar event or address-book change. Gmail supports this, as does me, and probably other services. So, it might be possible to have a iCal calendar event in the future (2013?) and then use an AppleScript to alter that event, causing the change to be pushed to the phone, and, if PUSH were enabled, waking it up - (with, of course, a nice increase in battery usage). Or, just have the phone check the calendar every 10 minutes, which with no other actions, cause the phone to wake periodically).

Perhaps a future version of the iTouch Indigo app might support a push event for the sole purpose of waking the phone.

2) For location detection, it might be possible to use Google Latitude (or me) in a low resolution mode: Like, which phone tower am I near, rather than constant GPS. Or, just infrequent GPS updates. The point would not be to determine when you were home, but when you were close enough to start regular waking of the phone and checking for presence (like via the arp table). SImilarly, if it is possible to determine that a exterior door has opened, then that could be used to trigger a wake action and a presence check.

3) Finally, regarding shell vs. AppleScript. For those who complain that the shell is too hard to learn, I'd just point out that there is really no difference between encapsulating a shell script in an AppleScript "do shell script" command and just writing the shell script... except the shell script is easier to maintain and debug.

anyway, just my 2¢

Posted on
Sun Apr 17, 2011 1:26 pm
dstrickler offline
User avatar
Posts: 340
Joined: Oct 08, 2010
Location: Boston, MA

Re: Detecting iPhone Wi-Fi connection

Adding a calendar item is very clever, and can already be done via AppleScript (I have it working). I don't know what the update times are like when I add a calendar item on my Mac and expect to see it on my iPhone, but I'll do some testing.

Assuming it's reasonably quick, it would solve the "I am home" variable well, since the arp table check would/should be accurate. Again, I will do some testing and report back.

I would add that an overarching problem is that we are trying to bend "location checks" on our phone to behave as fact, and they are just not designed to do that. But that doesn't mean we can't still try.

If we do figure this out, I'll try to write an attachment script that wraps it all up in a nice, easy function that fits into Indigo's architecture.

Dave

Posted on
Sun Apr 17, 2011 2:09 pm
berkinet offline
User avatar
Posts: 3289
Joined: Nov 18, 2008
Location: Berkeley, CA, USA & Mougins, France

Re: Detecting iPhone Wi-Fi connection

BTW, in reference to using syslog to determine the association/disassociation of a Wi-Fi client to an Airport, the information is available. At least from my Airport Express running v7.5.2. The messages logged via syslog look like:

Apr 17 12:57:33 office.local offic-nete 80211: Disassociated with station 00:24:36:ed:19:9e
Apr 17 12:57:37 office.local office-net 80211: Associated with station 00:24:36:ed:19:9e

Note that these messages occur almost instantly after the Wi-Fi connection is established or broken.

I tested this using syslogd on a FreeBSD box, but the same capability is available on MacOS. See:
https://discussions.apple.com/message/1 ... D=12141119

Note: getting the syslog.conf file setup correctly is not trivial and there may be security issues with accepting network syslog traffic.

But, once you have the logging working you can easily monitor the log output with something like
tail -f /var/log/airport.log | your_shell_script

I can post a simple example of the shell script if there is interest.
Last edited by berkinet on Sun Apr 17, 2011 5:46 pm, edited 1 time in total.

Who is online

Users browsing this forum: No registered users and 1 guest