benjp
Posts: 9
Joined: Mon Mar 17, 2014 3:47 pm

Using Python to detect my iPhone

Fri May 23, 2014 4:22 pm

I have created a python program that auto-starts when my Raspberry Pi boots up. I don't want the program to take any action while I am home. I figure that a cool way to do this would be for the program to determine if my phone is connected to my wireless network or not. I've been toying around with having the program ping my phone periodically to see if its on the network, but I can't figure out how to turn the result of the ping into information the python program can use. Any ideas or examples out there? Thanks!

User avatar
FLYFISH TECHNOLOGIES
Posts: 1750
Joined: Thu Oct 03, 2013 7:48 am
Location: Ljubljana, Slovenia
Contact: Website

Re: Using Python to detect my iPhone

Fri May 23, 2014 4:30 pm

Hi,
benjp wrote: can't figure out how to turn the result of the ping into information the python program can use.
An universal solution (applicable also to bash scripting, etc.) is to redirect output of a command to a file and then parse this file.

Output of the command can be redirected to a file using this syntax:
ping -c 1 192.168.0.1 > /tmp/ping_output

Another option is to chain more programs/scripts using pipe for data exchange between them.


Best wishes, Ivan Zilic.
Running out of GPIO pins and/or need to read analog values?
Solution: http://www.flyfish-tech.com/FF32

User avatar
DougieLawson
Posts: 35340
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Using Python to detect my iPhone

Fri May 23, 2014 5:30 pm

Your problem is that you don't know the IP address. You can discover the MAC address of it's WiFi but with a DHCP network that can be assigned to any IP address from your DHCP pool (and if the lease expires or the DHCP server is restarted it will be different).

There's a crude way to scan the network from a bash script

Code: Select all

#!/bin/bash
 for i in `seq 30 100`
 do 
   ping -c1 -W1  192.168.1.$i
 done
And we can see which stations are alive with

Code: Select all

 arp -an | grep ':'


The alternative (and better) way is to use nmap
sudo apt-get install nmap
sudo nmap -sn 192.168.1.0/24
which does a much faster ping sweep and does some decoding on MAC addresses.
Note: Having anything remotely humorous in your signature is completely banned on this forum.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

benjp
Posts: 9
Joined: Mon Mar 17, 2014 3:47 pm

Re: Using Python to detect my iPhone

Fri May 23, 2014 6:42 pm

@Flyfish - Can I ping the iPhone using its name? That way I can be sure that it will reach my phone even if its assigned a new IP? Could I do this?:

ping -c 1 bens-iphone > /tmp/ping_output

MadCow42
Posts: 104
Joined: Sun Jul 01, 2012 12:48 am

Re: Using Python to detect my iPhone

Fri May 23, 2014 8:19 pm

Set up your router to assign a static IP to your iPhone... most routers can do that. Then, you can ping that IP regularly to see if it's there.

I didn't think that an iPhone would respond to a ping by default, but mine does - just tested it.

So, piping the ping output to a file and parsing the file is not pretty, but it would work.

Kevin.

User avatar
cyrano
Posts: 714
Joined: Wed Dec 05, 2012 11:48 pm
Location: Belgium

Re: Using Python to detect my iPhone

Fri May 23, 2014 9:37 pm

benjp wrote:@Flyfish - Can I ping the iPhone using its name? That way I can be sure that it will reach my phone even if its assigned a new IP? Could I do this?:

ping -c 1 bens-iphone > /tmp/ping_output
Yes.

Unfortunately, with Lion, Apple has put a little gem out that's called "Bonjour Sleep Proxy". Any other Apple device might reply with your phone's name on a first call. Your router, if it's an Apple branded beast. Or an iMac on your network, or some other recent Apple compatible device, a NAS, for instance. It's a way to make battery powered devices "available" on the network while they are in deep sleep. Once it's really needed, the phone will wake up and take over from the proxy, of course. Otherwise, it would need to wake for each and every broadcast mdns call. :ugeek:

So you 'd need to experiment a bit with making multiple calls before deciding if it is your phone or the Sleep Proxy's ghost. Doing it that way will wake up the phone and shorten battery life.

And I'm afraid changing to MAC adress might face the same problem, since even MAC addresses get proxied, IIRC.

And it will make some people hate RendezVous/Bonjour/Avahi/ZeroConfig... even more.

