챗GPT로 코딩하기
[강의교안 이용 안내]
마이크로파이썬을 활용한 사물인터넷
Chapter
10
MQTT 서버 만들기
index
10.1
Mosquitto Getting started
10.2
Testing MQTT
10. 3
MQTT communication library
10.4
MQTT standard library
10.5
Creating an MQTT Server
3/28
Learning Objectives
4/28
10.1 MQTT Overview
1. What is MQTT ?
10.1 MQTT Overview
What is MQTT?
Protocol for lightweight data transmission and reception between IoT devices
Development and Management
It was developed by IBM and is currently Open standards managed by OASIS
How it works
can be used even on devices with small memory capacity. Possible
6/28
2. MQTT network Composition and Terminology
10.1 MQTT Overview
MQTT network composition
Topic and Payload
Publish & Subscribe
Client: Each IoT device that connects to the MQTT broker to send and receive data.
Broker: Manages and distributes data centrally in the network (e.g. Mosquitto)
Topic: A name that identifies the data (e.g., "temp")
Payload: The actual data contained in the topic (e.g., "25")
Publish : Client sends data to broker
Subscription : Requesting to receive data on a specific topic from a broker.
7/28
2. MQTT network Composition and Terminology
10.1 MQTT Overview
QoS 2: exactly once
Send only once, with mutual confirmation
QoS 1: at least once
At least once, can be transmitted multiple times
QoS 0: at most once
Transmit only once, may not be transmitted
Retained final value
Store the last message published in the broker and forward it to new subscribers. transmission
Last Will and Testament
issued by the broker when a client connection is lost. �message
8/28
10.2 Mosquitto Getting started
1. Download and install Mosquitto
10.2 Mosquitto Getting started
Mosquitto introduction
Preparation before installation
10/28
2. Setting access permissions
10.2 Mosquitto Getting started
Download the installation file and run it
Click 'More information' when prompted with a security warning and proceed.
Step-by-step installation by following the installation wizard's instructions.
Default settings
Edit the C:\Program Files\mosquitto\mosquitto.conf file
11/28
3. Port opening
10.2 Mosquitto Getting started
Open Windows Firewall settings
Windows Firewall in Control Panel In settings access
�
Port 1883 open (open Firewall)
port Check status
manager By authority command prompt After running
netstat -a check
12/28
4. Working from the Command Prompt
10.2 Mosquitto Getting started
Create a pw.txt file
Move directory
'cd C:\Program Files\mosquitto'
Create your first user password
'mosquitto_passwd -c pw.txt FIRST_USER'
Enter your password as prompted
Add a user password
'mosquitto_passwd -b pw.txt
SECOND_USER SECOND_PASSWORD'
13/28
4. Working from the Command Prompt
10.2 Mosquitto Getting started
Mosquitto Getting started
Mosquitto execution command
mosquitto -c mosquitto.conf -v
How to check execution
Verify normal operation through execution information and data transmission/reception logs.
14/28
10.3 Testing MQTT
Download and install MQTT Explorer
10.3 Testing MQTT
Using MQTT Explorer
Visually check
16/28
2. Connect to the server
10.3 Testing MQTT
Server connection information Enter
17/28
2. Connect to the server
10.3 Testing MQTT
Publishing Topics and Payloads
18/28
2. Connect to the server
10.3 Testing MQTT
Check the topic and payload
19/28
10.4 MQTT Communication Library
1. For MQTT communication necessary library
10.4 MQTT Communication Library
Wi-Fi connection
Connect your device to a Wi-Fi network connection
MQTT broker connection
to the MQTT broker using a WiFi network. connection
Data transfer
Sensor data collected from the device to the MQTT broker forwarding
Device Control
Based on the command data received from the MQTT broker Device control
Wi-Fi connection
Using the network module, connect in STA mode
MQTT communication
Using the umqtt.simple module, sending and receiving messages through MQTTClient
21/28
2. Wi-Fi Connect
10.4 MQTT Communication Library
Wi-Fi connection method
File name: 10_1_connect_wi_fi.py
Program 10-1 : Wi-Fi Connect
22/28
3. To MQTT server Connect
10.4 MQTT Communication Library
client_id
It is generated using the device's unique information and cannot be duplicated.
server
IP address of the MQTT server
port
Normally 1883 is used, but can be changed depending on server settings, such as 8883 when security is applied.
user
Username registered in pw.txt (can be duplicated on multiple devices)
password
Password registered in pw.txt
callback
A function called when a subscribed topic is received (code execution according to server commands)
Program 10-2: Connecting to an MQTT server and sending and receiving information
File name: 10_2_mqtt_send_rcv.py
23/28
10.5 MQTT Standard library
1. Class UseMQTTCilent outline
10.5 MQTT Standard library
The UseMQTTClient class continuously monitors the status of Wi-Fi and MQTT connections and automatically reconnects in case of communication failures, allowing users to focus solely on data processing.
Connect to Wi-Fi
Connect your device to a Wi-Fi network and automatically reconnect if the connection is lost.
Connect to MQTT broker
If the Wi-Fi connection is normal, it connects to the MQTT broker, and if the connection is lost, it attempts to reconnect every 30 seconds.
Runs when connected to an MQTT broker
Register a will, register a device connection, subscribe to a command topic, and execute additional functions.
Execution monitoring
Monitor the communication status by calling the run() method within the while loop, and call the specified callback function when a received topic occurs.
Program 10-3: A typical MQTT program using UseMQTTClient
File name: 10_3_usemqttclient_send_rcv.py
25/28
Practice Problems (1/2 )
1
In IoT Used communication of the protocol Type Enumerate Please explain .
2
In IoT Used wireless communication In the way about Please explain .
26/28
Practice Problems (2/2 )
3
[ Program 9-5] Next On the condition according to Please improve .
4
Topic structure Among the methods , in ' TASMOTA' Adopted The method Topic By type Please explain .
27/28
Q&A
Copyright© 2025 Hanbit Academy, Inc.
All rights reserved.