Search code examples
ibm-cloudiot

IBM IOT code for publishing and subscribing Topics


Hey i am new to IBM IOT but i am not getting the easy way to publish and subscribe the topic using paho-mqtt. If there is any good documentation please let me know.


Solution

  • I suggest to use the ibm iot python sdk. You can find installation instructions here: https://ibm-watson-iot.github.io/iot-python/

    below is a sample code on how you can connect and publish events

    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()
    

    and example of the device.yaml is

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

    The above information can be obtained from the IoT Platform interface