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.

Data from http request - to JSON - to KNX
#1
Hi!

I need a little help with this:

I now got a energy-gateway that can send a JSON-file when requested. The way to do it, is type "http://10.0.0.xxx/output.json" in the web browser.

I then get a file called "output.json" and the string is like this:

{"0015BC001B024257":{"ApplicationHeartbeat":"1","CurrentSummationDelivered":"12812.1","CurrentSummationReceived":"0.0","Tamper":"0","InstantaneousDemand":"4342.0","Battery":"0"}}

The information I need is CurrentSummationDelivered and InstantaneousDemand and the goal is to write this to two KNX groups with 30 seconds intervals.

Any tips where to start? Thanks from a LM5-newbie.  Smile

Attached Files
.json   output.json (Size: 178 bytes / Downloads: 13)
Reply
#2
Hi,

Try something like this:
Code:
require('socket.http')
require('json')

socket.http.TIMEOUT = 15

data, code = socket.http.request('http://10.0.0.xxx/output.json')

if data then
 data = json.pdecode(data)
end

if data then
 CurrentSummationDelivered = data['0015BC001B024257'].CurrentSummationDelivered
 InstantaneousDemand = data['0015BC001B024257'].InstantaneousDemand
 grp.update('1/1/1', tonumber(CurrentSummationDelivered))
 grp.update('1/1/2', tonumber(InstantaneousDemand))
end
BR,

Erwin
Reply
#3
(21.03.2018, 18:21)Erwin van der Zwart Wrote: Hi,

Try something like this:
Code:
require('socket.http')
require('json')

socket.http.TIMEOUT = 15

data, code = socket.http.request('http://10.0.0.xxx/output.json')

if data then
 data = json.pdecode(data)
end

if data then
 CurrentSummationDelivered = data['0015BC001B024257'].CurrentSummationDelivered
 InstantaneousDemand = data['0015BC001B024257'].InstantaneousDemand
 grp.update('1/1/1', tonumber(CurrentSummationDelivered))
 grp.update('1/1/2', tonumber(InstantaneousDemand))
end
BR,

Erwin

Something tells me that I am forgetting something? No action on the KNX-bus. (Picture attached)

Attached Files Thumbnail(s)
   
Reply
#4
Add log(data, code) after http request and log(data) at the end of the script, then check Logs tab for results. Do you have objects 4/0/20 and 4/0/21 created in LM?
Reply
#5

Here is the solution, thank to you that know who you are. Wink

And thanks for all the help also! Smile

Code:
sock = require('socket').tcp()
sock:settimeout(5)

res, err = sock:connect('10.0.0.112', 80)
if res then
 sock:send('GET /output.json HTTP/1.1\r\n\r\n')
 res = sock:receive('*a')
 log(res)

 if res then
   p, e = res:find('\r\n\r\n', 1, true)
   if e then
     data = res:sub(e + 1)
     data = require('json').pdecode(data)
     log(data)
   end
 end
end

if data then
 CurrentSummationDelivered = data['0015BC001B024257'].CurrentSummationDelivered
 InstantaneousDemand = data['0015BC001B024257'].InstantaneousDemand
 grp.write('4/0/20', tonumber(CurrentSummationDelivered))
 grp.write('4/0/21', tonumber(InstantaneousDemand))
end
Reply


Forum Jump: