0% found this document useful (0 votes)
15 views9 pages

Lab4 PubSubArch

Uploaded by

sana bargaoui
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views9 pages

Lab4 PubSubArch

Uploaded by

sana bargaoui
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on 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

Building an IoT System with ESP32 as Gateway and Edge


Devices with Automated Smart Trash Bin Case Study
Contents
Building an IoT System with ESP32 as Gateway and Edge Devices with Automated Smart Trash Bin Case
Study ............................................................................................................................................................. 1
Automated Smart Trash Bin Case Study Descrip on ................................................................................ 1
Objec ve ............................................................................................................................................... 1
Materials Needed.................................................................................................................................. 2
So ware Needed .................................................................................................................................. 2
Step-by-Step Guide ................................................................................................................................... 2
1. Se ng Up the Gateway ESP32 .......................................................................................................... 2
2. Se ng Up the Edge Device ESP32 .................................................................................................... 4
3. Installing and Se ng Up MQTT Explorer .......................................................................................... 6
Visualiza on and Results........................................................................................................................... 7
Edge Device Side ................................................................................................................................... 7
Gateway Side......................................................................................................................................... 8
MQTT Explorer ...................................................................................................................................... 8
Extension and Explora on ........................................................................................................................ 9

Automated Smart Trash Bin Case Study Descrip on


Objec ve

Create an IoT system where:

 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

 Two ESP32 boards


 Arduino IDE or similar development environment
 USB cables for programming the ESP32 boards
 Computer with the Arduino IDE installed
 Ultrasound Sensor: For object detection
 Servo Motor: For lid movement
 Weight Sensor: For waste level measurement
 Humidity Sensor: For moisture detection
 LED Indicator: For status display
 Buzzer (optional): For alerts
 Jumper wires and breadboard
 Power supply

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

for the online version skip this step

 Ensure the PubSubClient library is installed for MQTT communication.


 The WiFi library (default for ESP32) should be available.

1.2 Create the Access Point on the Gateway ESP32

 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>

#include <Firebase_ESP_Client.h> // Updated Firebase library


#include <PicoMQTT.h>

// Wi-Fi credentials for Access Point


const char* ap_ssid = ""; //Write here a ssid of your choice
const char* ap_password = ""; //Write here a password of your choice

// Wi-Fi credentials for Internet Access


const char* internet_ssid = ""; //Write here the ssid of Internet Wifi
const char* internet_password = ""; //Write here the password of Internet Wifi

//Create an MQTT broker


PicoMQTT::Server mqttBroker;

//Create a Wifi Client


WiFiClient espClient;

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

// Connect to the internet Wi-Fi network


WiFi.begin(internet_ssid, internet_password);
Serial.print("Connecting to internet Wi-Fi...");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("\nConnected to internet Wi-Fi");
Serial.print("Internet IP Address: ");
Serial.println(WiFi.localIP()); //the IP address printed by this line is
the IP address of the broker in the Internet Wifi network
mqttBroker.begin();
}
void loop() {
mqttBroker.loop(); // Keep the broker running
Page 3|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

//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.

2. Se ng Up the Edge Device ESP32


2.1 Connect to the Gateway's Access Point

 Program the edge ESP32 to connect to the Wi-Fi network created by the gateway.

2.2 Publish Data to the MQTT Broker

 Integrate sensors and actuators for the Automated Smart Trash Bin, and program the edge
device to send data to the MQTT broker.

2.3 Get IP address of 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

Edge Device Code:

#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

//Create WiFi client to connect to the gateway's access point


WiFiClient espClient;

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

// MQTT client setup


PicoMQTT::Client mqttClient("192.168.4.1", 1883); //Update here the IP address
of the broker if it is different

// Pin definitions for ultrasonic pins


#define ULTRASONIC_TRIG_PIN 5
#define ULTRASONIC_ECHO_PIN 18
//add the rest of pins for the rest of sensors and actuators

void setup() {
Serial.begin(115200);

// Connect to Wi-Fi provided by the gateway


WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected to Wi-Fi");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());

// Initialize components for the ultrasonic sensor


pinMode(ULTRASONIC_TRIG_PIN, OUTPUT);
pinMode(ULTRASONIC_ECHO_PIN, INPUT);

Serial.println("Edge device setup complete");

mqttClient.begin();
Serial.println("mqtt client start");

void loop() {
mqttClient.loop(); // to keep the mqtt client alive

// Ultrasound sensor for object detection


digitalWrite(ULTRASONIC_TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(ULTRASONIC_TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(ULTRASONIC_TRIG_PIN, LOW);
Page 5|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

long duration = pulseIn(ULTRASONIC_ECHO_PIN, HIGH);


int distance = duration * 0.034 / 2;

//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));

delay(5000); // Capture distance and Publish it every 5 seconds


}

2.4 Flash the Code to the Edge Device ESP32

 Upload the code and monitor the Serial output to confirm that it connects to the gateway
and publishes data to the MQTT topic.

3. Installing and Se ng Up MQTT Explorer

 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

 Disable Certificate and Encryption

Visualiza on and Results


Edge Device Side

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

Extension and Explora on


 Create and Configure a Firebase real-time database
 Add the necessary Arduino code from lab3 to the Gateway Sketch, so it will be able to
send the distance to the Firebase
 Add the rest of the sensors and the corresponding topics to handle data from multiple
sensors.
 Add the actuators and process the data flow in the inverse direction from the firebase to
the edge device passing via the gateway.
 Enhance the system by adding security features like authentication and encryption for
MQTT connections.

Page 9|9

You might also like