XDK Live
MQTT is defined as a many resource. That means, multiple instances of this resource can exist, and each is given a unique name by the developer. All possible configuration items and signals for the MQTT resource are listed below.
Setup
HTTP requires a network connection to the destination server, to which the HTTP requests are sent. With the XDK, this network is usually established using Wi-Fi.
Setting up MQTT resources
All MQTT resources are generally set up with the required configuration-items url
, clientId
and transport
. The first configuration-item is a URL, pointing to the desired MQTT broker, such as mqtt://my-broker.com:1883. The configuration-item clientId
will identify the XDK. The configuration-item transport
is the Wi-Fi resource, through which the MQTT messages are sent.
The following code can be used to create an MQTT resource:
Setting up variable topic-signals
In the above example, temperature
is a variable of type topic
. This variable is a so-called signal
, an MQTT instance can have multiple topic signals. For example, if it is desired to have one topic for temperature and one for light, the following code would initialize an MQTT resource with topics for light and temperature:
Every read/write operation on one of the signals will only send or receive MQTT messages to the broker on the associated topics.
Publishing on a Topic
Assuming that the following MQTT resource has been previously set up:
Then the following code will publish a message on the topic hello/world at the broker broker.hivemq.com every 5 seconds, with a JSON payload.
Subscribing To a Topic
TBD
Last updated
Was this helpful?