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.

Azure IoT Hub - MQTT
#13
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
Reply


Messages In This Thread
Azure IoT Hub - MQTT - by FatMax - 17.12.2018, 23:00
RE: Azure IoT Hub - MQTT - by FatMax - 21.12.2018, 07:52
RE: Azure IoT Hub - MQTT - by admin - 21.12.2018, 08:26
RE: Azure IoT Hub - MQTT - by FatMax - 10.01.2019, 08:26
RE: Azure IoT Hub - MQTT - by admin - 10.01.2019, 09:55
RE: Azure IoT Hub - MQTT - by Sandstrand - 21.05.2019, 09:15
RE: Azure IoT Hub - MQTT - by FatMax - 03.07.2019, 11:47
RE: Azure IoT Hub - MQTT - by FatMax - 10.01.2019, 11:01
RE: Azure IoT Hub - MQTT - by FatMax - 11.01.2019, 18:26
RE: Azure IoT Hub - MQTT - by Duvan2127 - 04.02.2019, 14:10
RE: Azure IoT Hub - MQTT - by admin - 04.02.2019, 14:50
RE: Azure IoT Hub - MQTT - by Duvan2127 - 04.02.2019, 16:00
RE: Azure IoT Hub - MQTT - by admin - 05.02.2019, 07:38
RE: Azure IoT Hub - MQTT - by Duvan2127 - 05.02.2019, 14:35
RE: Azure IoT Hub - MQTT - by admin - 07.02.2019, 09:04
RE: Azure IoT Hub - MQTT - by Duvan2127 - 15.02.2019, 22:20
RE: Azure IoT Hub - MQTT - by admin - 16.02.2019, 07:21
RE: Azure IoT Hub - MQTT - by edgars - 26.02.2019, 09:18
RE: Azure IoT Hub - MQTT - by FatMax - 01.05.2019, 21:26
RE: Azure IoT Hub - MQTT - by admin - 02.05.2019, 06:46
RE: Azure IoT Hub - MQTT - by FatMax - 02.05.2019, 07:22
RE: Azure IoT Hub - MQTT - by admin - 21.05.2019, 09:27
RE: Azure IoT Hub - MQTT - by admin - 03.07.2019, 12:33
RE: Azure IoT Hub - MQTT - by FatMax - 03.07.2019, 13:06
RE: Azure IoT Hub - MQTT - by admin - 03.07.2019, 13:18
RE: Azure IoT Hub - MQTT - by FatMax - 03.07.2019, 17:48
RE: Azure IoT Hub - MQTT - by admin - 04.07.2019, 06:04
RE: Azure IoT Hub - MQTT - by admin - 04.07.2019, 15:17

Forum Jump: