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
Active Participant JensG69
Active Participant

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...
Member cowen71
Member
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

 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

Member cowen71
Member

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.

Member cowen71
Member

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.

Member mRadziwon
Member

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?

Member cowen71
Member

Hi mRadziwon.

I would be very happy to do this actually.

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

Gruß

Member cowen71
Member

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.

 

Member ssingh1
Member

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

Member ssingh1
Member

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

Fedor_Protasov
NI Employee

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.

 

Member Alexandra11
Member

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?

Member Cedric51
Member

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

Member EstebanVS
Member

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!

Member Hatim
Member

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.

Member shrofsp
Member

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

Contributors