Department of Electrical Engineering
DEC50132 – Internet Based Controller
PRACTICAL WORK NO : 4
TITLE : Message Queuing Telemetry Transport (MQTT)
DATE :
LECTURER’S NAME :
PRACTICAL SKILL ATTAINMENT LAB REPORT ATTAINMENT
ASSESSMENT ASSESSMENT
(PLO5, P4,CLO 2)
S1
Able to install the tools without S2
supervision S3 Result
S4
S1
Able to establish connection to S2 Discussion
the broker from MQTTbox/ IOT
S3
MQTT panel
S4
S1
Able to deploy the node-red S2 Conclusion
and broker
S3
S4
S1
S2
Able to get the PBL output Score (30)
S3
S4
S1
Percentage
S2 (30%)
Score (40)
S3
S1
S4 S2
Total CA Marks
S1 (100%) S3
S2
Percentage (70%)
S3
S4
S4
BIL. GROUP MEMBERS REGISTRATION NO.
S1
S2
JKE PSP
PRACTICAL SKILL RUBRIC
Description Mark Scale Attainment
Student can complete all tasks assigned WITHOUT errors 5 x2 10
Student can complete all tasks assigned with A FEW errors 4 x2 8
Student can complete all tasks assigned with MORE errors 3 x2 6
Student can complete partial tasks assigned WITHOUT errors 2 x2 4
Student can complete partial tasks assigned with A FEW errors 1 x2 2
Student shows no response/task not attempted 0 x2 0
DATA ACQUISITION AND COMMUNICATION RUBRIC
Excellent Very Good Good Fair Unsatisfactory
Report Component
(PLO2,LD2)
5 4 3 2 1
Results Professional looking Accurate Accurate Incomplete result, Data are not shown
and accurate representatio representations of the major mistakes. OR are inaccurate.
• Results in the form representation of the n of the data data in written form,
of data, data in tables and/or in tables but no graphs or
calculation, graphs. Graphs and and/or tables are presented.
waveform, graph tables are labelled and graphs.
etc. titled.
Analysis/ Discussion All point of discussion Most points Some points of Some points of Very few points of
on the results obtained of discussion discussion on results discussion on discussion, not
• Ability to present, covered and on results obtained covered and results obtained properly
interpret and elaborated. obtained elaborated. covered and but elaborated.
analyse result. covered and not properly
elaborated. elaborated.
Conclusion Conclusion includes The closing The closing paragraph The closing No conclusion was
• Provide answers to whether the findings paragraph attempts to paragraph do not included in the
supported the summarizes summarize but draws attempts to report.
objectives stated hypothesis, possible and draws a a weak conclusion. summarize the
sources of error, and sufficient experiment OR
earlier. what was learned from conclusion. shows little effort
• Ability to learn the experiment. and reflection.
something from the
experiment.
JKE PSP
Practical Work 4
Title: MQTT
Objectives:
Upon completion of this practical work, students should be able to:
• Write code to established internet connection (MQTT Client)
• Write code to publish and subscribe MQTT
• Apply writing simple IoT application for monitoring sensor reading with Node-Red
dashboard
Equipments:
1. PC installed with Node.js, MQTTbox
2. Android phone with IOT MQTTPanel apps.
3. Internet connection (Access Point dedicated to the Laboratory)
Theory:
MQTT is one of the most commonly used protocols in IoT projects. It stands for Message Queuing
Telemetry Transport.
In addition, it is designed as a lightweight messaging protocol that uses publish/subscribe
operations to exchange data between clients and the server. Furthermore, its small size, low
power usage, minimized data packets and ease of implementation make the protocol ideal of the
“machine-to-machine” or “Internet of Things” world.
How do these devices communicate with each other?
Certainly, web browsers use HTTP protocol to communicate with the servers on the internet.
Likewise, IoT devices use specific protocols to communicate (send and receive data) over the
internet to each other.
JKE PSP
And here are some of the most known IoT protocols:
• CoAP (Constrained Application Protocol)
• AMQP (Advanced Message Queuing Protocol)
• DDS (Data Distribution Service)
• STOMP (Simple Text Oriented Messaging Protocol)
• MQTT (Message Queue Telemetry Transport)
The last one is the most used protocol in IoT projects. That’s due to it’s fast, lightweight and easy
to implement.
Why MQTT?
MQTT has unique features you can hardly find in other protocols, like:
It’s a lightweight protocol. So, it’s easy to implement in software and fast in data transmission.
It’s based on a messaging technique. Of course, you know how fast your messenger/WhatsApp
message delivery is. Likewise, the MQTT protocol.
Minimized data packets. Hence, low network usage.
Low power usage. As a result, it saves the connected device’s battery.
It’s real time! That’s is specifically what makes it perfect for IoT applications.
How MQTT works
Like any other internet protocol, MQTT is based on clients and a server. Likewise, the server is
the guy who is responsible for handling the client’s requests of receiving or sending data between
each other.
MQTT server is called a broker and the clients are simply the connected devices.
So:
When a device (a client) wants to send data to the broker, we call this operation a “publish”.
When a device (a client) wants to receive data from the broker, we call this operation a
“subscribe”.
In addition, These clients are publishing and subscribing to topics. So, the broker here is the one
that handles the publishing/subscribing actions to the target topics.
Example:
Let’s say there is a device that has a temperature sensor. Certainly, it wants to send his readings
to the broker.
JKE PSP
On the other side, a phone/desktop application wants to receive this temperature value.
Therefore, 2 things will happen:
The device defines the topic it wants to publish on, ex: “temp”. Then, it publishes the message
“temperature value”.
The phone/desktop application subscribes to the topic “temp”. Then, it receives the message that
the device has published, which is the temperature value.
Again, the broker role here is to take the message “temperature value” and deliver it to
phone/desktop application.
MQTT Components:
That takes us to the MQTT components, which are 5 as follows:
Broker, which is the server that handles the data transmission between the clients.
A topic, which is the place a device wants to put or retrieve a message to/from.
The message, which is the data that a device receives “when subscribing” from a topic or send
“when publishing” to a topic.
Publish, is the process a device does to send its message to the broker.
Subscribe, where a device does to retrieve a message from the broker.
How many devices you can connect to a broker
The number of connected devices “clients” to the broker depends on the broker service provider.
In fact, it can reach a massive number of clients those are publishing and subscribing all the time.
JKE PSP
but the amazing part of this isn’t only the huge number of these connected devices but also any
the fact that any device can get any other device’s data at any time. As a result, the applications
based on these quickly shared data are limitless.
Why not HTTP
HTTP is slower, more overhead and power consuming protocol than MQTT. So, let’s get into each
one separately:
• Slower: because it uses bigger data packets to communicate with the server.
• Overhead: HTTP request opens and closes the connection at each request, while MQTT stays
online to make the channel always open between the broker “server” and clients.
• Power consuming: since it takes a longer time and more data packets, therefore it uses much
power.
Additional notes
The difference to HTTP is that a client doesn’t have to pull the information it needs, but the broker
pushes the information to the client, in the case there is something new. Therefore each MQTT
client has a permanently open TCP connection to the broker. If this connection is interrupted by
any circumstances, the MQTT broker can buffer all messages and send them to the client when it
is back online. As mentioned before the central concept in MQTT to dispatch messages
are topics. A topic is a simple string that can have more hierarchy levels, which are separated by a
slash. A sample topic for sending temperature data of the living room could be house/living-
room/temperature. On one hand the client can subscribe to the exact topic or on the other hand
use a wildcard. The subscription to house/+/temperature would result in all message send to the
previously mention topic house/living-room/temperature as well as any topic with an arbitrary
value in the place of living room, for example house/kitchen/temperature. The plus sign is a single
level wild card and only allows arbitrary values for one hierarchy. If you need to subscribe to more
than one level, for example to the entire subtree, there is also a multilevel wildcard (#). It allows to
subscribe to all underlying hierarchy levels. For example house/# is subscribing to all topics
beginning with house.
Which broker to use?
There are many brokers that implement the MQTT protocol.
• Bevywise MQTTRoute
• Mosquito
• HiveMQ
JKE PSP
Procedures:
https://randomnerdtutorials.com/esp32-mqtt-publish-subscribe-arduino-ide/
1. https://nodejs.org/en/
Then run the application
JKE PSP
A. Install Node Red
1. Open this link and read carefully https://nodered.org/docs/getting-started/windows
2. Installing Node-RED as a global module adds the command node-red to your system path.
Execute the following at the command prompt:
3. Type this npm install -g --unsafe-perm node-red as shown below:
JKE PSP
4. Type this node-red
5. Then click Allow Access
6. On your browser type : localhost:1880
JKE PSP
7. The interface of your browser now will look like this
8. Then click the setting at the right of the panel (number 1) and choose manage palette
(number 2) as shown below:
JKE PSP
9. At the install panel, type dashboard, then install node red dashboard as shown below
Install this
10. Click CLOSE and now your dashboard is ready as shown below.
JKE PSP
C. MQTTBox installation
MQTTBox enables you to create MQTT clients to publish or subscript topics, create MQTT virtual
device networks, load test MQTT devices or brokers and much more. MQTTBox is a cross
platform application available on Linux, MAC and Windows.
1. Open link http://workswithweb.com/html/mqttbox/downloads.html
2. Choose Windows
3. Follow the instruction below and install it.
JKE PSP
4. Then the MQTTBox is ready to be used:
D. Start the flow
1. open node-red and node-red dashboard
2. Choose “mqtt in” as shown above.
3. Drag the “mqtt in” to flow1 plane as shown below
4. Double click the “mqtt in” node and change the setting as shown below:
JKE PSP
5. Change the properties below:
Then click “update”
6. Then, change the topic and name below:
Use your own topic and
remember this is case
sensitive
Use your own name or
tittle
JKE PSP
7. Drag “Gauge”
8. Edit the gauge properties
You can group your
gauge etc using this
property
The range can be set
Name your gauge
here.
JKE PSP
9. Join the 2 nodes together.
10. Then click “deploy”
11. Then after its successfully deployed. Click the open dashboard icon as shown below:
Open dashboard icon
JKE PSP
12. The node-red dashboard will be shown as below:
13. Then, open http://www.mqtt-dashboard.com/
JKE PSP
14. Open MQTTbox
15. Click “create MQTT client” and change MQTT client name, Protocol and Host as shown
below. Then click Save.
JKE PSP
16. Change the Topic to publish same as “topic” in your node-red
Choose your
own topic’s
name
Then click publish button.
JKE PSP
17. If success the display as below will be appeared
18. And the node-red dashboard gauge will be shown as below:
19. Try change the published value:
JKE PSP
E. using Android apps to connect to MQTT broker.
1. Go the AppStore and Install “IOT MQTT Panel”
2. Click icon “+” at the below on the right side panel to set the new connection.
- Name the connection name (use any name
you prefer”
- Type “broker.hivemq.com” at Broker web
- Port number:1883
- Network protocol: TCP
JKE PSP
-create device list, and name the device.
-then click “Create” button.
Click to create device
list
JKE PSP
- I name my device as “Meter” here.
- Click “ADD PANEL” button.
JKE PSP
Click “Gauge”
JKE PSP
Then, set the “panel name”, “topic”, Unit, QoS
JKE PSP
Then ADD PANEL and choose TEXT INPUT.
Then, click “TEXT INPUT” panel and set panel
name and topic
JKE PSP
Now, your panel will be shown like this
Try to send the value in “text input” and the
gauge will be changed accordingly based on
the value in text input box.
JKE PSP
Open your node red, add the charts and then open the dashboard, the dashboard view will be
shown like this.
JKE PSP
Problem Based Learning
Based on all the steps above, use your understanding to get the output in your node-red
dashboard.
Results:
PBL 1
Discussion:
Write your discussion based on practical activity and PBL. Explain how the MQTT works.
Conclusion:
Write your conclusion on this practical work.
References
http://www.iotsharing.com/2017/05/how-to-use-mqtt-to-build-smart-home-arduino-esp32.html
JKE PSP