22.11.2023, 13:17
(22.11.2023, 11:12)admin Wrote: For HTTP use this:
Code:require('json') require('ltn12') require('socket.http') body = json.encode({ temperature = 25 }) response = {} res, code = socket.http.request({ url = 'http://demo.thingsboard.io/api/v1/eoKkkZ6JL7UbmyOHGX9L/telemetry', method = 'POST', headers = { ['Content-Type'] = 'application/json', ['Content-Length'] = #body, }, source = ltn12.source.string(body), sink = ltn12.sink.table(response), }) if res and code == 200 then log('request ok') else log('request error', code, table.concat(response)) end
For MQTT search the forum, there are many examples of how to use it. The data must be encoded using JSON, same as for HTTP requests.
Thank you. How to pass a object value?
Code:
value = grp.getvalue('4/1/11')
body = json.encode({ temperature = value })That way doesn't work. What is correct syntax?