我正在使用蓝光IoT服务。我的程序由以下元素组成:
我目前正在执行以下步骤: Publisher (本地机器)>订户(Bluemix) > Publisher (Bluemix) >订户(本地机器)
我面临的问题是,当我尝试同时使用两个订阅者时,服务就会从两端取消订阅。如果我只保留订户,那么步骤就完美了。我使用的主题如下:
topic = "iot-2/type/mymqttdevice/id/mynewdev/evt/iotData/fmt/json“ "iot-2/type/mymqttdevice/id/mynewdev/evt/iotFile/fmt/json“= topic2
有人能指点我这里做错了什么吗?
编辑:添加代码
本地机器上的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()发布于 2017-03-30 14:31:54
很高兴你找到了解决办法。概括地说,正如上面提到的hardillb和amadain一样,每个Watson IoT Platform 文档不应该同时使用相同的客户机ID。
如果重复使用客户端ID,则当您试图连接到IoT平台时,您的设备或应用程序将收到错误。这可能表明您的断开连接是由于clientID被重复使用或“被盗”。

如果您有两个设备连接相同的clientId和凭据-这将导致clientId窃取。每个clientID只允许一个唯一的连接;您不能使用相同的ID拥有两个并发连接。
https://stackoverflow.com/questions/43110499
复制相似问题