Lab4 PubSubArch
Lab4 PubSubArch
One ESP32 acts as a gateway, establishing a Wi-Fi access point and running an MQTT
broker.
Another ESP32 acts as an edge device, connecting to the gateway and publishing data to
the MQTT broker.
Page 1|9
Lab Manual – Publisher/Subscriber Architecture
Topic: IoT by prac ce
Dr. Eng. Amira HENAIEN
Target Students: Computer Science or Computer Engineering level two or higher
The edge device will simulate an Automated Smart Trash Bin using various sensors and
actuators.
Materials Needed
So ware Needed
Wokwi: to prepare the wiring and Arduino Sketch for the physical layer of the edge device
Arduino IDE: to prepare Arduino Sketches
MQTT Explorer: to visualize the message flow
Wifi Internet network: to connect to the internet
Step-by-Step Guide
1. Se ng Up the Gateway ESP32
1.1 Install Required Libraries If you are using desktop Arduino IDE
Write code to set up the ESP32 as a Wi-Fi access point and MQTT broker.
Gateway Code:
Page 2|9
Lab Manual – Publisher/Subscriber Architecture
Topic: IoT by prac ce
Dr. Eng. Amira HENAIEN
Target Students: Computer Science or Computer Engineering level two or higher
#include <WiFi.h>
void setup() {
Serial.begin(115200);
// Set up Wi-Fi Access Point
WiFi.softAP(ap_ssid, ap_password);
Serial.println("Access Point Started");
Serial.print("AP IP Address: ");
Serial.println(WiFi.softAPIP()); //the IP address printed by this line is
the IP address of the broker in the local Wifi network
//This code is optional and is just here to visualize all the message
transferred by the broker
mqttBroker.subscribe("#",[](const char* topic, const char* payload){
Serial.printf("Received message in topic %s : %s\n",topic,payload);
});
}
1.3 Flash the Code to the Gateway ESP32
Upload the code using the Arduino IDE and monitor the Serial output for the IP address
of the access point.
Program the edge ESP32 to connect to the Wi-Fi network created by the gateway.
Integrate sensors and actuators for the Automated Smart Trash Bin, and program the edge
device to send data to the MQTT broker.
Power your gateway and wait until it prints its IP address in the local WiFi
Copy the IP address and use it to update
#include <WiFi.h>
#include <PicoMQTT.h>
// Wi-Fi credentials
const char* ssid = ""; //write here the ssid of the Wifi access point provided
by the gateway
const char* password = ""; //write here the password of the Wifi access point
provided by the gateway
Page 4|9
Lab Manual – Publisher/Subscriber Architecture
Topic: IoT by prac ce
Dr. Eng. Amira HENAIEN
Target Students: Computer Science or Computer Engineering level two or higher
void setup() {
Serial.begin(115200);
mqttClient.begin();
Serial.println("mqtt client start");
void loop() {
mqttClient.loop(); // to keep the mqtt client alive
//publishing the distance throw the local broker in the topic trash/distance
mqttClient.publish("trashBin/distance", String(distance));
Serial.println("Sending distance to gateway"+ String(distance));
Upload the code and monitor the Serial output to confirm that it connects to the gateway
and publishes data to the MQTT topic.
Ensure the gateway ESP32 is running and broadcasting the Wi-Fi access point.
Confirm that the edge device connects to the network and publishes data.
Download MQTT Explorer from here https://mqtt-explorer.com/
Install MQTT Explorer on your laptop
Run it
In the Host write the IP address of your gateway
Page 6|9
Lab Manual – Publisher/Subscriber Architecture
Topic: IoT by prac ce
Dr. Eng. Amira HENAIEN
Target Students: Computer Science or Computer Engineering level two or higher
Page 7|9
Lab Manual – Publisher/Subscriber Architecture
Topic: IoT by prac ce
Dr. Eng. Amira HENAIEN
Target Students: Computer Science or Computer Engineering level two or higher
Gateway Side
MQTT Explorer
Page 8|9
Lab Manual – Publisher/Subscriber Architecture
Topic: IoT by prac ce
Dr. Eng. Amira HENAIEN
Target Students: Computer Science or Computer Engineering level two or higher
Page 9|9