29.03.2022, 04:53
(This post was last modified: 29.03.2022, 10:17 by benanderson_475.)
Hi,
I Thought i would update this post as it wasn't straight forward to get this working,
After i had uploaded by ftp to the LM the file netatmo.lp to apps,
I created an app on netatmo dev platform (https://dev.netatmo.com/apps/) and entered my Webhook url as
http://mydomain/user/netatmo.lp (this is redirected through my modem to the lan ip of the lm) i also take parameters client ID and client secret for later on
Firstly we need to pass username and password with the client id and secret with function and authorize lm use to this app we created on netatmo dev,
call the function Get_Token() once, this authorizes the lm to use the netatmo app
(as per netatmo doc, although this wasn't really clear that this was required in order to use webhooks etc)
The script writes the token, refresh token and expiry time in seconds to 3 different virtual object data type 250byte string
Then run the scheduled script to refresh the token, call function Refresh_token() once an hour.
now I receive all Smart events from the netatmo camera in the log from the netatmo.lp script
user library as below, change/add all credentials and virtual obj to suit
I Thought i would update this post as it wasn't straight forward to get this working,
After i had uploaded by ftp to the LM the file netatmo.lp to apps,
I created an app on netatmo dev platform (https://dev.netatmo.com/apps/) and entered my Webhook url as
http://mydomain/user/netatmo.lp (this is redirected through my modem to the lan ip of the lm) i also take parameters client ID and client secret for later on
Firstly we need to pass username and password with the client id and secret with function and authorize lm use to this app we created on netatmo dev,
call the function Get_Token() once, this authorizes the lm to use the netatmo app
(as per netatmo doc, although this wasn't really clear that this was required in order to use webhooks etc)
The script writes the token, refresh token and expiry time in seconds to 3 different virtual object data type 250byte string
Then run the scheduled script to refresh the token, call function Refresh_token() once an hour.
now I receive all Smart events from the netatmo camera in the log from the netatmo.lp script
user library as below, change/add all credentials and virtual obj to suit
Code:
require 'ltn12'
require 'socket.http'
json = require("json")
https = require 'ssl.https'
local client_id = "xxxxxxxxxxxxxxxxxxxxx"
local client_secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx"
local scope = "read_presence read_camera" --Welcome camera use read_camera, Presence camera use read_presence
local username = "xxxxxxxxxxxxx"
local password = "xxxxxxxxxxxxxxxxxxx"
--Client credentials grant type
grant_acess_data = "grant_type=password&client_id=" .. client_id .."&client_secret=" .. client_secret .. "&username=" .. username .. "&password=" .. password .. "&scope=" .. scope
request_oauth_url = "https://api.netatmo.net/oauth2/token"
function Netatmo_Post(url, body, headers) --headers = {}
res_body ={}
local body, code, hdrs, stat = https.request
{
url = url;
method = "POST",
headers = headers,
source = ltn12.source.string(body);
sink = ltn12.sink.table(res_body);
}
if (code ~= 200) then
log("netatmo post error : "..tostring(code)..","..tostring(body))
return
end
return json.pdecode(res_body[1])
end
function Get_Token()
res = Netatmo_Post(request_oauth_url, grant_acess_data, {["Content-Type"] = "application/x-www-form-urlencoded", ["Content-Length"] = #grant_acess_data, })
--log(res)
if res then
grp.write('32/1/1', res.refresh_token)
grp.write('32/1/2', res.access_token)
grp.write('32/1/3', res.expires_in)
else
log('Netatmo token request failed')
end
end
function Refresh_token()
refresh_acess_data = "grant_type=refresh_token&refresh_token="..grp.getvalue('32/1/1').."&client_id=" .. client_id .."&client_secret=" .. client_secret
res = Netatmo_Post(request_oauth_url, refresh_acess_data , {["Content-Type"] = "application/x-www-form-urlencoded", ["Content-Length"] = #refresh_acess_data, })
log(res)
if res then
grp.write('32/1/1', res.refresh_token)
grp.write('32/1/2', res.access_token)
grp.write('32/1/3', res.expires_in)
else
log('Netatmo token refresh failed')
end
end