Welcome to using Industrial Communication
Toolbox in MATLAB.
In this video, we'll talk about how industrial Communication
Toolbox can be used for communication between IoT
devices using MQTT protocol.
So what is MQTT?
MQTT is an OASIS standard messaging protocol
for Internet of Things.
It is designed as an extremely lightweight publish,
subscribe messaging transport that
is ideal for connecting remote devices with a small code
footprint and minimal network bandwidth.
So for IoT devices communicating using the MQTT protocol
via any broker, the Industrial Communication Toolbox
provided by The MathWorks allows you to subscribe to topics,
read messages from them, and publish messages to them.
A horticulturist from Michigan State University
is studying the effect of climate
on the plants in a greenhouse.
As a part of the study, they monitor the temperatures
of the greenhouse with the help of temperature sensors set up
in the greenhouse.
They take note of the average temperature,
identify the plant condition, which is later
used for further research.
Now let's see how they can use the Industrial Communication
Toolbox to help with this research.
The greenhouse sensors are using HiveMQ broker, which
is running on their local host.
So the first step is to create an MQTT client using
that broker address.
MQTT client function creates a client object
and establishes connection between the client
and the broker.
This client object will be used in the rest of this example
for reading and writing messages.
The temperature sensor writes the data
to the topic, structured like so.
The MQTT client created in the previous step
should subscribe to this topic.
This is done via the Subscribe function.
The client and the topic are parsed as input parameters.
Once the MQTT client object subscribes to the topic
you can read the data returned to that topic,
using the read function.
The read function takes in the client object and the topic
as inputs.
Once the read operation is performed,
the buffer becomes empty.
Now that the data returned by the temperature sensor
is available, it can be visualized
by extracting the time and data from the table.
The data is always written as string or a character.
So we need to typecast it to double after extracting it
from the table.
Once this is done, the data can be plotted.
For further analysis, you want to write
the average temperature and the corresponding plant condition
to another topic.
This can be done via the write function.
Let's say there are three options for the plant
condition, as defined in the string array.
Next, the topic to which you want to write the condition
can be stored in this variable.
The message to be returned to the topic
can be constructed like this, by passing in whatever data
you want to.
As mentioned before, the MQTT read-write functions
only accept string or character messages.
So it's important to typecast all the information to a string
data type.
Now you can write this plant condition string
by using the write function and passing in the MQTT client
object and the topic you want to write to-- that
is, the plant condition topic.
And finally, the message, which is the plant condition, here.
Any MQTT clients who are already subscribed to this plant
condition topic can now access this information
for further study or analysis.
So this is how a horticulturist can
use Industrial Communication Toolbox for greenhouse study.
In this video, you have learned how to read and write
data via MQTT protocol using Industrial Communication
Toolbox.
Thanks for watching.
Check out more examples in the documentation
to learn about other workflows.