使用MQTT,前提是需要一个服务器Server/Broker,除了自己搭建服务,刚开始时可以使用第三方提供的在线公开的免费Broker服务。使用这些免费Broker服务,可以在刚开始时快速理解MQTT协议。
[img]http://dl2.iteye.com/upload/attachment/0128/3608/e523c06b-0ad1-3186-b4bc-3288f944c410.png[/img]
其他:https://github.com/mqtt/mqtt.github.io/wiki/public_brokers
这些Broker/Server已经搭建好了,只需要编写Client/Subscriber就能测试一下MQTT了。
以使用Python的paho-mqtt来实现客户端。连接test.mosquitto.org服务为例。
[b](1)安装Eclipse Paho客户端[/b]
[b](2)客户端连接[/b]
unencrypted-mqtt.py
执行 python unencrypted-mqtt.py
[img]http://dl2.iteye.com/upload/attachment/0128/3610/98519e1c-c7f0-3fee-836c-53dd850911e8.png[/img]
[b](2)发布消息[/b]
pub-mqtt.py
执行 python pub-mqtt.py
[img]http://dl2.iteye.com/upload/attachment/0128/3612/9133af4f-1fb9-3c33-988e-bf7f2f0c2c92.png[/img]
[b](3)订阅消息[/b]
sub-mqtt.py
执行 python sub-mqtt.py
[img]http://dl2.iteye.com/upload/attachment/0128/3614/1dbeee16-0b28-329a-b940-db96b3e10a89.png[/img]
[b]同时运行订阅和发布客户端。[/b]
[img]http://dl2.iteye.com/upload/attachment/0128/3616/a55ed0db-97b0-3e24-b459-1917d38bffdd.png[/img]
[b]加密通信,访问8883端口[/b]
先从 http://test.mosquitto.org/ 下载 mosquitto.org.crt文件。
pub-mqtt-tls.py
执行 python pub-mqtt-tls.py
[img]http://dl2.iteye.com/upload/attachment/0128/3618/6ce405f8-1f6f-3907-8b03-13d76e01f583.png[/img]
[img]http://dl2.iteye.com/upload/attachment/0128/3608/e523c06b-0ad1-3186-b4bc-3288f944c410.png[/img]
其他:https://github.com/mqtt/mqtt.github.io/wiki/public_brokers
这些Broker/Server已经搭建好了,只需要编写Client/Subscriber就能测试一下MQTT了。
以使用Python的paho-mqtt来实现客户端。连接test.mosquitto.org服务为例。
[b](1)安装Eclipse Paho客户端[/b]
pip install paho-mqtt
[b](2)客户端连接[/b]
unencrypted-mqtt.py
# coding=utf8
import paho.mqtt.client as mqtt
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
client.subscribe("test/rensanning/time")
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("test.mosquitto.org", 1883)
client.loop_forever()
执行 python unencrypted-mqtt.py
[img]http://dl2.iteye.com/upload/attachment/0128/3610/98519e1c-c7f0-3fee-836c-53dd850911e8.png[/img]
[b](2)发布消息[/b]
pub-mqtt.py
# coding=utf8
import paho.mqtt.client as mqtt
import time
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
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("test.mosquitto.org", 1883)
while client.loop() == 0:
msg = "test message from Publisher "+time.ctime()
client.publish("test/rensanning/time", msg, 0, True)
print("message published")
time.sleep(1.5)
pass
执行 python pub-mqtt.py
[img]http://dl2.iteye.com/upload/attachment/0128/3612/9133af4f-1fb9-3c33-988e-bf7f2f0c2c92.png[/img]
[b](3)订阅消息[/b]
sub-mqtt.py
# coding=utf8
import paho.mqtt.client as mqtt
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
client.subscribe("test/rensanning/time")
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("test.mosquitto.org", 1883)
client.loop_forever()
执行 python sub-mqtt.py
[img]http://dl2.iteye.com/upload/attachment/0128/3614/1dbeee16-0b28-329a-b940-db96b3e10a89.png[/img]
[b]同时运行订阅和发布客户端。[/b]
[img]http://dl2.iteye.com/upload/attachment/0128/3616/a55ed0db-97b0-3e24-b459-1917d38bffdd.png[/img]
[b]加密通信,访问8883端口[/b]
先从 http://test.mosquitto.org/ 下载 mosquitto.org.crt文件。
pub-mqtt-tls.py
# coding=utf8
import paho.mqtt.client as mqtt
import time
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
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.tls_set("mosquitto.org.crt")
client.connect("test.mosquitto.org", 8883)
while client.loop() == 0:
msg = "test message from Publisher "+time.ctime()
client.publish("test/rensanning/time", msg, 0, True)
print("message published")
time.sleep(1.5)
pass
执行 python pub-mqtt-tls.py
[img]http://dl2.iteye.com/upload/attachment/0128/3618/6ce405f8-1f6f-3907-8b03-13d76e01f583.png[/img]