Logic Machine Forum
openDTU - JSON - http post - "no values found" - 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: openDTU - JSON - http post - "no values found" (/showthread.php?tid=5455)



openDTU - JSON - http post - "no values found" - cgn - 08.06.2024

Hi all,

I'm trying to integrate / control openDTU² into my W4k. I can already read the data from my openDTU. Now I'm trying to write data to my openDTU, which fails. Any idea?

Code:
json = require('json')
http = require('socket.http')

body = json.encode({
    limit_value = '10',
    limit_type = '1',
    serial = '000000000000'
})

resp, code, hdrs, stat = http.request({
  url = 'http://192.168.1.000/api/limit/config',
  method = 'POST',
  headers = {
    ['Authorization'] = 'Basic '..(mime.b64('admin:password')),
    ['Content-Type'] = 'application/json',
    ['Content-Length'] = #body,
  },
  body
})

log(resp, code, hdrs, stat)

I get the following response in the log:

Code:
* arg: 1
  * nil
* arg: 2
  * string: closed
* arg: 3
  * nil
* arg: 4
  * nil



In case it's done; I'll post the full code here. 

²https://www.opendtu.solar/firmware/web_api/#list-of-urls


RE: openDTU - JSON - http post - "no values found" - admin - 10.06.2024

Try this:
Code:
json = require('json')
ltn12 = require('ltn12')
http = require('socket.http')

body = json.encode({
  limit_value = '10',
  limit_type = '1',
  serial = '000000000000'
})

resp, code, hdrs, stat = http.request({
  url = 'http://192.168.1.000/api/limit/config',
  method = 'POST',
  headers = {
    ['Authorization'] = 'Basic '..(mime.b64('admin:password')),
    ['Content-Type'] = 'application/json',
    ['Content-Length'] = #body,
  },
  source = ltn12.source.string(body),
})

log(resp, code, hdrs, stat)



RE: openDTU - JSON - http post - "no values found" - cgn - 10.06.2024

This is something which I already tried. In this case I get the following log:

Code:
* arg: 1
  * string: {"type":"warning","message":"No values found!","code":1002}
* arg: 2
  * number: 200
* arg: 3
  * table:
   ["content-length"]
    * string: 59
   ["accept-ranges"]
    * string: none
   ["content-type"]
    * string: application/json
   ["connection"]
    * string: close
* arg: 4
  * string: HTTP/1.1 200 OK

I log "body" as well and get the following result:

 
Code:
* string: {"limit_value":"10","limit_type":"1","serial":"000000000000"}

I don't understand why the serial is still in the thrid position. I changed the code and expect the serial to be on first position. Why does this not happen?
Code:
body = json.encode({   
   serial = '000000000000',
    limit_type = '1',   
    limit_value = '10',
})


For a common understandig - although it's not shown hier in the code, I use the correct serial and IP.


RE: openDTU - JSON - http post - "no values found" - admin - 11.06.2024

Try this:
Code:
json = require('json')
ltn12 = require('ltn12')
http = require('socket.http')
url = require('socket.url')

body = json.encode({
  limit_value = '10',
  limit_type = '1',
  serial = '000000000000'
})

body = 'data=' .. url.escape(body)

resp, code, hdrs, stat = http.request({
  url = 'https://192.168.1.000/api/limit/config',
  method = 'POST',
  headers = {
    ['Authorization'] = 'Basic '..(mime.b64('admin:password')),
    ['Content-Length'] = #body,
  },
  source = ltn12.source.string(body),
})

log(resp, code, hdrs, stat)



RE: openDTU - JSON - http post - "no values found" - cgn - 13.06.2024

This didn't work either. Nevertheless I found a solution for setting the limit without using the json lib. 


Code:
ltn12 = require('ltn12')
http = require('socket.http')

body = 'data={"serial":"000000000000", "limit_type":"1", "limit_value":"'..limit..'"} '

res, code, headers, status = http.request({
  url = 'http://192.168.1.000/api/limit/config',
  method = 'POST',
  headers = {
    ['Authorization'] = 'Basic '..(mime.b64('admin:password')),
    ['Content-Type'] = 'application/x-www-form-urlencoded',
    ['Content-Length'] = #body,
  },
    source = ltn12.source.string(body),
})

log(res, code, headers, status)