44IOTG Lab 5 - MQTT (2)
44IOTG Lab 5 - MQTT (2)
School of Engineering
Internet of Things (IoT) 44IOTG-012628
OBJECTIVES
Understand the architecture of MQTT protocol for communication
Use MQTT library and cloud server for communicate between devices
Test out a simple “battery-powered” sensor.
Test and implement a simple home monitoring & control system
EQUIPMENT
You only need the kitset issued to you.
You may connect AC lamps to G1 (P15) and G2 (P32) (HIGH to on) working in parallel with LED1 and LED2.
PREPARATION
All procedures can be done at home using the kitset with home WiFi router or hotspot.
ACKNOWLEDGEMENT: We will be using HiveMQ public broker (server) to learn MQTT for IoT. They have a
set of useful learning materials, including an eBook. This labsheets use some diagrams from there.
https://www.hivemq.com/mqtt-essentials/
https://www.hivemq.com/public-mqtt-broker/ (no security used on this server)
We will use the PubSubClient library for ESP32 documented here https://pubsubclient.knolleary.net/api
1. Software Installation
Install the following before coming for the lab:
1. Install MQTT-Explorer on your laptop. Refer here https://mqtt-explorer.com/
It is “Version 0.4.0-beta1” as at 2022-11-15. Choose the portable version which can be run directly.
Run it once to make sure that it works. We will use this to simulate the ESP32 MQTT client for testing.
2. On your Android device, phone or tablet, install “IoT MQTT Panel” (Rahul Kundu) in the Play Store.
https://play.google.com/store/apps/details?id=snr.lab.iotmqttpanel.prod
https://www.snrlab.in/iot/iot-mqtt-panel-user-guide/
3. MQTT Model
References:
https://youtu.be/2aHV2Fn0I60
https://www.hivemq.com/blog/mqtt-essentials-part2-publish-subscribe/
IOTG Lab 5 Apr 2023 Page 2 of 23
https://www.hivemq.com/blog/mqtt-essentials-part-3-client-broker-connection-establishment/
The MQTT architecture is based on publish/subscribe model with clients connected to a broker (server).
Only 1 broker is used.
Subscribe
to topic
Publish
“temperature”
to topic
“temperature” Subscribe
to topic
“temperature”
Once setup, the broker is transparent
The MQTT commands available are CONNECT, SUBSCRIBE, PUBLISH, UNSUBSCRIBE and DISCONNECT.
A client must first do a CONNECT using a unique client ID (a name) to the broker before any
subscribe/publish. If the ID is already used by another client, connection would fail!
Any client can PUBLISH a “topic” with “payload” containing the data/message. Any client can SUBSCRIBE
to the topic to receive the data in the payload. The broker takes care of sending the published topic and
data to clients that subscribed to the topic. Once connected, the broker is transparent to the clients.
A topic is just a name that follows proper MQTT rules. Topics do not have to be predefined.
Topic examples: (“send” for publisher and “receive” for subscriber(s))
s12345678/room/status/sensor Send/receive a sensor reading; payload = “0” to “4095”
s12345678/room/status/lamp1 Send/receive lamp1 status. Payload = “1” means on, “0” off
s12345678/room/cmd/lamp1 Send/receive command to toggle lamp1; payload = “T”
The client that publishes, does not need to know who (can be multiple clients) subscribes to the topic.
The client that subscribes to a topic, does not need to know who publishes the topic.
In the design of a system (must be secured), all client ID, topics, publishers and subscribers should be
properly predefined so that all clients in the system work together properly.
NOTE: MQTT is meant for low data volume traffic communication. Avoid constantly publishing at short
intervals, e.g. every second. Typically, there is a slight delay for the subscriber to receive the topic.
The procedure:
Connect to WiFi
Connect to MQTT broker
Publish the sensor reading
Disconnect from MQTT broker
IOTG Lab 5 Apr 2023 Page 5 of 23
Disconnect from WiFi
Deep sleep for an interval to save power
Repeat the above
This ESP32 application only publishes data. There’s no topic subscription.
C. The loop()
Refer to the loop(). Statement that must be called regularly in order for ESP32 MQTT client to work?
________________________________________________
3 broker.hivemq.com
1 Save setup
2
Connect now
We’ve just setup MQTT-Explorer to simulate the ESP32. It subscribes to the topics of interest.
We’ll next setup the Android client.
4 Panels
Indicates
Gauge
Connection is
Subscribe topic:
good
sxxxxxxxx/room/status/sensor
Payload:
“0” to “4095”
LED Indicator
Button Button
Subscribe topic:
Publish topic: Publish topic:
sxxxxxxxx/room/status/lamp1
sxxxxxxxx/room/cmd/lamps sxxxxxxxx/room/cmd/lamp1
Payload:
Payload: Payload:
“0” (off) or “1” (on)
“Q” (query) “T” (toggle)
The topics and payload is also summarized in the table found in preparation section 7.3B.
5. Run IoT MQTT Panel on Android (or BlueStacks 5). Initial screen asks you to create a connection. If you
already had connection(s) setup, you can also create a new one using the ‘+’ icon.
“iotg home”
1
3
After click +, enter this name
“home dashboard” 4
2
Start to add panels
Connection is good to dashboard
1
Touch to setup If you already have panels,
dashboard use this to add new panels
You can duplicate a panel
2
“sensor reading”
…/room/status/sensor
3
5
“4095”
1 4 “0” 6
Add a “Gauge”
1 “lamp1” 7
“lamp1 button”
…/room/status/lamp1 2 …/room/cmd/lamp1
8
3 “1”
9 “T”
4 “0” 5
6 10
1
“get lamps status”
…/room/cmd/lamps 2
3 “Q”
Set selected panel to different width
Final dashboard
with 4 panels
1 5
6 7
2 3 4
PC ESP32 Client Publish
Android Client
Sensor reading Display
received & displayed
The above proves that the 2 clients are communicating correctly with correct topics and payloads.
We can now proceed to test the Android client with the actual ESP32. We will first test with the simple
mqttSensor sketch on ESP32 that represents a battery-operated smart sensor.
CHECKPOINT 2: Show your working PC ESP32 client and Android client to supervisor: ______________
C. A Battery-Operated Sensor
The mqttSensor sketch simulates a battery-operated sensor, publishing sensor reading at regular intervals.
12. Open mqttSensor sketch. Replace “s12345678” at “rootT” with your student number.
With the Serial Monitor open, run the sketch. Make sure there’s no error encountered. Observe at
the Serial Monitor and the Android client. State the following:
WiFi AP name: ________________________________________________________________________
IP address given: ______________________________________________________________________
MQTT broker name and port number: _____________________________________________________
ESP32 MQTT client name: ______________________________________________________________
Topic published: ______________________________________________________________________
A sample payload content: _____________________________________________________________
Assuming that there’s no error detected during the program execution, what does this program do?
__________________________________________________________________________________
__________________________________________________________________________________
Time taken to complete the process of connect to WiFi, MQTT broker, publish and disconnect:
__________________________ (no fixed value)
What is the meaning when the Green LED blink once?
___________________________________________________________________________________
IOTG Lab 5 Apr 2023 Page 15 of 23
The program goes to deep sleep for how long before the next reading? _________________________
Note that the program reports the cause of the system boot at the beginning.
After the first boot, the subsequent boots “wake up reason” are cause by: _______________________
Unfortunately, sometimes “Brownout detector triggered” is encountered during wake-up.
Refer to the ESP32 kit. Anything can be done to the hardware to reduce power consumption further?
___________________________________________________________________________________
13. To simulate an error, change the wifi ssid to an invalid one. Which LED would blink? ______________
If you miss observing that, press ESP32 reset button.
Restore the correct ssid and try an invalid broker URL. Which LED would blink? ___________
Restore all after testing.
CHECKPOINT 3: Show your answers to the supervisor________________
Observed that the Yellow LED on ESP32 is turn on for 1.5 seconds and turn off for 1.5 seconds.
Observed that the Green LED on ESP32 is turn on for 2 seconds and turn off for 2 seconds.
Submit your codes to Politemall.
24. Save “mqttHomeD” as “mqttHomeE”. Remove all topics subscribed.
Observed that without topics subscription all the LEDs do not blink. Why? _______________________
Subscribe to ONLY s12345678/room/status/+ (replace 12345678 with your own ID).
Observed that all the LEDs start blinking again.
Submit your codes to Politemall.
CHECKPOINT 4: Show your answers to the supervisor________________
At Android:
Panel Type: _________________ Topic to publish: ___________________________________________
Panel Type: _________________ Topic to subscribe: _________________________________________
At ESP32:
Topic to publish: _______________________________________________
Topic to subscribe: sxxxxxxxx/ ______________________________________
Study the ESP32 program. Some places where you need to add or make changes at ESP32:
- Declare topics to publish and subscribe
- Create EVENT_INPUT_T object for pb2 with its callback that does the necessary
- Create OUTPUT_T for led2 and lamp2 (Grove G2 at pin 32)
- Subscribe to command from Android to toggle lamp2
- Update the callback function to process the command to toggle lamp2
- Publish lamp2 status when there’s any change in lamp2 status
- Update publishAllLampsStatus() to include publishing of lamp2 status
A comment: The topics in the program can streamlined. E.g. instead of two topics
“…/room/cmd/lamp1” and “…/room/cmd/lamps,” it can be just “…/room/cmd” with payloads say “1”
to toggle lamp1, “Q” to request for all lamps’ statuses.
CHECKPOINT 5: Show your working system to the supervisor________________
APPENDIXES