Posts: 13
Threads: 2
Joined: Nov 2015
Reputation:
1
Dear All,
I'm trying to retrieve consumption info from an "Ecocompteur" box from Legrand with the following code, but it does not work as expected.
The returned Content-Type is "text/plain" while it should be "application/json", so the json.decode function does not work.
Is there another way to retrieve data values from the returned "json" data ?
Thanks in advance
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Accessing "http://192.168.1.xx/inst.json" give this data in return :
{
"data1" :
27.000000 ,
"data2" :
0.000000 ,
"data3" :
0.000000 ,
"data4" :
0.000000 ,
"data5" :
0.000000 ,
"data6" :
0.000000 ,
"data6m3" :
0.000000 ,
"data7" :
0.000000 ,
"data7m3" :
0.000000 ,
"heure" :
22 ,
"minute" :
5
}
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
require 'ltn12'
require 'socket.http'
json =
require (
"json" )
local request_url =
"http://192.168.1.xx/inst.json"
local response_body = {}
local body ,
code ,
hdrs ,
stat =
socket.http.request
{
url =
request_url ;
sink =
ltn12.sink.table (
response_body );
}
log (
body ,
code ,
hdrs ,
stat )
log (
response_body )
...
Here is the log output :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
Ecocompteur 01.11.2017 20 :
33 :
54
*
arg :
1
*
number :
1
*
arg :
2
*
number :
200
*
arg :
3
*
table :
[
server ]
*
string :
lwip /
3.0
[
content-type ]
*
string :
text /
plain
*
arg :
4
*
string :
HTTP /
1.0 200 OK
Ecocompteur 01.11.2017 20 :
33 :
54
*
string : {
"data1" :
35.000000 ,
"data2" :
0.000000 ,
"data3" :
1.000000 ,
"data4" :
0.000000 ,
"data5" :
0.000000 ,
"data6" :
0.000000 ,
"data6m3" :
0.000000 ,
"data7" :
0.000000 ,
"data7m3" :
0.000000 ,
"heure" :
20 ,
"minute" :
33
}
HTTP /
1.0 200 OK
Server :
lwip /
3.0
Content-Type :
text /
plain
{
"data1" :
35.000000 ,
"data2" :
0.000000 ,
"data3" :
1.000000 ,
"data4" :
0.000000 ,
"data5" :
0.000000 ,
"data6" :
0.000000 ,
"data6m3" :
0.000000 ,
"data7" :
0.000000 ,
"data7m3" :
0.000000 ,
"heure" :
20 ,
"minute" :
33
}
Posts: 1793
Threads: 6
Joined: Jul 2015
Reputation:
120
01.11.2017, 21:50
(This post was last modified: 01.11.2017, 21:50 by Erwin van der Zwart .)
Hi,
Try this:
Code:
1 2 3 4 5 6
if type (
response_body ) ==
'table' then
data =
json.pdecode (
table.concat (
response_body ))
else
data =
json.pdecode (
response_body )
end
log (
data )
BR,
Erwin
Posts: 8071
Threads: 43
Joined: Jun 2015
Reputation:
470
Try running this then post both log entries. What it looks like is that the response is incorrect. JSON functions expect valid input which does not have "Content-Type" set anywhere so it's not the problem.
Code:
1 2 3 4 5 6 7 8
require (
'json' )
require (
'socket.http' )
url =
'http://192.168.1.xx/inst.json'
data =
socket.http.request (
url )
log (
data )
log (
json.pdecode (
data ))
Posts: 13
Threads: 2
Joined: Nov 2015
Reputation:
1
02.11.2017, 07:00
(This post was last modified: 02.11.2017, 07:34 by mooselight .)
(02.11.2017, 06:55) admin Wrote: Try running this then post both log entries. What it looks like is that the response is incorrect. JSON functions expect valid input which does not have "Content-Type" set anywhere so it's not the problem.
Code:
1 2 3 4 5 6 7 8
require (
'json' )
require (
'socket.http' )
url =
'http://192.168.1.xx/inst.json'
data =
socket.http.request (
url )
log (
data )
log (
json.pdecode (
data ))
Here is the log entries I got :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
Ecocompteur 02.11.2017 07 :
58 :
47
*
string : {
"data1" :
195.000000 ,
"data2" :
0.000000 ,
"data3" :
0.000000 ,
"data4" :
173.000000 ,
"data5" :
0.000000 ,
"data6" :
0.000000 ,
"data6m3" :
0.000000 ,
"data7" :
0.000000 ,
"data7m3" :
0.000000 ,
"heure" :
7 ,
"minute" :
58
}
HTTP /
1.0 200 OK
Server :
lwip /
3.0
Content-Type :
text /
plain
{
"data1" :
195.000000 ,
"data2" :
0.000000 ,
"data3" :
0.000000 ,
"data4" :
173.000000 ,
"data5" :
0.000000 ,
"data6" :
0.000000 ,
"data6m3" :
0.000000 ,
"data7" :
0.000000 ,
"data7m3" :
0.000000 ,
"heure" :
7 ,
"minute" :
58
}
Ecocompteur 02.11.2017 07 :
58 :
47
*
arg :
1
*
nil
*
arg :
2
*
string :
Expected the end but found invalid token at character 253
(I PM you more info)
Thx
(01.11.2017, 21:50) Erwin van der Zwart Wrote: Hi,
Try this:
Code:
1 2 3 4 5 6
if type (
response_body ) ==
'table' then
data =
json.pdecode (
table.concat (
response_body ))
else
data =
json.pdecode (
response_body )
end
log (
data )
BR,
Erwin
Thx Erwin, but it does not work.
Here is the log entry :
Ecocompteur 02.11.2017 08:33:27
* nil
Posts: 8071
Threads: 43
Joined: Jun 2015
Reputation:
470
Strange, I've tested it with your access details and it works correctly. Which firmware version are you running?
Posts: 8071
Threads: 43
Joined: Jun 2015
Reputation:
470
Can you also try running request like this?
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
sock =
require (
'socket' ).
tcp ()
sock :
settimeout (
5 )
res ,
err =
sock :
connect (
'192.168.1.xx' ,
80 )
if res then
sock :
send (
'GET /inst.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
Posts: 13
Threads: 2
Joined: Nov 2015
Reputation:
1
02.11.2017, 07:48
(This post was last modified: 02.11.2017, 08:08 by mooselight .)
(02.11.2017, 07:44) admin Wrote: Strange, I've tested it with your access details and it works correctly. Which firmware version are you running?
1Version 2: 20170620
It should be the last I think ?
I'll try again ..
(02.11.2017, 07:48) admin Wrote: Can you also try running request like this?
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
sock =
require (
'socket' ).
tcp ()
sock :
settimeout (
5 )
res ,
err =
sock :
connect (
'192.168.1.xx' ,
80 )
if res then
sock :
send (
'GET /inst.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
Thanks, it works perfectly !