27 September 2018

The MQTT command can be used to publish MQTT messages or subscribe to an MQTT topic given an MQTT server. The MQTT command has to be issued in order to be able to receive MQTT notifications. While publishing a message to an MQTT server is synchronous, messages received from the MQTT on the subscribed topic are asynchronous.

CommandRequired ParametersRequired Corrade PermissionsExample
MQTTgroup, password, actionbridge
// Subscribe to an MQTT server listening on
// 127.0.0.1 and port 1443 with "corrade"
// as username and "corrade" as password
// and to the topic "SIML" and additionally
// bind to the MQTT notification to be
// able to receive messages.
llInstantMessage(CORRADE,
    wasKeyValueEncode(
        [
            "command", "MQTT",
            "group", wasURLEscape(GROUP),
            "password", wasURLEscape(PASSWORD),
            // Subscribe to Corrade AI
            "action", "subscribe",
            // Corrade AI listening host.
            "host", "127.0.0.1",
            // Corrade AI listening port.
            "port", 1443,
            // Corrade AI credentials.
            "username", "corrade",
            "secret", "corrade",
            // Use the SIML module of Corrade AI.
            "topic", "SIML",
            // Send the result of the MQTT command to this URL.
            "callback", wasURLEscape(URL),
            // By adding an URL, Corrade will subscribe AND bind to the "MQTT" notification.
            "URL", wasURLEscape(URL)
        ]
    )
);
ParameterPossible ValueSub-ParameterPossible ValueDescriptionExample
actionpublishhostAn IP addressPublish a message to an MQTT server.
// The message to send - can also listen to local chat, for instance.
string text = "Hello!";
llInstantMessage(CORRADE,
    wasKeyValueEncode(
        [
            "command", "MQTT",
            "group", wasURLEscape(GROUP),
            "password", wasURLEscape(PASSWORD),
            "action", "publish",
            "host", "127.0.0.1",
            "port", 1443,
            "username", "corrade",
            "secret", "corrade",
            "topic", "SIML",
            // The payload, ie: wasURLEscape( "Hash=something&Message=Hello!" )
            // Note the double escaping that is necessary to prevent collisions!
            "payload", wasURLEscape(
                wasKeyValueEncode(
                    [
                        // The hash is an identifier that will allow responses from Corrade AI
                        // for various messages to be distinguished. It can be any identifier
                        // but a handy way of generating an identifier is to hash the message.
                        "Hash", llSHA1String(text),
                        // Note the double escaping!
                        "Message", wasURLEscape(text)
                    ]
                )
            ),
            "callback", wasURLEscape(URL)
        ]
    )
);
portA port number.
topicA topic to send the message to.
payloadA message payload.
usernameThe username and password to use for authentication to the MQTT server.
secret
typeSet to either text or binary.
QoSQuality of service level (defaults to 0).
retainWhether to retain the message (defaults to False).
subscribehostAn IP addressSubscribe to a topic from an MQTT server.
llInstantMessage(CORRADE,
    wasKeyValueEncode(
        [
            "command", "MQTT",
            "group", wasURLEscape(GROUP),
            "password", wasURLEscape(PASSWORD),
            // Subscribe...
            "action", "subscribe",
            // ... to an MQTT broker at 127.0.0.1
            "host", "127.0.0.1",
            // ... on port 1443
            "port", 1443,
            // ... using the following credentials
            "username", "corrade",
            "secret", "corrade",
            // ... to the SIML topic on the broker
            "topic", "SIML",
            // Send the result of the MQTT command to this URL.
            "callback", wasURLEscape(URL),
            // By adding an URL, Corrade will subscribe AND bind to the "MQTT" notification.
            "URL", wasURLEscape(URL)
        ]
    )
);
portA port number.
topicA topic to send the message to.
usernameThe username and password to use for authentication to the MQTT server.
secret
executeTrue or False (default, False)Whether to execute commands sent to the topic that Corrade subscribes to.
notificationsA list of notifications.Publish the requested notifications on the MQTT topic that Corrade subscribes to.
URLOptionally bind an MQTT notification for the subscription.
typeSet to either text or binary.
QoSQuality of service level (defaults to 0).
IDAn UUID used to track the binding (generated randomly by default).
unsubscribesessionAn MQTT subscription UUIDUnsubscribe from an MQTT server topic.
llInstantMessage(CORRADE,
    wasKeyValueEncode(
        [
            "command", "MQTT",
            "group", wasURLEscape(GROUP),
            "password", wasURLEscape(PASSWORD),
            "action", "unsubscribe",
            "session", "8384fd9d-1d86-4991-a925-3dc2cf22f06c",
            // Also remove the notification URL.
            "URL", wasURLEscape(URL)
        ]
    )
);
URLOptionally remove the MQTT notification.
listList all MQTT subscriptions for the calling group
llInstantMessage(CORRADE,
    wasKeyValueEncode(
        [
            "command", "MQTT",
            "group", wasURLEscape(GROUP),
            "password", wasURLEscape(PASSWORD),
            "action", "list",
            "callback", wasURLEscape(URL)
        ]
    )
);

Notes