1 of 28

챗GPT로 코딩하기

[강의교안 이용 안내]

  • 본 강의교안의 저작권은 양재삼한빛아카데미㈜에 있습니다.
  • 이 자료를 무단으로 전제하거나 배포할 경우 저작권법 136조에 의거하여 벌금에 처할 수 있고 �이를 병과(倂科)할 수도 있습니다.

마이크로파이썬을 활용한 사물인터넷

2 of 28

Chapter

10

MQTT 서버 만들기

3 of 28

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

4 of 28

Learning Objectives

  • IoT Understand the role of networks .
  • Learn the terminology of MQTT .
  • Install the MQTT broker on your PC .
  • Build an MQTT test environment .
  • Learn how to program to connect to a server using the MQTT standard library .

4/28

5 of 28

10.1 MQTT Overview

6 of 28

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

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

8 of 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

9 of 28

10.2 Mosquitto Getting started

10 of 28

1. Download and install Mosquitto

10.2 Mosquitto Getting started

Mosquitto introduction

Preparation before installation

  • Representative open source MQTT broker software
  • Installable on various operating systems including Windows, Linux, and Raspberry Pi
  • It is recommended to use a small, low-power device as it must always be in operation.
  • Download Mosquitto: mosquitto.org/download
  • Can be installed on Windows PC or Raspberry Pi

10/28

11 of 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

  • Set the port to listener 1883
  • Require user authentication by setting the allow_anonymous option to false
  • Set the password_file option (e.g. pw.txt)

11/28

12 of 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

13 of 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

14 of 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

15 of 28

10.3 Testing MQTT

16 of 28

Download and install MQTT Explorer

10.3 Testing MQTT

Using MQTT Explorer

  • After installing MQTT Explorer, the topic hierarchy of the broker is

Visually check

' https://mqtt-explorer.com '

  • Testing topic subscriptions and publications

16/28

17 of 28

2. Connect to the server

10.3 Testing MQTT

Server connection information Enter​

  • MQTT Explorer connection information Input (Host, Port, Username, Password)

17/28

18 of 28

2. Connect to the server

10.3 Testing MQTT

Publishing Topics and Payloads

  • Topic 'Online' message on tele/ESP32-01/ LWT publication

18/28

19 of 28

2. Connect to the server

10.3 Testing MQTT

Check the topic and payload

  • MQTT Explorer Issued Message and Subscribed Topic of data Can be confirmed
  • Topic hierarchy Structure and Real-time data Update Visually MQTT communication can be monitored In the test Useful​

19/28

20 of 28

10.4 MQTT Communication Library

21 of 28

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

22 of 28

2. Wi-Fi Connect

10.4 MQTT Communication Library

Wi-Fi connection method

  • The WLAN class controls the wireless network interface and can choose between AP and STA modes.
  • STA mode : In this book, ESP32 acts as a client, so it operates in STA mode.
  • AP mode : Used when ESP32 acts as a server by generating an SSID.
  • The router provides SSIDs that distinguish between 2.4GHz and 5GHz, but ESP32 only connects to 2.4GHz.

File name: 10_1_connect_wi_fi.py

Program 10-1 : Wi-Fi Connect

22/28

23 of 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

24 of 28

10.5 MQTT Standard library

25 of 28

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

26 of 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

27 of 28

Practice Problems (2/2 )

3

[ Program 9-5] Next On the condition according to Please improve .

  • Illumination The value is sent to the MQTT server in 10 second increments. Sending .
  • On the server Sensor value Sending Period ( teleperiod ) topic cmnd / device / TELEPERIOD Use it Should be able to be modified do .

4

Topic structure Among the methods , in ' TASMOTA' Adopted The method Topic By type Please explain .

27/28

28 of 28

Q&A

Copyright© 2025 Hanbit Academy, Inc.

All rights reserved.