Skip to content

MQTT Client Service

Summary

This service allows solution to act as a MQTT client, so that you can communicate with a 3rd-party MQTT broker, eg: AWS IoT.

Operations

Name Tag Summary
mqtt.publish() Publish messages to MQTT topics

Events

Name Summary
message Message received from MQTT broker

Configuration parameters

Name Type Description
host string Host address of MQTT broker
port integer Port of MQTT broker (Note: only support SSL)
Default: 8883
security object, null MQTT broker security settings
security.password string(password) Password used to login to MQTT broker
security.username string Username used to login to MQTT broker
security.sslOptions object SSL options
security.sslOptions.key string Private key pem
security.sslOptions.cert string Certificate pem
security.sslOptions.cacerts string Root CA Certificate
topics [ string ] Default topics to subscribe
advanced object, null Advanced settings
advanced.clientid string The clientid used when connecting to MQTT broker. By default, it is solution_id.

Operations

publish

Description

Publish message to MQTT topic, need to specify topic and message

Arguments

Name Type Description
messages [ object ] Topics and messages to be published
messages[].topic string A topic
messages[].message string A message to send to the topic

Responses

  • Returns nil for Published

Example

local res = Mqtt.publish({messages={ {topic="topic1", message="hello"} } })
return res

Events

message

Description

After subscribe to a topic, the message from that topic will be received here.

Arguments

Name Type Description
topic string Topic of this message
payload string Message content in json string
timestamp integer Timestamp in milliseconds

Example

-- Use `event` to get the payload
local len = table.getn(batch.messages)
print("Received " .. len .. " messages")
for i=1,len do
  print("Message #" .. i .. ": topic is " .. batch.messages[i].topic)
  -- print("Message #" .. i .. ": payload is " .. batch.messages[i].payload)
  -- print("Message #" .. i .. ": timestamp is " .. batch.messages[i].timestamp)
end