0

我正在使用 Bluemix IoT 服务。我的程序由以下元素组成:

  1. 发布者(本地计算机)
  2. 已订阅 (Bluemix)
  3. 发布者 (Bluemix)
  4. 订阅者(本地机器)

我目前正在遵循发布者(本地机器)>订阅者(Bluemix)>发布者(Bluemix)>订阅者(本地机器)的步骤

我面临的问题是当我尝试同时使用两个订阅者时,服务从两端取消订阅。如果我只保留订阅者,那么这些步骤就完美了。我使用的主题如下:

主题=“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()
4

1 回答 1

3

很高兴你找到了解决方案。总而言之,正如提到的 hardillb 和 amadin,根据 Watson IoT Platform文档,不应同时使用相同的客户端 ID 。

如果重复使用客户端 ID,当您尝试连接到 IoT 平台时,您的设备或应用程序会收到错误消息。这可能表明您的断开连接是由于 clientID 被重复使用或“被盗”。

在此处输入图像描述

如果您有两台设备使用相同的 clientId 和凭据连接 - 这会导致 clientId 被盗。每个 clientID 只允许一个唯一的连接;您不能有两个使用相同 ID 的并发连接。如果 2 个客户端尝试使用相同的客户端 ID 同时连接到 IoT,则会发生连接错误

于 2017-03-30T14:31:54.507 回答