MQTT 订阅在 Bluemix 容器中丢失
MQTT subscription gets lost in Bluemix container
我正在使用 Bluemix IoT 服务。我的程序由以下元素组成:
- Publisher (Local Machine)
- Subscribed (Bluemix)
- Publisher (Bluemix)
- Subscriber (Local Machine)
我目前正在按照以下步骤操作
发布者(本地机器)> 订阅者(Bluemix)> 发布者(Bluemix)> 订阅者(本地机器)
我面临的问题是当我尝试同时使用两个订阅者时服务从两端退订。如果我只保留订阅者,则这些步骤将完美无缺。我使用的题目如下:
topic = "iot-2/type/mymqttdevice/id/mynewdev/evt/iotData/fmt/json"
topic2 = "iot-2/type/mymqttdevice/id/mynewdev/evt/iotFile/fmt/json"
有人可以指导我在这里做错了什么吗?
编辑:添加代码
本地机器上的 Publisher 是一个 python 文件,包含典型的连接和发布方法。每次发布后,我都会断开与 IoT 服务的连接。
Bluemix 上的订阅者代码:
# -*- coding: utf-8 -*-
#!/usr/bin/env python
import paho.mqtt.client as mqtt
import os, json
import time
organization = "xel7"
username = ""
password = ""
#Set the variables for connecting to the iot service
broker = ""
devicename = "mynewdev"
topic = "iot-2/type/mymqttdevice/id/mynewdev/evt/iotData/fmt/json"
deviceType = "mymqttdevice"
topic2 = "iot-2/type/mymqttdevice/id/mynewdev/evt/iotFile/fmt/json"
clientID = "a:" + organization + ":appId"
broker = organization + ".messaging.internetofthings.ibmcloud.com"
mqttc = mqtt.Client(clientID)
if username is not "":
mqttc.username_pw_set(username, password=password)
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
def on_subscribe(mosq, obj, mid, granted_qos):
print("Subscribed: " + str(mid) + " " + str(granted_qos))
def on_message(client, userdata, msg):
with open('indurator.txt', 'w') as fd:
txt = (msg.payload.decode('string_escape'))
fd.write(txt)
#print txt
fd.close()
mqttc.publish(topic2,msg.payload);
mqttc.connect(host=broker, port=1883, keepalive=60)
test = mqttc.subscribe(topic,0)
mqttc.on_connect = on_connect
mqttc.on_subscribe = on_subscribe
mqttc.on_message = on_message
mqttc.loop_forever()
本地机器上用于接收从 Bluemix 订阅者发布的文件的订阅者代码:
-- 编码:utf-8 --
#!/usr/bin/env python
import paho.mqtt.client as mqtt
import os, json
import time
organization = "xel7"
username = ""
password = ""
#Set the variables for connecting to the iot service
broker = ""
devicename = "mynewdev"
deviceType = "mymqttdevice"
topic = "iot-2/type/mymqttdevice/id/mynewdev/evt/iotFile/fmt/json"
clientID = "a:" + organization + ":appId"
broker = organization + ".messaging.internetofthings.ibmcloud.com"
mqttc = mqtt.Client(clientID)
if username is not "":
mqttc.username_pw_set(username, password=password)
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
def on_subscribe(mosq, obj, mid, granted_qos):
print("Subscribed: " + str(mid) + " " + str(granted_qos))
def on_message(client, userdata, msg):
with open('receivednew.txt', 'w') as fd:
txt = (msg.payload.decode('string_escape'))
fd.write(txt)
#print txt
fd.close()
mqttc.connect(host=broker, port=1883, keepalive=60)
test = mqttc.subscribe(topic,0)
mqttc.on_connect = on_connect
mqttc.on_subscribe = on_subscribe
mqttc.on_message = on_message
mqttc.loop_forever()
很高兴您找到了解决方案。总结一下 hardillb 和 amadain 提到的,同一个客户端 ID 不应该根据 Watson IoT Platform 同时使用 documentation。
如果客户端 ID 被重复使用,当您尝试连接到 IoT 平台时,您的设备或应用程序会收到错误。这可能表明您的断开连接是由于 clientID 被重新使用或“被盗”造成的。
如果您有两台设备使用相同的 clientId 和凭据连接 – 这会导致 clientId 被盗。每个 clientID 只允许一个唯一的连接;您不能有两个使用相同 ID 的并发连接。
如果 2 个客户端尝试使用相同的客户端 ID 同时连接到 IoT,则会发生连接错误
我正在使用 Bluemix IoT 服务。我的程序由以下元素组成:
- Publisher (Local Machine)
- Subscribed (Bluemix)
- Publisher (Bluemix)
- Subscriber (Local Machine)
我目前正在按照以下步骤操作 发布者(本地机器)> 订阅者(Bluemix)> 发布者(Bluemix)> 订阅者(本地机器)
我面临的问题是当我尝试同时使用两个订阅者时服务从两端退订。如果我只保留订阅者,则这些步骤将完美无缺。我使用的题目如下:
topic = "iot-2/type/mymqttdevice/id/mynewdev/evt/iotData/fmt/json"
topic2 = "iot-2/type/mymqttdevice/id/mynewdev/evt/iotFile/fmt/json"
有人可以指导我在这里做错了什么吗?
编辑:添加代码
本地机器上的 Publisher 是一个 python 文件,包含典型的连接和发布方法。每次发布后,我都会断开与 IoT 服务的连接。
Bluemix 上的订阅者代码:
# -*- coding: utf-8 -*-
#!/usr/bin/env python
import paho.mqtt.client as mqtt
import os, json
import time
organization = "xel7"
username = ""
password = ""
#Set the variables for connecting to the iot service
broker = ""
devicename = "mynewdev"
topic = "iot-2/type/mymqttdevice/id/mynewdev/evt/iotData/fmt/json"
deviceType = "mymqttdevice"
topic2 = "iot-2/type/mymqttdevice/id/mynewdev/evt/iotFile/fmt/json"
clientID = "a:" + organization + ":appId"
broker = organization + ".messaging.internetofthings.ibmcloud.com"
mqttc = mqtt.Client(clientID)
if username is not "":
mqttc.username_pw_set(username, password=password)
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
def on_subscribe(mosq, obj, mid, granted_qos):
print("Subscribed: " + str(mid) + " " + str(granted_qos))
def on_message(client, userdata, msg):
with open('indurator.txt', 'w') as fd:
txt = (msg.payload.decode('string_escape'))
fd.write(txt)
#print txt
fd.close()
mqttc.publish(topic2,msg.payload);
mqttc.connect(host=broker, port=1883, keepalive=60)
test = mqttc.subscribe(topic,0)
mqttc.on_connect = on_connect
mqttc.on_subscribe = on_subscribe
mqttc.on_message = on_message
mqttc.loop_forever()
本地机器上用于接收从 Bluemix 订阅者发布的文件的订阅者代码:
-- 编码:utf-8 --
#!/usr/bin/env python
import paho.mqtt.client as mqtt
import os, json
import time
organization = "xel7"
username = ""
password = ""
#Set the variables for connecting to the iot service
broker = ""
devicename = "mynewdev"
deviceType = "mymqttdevice"
topic = "iot-2/type/mymqttdevice/id/mynewdev/evt/iotFile/fmt/json"
clientID = "a:" + organization + ":appId"
broker = organization + ".messaging.internetofthings.ibmcloud.com"
mqttc = mqtt.Client(clientID)
if username is not "":
mqttc.username_pw_set(username, password=password)
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
def on_subscribe(mosq, obj, mid, granted_qos):
print("Subscribed: " + str(mid) + " " + str(granted_qos))
def on_message(client, userdata, msg):
with open('receivednew.txt', 'w') as fd:
txt = (msg.payload.decode('string_escape'))
fd.write(txt)
#print txt
fd.close()
mqttc.connect(host=broker, port=1883, keepalive=60)
test = mqttc.subscribe(topic,0)
mqttc.on_connect = on_connect
mqttc.on_subscribe = on_subscribe
mqttc.on_message = on_message
mqttc.loop_forever()
很高兴您找到了解决方案。总结一下 hardillb 和 amadain 提到的,同一个客户端 ID 不应该根据 Watson IoT Platform 同时使用 documentation。
如果客户端 ID 被重复使用,当您尝试连接到 IoT 平台时,您的设备或应用程序会收到错误。这可能表明您的断开连接是由于 clientID 被重新使用或“被盗”造成的。
如果您有两台设备使用相同的 clientId 和凭据连接 – 这会导致 clientId 被盗。每个 clientID 只允许一个唯一的连接;您不能有两个使用相同 ID 的并发连接。 如果 2 个客户端尝试使用相同的客户端 ID 同时连接到 IoT,则会发生连接错误