Detecting iPhone Wi-Fi connection

Posted on
Wed Oct 27, 2010 11:54 am
anothersphere offline
Posts: 158
Joined: Jul 01, 2009

Re: Detecting iPhone Wi-Fi connection

Did not seem to help when I tried before posting.

Martin Miller

Auckland - New Zealand

Posted on
Thu Dec 23, 2010 11:10 pm
craigsheppard offline
Posts: 8
Joined: May 27, 2008

Re: Detecting iPhone Wi-Fi connection

I just spent the evening getting proximity detection working on my own, only to find that the phone does indeed drop "off the radar" when not in use.

I just set my Airport Extreme to use my indigo server as a syslog server. Then I have a launchd job watching the log for changes. A shell script reads the last line and updates variables in indigo. It was pretty simple.

It works great - until I realized that the connection is disassociated as soon as the phone sleeps. I can get it to wake by sending email to my .mac account (push), but I can't wake it by pinging it...

Anybody got something that works well? I can't use bluetooth for range issues, and because I'm also detecting (separately) my wife's iPhone...

I tried messing around with Google Latitude, but didn't get far when I was trying to pull data from my account... I'd love to get this sorted out - lots of cool things could be done with this type of detection...

Craig

Posted on
Fri Dec 24, 2010 9:31 am
loafbread offline
Posts: 137
Joined: May 25, 2009

Re: Detecting iPhone Wi-Fi connection

I ping the iPhones 5 minutes after a garage or house door opens/closes. That tells me who is home. I wake up the iPhone just prior to the ping with a push using http://dopushmail.com/. It is easy to email the push. We have a pushmail profile on the iPhone that is silent so the intrusion is minimal. I have tried many methods over the last 2 years and this one seems to work great. It doesn't detect arrival but I realized I didn't really need that. I just wanted to know when everyone was gone so I could close garage doors and turn off lights.

I was using https://github.com/tylerhall/sosumi PHP to locate the phones but the last update of Find My iPhone broke it for me. I played with Latitude but I like this method of pinging the phones best.

Posted on
Sat Dec 25, 2010 5:27 am
bschollnick2 offline
Posts: 1355
Joined: Oct 17, 2004
Location: Rochester, Ny

Re: Detecting iPhone Wi-Fi connection

loafbread wrote:
I ping the iPhones 5 minutes after a garage or house door opens/closes. That tells me who is home. I wake up the iPhone just prior to the ping with a push using http://dopushmail.com/. It is easy to email the push. We have a pushmail profile on the iPhone that is silent so the intrusion is minimal. I have tried many methods over the last 2 years and this one seems to work great. It doesn't detect arrival but I realized I didn't really need that. I just wanted to know when everyone was gone so I could close garage doors and turn off lights.

I was using https://github.com/tylerhall/sosumi PHP to locate the phones but the last update of Find My iPhone broke it for me. I played with Latitude but I like this method of pinging the phones best.


Just an FYI -- I will be updating Find My iPhone, probably after the public beta of Indigo 5... The current Applescript implementation just is not usable, do to changes Apple made...

------
My Plugins for Indigo (v4, v5, and v6) - http://bit.ly/U8XxPG

Security Script for v4 - http://bit.ly/QTgclf
for v5 - http://bit.ly/T6WBKu

Support Forum(s) - http://www.perceptiveautomation.com/userforum/viewforum.php?f=33

Posted on
Sun Jan 30, 2011 7:20 pm
hammer32 offline
Posts: 66
Joined: May 13, 2006
Location: Copperas Cove, TX

Re: Detecting iPhone Wi-Fi connection

I've tried to use the excellent advice in this thread to cobble together a script to detect our iPhones. If I run the script in AppleScript Editor or as an Embedded Script in Indigo it runs fine (but pauses Indigo to do it) and returns positive results when an iPhone is detected. However, if I have Indigo run the script as a file (so it executes in the background) the results are always negative.

Any idea why the results would be different for an embedded script versus running the script as a file in a date/time action?

Thanks!

Sean

Code: Select all
-- First we'll test for the iPhones via wi-fi:

with timeout of 10 seconds
   -- Test for Andrea's iPhone
   try
      (do shell script "ping -o -t 4 -c 4 192.168.1.11")
      
      tell application "IndigoServer"
         set value of variable "iPhoneAndrea" to "home"
         set value of variable "PresenceDetection" to "SomeoneHome"
         log "Andrea is Home"
         return
      end tell
   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"
         set value of variable "iPhoneAndrea" to "away"
         log "Andreas iPhone wasn't detected"
      end tell
   end try
   -- Test for Sean's iPhone
   try
      (do shell script "ping -o -t 4 -c 4 192.168.1.12")
      
      tell application "IndigoServer"
         set value of variable "iPhoneSean" to "home"
         set value of variable "PresenceDetection" to "SomeoneHome"
         log "Sean is Home"
         return
      end tell
   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"
         set value of variable "iPhoneSean" to "away"
         log "Seans iPhone wasn't detected"
      end tell
   end try
   
