pip3 install paho-mqtt 
#mqtt_sub.py

# -*- coding: utf-8 -*- 
import paho.mqtt.client as mqtt

def on_connect(client, userdata, flags, rc):
    print("Connected with result code "+str(rc))
    #client.subscribe("$SYS/#")
    client.subscribe("bit") # 토픽 설정 한 것

#서버로부터 publish message를 받을 때 호출되는 콜백
def on_message(client, userdata, msg):
    print(msg.topic+" "+str(msg.payload)) #토픽과 메세지를 출력한다.

client = mqtt.Client() #client 오브젝트 생성
client.on_connect = on_connect #콜백설정
client.on_message = on_message #콜백설정

client.connect('localhost', 1883)
client.loop_forever()


 

 

💡 https://developer-finn.tistory.com/1

 

Python 에서 MQTT 사용하기

1.MQTT MQTT는 클라이언트 publish / subscribe 메시지 전송 프로토콜입니다. 가볍고 개방적이며 간단하며 구현하기 쉬워서 M2M(Machine-to-Manchine)및 IOT(internet of Things) 에 적합한 선택이다. 1-1.MQTT 특..

developer-finn.tistory.com

 

블로그 이미지

hjc_

୧( “̮ )୨

,