Find My iPhone implementation in LUA script

Moderator: leecollings

Superpjeter
Posts: 11
Joined: Wednesday 29 August 2018 14:11
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Find My iPhone implementation in LUA script

Post by Superpjeter »

Here is my script

Code: Select all

-- Script to check the location of multiple iPhones every X minutes,
-- test if they are "home" and represent this using virtual switches

commandArray = {}
-- polling interval in minutes (1-59), setting this too low may drain the phones' batteries
interval = 10
local m = os.date('%M')
if (m % interval == 0) then

  json = require('json')

  -- Array of users to be checked
  users = {
            Peter = {username = "xxxxxxxxx" ; password = "xxxxxxx" ; devicename = "xxxxxxxx"};
--            Person2 = {username = "[email protected]" ; password = "password2" ; devicename = "iPhone Person2"};
--            Person3 = {username = "[email protected]" ; password = "password3" ; devicename = "iPhone Person3"}
          }
  -- The latitude and longitude of your house (use Google Maps or similar to find this)
  homelongitude = 0.000000
  homelatitude = 00.00000
  -- Radius (in km) which will be used to determine if a device is at home
  radius = 0.5

  for user,credentials in pairs(users) do
    stage1command = "curl -sk -X POST -D - -o /dev/null -L -u '" .. credentials.username .. ":" .. credentials.password .. "' -H 'Content-Type: application/json; charset=utf-8' -H 'X-Apple-Find-Api-Ver: 2.0' -H 'X-Apple-Authscheme: UserIdGuest' -H 'X-Apple-Realm-Support: 1.0' -H 'User-agent: Find iPhone/1.3 MeKit (iPad: iPhone OS/4.2.1)' -H 'X-Client-Name: iPad' -H 'X-Client-UUID: 0cf3dc501ff812adb0b202baed4f37274b210853' -H 'Accept-Language: en-us' -H 'Connection: keep-alive' https://fmipmobile.icloud.com/fmipservice/device/" .. credentials.username .."/initClient"
    local handle = io.popen(stage1command)
    local result = handle:read("*a")
    handle:close()

--    stage2server = string.match(result, "X%-Apple%-MMe%-Host%:%s(.*%.icloud%.com)")
    stage2server = "fmipmobile.icloud.com"
    stage2command = "curl -sk -X POST -L -u '" .. credentials.username .. ":" .. credentials.password .. "' -H 'Content-Type: application/json; charset=utf-8' -H 'X-Apple-Find-Api-Ver: 2.0' -H 'X-Apple-Authscheme: UserIdGuest' -H 'X-Apple-Realm-Support: 1.0' -H 'User-agent: Find iPhone/1.3 MeKit (iPad: iPhone OS/4.2.1)' -H 'X-Client-Name: iPad' -H 'X-Client-UUID: 0cf3dc501ff812adb0b202baed4f37274b210853' -H 'Accept-Language: en-us' -H 'Connection: keep-alive' https://" .. stage2server .. "/fmipservice/device/" .. credentials.username .."/initClient"
    local handle = io.popen(stage2command)
    local result = handle:read("*a")
    handle:close()

    output = json:decode(result)

    for key,value in pairs(output.content) do
      if value.name == credentials.devicename then
        lon = value.location.longitude
        lat = value.location.latitude
        bat = value.batteryLevel * 100 / 1
        powerstateval = value.batteryStatus
        table.insert(commandArray,{['UpdateDevice'] = otherdevices_idx['Iphone battery ' .. user] .. '|0|' .. bat})
        distance = math.sqrt(((lon - homelongitude) * 111.320 * math.cos(math.rad(lat)))^2 + ((lat - homelatitude) * 110.547)^2)  -- approximation
        table.insert(commandArray,{['UpdateDevice'] = otherdevices_idx['Position ' .. user] .. '|0|' .. math.floor(distance*100+0.5)/100})
--      print('iPhone ' .. user .. ': ' .. math.floor(distance*100+0.5)/100 .. ' km from home')
        if distance < radius  then
          if otherdevices['Iphone ' .. user] == 'Off' then
            commandArray['Iphone ' .. user] = 'On'
--            table.insert(commandArray, {['SendNotification'] = 'Presence update#' .. user .. ' komt thuis'})
          end
        else
          if otherdevices['Iphone ' .. user] == 'On' then
            commandArray['Iphone ' .. user] = 'Off'
--            table.insert(commandArray, {['SendNotification'] = 'Presence update#' .. user .. ' gaat van huis'})
          end
        end
      end
    end
  end

end

return commandArray
Wako
Posts: 2
Joined: Friday 04 January 2019 15:08
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Find My iPhone implementation in LUA script

Post by Wako »

Hi All,

I receiving the following error with the script.

2019-01-04 14:27:01.459 Error: EventSystem: in /home/pi/domoticz/scripts/lua/script_time_CheckPhones.lua: /home/pi/domoticz/scripts/lua/script_time_CheckPhones.lua:51: attempt to index field 'location' (a nil value)

If I print(result) then every information is there and I'm also can get other value like deviceModel etc. But value.location.longitude and value.location.latitude are not working. Does anybody know why ?

