Logic Machine Forum
NetAtmo Cameras into LM - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: NetAtmo Cameras into LM (/showthread.php?tid=2959)



NetAtmo Cameras into LM - Hyxion14 - 05.11.2020

Hello, I want to integrate several NetAtmo security cameras in LM, to read the events of the cameras, for example if I detect movement, turn on a light.

Has anyone been able to integrate NetAtmo cameras into LM?

Thanks a lot.


RE: NetAtmo Cameras into LM - admin - 09.11.2020

NetAtmo has webhook option that allows sending events via HTTP: https://dev.netatmo.com/apidocumentation/security#webhooks

On LM side you need to create a server-side .lp script that will handle events. Script should be uploaded via FTP using apps login to the user directory. If the file name is netatmo.lp then the HTTP path will be http://user:password@LM_IP/user/netatmo.lp
If username/password access does not work then you can disable password protection for user directory in User access settings.

Use this as a starting point:
Code:
<?

require('apps')
-- read POST data
body = ngx.req.get_body_data()

if body then
  -- decode data as JSON
  data = json.pdecode(body)

  -- check that data is a table
  if type(data) == 'table' then
    log(data)

    -- movement detected, you can check data.camera_id if you have multiple cameras
    if data.event_type == 'movement' then
      -- do something when movement happens
    end
  end
end



RE: NetAtmo Cameras into LM - Hyxion14 - 10.11.2020

Thanks a lot.

I will investigate it.


RE: NetAtmo Cameras into LM - pioneersteffen - 06.09.2021

Hi,

Have you’ve been successful to get the Netatmo security cams running? I’ve a Netatmo doorbell to implement in LM.

Many thanks in advance!

Best Regards
Steffen


RE: NetAtmo Cameras into LM - benanderson_475 - 16.03.2022

Hi All, 

i have this working in part, i am getting messages if i change some settings on the netatmo app (log as below) but i am not getting messages on event from the camera, any ideas- must be a netatmo setting?

* table:
["change"]
* string: home_updated
["home"]
* table:
["id"]
* string: xxxxxxxxxxxxxxxxxxxxxx
["user_id"]
* string: xxxxxxxxxxxxxxxxxxxxxxxxx
["push_type"]
* string: topology_changed
["user"]
* table:
["email"]
* string: xxxxxxxxxxxxxxxxxxxxxxx
["id"]
* string: xxxxxxxxxxxxxxxxxxxxx
["home_id"]
* string: xxxxxxxxxxxxxxxxxxxxx

Many Thanks


RE: NetAtmo Cameras into LM - admin - 16.03.2022

Most probably something is missing in the configuration on Netatmo side. LM is simply logging all received requests. It might be also possible that the JSON string is incorrect. Try logging the body variable contents:
Code:
<?

require('apps')
-- read POST data
body = ngx.req.get_body_data()
log(body)



RE: NetAtmo Cameras into LM - pioneersteffen - 16.03.2022

(16.03.2022, 00:41)benanderson_475 Wrote: Hi All, 

i have this working in part, i am getting messages if i change some settings on the netatmo app (log as below) but i am not getting messages on event from the camera, any ideas- must be a netatmo setting?

* table:
["change"]
  * string: home_updated
["home"]
  * table:
  ["id"]
    * string: xxxxxxxxxxxxxxxxxxxxxx
["user_id"]
  * string: xxxxxxxxxxxxxxxxxxxxxxxxx
["push_type"]
  * string: topology_changed
["user"]
  * table:
  ["email"]
    * string: xxxxxxxxxxxxxxxxxxxxxxx
  ["id"]
    * string: xxxxxxxxxxxxxxxxxxxxx
["home_id"]
  * string: xxxxxxxxxxxxxxxxxxxxx

Many Thanks

Hi,

would it be possible to share the script with us?

I like to get it working as well!

Many thanks for your help!

Best Regards 
Steffen


RE: NetAtmo Cameras into LM - admin - 16.03.2022

The script (.lp file) is here: https://forum.logicmachine.net/showthread.php?tid=2959&pid=19054#pid19054


RE: NetAtmo Cameras into LM - benanderson_475 - 17.03.2022

(16.03.2022, 09:24)admin Wrote: Most probably something is missing in the configuration on Netatmo side. LM is simply logging all received requests. It might be also possible that the JSON string is incorrect. Try logging the body variable contents:
Code:
<?

require('apps')
-- read POST data
body = ngx.req.get_body_data()
log(body)
Thanks, I added in the log(body), returns a string - same as decoded json
Looking into it, it seems this is broken on the Netatmo side, the camera I have is an outdoor camera “NOC” , it sends the smart events to the Netatmo app but not to the webhook… (but other events from the Netatmo web are sent to the webhook) just not the ones that are actually useful…  Huh
looks like it is a Netatmo firmware issue.
Has anyone else been successfully using this method? If so what camera/ and firmware version is being used?


RE: NetAtmo Cameras into LM - benanderson_475 - 29.03.2022

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