Community Documents

cancel
Showing results for 
Search instead for 
Did you mean: 

MQTT Driver for LabVIEW

Overview

MQTT brokers are becoming increasingly popular for various hardware suppliers and of course, the home automation market. In very simple terms, an MQTT broker is a message dispatcher. One or more Clients can Publish topics and values to the broker and additionally, One or more clients can Subscribe to be notified when someone writes values to these topics.

 

If you are searching for an MQTT driver, then I assume you know what this is. If not, mqtt.org can provide all of the additional information you could possibly need.

 

Additionally, the MQTT specification can be found here: MQTT-v3.1.1-os Specification

 

 

Description

Firstly I have to give Kudos to Peter at daq.io. With constant reference to the MQTT specification, and referring to Peters implementation saved me a lot of time. Thus I had something working quite quickly.

MQTT-Client-API-in-native (Peter - daq.io)

 

So why did I (re)develop an MQTT Driver? Simply put, I had a few issues that I found easier to solve by reworking the implementation.

 

Implementation

Several API calls exist for the various MQTT functions. All can be tested using the aptly named 'test.vi'.

An MQTT comms server VI must run in parallel to the event loop. The comms server VI sends and receives data to the broker and dispatches data to the relevant callers.

Published data from the Broker are received by the comms server VI and then distributed within my application via a User Event. Thus it is possible for multiple VI's to register to the user event and thus, be updated when data is received from the broker.

 

Note:

The broker configuration is via clusters in the 'test.vi' block diagram.

The driver has been tested with mosquitto MQTT broker and also io.adafruit.

 

 

What is missing?

  1. Unsubscribe command not currently supported.
  2. TCPIP auto reconnect (in the case of disconnection - auto recovery).
  3. Disconnect on error. The MQTT-v3.1.1-os Specification defines many cases where a bad message should cause the client to close the connection. These have not yet been implemented.
  4. The driver can be utilised in an application only once due to non-reentrancy, single queues/user events.

 

 

Hardware and Software Requirements

LabVIEW 2013+

 

 

Steps to Implement or Execute Code

  1. Get an MQTT broker server. During my testing, I used mosquitto. However, there are of course others. A list of available brokers can be found here.
  2. Launch the project and open the VI 'test.vi'.
  3. From the block diagram, open the INIT.vi and configure your broker settings.
  4. Run 'test.vi' and click connect. If the connection is accepted, you are now free to subscribe from and publish to your broker.

Note that I also used an independent MQTT client to subscribe to the broker and verify my LabVIEW MQTT events were really working. In my case, I used mqttfx

 

 

Additional Information or References

Huge thanks to Peter at daq.io. Peters driver was referred to often during this implementation. Peter is aware of my development.

 

Source code is now available in GitHub: https://github.com/cowen71/mqtt-LabVIEW

Comments
JensG69
Active Participant
Active Participant
on

Very nice API indeed. Smiley Happy

 

There's an error in VI "\class\mqtt\mqttCmd\mqttCmdPublish\mqttCmdBuildPayload.vi": The payload for a publish command can be anything, so changing the data string to UTF-8 is incorrect. I noticed that when publishing a JSON-String (that already had been converted to UTF-8). That did not work to well. Smiley Wink

 

Wishful improvements (IMHO):

  • connector settings to MQTT-brooker not part of source-code. Just add controls to api_mqttInit.vi
  • Add an conditional disable structure to the VI "TCP_NoDelay_Windows.vi". Thus the API can run on RT-targets without any adjustments
  • change connector panes to widely accepted 4x2x2x4 pattern.
  • the "api_mqttConnect.vi" will tell "connection accepted" when there's no answer from the broker. "Timed-Out" of the Wait für Notification should be handled.
  • It would be nice, if TCP-Open and TCP-Close could be started by the programm and not automatically inside the TCP-server.

Jens

Kudos are welcome...
cowen71
Member
Member
on
Hi Jens, Thank you for the feedback. I am sorry I am responding so late. I agree with most of your recommendations and when I have the time I will come back to this and update it further. The only small disagreement is regarding the conector pane. This was developed in my own time for a work application. The work application standardises on the patern 5x3x3x5 pattern. In this case, changing the pattern to match the online standard breaks standardisation in our own project. However, I do understand your point of view here. If you have already made modifications, please send them to me and I will republish with them (with redits to you of course ;-)).
袁航
Member
Member
on

 Thanks a lot for your sharement, 

However, in "INIT. VI", I couldn't configure my USERNAME and PASSWORD, so, the result is "Connection refused: bad username or password"

Could you please tell me how to configure username and password?

Thanks a lot

cowen71
Member
Member
on

Hi,

 

