Jump to content
  • 0

[Solved] Is it possible to import an JSON string into a virtual device?


Question

I get the following JSON string from my device in a browser by typing the IP of my device:

 {"data":{ "id":"8200007A","type":"8","detector":"SI29BG","voltage":382,"cpm":14,"temperature":9.45,"humidity":48.29,"pressure":100281,"voc":12800000,"co2":399,"noise":39,"ch2o":0.00,"pm25":3,"uptime": 107787}}

 

is it possible to import these values into a virtual device in HC2?

 

 

The closest I have found is:

local urad = Net.FHttp("192.168.8.106/j")
local urad = Net.FHttp("{{192.168.8.106/j}}",80)

local urad = assert(io.popen('curl http://192.168.8.106/j'))

 

but these will not get the json string

 

 

 

This is a part question for my thread:

 

Edited by Teza
Solved
Link to post
Share on other sites

5 answers to this question

Recommended Posts

  • 0

Hi @Teza,

 

Try this code:

 

local device = Net.FHttp("192.168.8.106", 80)
local response, status, _error = device:GET("/j", '{"true"}')

fibaro:debug("response: "..json.encode(response))
fibaro:debug("status: "..status)
fibaro:debug("error: ".._error)

 

If IP address and TCP ports are correct and also path "/j" then you should get answer in 'response'. If you get nothing then status and error should give you a clue what is wrong. If request is correct then status should be 200 and _error = 0

Link to post
Share on other sites
  • 0

Thank you for replying!

 

Skjermbilde1.PNG.24163c771c9a9a3da1127dd073e9b98d.PNG

Skjermbilde2.PNG.054debe05e57c742637f20955b1d766b.PNG

 

 

Skjermbilde3.PNG.0d97f4abcf0ae9c661de3c45fac15c46.PNG

 

 

Have also tried with only the IP in the VD setup.

 

 

 

This is what I get when typing the IP192.168.8.106  in a browser:

 

uRADMonitor 8200007A
type:8 hw:105 sw:126 SI29BG


radiation:14CPM
temperature:6.10C
pressure:100307Pa
humidity:52.37RH
VOC:11434000
noise:40dBA
CH2O:0.01ppm
PM2.5:3ug/m^3
CO2:422ppm
voltage:382V
duty:18%
frequency:15.50kHz
uptime:192094s wdt:26s/350s

ip:192.168.8.106
status:connected
interval:60 http:200 3035/3200


JSON WIFI [376]

 

 

 

and by typing the path http://192.168.8.106/j:

 

{"data":{ "id":"8200007A","type":"8","detector":"SI29BG","voltage":381,"cpm":7,"temperature":6.13,"humidity":52.79,"pressure":100303,"voc":11539041,"co2":429,"noise":39,"ch2o":0.01,"pm25":3,"uptime": 192537}}

 

 

Edited by Teza
Link to post
Share on other sites
  • 0

I have a HC2 getting json files from a raspberry PI.

 

In a VD I have this code in main part of the VD

MyId = fibaro:getSelfId()

local PI1 = Net.FHttp("172.17.1.205", 80);
res, status, errorCode = PI1:GET("/test.json");
-- rootDir voor webservice is /var/www/html

fibaro:debug ("status "..status.."; errorCode ".. errorCode.."; res ".. res);
arrData = json.decode(res);
-- de te decoderen data moet wel in json format zijn anders krijg je een vreemde foutmelding

fibaro:debug ("SupplyAirTemp: ".. arrData.supplyAirTemp)

 

Link to post
Share on other sites
  • 0

@robd Amazing, thank you very much!

 

By slightly changing the code to: 

MyId = fibaro:getSelfId()

local PI1 = Net.FHttp("192.168.8.106", 80);
res, status, errorCode = PI1:GET("/j");
-- rootDir voor webservice is /var/www/html

fibaro:debug ("status "..status.."; errorCode ".. errorCode.."; res ".. res);
arrData = json.decode(res);
-- de te decoderen data moet wel in json format zijn anders krijg je een vreemde foutmelding

fibaro:debug ("SupplyAirTemp: ".. arrData.supplyAirTemp)

It now return the following in the debug window:

 

[DEBUG] 14:32:38: status 200; errorCode 0; res {"data":{ "id":"8200007A","type":"8","detector":"SI29BG","voltage":379,"cpm":24,"temperature":19.64,"humidity":45.75,"pressure":100902,"voc":11987133,"co2":505,"noise":43,"ch2o":0.02,"pm25":3,"uptime": 63678}}
[ERROR] 14:32:38: line 19: attempt to concatenate field 'supplyAirTemp' (a nil value)
 
 
This is a huge step forward for me.
I can now focus on how to be able to use these values in HC2.
 
 
Link to post
Share on other sites
  • 0

Solved

 

--[[
%% autostart
%% properties
%% events
%% globals
124 value
--]]

MyId = fibaro:getSelfId()
fibaro:debug(MyId)

local uRad = Net.FHttp("192.168.60.150", 80)
local res, status, errorCode = uRad:GET("/j")
fibaro:debug("status "..status.."; errorCode ".. errorCode.."; res ".. res)

local jsonTable = json.decode(res)
fibaro:debug(jsonTable)

fibaro:setGlobal("uRad_temperature", jsonTable.data.temperature) -- Sensor 1: Temperature
fibaro:setGlobal("uRad_humidity", jsonTable.data.humidity) -- Sensor 2: Humidity
fibaro:setGlobal("uRad_pressure", jsonTable.data.pressure) -- Sensor 3: Pressure
fibaro:setGlobal("uRad_voc", jsonTable.data.voc) -- Sensor 4: VOC (Volatile Organic Compounds)
fibaro:setGlobal("uRad_pm25", jsonTable.data.pm25) -- Sensor 5: PM2.5 (Particulate matter)
fibaro:setGlobal("uRad_ch2o", jsonTable.data.ch2o) -- Sensor 6: Formaldehyde
fibaro:setGlobal("uRad_co2", jsonTable.data.co2) -- Sensor 7: Carbon Dioxide
fibaro:setGlobal("uRad_cpm", jsonTable.data.cpm) -- Sensor 8: Radiation
fibaro:setGlobal("uRad_noise", jsonTable.data.noise) -- Sensor 9: Noise
fibaro:sleep(60*1000)

 

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...