Forum Xamarin Xamarin.Android

MQTT implementation in my app

Hey developers
I am trying to build a wherein I need switch on and off devices I did some digging and found MQTT is helpful i such cases can someone help me on how exactly this is done also what mqttbroker is free
any help would be appreciated

Tagged:

Answers

  • jezhjezh Member, Xamarin Team Xamurai

    For this, you can check : https://github.com/xamarin/mqtt

    From the README.md in above link , we could find the Usage samples
    1.Creating and connecting an MQTT client with default options:

      var configuration = new MqttConfiguration();  
      var client = await MqttClient.CreateAsync("192.168.1.10", configuration);
      var sessionState = await client.ConnectAsync (new `MqttClientCredentials`(clientId: "foo"));
    

    2.Creating and connecting an MQTT client specifying configuration and clean session:

      var configuration = new MqttConfiguration {
        BufferSize = 128 * 1024,
        Port = 55555,
        KeepAliveSecs = 10,
        WaitTimeoutSecs = 2,
        MaximumQualityOfService = MqttQualityOfService.AtMostOnce,  
        AllowWildcardsInTopicFilters = true };
      var client = await MqttClient.CreateAsync("192.168.1.10", configuration);
      var sessionState = await client.ConnectAsync (new `MqttClientCredentials`(clientId: "foo"), cleanSession: true);
    

    For more specific information about the MQTT protocol, please see the latest Oasis spec.

  • NitishsantpurNitishsantpur Member ✭✭✭

    @jezh thanks for acknowledging I am a novice I just wanted to know if we need a webserver for this
    also, can the local SQLite database be connected to the webserver
    Will surely look into it thanks for reply

  • jezhjezh Member, Xamarin Team Xamurai

    From the official document MqttServer.cs , we will find it is MqttServer not webserver.

    can the local SQLite database be connected to the webserver

    This feature requires the client to communicate with the server, rather than the database directly exchanging data with the server.

    If you enter key words xamarin mqtt in https://github.com/, you will see many samples about this.

  • NitishsantpurNitishsantpur Member ✭✭✭

    thanks will try

  • jezhjezh Member, Xamarin Team Xamurai

    Hi @Nitishsantpur , how are you doing? I’m writing to check if provided information is helpful. Please let me know if you need further assistance. Thank you. :)

  • NitishsantpurNitishsantpur Member ✭✭✭

    Ya actually I am a novice in xamarin development I have installed mosquitto on my device for localhost and it works fine I am struggling to find on how to publish, subscribe on click events in xamarin. I have not used GitHub before and It is hard for me to find stuff there can u please give the code specific to what I may need.
    thanking you in anticipation

  • jezhjezh Member, Xamarin Team Xamurai

    Ya actually I am a novice in xamarin development I have installed mosquitto on my device for localhost and it works fine I am struggling to find on how to publish, subscribe on click events in xamarin.

    What nuget have you installed? There should be some introduction and usage samples we can refer to.

  • NitishsantpurNitishsantpur Member ✭✭✭

    yes, I installed system.mqtt.net and mqqtnet.extensions.managedclient

  • NitishsantpurNitishsantpur Member ✭✭✭
    edited September 18

    private async Task Button_ClickedAsync(object sender, EventArgs e)
    {
    var configuration = new MqttConfiguration();
    var client = await System.Net.Mqtt.MqttClient.CreateAsync("127.0.0.1", configuration);
    var sessionState = await client.ConnectAsync(new System.Net.Mqtt.MqttClientCredentials(clientId: "lens_3Ts6GxST4IkR25qghmQL0qILRv1"));

            var message1 = new System.Net.Mqtt.MqttApplicationMessage("test/", Encoding.UTF8.GetBytes("Foo Message 1"));
            await client.PublishAsync(message1, MqttQualityOfService.AtMostOnce);
    
    
        }`
    

    I've tried this for publishing and it says Severity Code Description Project File Line Suppression State
    Error Position 7:34. EventHandler "Button_Clicked" with correct signature not found in type "Iot.test"

  • NitishsantpurNitishsantpur Member ✭✭✭

    System.Net.Mqtt.MqttClientException '
    also this is what I get when I remove the button and run

  • NitishsantpurNitishsantpur Member ✭✭✭

    System.Net.Mqtt.MqttClientException '
    also this is what I get when I remove the button and run

  • jezhjezh Member, Xamarin Team Xamurai

    Could you please post a basic demo so that we can try to test on our side?

  • NitishsantpurNitishsantpur Member ✭✭✭
    edited September 22

    the code above is what I am using the one I sent

  • jezhjezh Member, Xamarin Team Xamurai

    I found you have posted an issue here: https://github.com/xamarin/mqtt/issues/185

    For this question, we don't have a test environment, you can continue follow it up in above link.

    Thank you for your support of xamarin.

    ​​​​​​​​​​​​​​

  • NitishsantpurNitishsantpur Member ✭✭✭
    edited September 25

    okay
    thanks

Sign In or Register to comment.