๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Lang/๐Ÿ Python

๐Ÿ Python * MQTT module paho๋ฅผ ์ด์šฉํ•œ MQTT ํ†ต์‹ 

 

์„ค์น˜ํ•˜๊ธฐ

1. Download

$ pip install paho-mqtt

2. MQTT ํ†ต์‹ 

$ sudo git clone https://github.com/eclipse/paho.mqtt.python.git
$ cd org.eclipse.paho.mqtt.python.git
$ sudo python setup.py install

- ์ฒ˜์Œ ๋ช…๋ น์–ด๋ฅผ ์ณค์„ ๋•Œ git์„ ์„ค์น˜๋ผ๊ณ  ํ•˜๋ฉด ์„ค์น˜ ํ›„ ์ง„ํ–‰ํ•˜์…”์•ผ ํ•ฉ๋‹ˆ๋‹ค. ( ์นœ์ ˆํ•˜๊ฒŒ ๋ช…๋ น์–ด ๊ฐ€๋ฅด์ณ ์ค˜์š” )

- ๋™์ž‘ ์‹œ root ๊ณ„์ •์œผ๋กœ ๋กœ๊ทธ์ธ ํ•˜์‹œ๊ณ  ์œ„์˜ ๋ช…๋ น์–ด๋ฅผ ์ณ์ฃผ์„ธ์š”.

- ๊ทธ๋ฆฌ๊ณ  ์ € ๊ฐ™์€ ๊ฒฝ์šฐ ์œ„์˜ ๋ช…๋ น์–ด์—์„œ ์ œ์‹œํ•œ ์œ„์น˜(org.eclipse.paho.mqtt.python.git)์— ๊น”๋ฆฌ์ง€ ์•Š์•„ setup.py ํŒŒ์ผ์ด ์žˆ๋Š” ๊ณณ์œผ๋กœ ์ฐพ์•„๊ฐ”์Šต๋‹ˆ๋‹ค. ๊ทธ๋ƒฅ ํ˜„ ์œ„์น˜์—์„œ ls ํ•˜๋‹ˆ๊นŒ paho.mqtt.python์ด ๋‚˜์˜ค๋”๋ผ๊ตฌ์š”.

3. Download ํ™•์ธ

- finished processing dependencies for paho-mqtt==1.3.0 ๋ฌธ๊ตฌ๋ฅผ ํ™•์ธ ํ•ด ์ค๋‹ˆ๋‹ค.

 

 

 

Python Paho ๋ชจ๋“ˆ ์‚ฌ์šฉํ•ด๋ณด๊ธฐ

1. Subscribe file ์ƒ์„ฑํ•˜๊ธฐ

# -*- coding: utf-8 -*-

import paho.mqtt.client as mqtt

def on_connect(client, userdata, flags, rc) :
	print "Connected with result cod", str(rc)
	client.subscribe("HELLO/WORLD")

def on_message(client, userdata, msg) :
	print "\n", msg.topic, " ", str(msg.payload)

client = mqtt.Client()
client.on_connect = on_connect
client.on_message =  on_message # on_message callback set

client.connect("test.mosquitto.org", 1883, 60) # MQTT server connect
client.loop_forever()

2. Publish file ์ƒ์„ฑํ•˜๊ธฐ

# -*- coding: utf-8 -*-

import paho.mqtt.client as mqtt

mqttc = mqtt.Client("python_pub")
mqttc.connect("test.mosquitto.org", 1883) 
mqttc.publish("HELLO/WORLD", "hello world!!!!!!!!!!!!!!!!!!!!!!!!!!!!")

mqttc.loop(2)

3. ์ฝ”๋“œ ๋™์ž‘ ์‹œ์ผœ๋ณด๊ธฐ

[ ์™ผ์ชฝ: Subscribe / ์˜ค๋ฅธ์ชฝ: Publish ]

 

 


Tanks for