07.02.2019, 09:04
Here'se a scheduled script that forms a JSON-encoded string with object values and then pushes them to Azure, then disconnects:
Code:
require('json')
data = json.encode({
obj1 = grp.getvalue('32/1/1'),
obj2 = grp.getvalue('32/1/2'),
})
device_id = 'DEV_TEST'
broker = 'DEV.azure-devices.net'
port = 8883
username = 'DEV.azure-devices.net/DEV_TEST'
password = 'SharedAccessSignature ...'
mqtt = require('mosquitto')
client = mqtt.new(device_id)
running = true
client.ON_CONNECT = function(stat, code, err)
if stat then
client:publish('devices/' .. device_id .. '/messages/events/', data)
else
log('mqtt connect error', err, code)
end
client:disconnect()
running = false
end
client:version_set(mqtt.PROTOCOL_V311)
client:tls_set('/home/ftp/azure.crt')
client:login_set(username, password)
res, err = client:connect(broker, port)
if not res then
log('mqtt initial connect error', err)
running = false
end
if res then
while running do
res, err, code = client:loop()
if not res then
if running then
log('mqtt loop error', err, code)
end
break
end
end
else
log('mqtt connection failed', err)
end