Logic Machine Forum
HifiBerry API GET - 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: HifiBerry API GET (/showthread.php?tid=3359)



HifiBerry API GET - Kai-Roger - 10.05.2021

Hi.

Now that i have somewhat control over how to POST to the API, i'm having problems understanding how to use GET from the API. I have made a Resident script with 2 sec. sleep interval. The goal is to retrive volume % status on my HifiBerry amplifier, and write it to a group adress.

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

Volume_status                = grp.find        ('25/2/12')


--body = require('json').encode({
--  percent = tostring(event.getvalue())
--})

res, code = http.request({
  url = 'http://192.168.1.91:81/api/volume',
    method = 'GET',
headers = {
    ['Content-Length'] = #body,
    ['Content-Type'] = 'application/json',
  },
  source = ltn12.source.string(body)
})

Volume_status            :write(GET_Volume)

log(res, code)

The code above is a mess. Any ideas how to do this?


RE: HifiBerry API GET - admin - 10.05.2021

See if you can get a response using simple request command:
Code:
res, code = require('socket.http').request('http://192.168.1.91:81/api/volume')
log(res, code)

Otherwise you need a sink variable to store the response. Similar example: https://forum.logicmachine.net/showthread.php?tid=3342&pid=21631#pid21631


! - Kai-Roger - 10.05.2021

(10.05.2021, 12:01)admin Wrote: See if you can get a response using simple request command:
Code:
res, code = require('socket.http').request('http://192.168.1.91:81/api/volume')
log(res, code)

Otherwise you need a sink variable to store the response. Similar example: https://forum.logicmachine.net/showthread.php?tid=3342&pid=21631#pid21631

Yes i get a response. How do i retract the percent from this and write it to a group adress?

* arg: 1
  * string: {"percent": 47.0}
* arg: 2
  * number: 200



RE: HifiBerry API GET - admin - 10.05.2021

Code:
if res then
  res = require('json').pdecode(res)
  if type(res) == 'table' and res.volume then
    grp.checkupdate('1/2/3', res.volume)
  end
end



RE: HifiBerry API GET - Kai-Roger - 10.05.2021

(10.05.2021, 12:30)admin Wrote:
Code:
if res then
  res = require('json').pdecode(res)
  if type(res) == 'table' and res.volume then
    grp.checkupdate('1/2/3', res.volume)
  end
end

Thanks a lot. The code below works perfectly.

Code:
res, code = require('socket.http').request('http://192.168.1.91:81/api/volume')
--log(res, code)

if res then
  res = require('json').pdecode(res)
  if type(res) == 'table' and res.percent then
    grp.checkupdate('25/2/12', res.percent)
  end
end