end timeout


-- If the iPhones aren't found, then we'll try sending the pushmail to wake them up:

--Variables
set theAccount to "**************"
set recipientName to "Home Automation"
set recipientAddress to "**********@dopushmail.com"
set theSubject to "AppleScript Automated Email"
set theContent to "This is a test for your iPhone."

--Mail Tell Block
tell application "Mail"
   
   --Create the message
   set theMessage to make new outgoing message with properties {subject:theSubject, content:theContent, visible:true}
   
   --Set a recipient
   tell theMessage
      make new to recipient with properties {name:recipientName, address:recipientAddress}
      
      --Send the Message
      send
      
   end tell
end tell

-- Wait for 10 seconds for the message to be sent and received at the iPhone, waking it up and putting it back on wifi:
delay 10

-- Now look for the IPhones using wifi at home:
with timeout of 4 seconds
   -- Test for Andrea's iPhone
   try
      (do shell script "ping -o -t 4 -c 4 192.168.1.11")
      
      tell application "IndigoServer"
         set value of variable "iPhoneAndrea" to "home"
         set value of variable "PresenceDetection" to "SomeoneHome"
         log "Andrea is Home"
      end tell
   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"
         set value of variable "iPhoneAndrea" to "away"
         log "Andrea is Away"
      end tell
   end try
   -- Test for Sean's iPhone
   try
      (do shell script "ping -o -t 4 -c 4 192.168.1.12")
      
      tell application "IndigoServer"
         set value of variable "iPhoneSean" to "home"
         set value of variable "PresenceDetection" to "SomeoneHome"
         log "Sean is Home"
      end tell
   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"
         set value of variable "iPhoneSean" to "away"
         log "Sean is Away"
      end tell
   end try
   
end timeout

Posted on
Sun Jan 30, 2011 7:47 pm
craigsheppard offline
Posts: 8
Joined: May 27, 2008

Re: Detecting iPhone Wi-Fi connection

I'm having similarly strange behavour on a completely separate topic - my Applescripts that call ssh work fine in script editor, but when run from Indigo, they do not work... In this case, it seems to be a PAM/Kerberos error, not sure if this applies to your applescript or not, just thought I'd mention it in case it sparked a solution ;)

Craig

Posted on
Sun Jan 30, 2011 7:51 pm
jay (support) offline
Site Admin
User avatar
Posts: 18246
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Detecting iPhone Wi-Fi connection

My suspicion is that the timeouts are conflicting giving you erratic results - you've got the ping timing out at 4 seconds AND the AppleScript timeout at 4 seconds - it's unclear which one is timing out and in fact may be timing out differently based on where the script is executing.

Given you're running it as an external script you can safely increase the AppleScript timeout - maybe up to 10 seconds or so. In fact, for testing, you might want to just comment out the AppleScript timeouts to make sure something else isn't going wrong but getting obscured by those timeouts.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Sun Jan 30, 2011 8:15 pm
hammer32 offline
Posts: 66
Joined: May 13, 2006
Location: Copperas Cove, TX

Re: Detecting iPhone Wi-Fi connection

Thanks for the quick replies!

I deleted the "with timeout of..." statements, leaving only the ping timeout. No joy. I upped the ping timeout to 10 seconds - it completes very fast, but still always yields a negative result.

Posted on
Sun Jan 30, 2011 9:18 pm
dstrickler offline
User avatar
Posts: 340
Joined: Oct 08, 2010
Location: Boston, MA

Re: Detecting iPhone Wi-Fi connection

I would try an put a full path to your ping statement(s) - it may not be running ping at all. On my MBP with Snow Leopard, I show ping as "/sbin/ping", but you should verify that on your system. And please note I have not tried your clever script, so I don't know if this will work - just a guess.

On a semi-related note, I am using the SoSueMe script with MobileMe, and it works well, but drains the phone battery if you run it more then once every 30 minutes. It's given me accurate GPS readings except in a big snowstorm we had here. The other data it pulls isn't as accurate (Apple's fault - not the developer's) such as battery life. I use a little math to check my distance to home from it's GPS coordinates, and if I am within 100 feet, it assumes I am home.

Dave

Posted on
Mon Jan 31, 2011 12:24 pm
hammer32 offline
Posts: 66
Joined: May 13, 2006
Location: Copperas Cove, TX

Re: Detecting iPhone Wi-Fi connection

dstrickler wrote:
I would try an put a full path to your ping statement(s) - it may not be running ping at all. On my MBP with Snow Leopard, I show ping as "/sbin/ping", but you should verify that on your system. And please note I have not tried your clever script, so I don't know if this will work - just a guess.