all help is welcome I have tried all kinds of things
Hempie
Posts: 11
Joined: Wednesday 15 November 2017 17:22
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Aalsmeer
Contact:

Re: Find My iPhone implementation in LUA script

Post by Hempie »

For me the same error, weird that it's not working anymore! :?
RFXTRX433E, Aeon Z-Stick Gen 5, Harmony Hub, Nefit Easy, Homebridge, Somfy RTS, Fibaro
Wako
Posts: 2
Joined: Friday 04 January 2019 15:08
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Find My iPhone implementation in LUA script

Post by Wako »

Anyone any idea ??? why it is not work anymore
scheelings
Posts: 11
Joined: Thursday 20 April 2017 12:22
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Find My iPhone implementation in LUA script

Post by scheelings »

Hi,

At request https://fmipmobile.icloud.com/fmipservice/device/" .. credentials.username .."/initClient I get the following response:
{"desc":"default text from ResponseContentFilter"}; HTTP status code 330.
Usually a list of my devices in the JSON response should be returned.

Does anyone have an idea what the problem is?

Thx,
Danny
scheelings
Posts: 11
Joined: Thursday 20 April 2017 12:22
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Find My iPhone implementation in LUA script

Post by scheelings »

I just found it: change the base URL to https://p43-fmipmobile.icloud.com
hennemarc1
Posts: 2
Joined: Wednesday 06 March 2019 22:56
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Find My iPhone implementation in LUA script

Post by hennemarc1 »

Excellent !
Thank you scheelings !!!
hennemarc1
Posts: 2
Joined: Wednesday 06 March 2019 22:56
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Find My iPhone implementation in LUA script

Post by hennemarc1 »

scheelings wrote: Tuesday 05 March 2019 15:07 I just found it: change the base URL to https://p43-fmipmobile.icloud.com
Excellent !
Thank you scheelings !!!
Dellie
Posts: 10
Joined: Wednesday 03 April 2019 12:03
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.9
Location: Netherlands
Contact:

Re: Find My iPhone implementation in LUA script

Post by Dellie »

Could anyone please post his (working) script with the changed url (https://p43-fmipmobile.icloud.com)

Or do I just have to change rule 31 from stage2server = "fmipmobile.icloud.com" to stage2server = "p43-fmipmobile.icloud.com"

Thank you very much :D
scheelings
Posts: 11
Joined: Thursday 20 April 2017 12:22
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Find My iPhone implementation in LUA script

Post by scheelings »

I am not using this script so I am not sure, but when I look at the script it looks like you have to use this code:

Code: Select all

stage2server = string.match(result, "X%-Apple%-MMe%-Host%:%s(.*%.icloud%.com)")
--stage2server = "fmipmobile.icloud.com"
in stead of

Code: Select all

--stage2server = string.match(result, "X%-Apple%-MMe%-Host%:%s(.*%.icloud%.com)")
stage2server = "fmipmobile.icloud.com"
When this works, you do not have to use the p43-fmipmobile.icloud.com URL!
Dellie wrote: Wednesday 03 April 2019 12:06 Could anyone please post his (working) script with the changed url (https://p43-fmipmobile.icloud.com)

Or do I just have to change rule 31 from stage2server = "fmipmobile.icloud.com" to stage2server = "p43-fmipmobile.icloud.com"

Thank you very much :D
Seidel
Posts: 5
Joined: Monday 08 January 2018 12:56
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Find My iPhone implementation in LUA script

Post by Seidel »

I've an error since one week:

Code: Select all

Error: EventSystem: in iCloud iPhone: [string "-- Script to check the location of multiple i..."]:41: attempt to index a nil value (field 'location')
What is the problem ? I use p43-fmipmobile.icloud.com
K0enH
Posts: 32
Joined: Monday 20 March 2017 14:13
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Find My iPhone implementation in LUA script

Post by K0enH »

anybody a clue if apple recently changed something?

I Only manage to get this response from their servers:

Code: Select all

{"desc":"default text from ResponseContentFilter"}
I use this curl statement:

Code: Select all

curl -sk -X POST -L -u '[username]:[password]' -H 'Content-Type: application/json; charset=utf-8' -H 'X-Apple-Find-Api-Ver: 2.0' -H 'X-Apple-Authscheme: UserIdGuest' -H 'X-Apple-Realm-Support: 1.0' -H 'User-agent: Find iPhone/1.3 MeKit (iPad: iPhone OS/4.2.1)' -H 'X-Client-Name: iPad' -H 'X-Client-UUID: 0cf3dc501ff812adb0b202baed4f37274b210853' -H 'Accept-Language: en-us' -H 'Connection: keep-alive' https://p43-fmipmobile.icloud.com/fmipservice/device/[username]/initClient
K0enH
Posts: 32
Joined: Monday 20 March 2017 14:13
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Find My iPhone implementation in LUA script

Post by K0enH »

nobody? ;)
BobKooi
Posts: 4
Joined: Sunday 15 May 2022 13:10
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Netherlands
Contact:

Re: Find My iPhone implementation in LUA script

Post by BobKooi »

Hello, is this script still used by anyone?
-If so, what needs to be adjusted to get it working?
-If no, then perhaps delete this item entirely.
Is there perhaps a better alternative?
Thank you in advance! :D
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests