This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

NetAtmo Cameras into LM
#10
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 


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
Reply


Messages In This Thread
NetAtmo Cameras into LM - by Hyxion14 - 05.11.2020, 14:51
RE: NetAtmo Cameras into LM - by admin - 09.11.2020, 07:42
RE: NetAtmo Cameras into LM - by Hyxion14 - 10.11.2020, 08:49
RE: NetAtmo Cameras into LM - by admin - 16.03.2022, 09:24
RE: NetAtmo Cameras into LM - by benanderson_475 - 29.03.2022, 04:53
RE: NetAtmo Cameras into LM - by admin - 16.03.2022, 12:56

Forum Jump: