Skip to main content
1-Visitor
June 17, 2014
Question

MQTT support in ThingWorx

  • June 17, 2014
  • 9 replies
  • 26038 views

Hi,

I wish to know more about the current available MQTT support in ThingWorx. I understand it is available through MQTT extension, but it doesn't seems to have a detailed documentation on it in this community.

Can you please explain the level of integration of MQTT support in ThingWorx, and how does it communicate to the MQTT device through ThingWorx, and if any message broker involved etc.?

Also, it is much appreciated if you could point me to any available detailed documentation/link and if there is any sample demo ThingWorx application that could be referred to.

Thanks in advance.

9 replies

12-Amethyst
January 18, 2015

Same request,

Wondering why MQTT_Conenction and MQTT_subscriber dont have services...

A quick start quide/video would help



1-Visitor
February 12, 2015

I'll add this as a third request for some samples of an MQTT connectivity subscribe and publish event.  How do you accomplish this from Thingworx?  A sample or video would be very much appreciated. Thanks, Rob

1-Visitor
September 16, 2015
1-Visitor
September 22, 2015

Hi,

Here's a steps by steps:


Installing an MQTT broker

    
     The MQTT ThingWorx extension acts as an MQTT client, so in order to test it we need to install a local broker.
     One such open-source broker can be downloaded from
http://mosquitto.org/download/

   
Download the binary Windows installation: mosquitto-X.Y.Z-install-win32.exe from the download page and install it locally.

     Mosquitto depends on PThreads and OpenSSL, but does not redistribute them, so you need to download them following the instructions      in the readme-windows.txt included in the package.

     Please note that the Mosquitto build is 32-bit, so you must download 32-bit versions of both PThreads and OpenSSL DLL’s.

     After downloading and installing those two dependencies, you’ll maybe notice the famous DLL hell come true: your Windows system is      already spammed with all sorts of OpenSSL libraries, dropped in the PATH by various applications that you might have been installed in      the past: some of them are 32-bit, some 64-bit. Since we want to be sure that we use a 32-bit version (and probably the one we have just      downloaded), you have to adjust the PATH as seen by Mosquitto, remove all directories from the path that have nothing to share with      mosquitto, and add the new one. While you might think you can copy the new DLL’s to the Mosquitto folder, you’ll soon realize that      adjusting the PATH is a much better and resilient option.


     This is the PATH that I’ve set in the Command Prompt before running Mosquitto:

     Set PATH=C:\Windows\system32;C:\Windows;D:\opt\OpenSSL-Win32;D:\opt\OpenSSL-Win32\bin;D:\opt\pthreads-w32-2-9-1-release\Pre-built.2\dll\x86


Testing Mosquitto


     Before configuring the MQTT extension, test your local Mosquitto instance, as follows.
     Open three command prompts and set the PATH in them according to the previous section:

    

     - Command Prompt for the broker:

o   Run mosquitto –v to start the broker in verbose mode


     - Command Prompt for the client that subscribes to a topic

o   Run mosquitto_sub -v -i subclient-q 2 -t random/temp to start the subscriber client in verbose mode, using the subclient ID, quality of service 2, and topic random/temp

o   You will see that the broker logs a subscription request in its standard output


     - Command Prompt for the client that publishes a message to a topic

o   Run mosquitto_pub -q 2 -t random/temp -m 33 to start the publisher client with quality of service 2, topic random/temp, and message 33

o   You will see that the broker logs a publish request in its standard output, as well as the subscriber client

