Author Topic: Luup script to see if you are home using Find My iPhone  (Read 107945 times)

Offline jasongill

  • Newbie
  • *
  • Posts: 9
  • Karma: +0/-0
Luup script to see if you are home using Find My iPhone
« on: July 07, 2013, 05:31:24 pm »
This is one of my first posts so please excuse me if this is in the wrong subforum.

I've written a simple Lua/Luup script which will return true (and thus, allow your scene to execute) if your iPhone or other iOS device is at a specified latitude and longitude. This way, you can have a scene which only runs when you're at home, or only runs when you aren't at home.

It's not perfect and the script itself is very rough right now - one of my first attempts at Lua - but it does work for me. Hopefully this helps someone else or gives some inspiration for a project of your own.

To make this work, you should be able to just paste it in to the Luup section for a scene and then modify the variables at the top to include your correct iCloud username (normally your email address), password, and your latitude/longitude. Additionally, there is a "skew" value which is used to determine when you are "outside" of your house - adjust that to change how big of a radius your "home" should be (this part could use some work - improvements welcomed).

You can find the Luup code here: https://gist.github.com/jasongill/5945055

If you want to make improvements, feel free to edit the gist. Perhaps someone more ambitious than I can turn this into a plugin that creates a "motion sensor" type virtual device, triggering events when you enter or leave the area with your iPhone.

Hope this helps someone else!

Offline guessed

  • Master Member
  • *******
  • Posts: 5300
  • Karma: +92/-22
  • Release compat is not a bolted-on afterthought
Re: Luup script to see if you are home using Find My iPhone
« Reply #1 on: July 07, 2013, 05:52:09 pm »
Good stuff, and congrats on your early coding post... 

If you want to do it all in-process, you can use LuaSocket to do HTTP POST calls also.

There are snippets here that show the basics:
     http://wiki.micasaverde.com/index.php/Luup_Scenes_Events#Invoke_HTTP_URL_with_POST_request_.28Method_3.29

This will avoid forking an OS-level process, so in theory it'll be a little faster and/or lightweight.... which is important because you're running it in-line in scene execution (at least until you want to try your hand at turning it into a plugin  8) )

Offline jasongill

  • Newbie
  • *
  • Posts: 9
  • Karma: +0/-0
Re: Luup script to see if you are home using Find My iPhone
« Reply #2 on: July 07, 2013, 06:27:59 pm »

If you want to do it all in-process, you can use LuaSocket to do HTTP POST calls also.

There are snippets here that show the basics:
     http://wiki.micasaverde.com/index.php/Luup_Scenes_Events#Invoke_HTTP_URL_with_POST_request_.28Method_3.29

Thanks! I did look at that example but wasn't sure of a way to set request headers, and retrieve response headers, both of which are required for the Find My iPhone "API". I haven't dug in too deep, but do you know if that is possible with the socket.http module?

Offline guessed

  • Master Member
  • *******
  • Posts: 5300
  • Karma: +92/-22
  • Release compat is not a bolted-on afterthought
Re: Luup script to see if you are home using Find My iPhone
« Reply #3 on: July 07, 2013, 06:35:05 pm »
This is an example of a more complex/complete call with headers:
    http://code.mios.com/trac/mios_sonos-wireless-music-systems/browser/trunk/L_Sonos1.lua#L115

the code before it sets up the headers into the array that's being passed in.  There's also code that handles the returned headers.
« Last Edit: July 07, 2013, 06:36:56 pm by guessed »

Offline duiffie

  • Full Member
  • ***
  • Posts: 126
  • Karma: +2/-0
Re: Luup script to see if you are home using Find My iPhone
« Reply #4 on: July 28, 2013, 05:49:39 pm »
You could add the following code to calculate the distance between you and your home, for replacing your skew with a 'safe-radius' distance for example. You can also calculate when the next check should be based on the distance (for iPhone battery savings): why should the script check your location again within 5 minutes if it has already detected you're much further away from home?

Code: [Select]
local R = 6378.137                 --- Radius of earth in KM
local dLat = (value.location.latitude - homelatitude) * math.pi / 180
local dLon = (value.location.longitude - homelongitude) * math.pi / 180
local a = math.sin(dLat/2) * math.sin(dLat/2) +
math.cos(homelatitude * math.pi / 180) * math.cos(value.location.latitude * math.pi / 180) *
math.sin(dLon/2) * math.sin(dLon/2)
local c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
local d = R * c
local distance = d * 1000      --- in meters
Vera2 (1.5.622)

Offline Intrepid

  • Hero Member
  • *****
  • Posts: 536
  • Karma: +4/-0
Re: Luup script to see if you are home using Find My iPhone
« Reply #5 on: July 28, 2013, 06:12:39 pm »
Way over my head, but I've got an iPhone, a Vera, and the need to know whether or not I'm home.

This looks like great stuff.


