on_subscribe 不工作 - paho python 使用 IBM 物联网平台

on_subscribe not working - paho python with IBM iot platform

我尝试了我的订户,它是使用 Paho python 客户端和 HiveMQ 代理编写的,它工作得很好,但它不适用于 IBM。

Subscribing to application status messages, and this question,我按如下方式实现了订阅者客户端(我从我的 IBM Watson 平台的应用程序部分获得了 "a:<ORG-ID>:<App-ID>"):

def on_connect(client, userdata, flags, rc):
    print("CONNACK received with code %d." % (rc))
    (result, mid) = client.subscribe("iot-2/app/MyAppID/sensordata", 2)
    print("result: ", result, ", mid: ", mid)

    if result == paho.MQTT_ERR_SUCCESS:
        print("success in subscribing.")


def on_subscribe(client, userdata, mid, granted_qos):
    print("Subscribed: "+str(mid)+" "+str(granted_qos))


client = paho.Client("a:<ORG-ID>:<App-ID>")

# adding callbacks to client
client.on_connect = on_connect
client.on_subscribe = on_subscribe
client.on_message = on_message
client.username_pw_set("a-<ORG-ID>-<App-ID>","my authentication token")

client.tls_set( ca_certs=None, certfile=None, keyfile=None, cert_reqs=ssl.CERT_REQUIRED,
           tls_version=ssl.PROTOCOL_TLS, ciphers=None)


client.connect("<ORG-ID>.messaging.internetofthings.ibmcloud.com", 8883, 60)
client.loop_start()

当我 运行 项目时,我得到的 rc 值为 0,这意味着 successful connection

这是 on_connect() 回调打印:

CONNACK received with code 0. result: 0 , mid: 2 success in subscribing.

并且 on_subscribe() 回调未被调用。我做错了什么?

如果您想订阅申请状态消息那么

一个应用可以订阅一个或多个应用的​​监控状态,例如:

Subscribe to topic iot-2/app/appId/mon

注意:要订阅所有应用程序的更新,请使用 MQTT“任何”通配符 (+) 作为 appId comp

基于以上,行:

(result, mid) = client.subscribe("iot-2/app/MyAppID/sensordata", 2)

应该是

(result, mid) = client.subscribe("iot-2/app/MyAppID/mon", 2)

(result, mid) = client.subscribe("iot-2/app/+/mon", 2)

如果要接收传感器数据,请使用以下行:

Subscribe to topic iot-2/type/device_type/id/device_id/evt/event_id/fmt/format_string

您需要替换:device_type、device_id、event_id、format_string(可能是 json、txt)

对于每个可能的事件:

(result, mid) = client.subscribe("iot-2/type/+/id/+/evt/+/fmt/+",2)