Testing the MQTT extension in Thingworx

  • Download the MQTT extension from the PTC marketplace
  • Import the saved zip file from the Import/Export menu tab (Import/Export > Extension > Import) in the Thingworx composer.
  • Create a new thing and choose the MQTT template when asking for Thing Template in the General Information page

               pict01.png

  • Create the different properties related to the topic you wish to use (publish and subscribe) on the Properties page (e.g. temperature).


    pict02.png
  • To record the value history of a property checks the Logged box and add/create the value stream name in the General information page.
  • Auto mapping Settings on the Configuration page controls whether you want Thingworx to automatically generate topic name(s) based on a pattern that can include server name {s}, thing name {t} and property name {p} token(s)

          pict03.png

  • Property to MQTT topic mapping paragraph allow you to map the topic (e.g. temp/random) with the property name (e.g. temperature) and publish and/or subscribe check box.

    pict04.png

  • Connection Settings requires, at least, the serverName (e.g. test.mosquitto.org), the serverPort (e.g. 1883) and a qos (Quality of Service) value from 0 to 2:
    • 0: The broker/client will deliver the message once, with no confirmation.
    • 1: The broker/client will deliver the message at least once, with confirmation required.
    • 2: The broker/client will deliver the message exactly once by using a four step handshake.

 
           pict05.png


 

  • Save your thing and test the connection by checking the isConnected service (value = true) on the Properties page - MQTT (ThingTemplate) - Properties paragraph

    pict06.png

    (Note : I installed the Mosquitto client, subscribe to the topic and publish a message with value = 20)

  • Once validated create a new mashup:
    • mashup type = Page and Layout Options = Static
    • add three widget: a gauge, a LED Display and a Time Series Chart
    • add an Entity by clicking on the Green Plus button on the upper right corner of the Entity area and choose the previously created thing.
    • add the GetProperties service, check the Mashup Loaded box, add the QueryPropertyHistory without checking the Mashup Loaded box and click Done button.
    • expand the GetProperties service and drag and drop your property (e.g. temperature) to both gauge and LED Display widgets (Select Binding Target = data)
    • expand the QueryPropertyHistory service and drag and drop the All Data parameter to the Time Series Chart widget (Select Binding Target = data)
    • save the mashup, give it a name and click on the View Mashup button.

pict07.png


Here’s the result:

pict08.png

If you want to get more information about the Mosquitto client please check the following website:
http://mosquitto.org/

    

     pict09.png


To test the extension without installing a MQTT broker please check the following website:
http://test.mosquitto.org/

             

      pict10.png

I hope this information helps you.
Please let me knowif you have question or need more information about this topic.
Regards,


Franck Bailleux

1-Visitor
September 23, 2015

Thank you!

I've finally got a Thing running with 2-way communication to a public MQTT broker thanks to this walkthrough!

BTW, I could not get the MQTT ThingTemplate method to work -- my Thing failed to initialize.  Instead, I used the pair of MQTTConnection and MQTTSubcriber and that alternate method worked quite easily.  If I learn more about the initialization issue, I'll post it here.

For a public MQTT broker, I'm using:

   broker.mqttdashboard.com

And I'm monitoring/testing with this very nice web-based client:

   http://www.hivemq.com/demos/websocket-client/

I'll be working with MQTT I/O a lot more in the next few days.  Thanks for getting me off the ground.

Dan Thornton

ThingWorx Newbie

1-Visitor
September 22, 2015

The mqtt extention maps mqtt topics to thing properties. After installing the extension you can choose between two different solutions for the connection and two different types of binding.

Connection:

1. Use the mqtt template in a new thing and configure your broker right in the things configuration

2. Create a thing that uses mqttConnection template and configure your broker + Create a thing that inherits from mqttSubscriber and configure it to use the mqttConnection thing.

Binding:

1. Automapping: Check "Enable" in automapping section. Define a topic format for the binding. Here {s} is the servername from the federation, {t} is the thingname and {p} is the property name. For example your thingname is "MyFirstMqttThing" and that one has a property named "MyProp" and you use "/tw/{t}/{p}" as format, than every new value on the property gets published to the topic /tw/MyFirstMqttThing/MyProp, every external publish to that topic is used as the new value. Automapping seems to be bidirectional!

2. manual Mapping: Add a new line to "Property to MQTT topic mappings". Set topic to a mqtt topic and name to a property name. If you check the publish check mark every change in thingworks gets published to the mqtt topic and if you check subscribe every published value on that topic gets copied to the property. Here you can bind one thing to different topic structures.

Pitfalls:

If you change something that changes subscriptions, call restartThing after that or restart tomcat. There is an initialization problem otherwise.

Use:

For a more Event driven approach simply subscribe to the data change events.

Regards,

Mario Wolff

Solution Architect

________________________________________

exceet Secure Solutions AG

