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
'RaspberryPi & Arduino' 카테고리의 다른 글
Hadoop 관련 자료 (0) | 2021.07.14 |
---|---|
라즈베리파이 재부팅 후 프로그램 자동 시작 (0) | 2020.08.02 |
Raspberry pi(9) mosquitto - MQTT (0) | 2020.07.03 |
Raspberry pi(8) 라즈베리파이 카메라 스트리밍 + 외부 접속 설정 (1) | 2020.07.03 |
Raspberry pi(7) PiCamera - 라즈베리파이 카메라 (0) | 2020.07.03 |