1
votes

I'm really new to MQTT protocol and do some tests in ubuntu and openwrt platforms using terminal commands. I have successfully install opensource MQTT broker Mosquitto on openwrt chaos calmer and I now able to publish/subscribe topic using two different terminal windows. Im planning to build a simple GUI application with minimal user interface to publish the MQTT message. I saw that M2Mqtt library for support. But when it comes to the documentation there is no proper guidance for how to connect external MQTT broker.

I followed this document http://www.hivemq.com/blog/mqtt-client-library-encyclopedia-m2mqtt

How can I publish message to MQTT broker hosted externally (in my case virtualbox - on top of openwrt ) with C# ?

1
Post what you have already tried and somebody will likely point out any problems of ithardillb

1 Answers

3
votes

The Link you mentioned already contains your answer.

To connect to the MQTT broker:

MqttClient client = new MqttClient("broker.hivemq.com");
byte code = client.Connect(Guid.NewGuid().ToString());

To publish a message, do the following:

ushort msgId = client.Publish("/my_topic", // topic
                              Encoding.UTF8.GetBytes("MyMessageBody"), // message body
                              MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, // QoS level
                              false); // retained