david_wallis
Posts: 41
Joined: Thu Jul 05, 2012 8:21 pm

Re: Using Python to detect my iPhone

Sat May 24, 2014 7:49 am

What about using Bluetooth instead?

benjp
Posts: 9
Joined: Mon Mar 17, 2014 3:47 pm

Re: Using Python to detect my iPhone

Tue May 27, 2014 8:04 pm

Thanks for the input everyone!

I've run into the problem when I ping the phone when it is locked, the ping fails. Wow, this is getting complicated real quick. Is there a way to get the iPhone to listen for pings without draining the battery?

@david_wallis - Didn't think about Bluetooth. Sounds like a good idea. I have no idea how to go about using it, though. Any pointers or webpages with examples?

RockTractor
Posts: 1
Joined: Thu May 29, 2014 8:49 pm

Re: Using Python to detect my iPhone

Thu May 29, 2014 8:55 pm

It's my understanding that you can wake up your iPhone with a TCP three-way (Synchronize) handshake on port 62078.

oguime
Posts: 19
Joined: Fri Feb 14, 2014 3:14 am

Re: Using Python to detect my iPhone

Sun Jun 22, 2014 3:01 am

It's my understanding that you can wake up your iPhone with a TCP three-way (Synchronize) handshake on port 62078.
I'm also interested in detecting iPhones connected to my network.

Has anyone implemented this?

nekomatic
Posts: 20
Joined: Wed Nov 16, 2011 8:06 pm

Re: Using Python to detect my iPhone

Wed Jul 02, 2014 10:39 pm

Does your wireless router have a status webpage you can access by http? If so, does it show information you could screenscrape and use to determine whether your iPhone is currently on, or has just joined, the network?

DigiPi
Posts: 1
Joined: Wed Aug 27, 2014 4:42 am

Re: Using Python to detect my iPhone

Wed Aug 27, 2014 4:50 am

oguime wrote:
It's my understanding that you can wake up your iPhone with a TCP three-way (Synchronize) handshake on port 62078.
I'm also interested in detecting iPhones connected to my network.

Has anyone implemented this?
I'm very interested in this as well and found the following

http://www.power-home.com/forum/forum_p ... p?TID=3250

So far I have been unsuccessful in reproducing
ph_sendsocketdata1 ( "192.168.1.92", 62078, 6, "\x16" )
with Python sockets though.

Paul Liddicott
Posts: 4
Joined: Tue Jul 29, 2014 6:34 am

Re: Using Python to detect my iPhone

Wed Aug 27, 2014 6:47 am

Hi Benjp,

Earlier this year I attended a Rpi course run by a colleague at work.

We used the Rpi to detect when a phone/laptop etc attempted to join a network.

this was done using MAC address of joining device,

The details are published on the IET website (see link).

http://mycommunity.theiet.org/communiti ... s/173/5822

It was all quite easy to set up, the only requirement was that the wifi card on the pi could operate in promiscuous mode to achieve packet sniffing.

Hope this is of some use.

Paul

domjacko
Posts: 2
Joined: Fri Sep 05, 2014 3:16 pm

Re: Using Python to detect my iPhone

Fri Sep 05, 2014 10:45 pm

I've recently done a Python script to detect if a certain Bluetooth device is in range. It's a really neat way of detecting a device, but obviously the range would be an issue and you'd have to modify it to remove the LED bit and change to what you want
Last edited by domjacko on Mon Oct 26, 2015 4:19 pm, edited 1 time in total.

tronb
Posts: 1
Joined: Wed Jan 21, 2015 9:35 pm

Re: Using Python to detect my iPhone

Wed Jan 21, 2015 9:38 pm

DigiPi,
Did you have any luck getting the code to work on python?
If you do, could you share your code?

Thanks

Absimiliard
Posts: 1
Joined: Wed May 24, 2017 8:26 am

Re: Using Python to detect my iPhone

Wed May 24, 2017 8:33 am

I have implemented a daemon that check for my iphone using bluetooth. I write it in C++ using bluez and this doc
https://people.csail.mit.edu/albert/blu ... /x604.html

But it give you also samples in python in the same documentation :

My server is available on github : https://github.com/marc-despland/alarm-controller

The class that do the detection is src/blueping.cpp

Return to “Python”