用于发布和订阅主题的IBM IOT代码

时间:2019-10-12 17:31:05

标签: ibm-cloud iot

嘿,我是IBM IOT的新手,但是我没有使用paho-mqtt来发布和订阅主题的简便方法。如果有任何好的文档,请告诉我。

1 个答案:

答案 0 :(得分:1)

我建议使用ibm物联网python sdk。您可以在此处找到安装说明: https://ibm-watson-iot.github.io/iot-python/

下面是有关如何连接和发布事件的示例代码

https://ibm-watson-iot.github.io/iot-python/device/#sample-code

import wiotp.sdk.device

def myCommandCallback(cmd):
    print("Command received: %s" % cmd.data)

# Configure
myConfig = wiotp.sdk.device.parseConfigFile("device.yaml")
client = wiotp.sdk.device.DeviceClient(config=myConfig, logHandlers=None)
client.commandCallback = myCommandCallback

# Connect
client.connect()

# Send Data
myData={'name' : 'foo', 'cpu' : 60, 'mem' : 50}
client.publishEvent(eventId="status", msgFormat="json", data=myData, qos=0, onPublish=None)

# Disconnect
client.disconnect()

device.yaml的示例是

myConfig = { 
    "identity": {
        "orgId": "org1id",
        "typeId": "raspberry-pi-3"
        "deviceId": "00ef08ac05"
    }.
    "auth" {
        "token": "Ab$76s)asj8_s5"
    }
}
client = wiotp.sdk.device.DeviceClient(config=myConfig)

以上信息可从IoT Platform界面获取