16.08.2015, 10:59
(13.07.2015, 21:43)Matt Wrote: Updated version of Netatmo script with HTTPS (http is not allowed by netatmo anymore)
Code:require 'ltn12'
require 'socket.http'
json = require("json")
https = require 'ssl.https'
function refresh_netatmo()
local netatmo_debug = false
local client_id = ""
local client_secret = ""
local username = ""
local password = ""
local scope = "read_station"
-- optional
--local device_id = ""
local request_token_url = "https://api.netatmo.net/oauth2/token"
local request_device_list = "https://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 .. "&scope=" .. scope
local body, code, hdrs, stat = https.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")
error("netatmo auth error : "..tostring(code)..","..tostring(body))
return
end
local response_decode = json.decode(table.concat(response_body))
local access_token = response_decode.access_token
if (netatmo_debug) then
alert("netatmo token %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 = https.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
error("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
Matthieu
Thx Matt