Netatmo weather station integration with LM
Example: Netatmo weather station integration with LogicMachine
Task
How to get my Netatmo weather station readings into my KNX network using LogicMachine?
Things to prepare in advance
- Buy Netatmo weather station
- Create a private app at http://dev.netatmo.com
Netatmo function
- Add the following function in Common Functions or User Library
- Fill in the script with client_id, client_secret, username and password
- require 'ltn12'
- require 'socket.http'
- json = require("json")
-
- function refresh_netatmo()
-
- local netatmo_debug = false
- local client_id = ""
- local client_secret = ""
- local username = ""
- local password = ""
- -- optional
- --local device_id = ""
- local request_token_url = "http://api.netatmo.net/oauth2/token"
- local request_device_list = "http://api.netatmo.net/api/devicelist"
- local response_body = { }
-
-
- if (netatmo_debug) then
- alert("netatmo start")
- end
-
- local request_body = "grant_type=password&client_id=" .. client_id .."&client_secret=" .. client_secret .. "&username=" .. username .. "&password=" .. password
-
- local body, code, hdrs, stat = socket.http.request
- {
- url = request_token_url;
- method = "POST";
- headers =
- {
- ["Content-Type"] = "application/x-www-form-urlencoded";
- ["Content-Length"] = #request_body;
- };
- source = ltn12.source.string(request_body);
- sink = ltn12.sink.table(response_body);
- }
-
- if (code ~= 200) then
- alert("netatmo auth error")
- return
- end
-
- local response_decode = json.decode(table.concat(response_body))
- local access_token = response_decode.access_token
- if (netatmo_debug) then
- alert("netatmo tocken %s",access_token)
- end
-
-
- local request_body = "access_token=" .. access_token .. "&app_type=app_station"
- -- to limit to only one device :
- -- local request_body = "access_token=" .. access_token .. "&app_type=app_station&device_id=" .. device_id
-
- local response_body = { }
-
- local body, code, hdrs, stat = socket.http.request
- {
- url = request_device_list;
- method = "POST";
- headers =
- {
- ["Content-Type"] = "application/x-www-form-urlencoded";
- ["Content-Length"] = #request_body;
- };
- source = ltn12.source.string(request_body);
- sink = ltn12.sink.table(response_body);
- }
-
- if (code ~= 200) then
- alert("netatmo devicelist error")
- return
- end
-
- local response_decode = json.decode(table.concat(response_body))
-
- -- Module int
- if (netatmo_debug) then
- alert("netatmo device 1")
- end
-
- grp.write("Netatmo Température intérieure",response_decode.body.devices[1].dashboard_data.Temperature,dt.float16)
- grp.write("Netatmo Humidité intérieure",response_decode.body.devices[1].dashboard_data.Humidity,dt.scale)
- grp.write("Netatmo Pression",response_decode.body.devices[1].dashboard_data.Pressure,dt.uint16)
- grp.write("Netatmo CO2",response_decode.body.devices[1].dashboard_data.CO2,dt.uint16)
- grp.write("Netatmo Sonomètre",response_decode.body.devices[1].dashboard_data.noise,dt.uint8)
- grp.write("Netatmo Température intérieure minimum",response_decode.body.devices[1].dashboard_data.min_temp,dt.float16)
- grp.write("Netatmo Température intérieure maximum",response_decode.body.devices[1].dashboard_data.max_temp,dt.float16)
- grp.write("Netatmo Pression absolue",response_decode.body.devices[1].dashboard_data.AbsolutePressure,dt.uint16)
-
- -- Module ext
- if (netatmo_debug) then
- alert("netatmo module 1")
- end
-
- grp.write("Netatmo Température extérieure",response_decode.body.modules[1].dashboard_data.Temperature,dt.float16)
- grp.write("Netatmo Humidité extérieure",response_decode.body.modules[1].dashboard_data.Humidity,dt.float16)
- grp.write("Netatmo Température extérieure minimum",response_decode.body.modules[1].dashboard_data.min_temp,dt.float16)
- grp.write("Netatmo Température extérieure maximum",response_decode.body.modules[1].dashboard_data.max_temp,dt.float16)
-
- -- Module Pluvio
- if (netatmo_debug) then
- alert("netatmo module 2")
- end
-
- grp.write("Netatmo Pluviomètre",response_decode.body.modules[2].dashboard_data.Rain,dt.float16)
- grp.write("Netatmo Pluviomètre 1h",response_decode.body.modules[2].dashboard_data.sum_rain_1,dt.float16)
- grp.write("Netatmo Pluviomètre 24h",response_decode.body.modules[2].dashboard_data.sum_rain_24,dt.float16)
-
- if (netatmo_debug) then
- alert("netatmo end")
- end
-
- end
Use the function in your programs
Either in Event-based or Resident script run the function by command refresh_netatmo().
Created by Matthieu Bouthors