Ubuntu MQTTを使用にインストール
2018年11月7日12:22:13
環境:
1.サードパーティ製ライブラリのインストール
2、コードは以下の通り
server_study.py
client_study.py
実行:
注意:mosquittoサーバがインストールされていないため、実行中にエラーが発生します.
3、mosquittoのインストール
インストールに成功したかどうかをテスト
インストールに成功した後、2ステップ目のプログラムを実行すれば成功します.
4、mosquitto可視化プログラム
環境:
Ubuntu 18.04
Python 3.6
1.サードパーティ製ライブラリのインストール
pip install paho-mqtt
2、コードは以下の通り
: https://www.cnblogs.com/nulige/p/8993719.html
server_study.py
import paho.mqtt.client as mqtt
import paho.mqtt.publish as publish
import time
idx = 0 # paho/temperature
while True:
print("send success")
publish.single("paho/temperature",
payload="this is message:%s" % idx,
hostname="localhost",
client_id="lora1",
# qos = 0,
# tls=tls,
port=1883,
protocol=mqtt.MQTTv311)
idx = 1
time.sleep(5)
client_study.py
import paho.mqtt.client as mqtt
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
print("Connected with result code " str(rc))
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
#
print(msg.topic " " str(msg.payload))
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("localhost", 1883, 60) #
client.subscribe("paho/temperature")
# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
# Other loop*() functions are available that give a threaded interface and a
# manual interface.
client.loop_forever()
実行:
:
python server_study.py
:
python client_study.py
注意:mosquittoサーバがインストールされていないため、実行中にエラーが発生します.
3、mosquittoのインストール
:https://blog.csdn.net/swedenfeng/article/details/53510048
sudo apt-get install mosquitto
sudo apt-get install libmosquitto-dev
sudo apt-get install mosquitto-clients
sudo service mosquitto status
インストールに成功したかどうかをテスト
#
mosquitto_sub -h localhost -t "mqtt"-v
#
mosquitto_pub -h localhost -t "mqtt"-m "Hello MQTT"
インストールに成功した後、2ステップ目のプログラムを実行すれば成功します.
4、mosquitto可視化プログラム
:http://www.jensd.de/apps/mqttfx/1.5.0/
:https://blog.csdn.net/nicholaszao/article/details/79211965