0% found this document useful (0 votes)
40 views5 pages

Materi Kuliah MQTT ESP32

This document outlines a course on MQTT protocol implementation using ESP32, aiming to teach students about MQTT concepts, architecture, and practical applications in IoT. It includes topics such as MQTT introduction, broker installation, coding examples, and a practical project involving temperature monitoring. Additionally, it provides exercises for students to enhance their understanding of MQTT functionalities with ESP32.

Uploaded by

lordputri01
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)
40 views5 pages

Materi Kuliah MQTT ESP32

This document outlines a course on MQTT protocol implementation using ESP32, aiming to teach students about MQTT concepts, architecture, and practical applications in IoT. It includes topics such as MQTT introduction, broker installation, coding examples, and a practical project involving temperature monitoring. Additionally, it provides exercises for students to enhance their understanding of MQTT functionalities with ESP32.

Uploaded by

lordputri01
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/ 5

📘 Materi Kuliah: Protokol MQTT pada ESP32

🎯 Tujuan Pembelajaran

Setelah mengikuti perkuliahan ini, mahasiswa diharapkan mampu:

• Menjelaskan konsep dasar MQTT dan arsitekturnya.


• Memahami peran MQTT dalam komunikasi IoT.
• Mengimplementasikan komunikasi MQTT menggunakan ESP32.
• Membangun aplikasi sederhana IoT berbasis MQTT (Publish & Subscribe).

🧠 Pokok Materi

1. Pengenalan MQTT
2. Arsitektur MQTT (Broker, Publisher, Subscriber)
3. Instalasi dan konfigurasi broker MQTT (public broker / lokal broker)
4. Library dan kode program MQTT pada ESP32 (Arduino IDE)
5. Praktikum: Implementasi Publish-Subscribe menggunakan ESP32 dan MQTT

📖 Penjelasan Materi

1. Pengenalan MQTT

• MQTT (Message Queuing Telemetry Transport) adalah protokol komunikasi


ringan yang banyak digunakan dalam proyek IoT.
• Cocok untuk perangkat dengan daya dan bandwidth rendah.
• Menggunakan model Publish-Subscribe.

2. Arsitektur MQTT

• Broker: pusat komunikasi yang menerima pesan dari publisher dan


mendistribusikannya ke subscriber.
• Publisher: perangkat yang mengirim data ke topik tertentu.
• Subscriber: perangkat yang berlangganan pada topik dan menerima data.

Contoh broker MQTT publik:

• broker.hivemq.com
• test.mosquitto.org

3. Instalasi Broker Lokal (Opsional)

Jika menggunakan lokal broker:


• Instal Mosquitto MQTT Broker di PC atau Raspberry Pi.
• Jalankan dengan perintah: mosquitto -v

4. Coding MQTT pada ESP32 (Arduino IDE)

📦 Library:

#include <WiFi.h>
#include <PubSubClient.h>

// Update these with values suitable for your network.

const char* ssid = "wifimasing2"; //ssid masing2


const char* password = "passwordmasing2";//password hotspot masing2
const char* mqtt_server = "broker.mqtt.cool"; //broker MQTT

WiFiClient espClient;
PubSubClient client(espClient);
unsigned long lastMsg = 0;
#define MSG_BUFFER_SIZE (50)
#define BUILTIN_LED 2
char msg[MSG_BUFFER_SIZE];
int value = 0;

void setup_wifi() {

delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);

WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {


delay(500);
Serial.print(".");
}

randomSeed(micros());

Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();

// Switch on the LED if an 1 was received as first character


if ((char)payload[0] == '1') {
digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the
voltage level
// but actually the LED is on; this is because
// it is active low on the ESP-01)
} else {
digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off by making the
voltage HIGH
}

void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Create a random client ID
String clientId = "mqtt-ce4a";
clientId += String(random(0xffff), HEX);
// Attempt to connect
if (client.connect(clientId.c_str())) {
Serial.println("connected");
// Once connected, publish an announcement...
client.publish("/ce4a/outTopic", "hello world");
// ... and resubscribe
client.subscribe("/ce4a/inTopic");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}

void setup() {
pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an
output
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}

void loop() {

if (!client.connected()) {
reconnect();
}
client.loop();

unsigned long now = millis();


if (now - lastMsg > 2000) {
lastMsg = now;
++value;
snprintf (msg, MSG_BUFFER_SIZE, "hello world #%ld", value);
Serial.print("Publish message: ");
Serial.println(msg);
client.publish("/ce4a/outTopic", msg);
}
}

🛠️ Praktikum

Proyek Sederhana: Monitoring Suhu dan Kirim ke MQTT Broker

🧰 Alat dan Bahan:

• ESP32
• Sensor DHT11 / DHT22
• Koneksi Internet

🧰 Deskripsi:

• ESP32 membaca suhu dan kelembapan dari sensor DHT11.


• Mengirim data ke MQTT broker (esp32/suhu) setiap 5 detik.
• Dapat dimonitor dari aplikasi seperti MQTT Explorer atau Node-RED.

📌 Latihan dan Tugas


1. Ubah kode agar data hanya dikirim jika suhu > 30°C.
2. Buat sistem kontrol LED via MQTT:
o ESP32 sebagai subscriber pada esp32/led
o Jika pesan ON, LED menyala; jika OFF, LED mati.

📚 Referensi

• https://mqtt.org/
• Documentation PubSubClient: https://pubsubclient.knolleary.net/
• Arduino MQTT Examples: https://randomnerdtutorials.com

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy