23.04.2018, 07:12
(This post was last modified: 26.04.2018, 06:24 by gjniewenhuijse.)
Anyone already linked a Gardena Smart gateway?
Here my first code to read a Sensor and write the moisturelevel and batterylevel.
Here my first code to read a Sensor and write the moisturelevel and batterylevel.
Code:
function getData()
https = require 'ssl.https'
require('json')
socket.http.TIMEOUT = 5
local cUsername = 'email@abc.com'
local cPassword = 'password'
-- get session info
local cBody = '{"sessions":{"email":"' .. cUsername .. '","password":"' .. cPassword ..'"}}'
local cReq = {}
local cUrl = 'https://sg-api.dss.husqvarnagroup.net/sg-1/sessions'
result1 = https.request({
url = cUrl,
method = 'POST',
headers = {
['content-length'] = #cBody,
['content-type'] = 'application/json'
},
source = ltn12.source.string(cBody),
sink = ltn12.sink.table(cReq)
})
if result1 then
--log(cReq)
cReq = table.concat(cReq)
cReq = json.pdecode(cReq)
if cReq then
--log(cReq)
token = cReq.sessions.token
user_id = cReq.sessions.user_id
end
end
-- get location info
local cUrl = 'https://sg-api.dss.husqvarnagroup.net/sg-1/locations/?user_id=' .. user_id
result2 = https.request({
url = cUrl,
method = 'GET',
headers = {
['content-type'] = 'application/json',
['X-Session'] = token
},
sink = ltn12.sink.table(cReq)
})
if result2 then
--log(cReq)
cReq = table.concat(cReq)
cReq = json.pdecode(cReq)
if cReq then
--log(cReq)
local loc_name = cReq.locations[1].name
loc_id = cReq.locations[1].id
end
end
-- get device info
local cUrl = 'https://sg-api.dss.husqvarnagroup.net/sg-1/devices?locationId=' .. loc_id
result3 = https.request({
url = cUrl,
method = 'GET',
headers = {
['content-type'] = 'application/json',
['X-Session'] = token
},
sink = ltn12.sink.table(cReq)
})
if result3 then
--log(cReq)
cReq = table.concat(cReq)
cReq = json.pdecode(cReq)
if cReq then
--log(cReq)
local dName = cReq.devices[1].name
for k,v in pairs(cReq.devices) do
--log(v)
--log("Found the following GARDENA "..v.category)
--log("Name: "..v.name)
--log("Identifier: "..v.id)
-- gateway
if (v.category == "gateway") then
-- sensor
elseif (v.category == "sensor") then
for a, b in pairs(v.abilities) do
for c, d in pairs(b.properties) do
--log(d.name)
--log(d.value)
if (d.name == 'last_time_online') then
--check = tostring(d.value)
elseif (d.name == 'humidity') then
grp.write('1/1/1', tonumber(d.value))
elseif (d.name == 'level') then
grp.write('1/1/2', tonumber(d.value))
end
end
end
-- onbekend apparaat
else
log('onbekend gardena apparaat: '..v.category)
end
end
end
end
end