The MQTT object includes the fields for the username and password and so the following actions are needed:

  • Enable the username and password checkboxes in the connflags cluster (in the init.vi).
  • Write the username and password to the user info cluster in the object (the accessor needs adding to the init.vi but...
    • The accessor to write this data should be located here: "....\mqtt_lv2013\src\class\mqtt\". But it is not there, I can only assume I forgot to create it!
      I will try and find a few moments to add this this week. However, it is very easy if you want to do this yourself.

.Greetings.

cowen71
Member
Member
on

OK, time seems to be my biggest problem currently.

 

I have uploaded a newer version of the driver but with only very minor modification unfortunately.

  1. The call to the  "TCP_NoDelay_Windows.vi" can now be skipped by the use of an conditional disable structure.
  2. The username and password can now be provided from the main test.vi.
  3. All configurations can now be entered from the test VI due to control inputs to the init vi.
  4. I believe the 'connection accepted' message when we have a connection timeout will not appear.

I really do welcome input from other developers to improve the driver further. There are several comments/requests above that make good sense to mebut I do not have the opportunity to implement them.

mRadziwon
Member
Member
on

Thanks Cowen for a nice expansion of peter's work.

As I work a lot with MQTT-LV applications I'd love to share my findings and contribute to your project. Have you considered publishing it on github or similar platform to make collaboration easier and more efficient?

cowen71
Member
Member
on

Hi mRadziwon.

I would be very happy to do this actually.

I try and find time over the weekend to organise this.

Gruß

cowen71
Member
Member
on

Hi,

My first experience of using GitHub to share my code - of course used it to access code previously.

It is here:

https://github.com/cowen71/mqtt-LabVIEW

If I have something incorrect, please shout.

 

ssingh1
Member
Member
on

Hello cowen71,

 

Thanks for your MQTT Driver implementation for LabVIEW. I was waiting for something like this for a long time.

 

I am getting error "OpenSSL Error: error:140760FC: SSL routines: SSL23_GET_CLIENT_HELLO:unknown protocol" when connecting to CloudMQTT Server.

 

The local instance when connecting to broker on my PC is working fine. Can we connect to MQTT Server without SSL Port, as error seems something related to SSL?

 

regards

ssingh1
Member
Member
on

Regarding my above Post the problem was solved after Selecting User Name and Password Flags in Connection Info.

FredPr
NI Employee (retired)
on

I think, what i found error in your code.

For example, client recieved connection refused (error 66 from TCP write) from broker (i don`t know why):

TCP connection in this time remain open. Next subVI set connection ID in 0.

In next iteration, you run MQTT open again, but TCP connection from client is opened. In this case, connection ID use old (what was refused by server). Error loop.

 

I place connection ID away from case:

 Снимок.PNG

Then, subVI "MQTT close" get connection ID, clouse it. In next iteration connection is established.

 

Alexandra11
Member
Member
on

Hello, good afternoon.
We are trying to connect a publisher and subscriber in a NI MyRIO, but the connection is not done correctly, how I could have several clients in the same computer?

Cedric51
Member
Member
on

Hi,

 

Very nice job!

I would like to test this library but unfortunately, I have Labview 2011. Is it possible to convert the code for Labview 2011 or older and share it on this page? thank you.

 

Cedric

EstebanVS
Member
Member
on

Hello, i know this is too much to ask, but, can anybody can share this AWESOME Driver but for LabView 2011, will be vey nice if anybody can do,

thanks in advance!!!! and for what i can see, awesome job!

Hatim
Member
Member
on

Hi

 

I am slightly new to MQTT protocol. I am using the code provided by Cowen71, thanks by the way for sharing the code, it really helpful. I can send the data to my local computer using local mosquito broker (MQTT.fx) as described in the overview of MQTT at the top of this page. The problem is that I cannot send data to the Azure Internet of Things (IoT) hub using this code as the data is not secured. I am using share access signature (SAS) to access the hub.  I am not sure how to secure the data sent by the code provided by Cowen71 (e.g. to include CA cerfificate) in the code. Any help is greatly appreciated. Many thanks in advance.

shrofsp
Member
Member
on

Hi,

Thanks a ton for providing such a useful MQTT library. I want ot develop a MQTT subscriber with TLS ( a CA certificate enabled option). How could I do it? As I donot have have option of identifying the certificate files and key files which is what is needed for a typical subscriber application in Certificate mode.

Any pointers on how implement it also would be very helpful.

Looking forward to prompt responses.

Best regards

Limsg
Member
Member
on

 

+1 to the post above. Any advise on using the mqtts/tls protocol? Thank you.

Mads
Active Participant
Active Participant
on

With TLS now natively supported in LabVIEW 2020 extending the MQTT client with TLS support might be relatively simple.  I'll upgrade to 2020 soon and have a look...Perhaps someone is already working on it though?

Mads
Active Participant
Active Participant
on

Check out this thread for an MQTT client that works for secure connections:

 

https://lavag.org/topic/21615-azure-iot-mqtt/?tab=comments#comment-132669

 

Or download it directly here:

https://github.com/LabVIEW-Open-Source/LV-MQTT-Broker/releases/tag/1.0.2

 

alexderjuengere
Active Participant
Active Participant
on

@cowen71 wrote:
 
  1.  

Note that I also used an independent MQTT client to subscribe to the broker and verify my LabVIEW MQTT events were really working. In my case, I used mqttfx

 

 


nice, I used the open-source http://mqtt-explorer.com/ to validate 

andy_714511
Member
Member
on

@cowen71 wrote:

  1. when i subscribe message, the time over 60 mins , the message will stop. i update the subTimeout, it sames no Response?Timeout.PNG

bjollies
Member
Member
on

This API is well written and very useful.  I have modified it slightly to work with my solution, but one feature I cannot figure out is how to unsubscribe to a broker.  I don't want to disconnect and reconnect with a new subscription.  I see support for "subscribe", but nothing for "unsubscribe".  Does anyone have insight as how to accomplish that?

Mark_Ramsdale
Active Participant
Active Participant
on

How does this model handle running multiple publisher/subscriber groups under the same implementation?


Can it be used to manage multiple instances of servers?

A bit of a rooke, but I am already connected and using the Mosquito MQTT?  Sorry for the semi-newbie questions on this 🙂

-------
Mark Ramsdale
-------
Contributors