使用Python发布到Azure IoT中心-使用C#订阅不起作用 [英] Publish with Python to Azure IoT Hub - Subscribe with C# not working

查看:110
本文介绍了使用Python发布到Azure IoT中心-使用C#订阅不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用M2MQTT Paho Python库在设备上发布,如下所示:

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

然后,我正尝试使用M2MQTT C#库(成功连接到Azure IoT中心之后)再次读取C#应用程序中的数据,如下所示:

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

其中qosArray只有一项,它是0或1. ...但是,我没有收到任何消息.实际上,我的回调MqttClient_MqttMsgSubscribed也未调用,这意味着订阅未成功,但是我没有收到任何错误消息或异常...

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...

推荐答案

尝试以下修复:

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

这篇关于使用Python发布到Azure IoT中心-使用C#订阅不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