Netduino home hardware projects downloads community

Jump to content


Photo

M2Mqtt Library

M2Mqtt

  • Please log in to reply
12 replies to this topic

#1 CT1

CT1

    Advanced Member

  • Members
  • PipPipPip
  • 36 posts

Posted 17 July 2015 - 10:57 PM



public static void mqttSubscriber()
(   
    // create client instance
    MqttClient client = new MqttClient(IPAddress.Parse(MQTT_BROKER_ADDRESS));

    // register to message received
   client.MqttMsgPublishReceived += client_MqttMsgPublishReceived;
   string clientId = Guid.NewGuid().ToString();
   client.Connect(clientId);

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

   static void client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
   {
     // handle message received
   }

I am trying to use the M2Mqtt Library written by Paolo which I downloaded from codeplex.

I extracted the library and added M2Mqtt as a Project to Solution Explorer.

 

In my main project I have added the code listed above.

I am getting an error saying MqttClient not found am I missing a directive or a reference?

 

This is the first time I have tried adding a library and I'm guessing I'm not doing it right, can someone point me in the right direction?

Thanks

 

 



#2 CT1

CT1

    Advanced Member

  • Members
  • PipPipPip
  • 36 posts

Posted 20 July 2015 - 10:07 PM

using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.NetduinoPlus;
using uPLibrary.Networking.M2Mqtt;
using uPLibrary.Networking.M2Mqtt.Messages;
using System.Text;

namespace netduinoplus.app
{
    public class Program
    {
        public static void Main()
        {
            // create client instance 
            //MqttClient client = new MqttClient(IPAddress.Parse("iot.eclipse.org"));
            //MqttClient client = new MqttClient("85.119.83.194");
            //MqttClient client = new MqttClient("dev.rabbitmq.com", 1883, false, null);
            MqttClient client = new MqttClient("iot.eclipse.org", 1883, false, null);
 
 
            // register to message received 
            client.MqttMsgPublishReceived += client_MqttMsgPublishReceived; 
            client.MqttMsgSubscribed += client_MqttMsgSubscribed;
            client.MqttMsgUnsubscribed += client_MqttMsgUnsubscribed;
            client.MqttMsgPublished += client_MqttMsgPublished;

            client.Subscribe(new string[] { "sensor/temp" }, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE });
            client.Publish("sensor/temp", Encoding.UTF8.GetBytes("10.1"));
            var state = client.Connect("netduino_Client", null, null, false, 0, false, null, null, true, 60);

            /*
            float temp = 80;
            while (true)
            {
                // get temperature value... 
                temp = temp + 1;
                // ...publish it to the broker 
                client.Publish("sensor/temp", Encoding.UTF8.GetBytes("10.1"));
                mqttClient.Publish(MQTT_TEMP_TOPIC, Encoding.UTF8.GetBytes(temp.ToString()));
                Thread.Sleep(10000);
            }

            */

            Thread.Sleep(Timeout.Infinite); 
        }
      

    public static void client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
    {
        // access data bytes throug e.Message
        Debug.Print("Message: " + e.Message);
    }

    public static void client_MqttMsgUnsubscribed(object sender, MqttMsgUnsubscribedEventArgs e)
    {
        // write your code
        Debug.Print("Unsubscribed ");
    }

    public static void client_MqttMsgSubscribed(object sender, MqttMsgSubscribedEventArgs e)
    {
        // write your code
        Debug.Print("Subscribed ");
    }

    public static void client_MqttMsgPublished(object sender, MqttMsgPublishedEventArgs e)
    {
        // write your code
        Debug.Print("Published ");
    }   
    }
}

Ok, I'm making progress.  I learned how to include M2Mqtt in the project using NuGet. :)

 

I've recoded the netduino piece following Paolo's examples but am having a problem connecting 

A number of First Chance exceptions are coming up....

 

Can someone tell me the secret to getting Paste to work in this box?

Does MQTT work on the NetDuino Plus version 1?

thanks

 

 

 

 

 

 

 

 

 



#3 Cuno

Cuno

    Advanced Member

  • Members
  • PipPipPip
  • 144 posts
  • LocationZrich / Switzerland

Posted 21 July 2015 - 08:24 AM

Can someone tell me the secret to getting Paste to work in this box?

Does MQTT work on the NetDuino Plus version 1?

thanks

Unfortunately it looks like the forum software does not support Internet Explorer. It works in Firefox...

 

Cuno



#4 Nevyn

Nevyn

    Advanced Member

  • Members
  • PipPipPip
  • 1072 posts
  • LocationNorth Yorkshire, UK

Posted 21 July 2015 - 09:00 AM

Go to the More Reply Options editor and that supports paste from IE, well version 8 anyway.

 

Regards,

Mark


To be or not to be = 0xFF

 

Blogging about Netduino, .NET, STM8S and STM32 and generally waffling on about life