Thanks! Adding the full path seems to have done the trick!

Posted on
Mon Jan 31, 2011 3:05 pm
dstrickler offline
User avatar
Posts: 340
Joined: Oct 08, 2010
Location: Boston, MA

Re: Detecting iPhone Wi-Fi connection

Glad it worked!

Another tip when calling programs on the CLI in Linux is to pipe the output for examining post-morten. You can do this by using something like:
Code: Select all
do shell script "/sbin/ping -o -t 4 -c 4 192.168.1.11 > /tmp/ping_output.log"

Note in this example I assume there is a "/tmp" directory (check your O/S and choose another if appropriate), so that you can look at the file " /tmp/ping_output.log" afterwards and see what the output is. You can do this from the CLI with a command like:
Code: Select all
cat  /tmp/ping_output.log"


Note that the text output that would normally go to the screen in linux command will get "piped" to this file, but the error value will still be returned. This means that your code should (no guarantees!) work as it's looking for an error code from the ping command.

Anyway, play around with it. It should work OK...

Dave

Posted on
Thu Feb 10, 2011 8:43 am
downindm offline
Posts: 26
Joined: Nov 07, 2010

Re: Detecting iPhone Wi-Fi connection

I'm working on doing this myself as well. If your iPhone is jailbroken, you can download a Cydia app called "Prevent Sleep". It adds a SBSetting that will keep the WiFi connected even when the phone is not plugged in (screen will still sleep). Downside is it does shorten the battery life to some extent - not too bad in my case.

Posted on
Sun Feb 13, 2011 10:02 pm
hamw offline
Posts: 1212
Joined: Mar 31, 2008

Re: Detecting iPhone Wi-Fi connection

This is a neat idea; do you have a complete and working script that you could share? What are you all's thoughts re GPS location detection vs wifi?

http://www.perceptiveautomation.com/userforum/viewtopic.php?f=11&t=5604&p=38582

Does sending all that pushmail crowd your outbox? Is there an autodelete in mail?

Posted on
Mon Feb 14, 2011 4:20 pm
hammer32 offline
Posts: 66
Joined: May 13, 2006
Location: Copperas Cove, TX

Re: Detecting iPhone Wi-Fi connection

hamw wrote:
This is a neat idea; do you have a complete and working script that you could share? What are you all's thoughts re GPS location detection vs wifi?
http://www.perceptiveautomation.com/userforum/viewtopic.php?f=11&t=5604&p=38582
Does sending all that pushmail crowd your outbox? Is there an autodelete in mail?


I think GPS tracking would be a great addition, I might use a combination of both WiFi and GPS - in case the GPS doesn't have a clear signal and it sends back the coords of the nearest cell tower. If the phone was farther away, I could skip the email test my script uses and save some battery life when we're on the road.

I use a separate account for email from the 'house' so I can filter out these emails if I need to, but they do tend to fill up the sent items folder.

Here's the script I use. I run it as a separate file every ten minutes, so it doesn't slow down Indigo while it tests for the iPhones. If neither iPhone responds I have a timer that runs for 30 minutes and continues to look for the phones before the house is 'armed'. Each iPhone has a reserved LAN IP address. I'm not much of a programmer so it's pieces of code found either elsewhere on this site or via Google :) I'm sure there's probably a more elegant way to do this:

Code: Select all
-- The purpose of this script is to detect the presence of Sean and Andrea's iPhones.  If the phones are detected then the appropriate status will be set to "home" otherwise it will be set to "away".  iPhones only respond if they are using wi-fi.  Currently, if the iPhone is screen-locked it won't continiously use wi-fi - it'll only connect to get email.  To get around this, we send a push email to xxxxx@dopushmail.com to knock the phone back on to wifi, wait 10 seconds, then test to see if it's in the house.

-- First we'll test for the iPhones via wi-fi:


-- Now look for the IPhones using wifi at home:
-- Test for Andrea's iPhone
try
   (do shell script "/sbin/ping -o -t 2 -c 4 192.168.1.11")
   
   tell application "IndigoServer"
      set value of variable "iPhoneAndrea" to "home"
      log "Andrea is Home"
      return
   end tell
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"
      log "Andreas iPhone wasn't detected - will try to wake it up"
   end tell
end try
-- Test for Sean's iPhone
try
   (do shell script "/sbin/ping -o -t 2 -c 4 192.168.1.12")
   
   tell application "IndigoServer"
      set value of variable "iPhoneSean" to "home"
      log "Sean is Home"
      return
   end tell
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"
      log "Seans iPhone wasn't detected - will try to wake it up"
   end tell
end try



-- If the iPhones aren't found, then we'll try sending the pushmail to wake them up:

