$USD
  • EUR€
  • £GBP
  • $USD
TUTORIALS OBLOQIoT

UART OBLOQ IoT Module Tutorial 6: Sending message to MQTT topic

DFRobot Oct 18 2018 1164

In this tutorial we will check how to send a message to a MQTT topic, using the UART OBLOQ.

As MQTT broker we will use CloudMQTT, a cloud based MQTT broker for the Internet of Things. You can check in more detail in this previous post how to connect the OBLOQ to a CloudMQTT broker. Note that CloudMQTT offers a free plan for testing.

In order to be able to send the commands to the OBLOQ from a computer, we will use a serial to USB converter. You can check here the connection diagram between the OBLOQ and the serial to USB converter.

As a tool to establish a serial connection from the computer, we will use the Arduino IDE serial monitor, although this can be tested with any other serial communication tool.

As an additional testing tool, we will use MQTTLens to also connect to the broker and receive messages published in a specific topic.

 

MQTTLens

MQTTLens is a very useful tool for Google Chrome which allows to connect to a MQTT broker and publish and subscribe to topics. Using it, we will be able to receive the message that the OBLOQ will publish.

So, after installing and running the app, the first thing we need to do is creating a connection to the MQTT broker. To do it, we need to click on the + sign on the connections tab, on the top left corner of the window, as shown in figure 1.

MQTTLens create connection.png

Figure 1 – Create new connection in MQTTLens.

On the menu that appears, we need to configure the connection to the CloudMQTT broker. We will need the following credentials from the broker, which can be obtained from the CloudMQTT broker instance details page:

  • Server
  • Port
  • Username
  • Password

You can check in figure 2 where to put the mentioned configurations. Note that we need to set a name for this connection and you can call it whatever you want. When everything is setup, click the “Create connection” button at the bottom right corner and the application should establish the connection.

MQTTLens configuring new connection to broker.png

Figure 2 – Configuring the connection to the broker.

To confirm that the application is successfully connected, you can go to the connections tab of the CloudMQTT broker instance and a connection should be listed.

Now, in order to later receive the messages published by the OBLOQ, we will need to subscribe the topic it will use. We will assume a topic named “test“.

To subscribe to a topic, simply put its name on the subscribe text area and click on the subscribe button in the MQTTLens UI, as shown in figure 3.

MQTTLens subscribe to topic for test.png

Figure 3 – Subscribe to “test” topic.

After subscribing, the “test” topic should appear in the “Subscriptions” list, as shown in figure 4.

MQTTLens topics subscribed list.png

Figure 4 – List of subscribed topics.

When we send a message to the “test” topic, MQTTLens should now receive it and display it.

 

The commands

The first thing we need to do is connecting the UART OBLOQ to a WiFi network, so we can later connect to the MQTT broker.

To do it, we need to send the command below, using the credentials of the network, namely the network name and the network password.

|2|1|yourNetworkName,yourNetworkPassword|

As response, the OBLOQ should return |2|1| to indicate the reception of the command and then periodically print |2|2|, indicating that the connection procedure is ongoing. Once it finishes, it will print the local IP assigned to the device in the network, as illustrated in figure 5.

UART OBLOQ Connection to WiFi success

Figure 5 – Connection to the WiFi network.

Now, we need to connect to the cloud MQTT broker. To do it, we need to send the command below, where we should put the parameters of the cloud MQTT broker instance. Remember from the previous tutorial that the credentials can be obtained from the details page of the CloudMQTT broker instance.

|4|1|1|server|port|user|password|

If the connection to the broker is successful, then the OBLOQ should return |4|1|1|1|, as indicated in figure 6.

UART OBLOQ connect to MQTT server

Figure 6 – Connection to the cloud based MQTT broker.

Note that you can confirm that the connection is successful also by going to the connections page of the broker instance, as shown in figure 7. In this case I have two connections in the list since I also have MQTTLens already connected to the broker.

UART OBLOQ plus MQTT lens connected to broker

Figure 7 – List of connections to the MQTT broker.

Finally, to send a message to a topic, we need to send the command below, changing topicName by the name of the topic to which we want to publish (in our case, the topic is called “test“), and changing message by the actual message we want to send.

|4|1|3|topicName|message|

You can check below at figure 8 the format of the command after we put the name of the topic and the message.

OBLOQ send message to MQTT broker in the cloud.png

Figure 8 – Sending the message from OBLOQ.

If the command is executed with success, OBLOQ will return |4|1|3|1|, as highlighted in figure 9.

OBLOQ MQTT message sent.png

Figure 9 – Answer to the send MQTT message command.

If you go back to MQTTLens, the message sent by OBLOQ should have been received in the “test” topic subscription, as shown in figure 10.

MQTTLens receive MQTT message from OBLOQ.png

Figure 10 – Receiving the message in MQTTLens.

UART OBLOQ module Tutorials:

REVIEW