Publish with Python to Azure IoT Hub - Subscribe with C# not working

吃可爱长大的小学妹 提交于 2019-12-24 20:26:48

问题


I publish with M2MQTT Paho Python library on the device as follows:

    msg = "POS {} {} {} {} {} {} {}"

    msgtosend=msg.format(tagNum, tagID, x_pos, y_pos, z_pos, qos, unk)
    print(msgtosend)
    # qos=1 - (must be when clean_session is False) 
    mqttinfo=rpiclient.publish("dwm/position", msgtosend, 1) 
    if mqttinfo.rc != 0:
        print("Publishing message with ID {) failed. Error Code={}".format(mqttinfo.mid, mqttinfo.rc))

Then, I am trying to read the data in C# application again with M2MQTT C# library (after successful connection to Azure IoT Hub) as follows:

    tpc[0] = "devices/MasterTag/messages/devicebound/#";
    tpc[1] = "$iothub/twin/PATCH/properties/desired/#";
    tpc[2] = "$iothub/methods/POST/#";
    tpc[3] = "$iothub/twin/res/#";

    mqttClient.Subscribe(tpc, qosArray);

where qosArray has only one item and it is either 0 or 1. ...and yet, I do not get any messages. In fact my callback MqttClient_MqttMsgSubscribed is not called either, which means that subscription was not successful and yet I get no error messages or exceptions...


回答1:


try the following fix:

tpc = new string[] { 
    "devices/MasterTag/messages/devicebound/#",
    "$iothub/twin/PATCH/properties/desired/#",
    "$iothub/methods/POST/#", 
    "$iothub/twin/res/#"
    };

qosArray = new byte[] { 
    MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE, 
    MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE, 
    MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE,
    MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE
   };

var subCode = mqttClient.Subscribe(tpc, qosArray); 


来源:https://stackoverflow.com/questions/57281239/publish-with-python-to-azure-iot-hub-subscribe-with-c-sharp-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!