MQTT Publish

Description

The MQTT Publish Action Component allows you to publish messages to topics within an MQTT PubSub system.

Output PayloadExplanation
cv.PayloadOutputs the output payload of the previous component as is. (Pass through)

Component Properties

NameRequiredDescription
TopicTrueMQTT Topic
ClientIdTrueMQTT ClientID
QoSTrueQoS value
RetainTrueRetain flag
TimeoutTrueTimeout (ms)
MQTT ConnectionFalseMQTT Connection Settings from the Base Property Profile
URLTrueURL, note, leave the protocol out for unencrypted connections (e.g. test.mosquitto.org) but add ssl:// for encrypted connections
PortTruePort
UsernameFalseUsername
PasswordFalsePassword
CertificateFalseCertificate
Private KeyFalsePrivate Key
Private Key PassphraseFalsePrivate Key Passphrase
Root CAFalseRoot CA

test.mosquitto.org Example

You can easily test the MQTT function by using the free public "test broker provided by mosquito.org":http://test.mosquitto.org. A sample setup could be that you use the free "MQTTX":https://mqttx.app/ client alongside with Gravio using the following details:

Topictesttopic/gravio
Client IDChoose something Unique for Gravio and something different for MQTTX
QoS0
Timeout60000
URLtest.mosquitto.org
Port1883
Username_blank_
Password_blank_
Certificate_blank_
Private Key_blank_
Private Key Passphrase_blank_
Root CA_blank_

Then set the contents to cv.Payload = {"msg": "your test message"}

Triggering the action from Gravio Studio:

You should see something similar to the following in MQTTX:

Note: Ensure that both, MQTTX and Gravio have different Client IDs as the MQTT broker will disconnect the clients if they share the same ID.

Google Cloud MQTT Example

Here is an example of how to publish messages to a Google Cloud MQTT System:

Navigate to your Google Cloud console and create a project:

actioncomponent-mqtt-google-create-project

actioncomponent-mqtt-google-create-project-2

Navigate to Google Cloud Platform IoT Core

actioncomponent-mqtt-google-iot-platform

Enable the Google IoT Core API.

Create a Device Registry

actioncomponent-mqtt-google-iot-core-device-registries

Enter the name of your registry, select the region and add the topics (here events and states for example):

actioncomponent-mqtt-google-create-registry

Click on "Add Device" in this screen:

actioncomponent-mqtt-google-add-device

And you will see:

actioncomponent-mqtt-google-device-settings

Create a certificate that you can paste into the field by entering this command in your command line:

openssl req -x509 -newkey rsa:2048 -keyout rsa_private.pem -nodes -out rsa_cert.pem -subj "/CN=unused"

This will create a file rsa_private.pem and a file called rsa_cert.pem

Copy the contents of the rsa_cert.pem file into the window:

actioncomponent-mqtt-google-rsa-cert

actioncomponent-mqtt-google-cert

We're now ready to take on messages from Gravio.

Note: The MQTT Publish component will take the variables as payload and make one MQTT publish per variable. And if you use JSON or CSV the variable name will be the key/column.

Typically you can use the CreateRecord component to inject a variable into the MQTTPublish component.

To see how to retrieve MQTT messages, please refer to the "MQTT subscription section"MQTT Configuration of this documentation.