This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

sending data to thingsboard.io
#1
hi, I would use the platform Thingsboard.io to send and collect value data from LM objects.

they allows to do it by 2 ways: 

HTTP request ( es.  curl -v -X POST http://demo.thingsboard.io/api/v1/eoKkkZ.../telemetry --header Content-Type:application/json --data "{temperature:25}")    or 

MQTT (es. curl -v -X POST http://demo.thingsboard.io/api/v1/eoKkkZ.../telemetry --header Content-Type:application/json --data "{temperature:25}")

how could I implement everything on LM?

Thanks in advance

Peppe
Reply
#2
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.
Reply
#3
(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?
Reply
#4
The syntax is correct, for event scripts use event.getvalue() instead of grp.getvalue().
What kind of error are you getting?
Reply
#5
(22.11.2023, 13:38)admin Wrote: The syntax is correct, for event scripts use event.getvalue() instead of grp.getvalue().
What kind of error are you getting?

No errors, sorry. It's all right now.

Thanks

Peppe
Reply
#6
(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.

I am trying to provision an object in mqtt. Thingsboard says me to execute the command:

Code:
mosquitto_pub -d -q 1 -h demo.thingsboard.io -p 1883 -t v1/devices/me/telemetry -u "gximaxia" -P "12345678" -m "{temperature:25}"
How I can arrange it via script?

I need send data of dozen of knx objects, how I can do it without give different username and password? What is the short/best way to do it?
Thanks

Peppe
Reply
#7
See this example: https://kb.logicmachine.net/integration/mqtt-client/
Reply


Forum Jump: