Posts: 15
Threads: 8
Joined: Nov 2023
Reputation:
0
08.06.2024, 10:12
(This post was last modified: 09.06.2024, 08:51 by cgn.)
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
Posts: 8402
Threads: 45
Joined: Jun 2015
Reputation:
481
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)
Posts: 15
Threads: 8
Joined: Nov 2023
Reputation:
0
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.
Posts: 8402
Threads: 45
Joined: Jun 2015
Reputation:
481
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)
Posts: 15
Threads: 8
Joined: Nov 2023
Reputation:
0
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)
|