Follow @nevynuk on Twitter


#5 CT1

CT1

    Advanced Member

  • Members
  • PipPipPip
  • 36 posts

Posted 21 July 2015 - 12:30 PM

Thanks for the tip on the paste, couldn't get it to work for anything yesterday.

So back to my main problem. Is MQTT suppose to work on a NetduinoPlus v1 or am I just spinning my wheels?

Here's the error messages I'm currently getting

A first chance exception of type 'System.ArgumentException' occurred in System.dll
A first chance exception of type 'System.NotSupportedException' occurred in M2Mqtt.NetMf.dll
A first chance exception of type 'uPLibrary.Networking.M2Mqtt.Exceptions.MqttCommunicationException' occurred in M2Mqtt.NetMf.dll
An unhandled exception of type 'uPLibrary.Networking.M2Mqtt.Exceptions.MqttCommunicationException' occurred in M2Mqtt.NetMf.dll

#6 Nevyn

Nevyn

    Advanced Member

  • Members
  • PipPipPip
  • 1072 posts
  • LocationNorth Yorkshire, UK

Posted 21 July 2015 - 12:56 PM

Instant thing that springs to mind - is the library using SSL because this was not supported on N+1.

 

Regards,

Mark


To be or not to be = 0xFF

 

Blogging about Netduino, .NET, STM8S and STM32 and generally waffling on about life

Follow @nevynuk on Twitter


#7 CT1

CT1

    Advanced Member

  • Members
  • PipPipPip
  • 36 posts

Posted 22 July 2015 - 11:21 AM

So back to my main problem. Is MQTT suppose to work on a NetduinoPlus v1 or am I just spinning my wheels?


I found an old example in the forum yesterday by Dan Anderson & I got it to work! So the answer is YES MQTT does work on the NetduinoPlus V1. Will need to figure out what I'm doing wrong on Paolo's version but for now I'm moving on to the other side of this project...the Android Phone

Thanks

#8 spawnkid

spawnkid

    New Member

  • Members
  • Pip
  • 5 posts

Posted 30 September 2015 - 08:16 AM

This MQTT library (version tested 4.2.0.1 from nuget) doesn't work on a new Netduino.IP ver 1.0.1 firmware

 

Exception thrown: 'System.ArgumentException' in System.dll
Exception thrown: 'System.Net.Sockets.SocketException' in Netduino.IP

 

Edit: tested using Netduino Plus 2 device



#9 Dr Who

Dr Who

    Advanced Member

  • Members
  • PipPipPip
  • 261 posts
  • LocationNYC

Posted 30 September 2015 - 03:43 PM

This MQTT library (version tested 4.2.0.1 from nuget) doesn't work on a new Netduino.IP ver 1.0.1 firmware

 

Exception thrown: 'System.ArgumentException' in System.dll
Exception thrown: 'System.Net.Sockets.SocketException' in Netduino.IP

This has been covered before. The version 1 device doesn't do SSL stuff because the other physical objects, what makes it work, are not present.

 

Of course there might be other issues present. Make sure you're using the right kit, and the right software for this job.



Doctor Who
"This signature does not exist!"

#10 spawnkid

spawnkid

    New Member

  • Members
  • Pip
  • 5 posts

Posted 16 October 2015 - 04:52 PM

I am refering to Netduino Plus 2 device :)



#11 Dr Who

Dr Who

    Advanced Member

  • Members
  • PipPipPip
  • 261 posts
  • LocationNYC

Posted 20 October 2015 - 06:19 AM

I am refering to Netduino Plus 2 device :)

And that is exactly what I mean. Even the NP2 might not have the intelligence for using SSL features. Was the code originally written and tested on the NP2? Or for example was it built and tested on on of the newer N3s? What release (read what year) was the Visual Studio environment, and what release of the Dot-Net Microframework did you use, et cetera.



Doctor Who
"This signature does not exist!"

#12 piiper

piiper

    New Member

  • Members
  • Pip
  • 6 posts

Posted 16 June 2016 - 08:35 AM

This problem has nothing to do with SSL, as long as you are not using secure connections.

 

The library does not work with Netduino1plus and 2 plus because of "Environment.TickCount". This throws the "'System.NotSupportedException". Somehow it's not available on Netduino platform but it's included in .net micro framework.

 

If you still want to use the library replace all "Environment.TickCount" with "(Int32)(DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond)". There are 11 replacements to be done. After this the M2Mqtt library works fine.



#13 spawnkid

spawnkid

    New Member

  • Members
  • Pip
  • 5 posts

Posted 26 October 2016 - 02:10 AM

Thank you for the clarification Mr. Piiper, In the first place I don't use SSL at all (as what Dr. Who thought). Using SSL over LAN is not necessary as long as the traffic doesn't go out of your WAN/Internet and having SSL can have performance impact to the Netduino because of encryption stuffs going on.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.