Tutorial: How to use MQTT by adding one block to your application
Summary. This tutorial shows how to use MQTT, a machine-to-machine connectivity protocol, for
an existing application by adding the library block Robust MQTT. We will be using the Selfie Basic application as an example and publish a picture via MQTT.
Prerequisite: You must have downloaded the block Selfie Basic.
Step 1: Create your own project
As you are going to extend an imported system, first make your own project and duplicate
the System block Selfie Basic:
Create a new project, by clicking this icon:
Choose a name, e.g. org.bitreactive.selfie.with.mqtt
Right click the block Selfie Basic and choose Duplicate, then select your new project
as target project
Now you have your own block within your own project to play with.
Step 2: Add the block Robust MQTT to your application
If you do not have the MQTT library, you need to import this first. You will find it in team Reference Systems:
Now you are ready to use blocks from the MQTT library. Drag and drop the Robust MQTT from the library MQTT into your application. You may adjust its size and position
as well as the orientation and position of its pin to fit your application. The block is now in your application, however it is not connected yet.
Step 3: Integrate the block into the existing application
When using a block it is useful to first have a look at its contract. The contract of
Robust MQTT looks like this:
The pin init requires data of type Parameters. Add an operation providing Parameters to specify the MQTT broker which will be used to initialise Robust MQTT. The pin publish requires data of type MQTTMessage Add another operation to transform the BufferedImage into a MQTTMessage.
The java file of your block would then look like this:
Connect everything. Use analysis to make sure that your new system does not contain any issues. It will look something like this:
Robust MQTT is initialised, when block Photo emits a BufferedImage via pin photo. When Robust MQTT is ready, the Buffered Image is converted into a MQTTMessage, which is published to the MQTT broker. The zero timer is added because Robust MQTT can handle data via pin publish after it has emitted ready, not at the same time.
As the contract of Robust MQTT shows that it has to be stopped to terminate, we do so before terminating the application traversing the pins stop / stopped.
Step 4: Build and Run the Application
Before you can start the application, you need to build it's code, which generates an executable project ready to run.
Step 5: Receiving data from the MQTT broker
You have added Robust MQTT to publish the image to an MQTT broker.
The image can be received from any other machine having subscribed to the “image” messages. To do this it just requires a small application running on that machine. In Reactive Blocks this small application would look like this:
Here as well Robust MQTT hast to be initialised to provide the broker. In addition we are going to subscribe to the topic we have published the image to:
The arriving MQTTMessage contains the image taken, which is easy to extract:
In this example application the image is displayed once it has been received.