Rethelstraße 47

40237 Düsseldorf

www.exceet-secure-solutions.de

________________________________________

Vorstand: Christian Methe

Aufsichtsratsvorsitzender: Wolf-Günter Freese

Amtsgericht Düsseldorf HRB: 44361

1-Visitor
October 28, 2015

Hi,

I am trying to set one sample thing with mqtt,

It connects to mosquitto and also subscribes to the topic, but I always get "Socket error on client" when mosquitto is trying to push the message to Thingworx mqtt thing.

I am using thingworx 6.5.0 on tomcat 7.

This mosquitto server works well with eclipse paho based application on the same tomcat.

Any pointers would really of a great help.

Many Thanks

Amit

1-Visitor
October 28, 2015

Update:

I could get MQTT Thing based solution working with 2 changes.

1. Adding an auto refresh widget

http://www.thingworx.com/~/media/Files/PDFs/Academics/41RPHowtocreateaMashupforRPIstep1.ashx?la=en

2. getting the latest MQTT extension for Thingworx 6.5. The one on market place is old.

(download link worked for me was http://support.ptc.com/appserver/wcms/standards/freefull_cs.jsp?&im_dbkey=162654&icg_dbkey=893)

Hope it may provide hints for others who might be facing challenge

1-Visitor
November 9, 2015

Has anyone been able to configure TW5 to use RabbitMQ 3.5.6?

When I try to connect to the server I get an error

=ERROR REPORT==== 9-Nov-2015::11:15:49 ===

closing AMQP connection <0.298.0> (127.0.0.1:49887 -> 127.0.0.1:5672):

{bad_header,<<22,3,1,0,149,1,0,0>>}

1-Visitor
November 24, 2015

Hi Graham,

AMQP is a different protocol than MQTT.... the log message doesn't mention anything about MQTT or related ports.

You should verify your client configuration, addresses, ports, as well as the server side that it is allowing MQTT connections.

To verify the MQTT connection, try using the HiveMQ online MQTT web-client posted above in order to be sure that your broker is visible and working on the Internet.

Cheers,

Greg

1-Visitor
June 28, 2016

Can someone help me with the setup , I followed the exact steps and still it's not showing its as Connected.

What could have been wrong.

When i am testing through chrome mqttlens, its working fine. what am i doing wrong

1-Visitor
June 29, 2016

Hi,

1 - Could you check if the MQTT extension has been correctly installed.
Go to Monitor (from the composer) and choose extension item and search for MQTT extension
If something goes wrong during MQTT installation you 'll be able to find some information.

2 - What is your operating system ? and the MQTT extension release ?

1-Visitor
August 22, 2016

Hi Frank,

I have the same issue that the MQTT extension is not able to connect to my public mosquitto server.

MQTT_spy can connect without problem.

MQTT extension is also imported fine. I could not find any errors.

Any idea what I´m missing ?

1-Visitor
June 29, 2016

Have been able to figure out the issue.

Seems the extension on the marketplace is not working and as per the comment on other thread , need to download the below extension for which suppor tis requried.

http://support.ptc.com/appserver/wcms/standards/freefull_cs.jsp?&im_dbkey=162654&icg_dbkey=893

Can someone help if the above extension is available in public domain.

1-Visitor
October 11, 2016

Hello, In the meantime we understood that the MQTT extension was giving problems under Linux. MQTT extension on Thingworx Server running on Windows does not have any problems.

PTC offered a new MQTT extension begin of October and now the MQTT issue on Linux based Thingworx server is solved.

Solution: Use the latest MQTT extension from the marketplace.

1-Visitor
November 25, 2016

Hello

I have put the extension in place, everything is working fine from the device To Thingworks (subscribe MQTT)

(remark from Amit Teli about system user rigth is very important and I loose some time before to understand that my problems were coming from that)

My Question : how to publish ?

I have created a property with the publish topic (auto mapped) but how to activate it  and how to publish a message ?

EDIT :

the solution was in the explanations and it works fine (and very simple !)

"If you check the publish check mark every change in thingworks gets published to the mqtt topic and if you check subscribe every published value on that topic gets copied to the property. Here you can bind one thing to different topic structures."

Regards