cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

MQTT support in ThingWorx

Newbie

MQTT support in ThingWorx

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.

Tags (1)
24 REPLIES 24

MQTT support in ThingWorx

Same request,

Wondering why MQTT_Conenction and MQTT_subscriber dont have services...

A quick start quide/video would help



MQTT support in ThingWorx

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

Re: MQTT support in ThingWorx

Re: MQTT support in ThingWorx

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

Re: MQTT support in ThingWorx

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

Highlighted

Re: MQTT support in ThingWorx

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

Re: MQTT support in ThingWorx

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

Re: MQTT support in ThingWorx

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

Re: MQTT support in ThingWorx

Hi Mario,

Now I got into another problem.

After the value of a property is updated as per mqtt subscription, I want to trigger some action.

e.g. Say if temperature is above 35 degrees, I want to send an email.

I coded required snippet.

If I manually set the property of the temperature, it sends an email.

However if same property is update from mqtt, the email subscription code does run.

How can it be fixed?

Any work around/pointers?

Need help badly as I already spent couple of days, but did not get a clue.

Thanks much

Amit