问题
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