์ต๊ทผ์ MQTT ํต์ ๊ฐ๋ฐ์ ์งํํ๋ฉด์ Keep Alive ์ค์ ์ ๊ด๋ จํ ๋ฌธ์ ๋ฅผ ๊ฒช์๋ค.
Status Update ํ๋ mqtt ํด๋ผ์ด์ธํธ๋ฅผ ์๋์ ๊ฐ์ด ์์ฑํ๊ณ
send_status ๋ผ๋ ํจ์๋ฅผ ํธ์ถํด์ ์ฌ์ฉํ๋ ค๊ณ ํ๋ค.
import paho.mqtt.client as mqtt
import uuid
import time
class RobotStatusUpdater:
def __init__(self, broker_ip, topic="robot/status"):
self.client = mqtt.Client(f"Robot_Status_Updater_{uuid.uuid4()}")
self.client.connect(broker_ip, 1883)
self.topic = topic
self.client = None
def send_status(self, client, queue):
while True:
message = queue.get()
if message is None:
break
client.publish('robot/status', message, qos=1)
time.sleep(0.5)
status_updater = RobotStatusUpdater("127.0.0.1")
def send_status(message):
status_updater.send_status(message)
์ฒ์์ ํธ์ถํ์ ๋
ํด๋ผ์ด์ธํธ์์ ๋ธ๋ก์ปค์ ๋ฉ์์ง๋ฅผ ์ฑ๊ณต์ ์ผ๋ก ๋ณด๋๋ค.
๊ทผ๋ฐ ์ด๋ ์๊ฐ์ด ์ง๋๋ฉด ๋ฉ์์ง ์ ์ก์ ์คํจํ๋ค.
ํ๋ฒ ์ฐ๊ฒฐ์ ํ๋ฉด ๊ณ์ ์ง์๋๋ค๊ณ ์๊ฐํ๊ณ ๊ทธ ๊ฑธ ๊ธฐ๋ฐ์ผ๋ก ๊ฐ๋ฐ์ ํ๋๋ฐ ..
๋ถ๋ช ๋ด๊ฐ ๋น ๋จ๋ฆฐ ๋ฌด์ธ๊ฐ๊ฐ ์๋ค ์๊ฐํ๊ณ Mqtt ๊ณต์ ๋ฌธ์๋ฅผ ์ฝ์๋ค.
๋ด๊ฐ ๋น ๋จ๋ฆฐ ๊ฑด loop๋ก ์คํํ๊ธฐ, ๊ทธ๋ฆฌ๊ณ Keep Alive ์ PINGREQ.
๊ทธ๋์ ๊ณ ์น ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ๋ค
import paho.mqtt.client as mqtt
import json
import time
from datetime import datetime
class RobotStatusUpdater:
def __init__(self, broker_ip, topic="robot/status"):
self.client = mqtt.Client("Robot_Status_Updater")
self.client.connect(broker_ip, 1883, keepalive=120)
self.topic = topic
def connect_mqtt(self):
"""MQTT ๋ธ๋ก์ปค์ ์ฐ๊ฒฐํ๊ณ ์๋ ์ฌ์ฐ๊ฒฐ์ ์ค์ """
try:
self.client.connect(self.broker_ip, 1883)
self.client.loop_start() # ์๋ ์ฌ์ฐ๊ฒฐ ํ์ฑํ
print(f"MQTT ์ฐ๊ฒฐ๋จ: {self.broker_ip}")
except Exception as e:
print(f"MQTT ์ฐ๊ฒฐ ์คํจ: {e}")
def check_connection(self):
"""์ฐ๊ฒฐ ์ํ๋ฅผ ํ์ธํ๊ณ , ๋์ด์ก๋ค๋ฉด ์ฌ์ฐ๊ฒฐ"""
while True:
try:
self.client.loop() # ์ฐ๊ฒฐ ์ ์ง ์ฒดํฌ
self.client.publish(self.topic, "ping") # ํ
์คํธ ๋ฉ์์ง ์ ์ก
except Exception as e:
print(f"์ฐ๊ฒฐ ๋๊น, ์ฌ์ฐ๊ฒฐ ์๋ ์ค... {e}")
self.connect_mqtt() # ์ฌ์ฐ๊ฒฐ ์๋
time.sleep(10) # 10์ด๋ง๋ค ์ฐ๊ฒฐ ์ํ ํ์ธ
def send_status(self, message):
"""MQTT ๋ฉ์์ง ์ ์ก"""
try:
self.client.publish(self.topic, message)
print(f"[์ํ ์ ์ก๋จ]: {datetime.now()}")
except Exception as e:
print(f"์ํ ์ ์ก ์คํจ: {e}, ์ฌ์ฐ๊ฒฐ ์๋")
self.connect_mqtt()
status_updater = RobotStatusUpdater("127.0.0.1")
# ์ฃผ๊ธฐ์ ์ธ ์ฐ๊ฒฐ ์ํ ํ์ธ์ ์ํ ๋ฐฑ๊ทธ๋ผ์ด๋ ์ค๋ ๋ ์คํ
import threading
connection_thread = threading.Thread(target=status_updater.check_connection, daemon=True)
connection_thread.start()
def send_status(message):
"""์ธ๋ถ์์ ํธ์ถํ ์ํ ์ ์ก ํจ์"""
status_updater.send_status(message)
- ์ฐ์ loop_start()๋ก ์คํ์์ผ์ ์๋์ผ๋ก PINGREQ๋ฅผ ๋ณด๋ด์ด ์ฐ๊ฒฐ์ ๊ด๋ฆฌํ๋๋ก ํด์ฃผ์๋ค.
- ์ฌ์ค ์ด ๋ฐฉ๋ฒ์ผ๋ก ๋ง๋ฌด๋ฆฌ์ง์ด๋ ๋์ง๋ง ๋ถ์ํ๊ธฐ์ ์ฐ๊ฒฐ์ ์ง์ํ๊ธฐ ์ํด 10์ด์ ํ๋ฒ์ฉ ์ง์ ping์ ๋ณด๋ด์ฃผ์๋ค.
- ping๋ฅผ ๋ณด๋ด๋ฉด์ ์ฐ๊ฒฐ์ ํ์ธํ๋๋กํ๊ณ , ์ฐ๊ฒฐ์ด ์ข ๋ฃ๋์ด์๋ ๊ฒฝ์ฐ์ ์ฌ์ฐ๊ฒฐ์ ์๋ํ๋๋ก ์ถ๊ฐํ๋ค.
- ์ด๋ ๊ฒ ๋ณ๊ฒฝํ๋, ๊ณ~~~~~์ ์ฐ๊ฒฐ๋์ด ์์ด์ ํต์ ์ด ์ ๋์๋ค .. ํดใ ใ ใ ใ ใ ใ ใ ใ ใ ํํํ
๊ณต์๋ฌธ์์ ์ฝ๊ณ ๋ด๊ฐ ๋์ณค๋ ๋ถ๋ถ๋ค์ ๊ณต๋ถํ๋ค.
MQTT Keep Alive Interval Explained With Examples
MQTT uses a keepalive mechanism for checking the status of the TCP/IP connection.On a quiet TCP/IP connection an MQTT client will send a PINGREQ at set intervals and expect to receive a PINGRESP.
www.steves-internet-guide.com
MQTT๋ TCP/IP ์ฐ๊ฒฐ์ ์ฌ์ฉํ๋ค.
- MQTT ํด๋ผ์ด์ธํธ๋ TCP/IP๋ฅผ ํตํด ๋ธ๋ก์ปค์ ์ฐ๊ฒฐ๋๋ค.
- ์ด ์ฐ๊ฒฐ์ ํญ์ ์ด๋ ค ์๋ ์ํ๋ก ์ ์ง๋์ด ์ธ์ ๋ ์ง ๋ฐ์ดํฐ๋ฅผ ์ก์์ ํ ์ ์๋ค.
Keep Alive ๊ธฐ๋ฅ
- ์ผ์ ์๊ฐ ๋์ ๋ฐ์ดํฐ๊ฐ ํ๋ฅด์ง ์์ผ๋ฉด ํด๋ผ์ด์ธํธ๋ PINGREQ(ํ ์์ฒญ) ๋ฉ์์ง๋ฅผ ๋ธ๋ก์ปค๋ก ์ ์กํ๋ค.
- ๋ธ๋ก์ปค๋ ์ด์ ๋ํด PINGRESP(ํ ์๋ต) ๋ฉ์์ง๋ฅผ ๋ฐํํ๋ค.
- ์ด ๊ณผ์ ์ ์ฐ๊ฒฐ์ด ์ฌ์ ํ ํ์ฑ ์ํ์ธ์ง ํ์ธํ๋ ์ญํ ์ ํ๋ค.
Keep Alive Period (์ ์ง ์๊ฐ)
- ํด๋ผ์ด์ธํธ๊ฐ ๋ฐ์ดํฐ๋ฅผ ์ก์์ ํ์ง ์๋ ์ต๋ ํ์ฉ ์๊ฐ์ ์๋ฏธํ๋ค.
- ์ด ์๊ฐ์ด ์ง๋๋๋ก ๋ฐ์ดํฐ๊ฐ ์์ผ๋ฉด PINGREQ๊ฐ ๋ฐ์ํ์ฌ ์ฐ๊ฒฐ ์ํ๋ฅผ ํ์ธํ๋ค.
- ๋ธ๋ก์ปค๊ฐ ์ผ์ ์๊ฐ ๋ด PINGRESP๋ฅผ ๋ณด๋ด์ง ์์ผ๋ฉด ์ฐ๊ฒฐ์ด ๋๊ธด ๊ฒ์ผ๋ก ๊ฐ์ฃผํ๋ค.
Python MQTT ํด๋ผ์ด์ธํธ์์ Keep Alive ์ฒ๋ฆฌ
- Python MQTT ํด๋ผ์ด์ธํธ(paho-mqtt)๋ฅผ ์ฌ์ฉํ ๋, PINGREQ ๋ฉ์์ง๋ฅผ ์ง์ ์์ฑํ ํ์ ์๋ค.
- ์ด์ : loop() ํจ์๊ฐ ์๋์ผ๋ก ๊ด๋ฆฌํ๊ธฐ ๋๋ฌธ
- Keep Alive ๊ฐ์ 20์ด๋ก ์ค์ ํ ๊ฒฝ์ฐ, 20์ด๋ง๋ค PINGREQ๋ฅผ ๋ณด๋ด๊ณ PINGRESP๋ฅผ ๋ฐ๋๋ค.
Keep Alive๋ ์ก์์ (TX/RX) ๋ชจ๋๋ฅผ ๊ณ ๋ คํ๋ค.
- MQTT Kwwp Alive๋ ํด๋ผ์ด์ธํธ๊ฐ ๋ธ๋ก์ปค์ ๋ง์ง๋ง์ผ๋ก ์ฃผ๊ณ ๋ฐ์(Control Packet) ์๊ฐ์ ๊ธฐ์ค์ผ๋ก ์ธก์ ๋๋ค.
- ํด๋ผ์ด์ธํธ๊ฐ ์ฃผ๊ธฐ์ ์ผ๋ก ์ก์ (Publish)๋ง ํ๋ฉด Keep Alive ์๊ฐ์ด ์ด๊ณผ๋ ์ ์์ด PINGREQ๊ฐ ํ์ํ๋ค.
- ๊ทธ๋ฌ๋ ๊ตฌ๋ (Subscribe) ํ ๋ฉ์์ง๋ฅผ ์์ (Receive)ํ๋ฉด, Keep Alive ํ์ด๋จธ๊ฐ ์ด๊ธฐํ๋๋ค.
- ๊ฒฐ๊ณผ์ ์ผ๋ก ๋ฐํ(Publish)๊ณผ ์์ (Receive) ํ๋์ด ์ง์์ ์ผ๋ก ์ด๋ฃจ์ด์ง๋ฉด PINGREQ๋ฅผ ๋ณด๋ผ ํ์๊ฐ ์์ด์ง๋ค.
MQTT ํ๋กํ ์ฝ์ ๊ท์ (MQTT-3.1.2-24)
- Keep Alive๋ฅผ ์ค์ ํ๋๋ผ๋,
- ๋ธ๋ก์ปค๊ฐ Keep Alive ์๊ฐ์ 1.5๋ฐฐ ๋์ ํด๋ผ์ด์ธํธ๋ก๋ถํฐ ์ด๋ค Control Packet๋ ๋ฐ์ง ๋ชปํ๋ฉด
- ๋คํธ์ํฌ ์ฅ์ ๋ก ๊ฐ์ฃผํ๊ณ ํด๋ผ์ด์ธํธ๋ฅผ ๊ฐ์ ์ฐ๊ฒฐ ์ข ๋ฃ(MUST disconnect) ํด์ผ ํ๋ค.
- ์ด ๊ธฐ๋ฅ์ ๋คํธ์ํฌ ์ฅ์ ๊ฐ์ง๋ฅผ ์ํ MQTT์ ๊ธฐ๋ณธ์ ์ธ ์์ ์ฅ์น์ด๋ค.
Keep Alive ๋ฉ์์ง๋ฅผ ๋นํ์ฑํ
- Keep Alive ๊ฐ์ 0์ผ๋ก ์ค์ ํ๋ฉด PINGREQ/PINGRESP ๋ฉ์์ง๊ฐ ์ ์ก๋์ง ์๋๋ค.
- ์ด ๊ฒฝ์ฐ, ํด๋ผ์ด์ธํธ๋ ๋ธ๋ก์ปค๊ฐ ์ฐ๊ฒฐ์ ๊ฐ์งํ๋ ๋ฉ์ปค๋์ฆ์ด ์์ด ๊ณ์ ์ฐ๊ฒฐ๋๋ค.
Keep Alive ์๊ฐ
- ์๊ฐ์ ๋งค์ฐ ๊ธธ๊ฒ ์ค์ ํ๋ ๊ฒ์ ๋นํจ์จ์ ์ผ ์ ์๋ค.
- 15๋ถ ์ด์ ์๋ฌด๋ฐ ๋ฐ์ดํฐ ์ก์์ ์ด ์์ ๊ฒฝ์ฐ, ์ฐ๊ฒฐ์ ํด์ ํ๊ณ ํ์ํ ๋ ๋ค์ ์ฐ๊ฒฐํ๋ ๊ฒ์ด ๋ ๋ซ๋ค.
- ์ฅ์๊ฐ ์ฐ๊ฒฐ์ ์ ์งํ๋ฉด ๋ฆฌ์์ค๋ฅผ ์๋นํ๊ณ , ๋คํธ์ํฌ ์ํ๋ฅผ ๊ฐ์งํ๋ ๋ฐ ์ด๋ ค์์ด ์๊ธธ ์ ์๋ค.
โ ์ดํดํ๊ธฐ ์ฝ๊ฒ ์ ๋ฆฌ๋ฅผ ํ๋ฉด ...
Keep Alive ๊ฐ์ 20์ด๋ก ์ค์ ํ๋ค๊ณ ๊ฐ์ ํ์
- ๊ทธ๋ผ 20์ด ๋ด์ ์ต์ 1๊ฐ์ Control Packet์ด ํ์ํ๋ค.
- 20์ด ํ PINGREQ๋ฅผ ๋ณด๋ด์ง ์์ผ๋ฉด, 30์ด (20์ด*1.5) ๋ด์ ์ด๋ค ๋ฐ์ดํฐ๋ ์์ ๋์ง ์์ ๊ฒฝ์ฐ ๋ธ๋ก์ปค๊ฐ ๊ฐ์ ์ฐ๊ฒฐ ์ข ๋ฃ๋ฅผ ์งํํ๋ค.
- ์ฃผ๊ธฐ์ ์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ์ก์์ ํ๋ฉด PINGREQ ์์ด๋ ์ฐ๊ฒฐ ์ ์ง ๊ฐ๋ฅํ๋ค.
'๐ฉ๐ปโ๐ป > python' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Redis ์ค์๊ฐ ๋ชจ๋ํฐ๋ง (0) | 2025.02.13 |
---|---|
Redis and Celery (0) | 2025.02.12 |
Celery๋ฅผ ์ด์ฉํ ํฌ๋กค๋ง ์์ (1) | 2025.02.05 |
[python] gRPC ๊ฐ๋จํ๊ฒ ๊ตฌํํด๋ณด๊ธฐ (0) | 2025.01.02 |
[python] PySide6๋ก ์ฌ๋ฆฐ gui๋ก ์๊ฒฉ ์๋ฒ์ ํ์ผ ๋ค์ด๋ก๋ํ๊ธฐ (1) | 2024.12.31 |