Offline Crismaison

  • Sr. Member
  • ****
  • Posts: 451
  • Karma: +2/-0
Re: Luup script to see if you are home using Find My iPhone
« Reply #6 on: July 29, 2013, 04:03:29 am »
WOW, can't wait to get home to test.....this could be a proper replacement of the latitude plugin!!

Indeed a good remark by duiffie, if I am away more than 60 km, it should check again one hour later, if > 120 after two hours etc.
Is it also possible to have 2 Iphones and use the outcome as a variable?
« Last Edit: July 29, 2013, 04:22:28 am by Screamhouse »
Vera lite - Everspring smoke & flood detectors - Fibaro doorsensors - 2 Foscams - Greenwave 6 node smartplug - Several Switches -Netatmo - PLEG - Twilio - DropBoxuploader - FindmyIphone user

Offline LightsOn

  • Hero Member
  • *****
  • Posts: 751
  • Karma: +4/-3
Re: Luup script to see if you are home using Find My iPhone
« Reply #7 on: July 29, 2013, 05:16:15 am »
Hi All,

This looks like great stuff - great first code post also :-)

Sadly if not an iphone user this does limit it somewhat so for others reading this with other phones you can achieve similar results with the "ping sensor" plugin by setting scenes to run based on if ping tripped or not.  works best if you download app from play store to keep Wi-Fi always on, on the phone to avoid delays etc.  works okay. I guess this approach is better though.

Would love the latitude or this iPhone app though - sounds and looks great stuff.

Thank you for sharing.

Offline Crismaison

  • Sr. Member
  • ****
  • Posts: 451
  • Karma: +2/-0
Re: Luup script to see if you are home using Find My iPhone
« Reply #8 on: July 29, 2013, 06:04:53 am »
is there an Iphone app to keep wifi on?
Vera lite - Everspring smoke & flood detectors - Fibaro doorsensors - 2 Foscams - Greenwave 6 node smartplug - Several Switches -Netatmo - PLEG - Twilio - DropBoxuploader - FindmyIphone user

Offline LightsOn

  • Hero Member
  • *****
  • Posts: 751
  • Karma: +4/-3
Re: Luup script to see if you are home using Find My iPhone
« Reply #9 on: July 29, 2013, 06:14:43 am »
I don't have an IPhone so limited knowledge here but I think there are a few ways to achieve it.  One here:

Code: [Select]
http://iphonemonsta.com/insomnia-iphone-sleep-wifi-stay-awake
bound to have limited success though as I have found in my instance also.  as mentioned above the dev work here seems much better than use of ping sensor for this purpose but options all the same :-)

Offline Crismaison

  • Sr. Member
  • ****
  • Posts: 451
  • Karma: +2/-0
Re: Luup script to see if you are home using Find My iPhone
« Reply #10 on: July 29, 2013, 01:39:42 pm »
Can someone add an example how to use the returned value and set eg a Virtual Switch with this value?
Vera lite - Everspring smoke & flood detectors - Fibaro doorsensors - 2 Foscams - Greenwave 6 node smartplug - Several Switches -Netatmo - PLEG - Twilio - DropBoxuploader - FindmyIphone user

Offline sjolshagen

  • Sr. Member
  • ****
  • Posts: 416
  • Karma: +5/-0
Re: Luup script to see if you are home using Find My iPhone
« Reply #11 on: July 30, 2013, 06:35:37 am »
@jasongill,

Any objections to me taking a crack at incorporating this into the Google Latitude Plugin (which then would need to be renamed, etc, but I digress)?
Vera3/UI5 @ v1.5.622

Offline brwblge

  • Newbie
  • *
  • Posts: 14
  • Karma: +0/-0
Re: Luup script to see if you are home using Find My iPhone
« Reply #12 on: July 30, 2013, 07:46:27 am »
Hi all,

Looks like a great plugin in the making.
I've been looking to find info on the HTTP requests for Icloud (such as location) as GUESSED suggested but can't seem to find it.
If they exist.

Any ideas ?

Thanks.

Offline duiffie

  • Full Member
  • ***
  • Posts: 126
  • Karma: +2/-0
Re: Luup script to see if you are home using Find My iPhone
« Reply #13 on: July 30, 2013, 03:26:59 pm »
I'm trying to create an app for this. I used the PingSensor as a reference, and modified that with the above code. I still get a 'Lua Engine Failed to Load' error, so I'm still debugging it... Does someone have some tips on how to find the error(s)? LuaUPnP.log doesn't seem to help me that much, even when debug mode is enabled.
Vera2 (1.5.622)

Offline brwblge

  • Newbie
  • *
  • Posts: 14
  • Karma: +0/-0
Re: Luup script to see if you are home using Find My iPhone
« Reply #14 on: July 31, 2013, 04:54:59 am »
Could you post the code.
Perhaps I can help find the error.

Greetz