0

I've already worked with mqtt in Java. Now I need to create a C# application to subcribe and publish mqtt messages.

using MqttDotNet library

IMqtt _client = MqttClientFactory.CreateClient(connectionString, clientId);

What is the connectionString?

using M2Mqtt library

The connection succeeded, but I did not receive any published messages.

This is my code:

class Program
{
    static void Main(string[] args)
    {
        var client = new MqttClient(IPAddress.Parse("myTestIP"));

        // register to message received
        client.MqttMsgPublishReceived += client_MqttMsgPublishReceived;

        var clientId = Guid.NewGuid().ToString();
        client.Connect(clientId);

        // subscribe to the topic "/home/temperature" with QoS 2
        client.Subscribe(
            new string[] {"testTopic"},
            new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE });
    }

    static void client_MqttMsgPublishReceived(
        object sender, MqttMsgPublishEventArgs e)
    {
        // handle message received
        Console.WriteLine("message=" + e.Message.ToString());
    }
}

This my message publishing code:

mosquitto_pub -d -h testIp  -t "testTopic" -m "haai"
Massimiliano Kraus
  • 3,638
  • 5
  • 27
  • 47
Abdul Manaf
  • 4,933
  • 8
  • 51
  • 95
  • You shouldn't use MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE. Instead try to use MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE. Maybe the QoS 2 is not supported (for example, AWS IoT does not support it). – Dušan Knežević Feb 10 '17 at 15:17

2 Answers2

1

I don't think that the MqttDotNet is currently mantained. I could suggest to use my M2Mqtt client and found documentation on official web site here : https://m2mqtt.wordpress.com/

The M2Mqtt client is available on Nuget as package too here : https://www.nuget.org/packages/M2Mqtt/

Paolo.

ppatierno
  • 9,431
  • 1
  • 30
  • 45
  • but using m2mqtt, did not receive any published messages – Abdul Manaf Nov 09 '15 at 10:18
  • can be more precise please ? – ppatierno Nov 09 '15 at 11:25
  • messages published by c# client (using m2mqtt library) is successfully getting in c# subscriber( using m2mqtt library). where as messages published from linux terminal is not got. that is the actual issue. – Abdul Manaf Nov 09 '15 at 11:40
  • Too few information ... on what's the client on Linux, what's the broker, what's the topic ... – ppatierno Nov 09 '15 at 11:56
  • this is the publish command from ubuntu terminal mosquitto_pub -d -h testIp -t "testTopic" -m "haai" – Abdul Manaf Nov 09 '15 at 12:59
  • brocker is a cloud ip address – Abdul Manaf Nov 09 '15 at 12:59
  • what's the broker ? HiveMQ, Mosquitto, GnatMQ ... ? – ppatierno Nov 09 '15 at 13:09
  • can you execute two distinct tests ? First try to use parameter "-q 2" for the mosquitto_pub to publish with QoS 2 (you are subscribed as QoS 2). Other test, try to use /testTopic instead simple testTopic. – ppatierno Nov 09 '15 at 13:26
  • However it's strange .... you can publish with a QoS but subscribe with a different QoS. The QoS is established between client and broker not between client and client. It seems to be a mosquitto bug on broker side ... – ppatierno Nov 09 '15 at 14:46
  • @ppatierno is your m2mqtt still being maintained? Last real commit was a while back. Can you suggest an alternative c# mqtt lib? – Jeff Mar 17 '17 at 02:43
0

The connection string is (according to the sample code documentation on Steven Lovegroves website http://www.stevenlovegrove.com/?id=37):

Connection Strings

  • TCP connection, eg. tcp://brokerhost.com:1883
  • Bluetooth connection, eg. bt://00:10:dc:af:66:48
SysRq
  • 28
  • 4