--Variables
set theAccount to "Answer Phone"
set recipientName to "Home Automation"
set recipientAddress to "xxxxx@dopushmail.com"
set theSubject to "AppleScript Automated Email"
set theContent to "This is a test for your iPhone."

--Mail Tell Block
tell application "Mail"
   
   --Create the message
   set theMessage to make new outgoing message with properties {subject:theSubject, content:theContent, visible:true}
   
   --Set a recipient
   tell theMessage
      make new to recipient with properties {name:recipientName, address:recipientAddress}
      
      --Send the Message
      send
      
   end tell
end tell

-- Wait for 10 seconds for the message to be sent and received at the iPhone, waking it up and putting it back on wifi:
delay 10

-- Now look for the IPhones using wifi at home:
-- Test for Andrea's iPhone
try
   (do shell script "/sbin/ping -o -t 3 -c 4 192.168.1.11")
   
   tell application "IndigoServer"
      set value of variable "iPhoneAndrea" to "home"
      log "Andrea is Home"
   end tell
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"
      set value of variable "iPhoneAndrea" to "away"
      log "Andrea is Away"
   end tell
end try
-- Test for Sean's iPhone
try
   (do shell script "/sbin/ping -o -t 3 -c 4 192.168.1.12")
   
   tell application "IndigoServer"
      set value of variable "iPhoneSean" to "home"
      log "Sean is Home"
   end tell
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"
      set value of variable "iPhoneSean" to "away"
      log "Sean is Away"
   end tell
end try

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

Re: Detecting iPhone Wi-Fi connection

Modded your script for my phones and IP addresses. Seems to work but won't go past the first phone to execute the second phone's detection. I'm running it as an external attachment script. When the phones are asleep, it does send pushmail to wake them up. I did change home and away to true and false so that I could just use an icon.

Also, can one disable the pushmail display so that it is invisible to the user?

Thanks!

Code: Select all
-- The purpose of this script is to detect the presence of Sean and Andrea's iPhones. If the phones are detected then the appropriate status will be set to "home" otherwise it will be set to "away". iPhones only respond if they are using wi-fi. Currently, if the iPhone is screen-locked it won't continiously use wi-fi - it'll only connect to get email. To get around this, we send a push email to xxxxx@dopushmail.com to knock the phone back on to wifi, wait 10 seconds, then test to see if it's in the house.

-- First we'll test for the iPhones via wi-fi:


-- Now look for the IPhones using wifi at home:
-- Test for Marion's iPhone


try
   (do shell script "/sbin/ping -o -t 2 -c 4 10.0.1.51")
   
   tell application "IndigoServer"
      set value of variable "iPhoneMarion" to "true"
      log "Marion is Home"
      return
   end tell
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"
      log "Marion's iPhone wasn't detected - will try to wake it up"
   end tell
end try
--end try

--Note that the script doesn't seem to advance beyond this point if Marion's phone is detected--

-- Test for Ham's iPhone
try
   (do shell script "/sbin/ping -o -t 2 -c 4 10.0.1.50")
   
   tell application "IndigoServer"
      set value of variable "iPhoneHam" to "true"
      log "Ham is Home"
      return
   end tell
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"
      log "Ham's iPhone wasn't detected - will try to wake it up"
   end tell
end try


-- If the iPhones aren't found, then we'll try sending the pushmail to wake them up:

--Variables
set theAccount to "Answer Phone"
set recipientName to "Home Automation"
set recipientAddress to "xxxx@dopushmail.com"
set theSubject to "AppleScript Automated Email"
set theContent to "This is a test for your iPhone."

--Mail Tell Block
tell application "Mail"
   
   --Create the message
   set theMessage to make new outgoing message with properties {subject:theSubject, content:theContent, visible:true}
   
   --Set a recipient
   tell theMessage
      make new to recipient with properties {name:recipientName, address:recipientAddress}
      
      --Send the Message
      send
      
   end tell
end tell
--tell application "Mail"
--hide
--end tell


-- Wait for 10 seconds for the message to be sent and received at the iPhone, waking it up and putting it back on wifi:
delay 10



-- Now look for the IPhones using wifi at home:
-- Test for Marion's iPhone
try
   (do shell script "/sbin/ping -o -t 3 -c 4 10.0.1.51")
   
   tell application "IndigoServer"
      set value of variable "iPhoneMarion" to "true"
      log "Marion is Home"
   end tell
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"
      set value of variable "iPhoneMarion" to "false"
      log "Marion is Away"
   end tell
end try


-- Test for Ham's iPhone
try
   (do shell script "/sbin/ping -o -t 3 -c 4 10.0.1.50")
   
   tell application "IndigoServer"
      set value of variable "iPhoneHam" to "true"
      log "Ham is Home"
   end tell
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"
      set value of variable "iPhoneHam" to "false"
      log "Ham is Away"
   end tell
end try


Who is online

Users browsing this forum: No registered users and 11 guests