Tutorial: Using the Sense Tecnic MQTT platform with FRED (Cloud Node-RED)

MQTT is a popular protocol for data transmission for many developers, especially for Internet of Things applications. The new Sense Tecnic MQTT service (STS MQTT) provides FRED users an easy way to setup and to manage MQTT connections with devices and Node-RED on FRED, devices and elsewhere. This tutorial shows you how to use the STS-MQTT service with Node-RED on FRED.

About STS-MQTT

MQTT is a lightweight connection protocol based on a publish and subscribe (pub/sub) model that decouples senders or publishers of data from receivers, or subscribers.  STS-MQTT is a cloud-hosted MQTT broker service that not only provides a reliable MQTT connection but also enhances the user experience with a simple, clean and efficient user interface for managing topics and connections.

Create a STS service Account

If you don’t already have an account on FRED, set one up now. You will need to be a paid user (FRED Tall or Grande plan) in order to create clients as it is required in this blog post. You can visit https://fred.sensetecnic.com or https://mqtt.sensetecnic.com to create your STS account and check out the subscription options.

After registering, make sure to activate your account by email. You will not be able to log in until you validate your account.

Getting Started

Lets start this tutorial with a simple use case: Assume that you have a Raspberry Pi generating data, and you would like to send the data to your FRED instance via the MQTT platform.

Prerequisites for this tutorial:

  1. You have Node-RED running on your Raspberry Pi. You can refer to this if you haven’t done so.
  2. You have an already activated FRED account.

So, here is the overviews of steps in the tutorial

  1. Login to STS-MQTT platform
  2. A basic tour of STS-MQTT platform
  3. Create clients
  4. Create topics
  5. Connect to STS-MQTT in FRED and Raspberry Pi
  6. Troubleshooting
  7. Advanced tips on using STS-MQTT

Login to STS-MQTT platform

To log in to STS-MQTT, make sure you have already registered your STS service account. Since the accounts are unified on STS service platform, you can use your FRED accounts to log in on STS-MQTT platform. Go to https://mqtt.sensetecnic.com/, if you already have signed in before, you will be automatically logged in, otherwise, you will be prompted to log in with your STS service account.

If you don’t have a STS service account, you’ll need to register on the Sense Tecnic Account Registration page. Don’t forget, you’ll need to subscribe to a paid plan (Tall or Grande) to use the STS-MQTT service. Sign up here

If you have a FRED account already, since the accounts are unified, you can just click “Login here” at the landing page and enter your STS service user credentials. Go to https://mqtt.sensetecnic.com/

A Tour of STS-MQTT

As you can see from the left side navigation bar, there are three main function tabs: Dashboard, Clients, and Topics. We also have a ‘Topics Gallery’ under Community where we can see the public topics shared by other users on the service.

Dashboard

The dashboard is where you can see an overview of your MQTT activity. It provides status information on your clients, topics, system messages and error messages.

Clients

Screen Shot 2017-02-28 at 3.35.08 PM

The Clients tab shows all the clients you’ve created on the service.  You can add new clients, edit existing clients or delete clients here.

Topics

Screen Shot 2017-02-28 at 3.35.15 PM

The Topics tab shows the topics you’ve created under your instance. You can add new topics, edit existing topics, view messages sent to topics, and set a topic to public/private here.

Topic Gallery

Screen Shot 2017-02-28 at 3.35.26 PM

Under the Topic Gallery tab, you can see the public topic channels available on STS-MQTT platform shared by yourself and other users.

Create your clients

Let’s begin with creating a client. We will need to create a client for the connection that the Raspberry Pi publish data to STS-MQTT.

We go to the Clients tab, and click Screen Shot 2017-02-28 at 3.55.55 PM

This will bring up a dialog for you to create your client as shown.

* Note that only paid subscribers can create new Clients.

First, you need to enter a unique Client name for you to manage the clients on STS-MQTT platform. The Client ID and Client Key are auto-generated when the dialog shows up, but you can also overwrite with your own Client ID and Client Key. Once you are ready, you can click Add button to add the client.

Note: If you decide to edit the Client ID, remember that it must be unique across the entire service.

Note: You should copy the Client Key somewhere else as you will not be able to view the Client Key later on. If by any chance that you lost your client key, you will need to update the client with a re-generated client key.

Once you are done, you should see the new client show up in your Clients tab.

Screen Shot 2017-03-01 at 10.42.37 AM

We will repeat the same steps to create another client for connecting from FRED.  This client will subscribe to the data from the STS-MQTT broker.

Screen Shot 2017-03-01 at 10.54.18 AM

And the end result on the Clients tab should look something like this:

Screen Shot 2017-03-01 at 10.55.29 AM

Note: You should have a unique client for each connection you are making to the STS-MQTT broker.

 Create a Topic

Next, let’s go to the Topics tab and create a topic by clicking Screen Shot 2017-02-28 at 3.55.55 PM

Again, a dialog will appear. Let’s go ahead to fill in some information.

* Note that only paid subscribers can create new Topics.

Screen Shot 2017-03-01 at 11.00.38 AM

Once you click the Add button, we will see the topic shows on the Topics tab.

Screen Shot 2017-03-01 at 11.11.37 AM

Note that, the default prefix for the topic you created is “users/{username}”. For example, the username, in this case, is testrobot1, and the next level of the topic is called “demo”.

Connect to STS-MQTT from the Raspberry Pi

Let’s start this part with setting up the connection on Raspberry Pi. To start Node-RED on your pi, you can:

  • on the Desktop, select Menu -> Programming -> Node-RED.
  • or run node-red-start in a new terminal window.

Or redirect to your Node-RED folder in a new terminal window, and then enternode red.js.

Once you have your Node-RED running on the pi, access your Node-RED editor by entering http://{your-ip-address}:1880. Replace your actual IP address in the Url. For example, when I access my local Node-RED, I used http://localhost:1880 or http://127.0.0.1:1880.

Configure the MQTT node

We first drag a MQTT Out node to the editor, and fill in this information in the config tab.

Since this is the first time you have used the STS-MQTT server, you need to create a  new broker node in the server field. Select, new broker in the server field drop down, click the edit button to the right and fill in the fields in the new broker. Firstly fill in the connection data using mqtt.sensetecnic.com in the server field and your own client ID (from the STS-MQTT UI) in the Client ID field

Screen Shot 2017-03-01 at 12.44.27 PM

Then select the security tab and fill in your security information. You use your regular username for the username field. However, you have to use your client key that you created before (and noted down) before when you created the MQTT client using the STS-MQTT UI. Don’t use your regular FRED password.

Screen Shot 2017-03-01 at 12.44.39 PM

Note again:-): Do not put your FRED password in the MQTT password field, this will not work.  Enter the Client Key there.

Finally, you click add and then you use the new broker in the MQTT node configuration as shown in the screenshot below. The topic is the topic you just created using the STS-MQTT UI. In our case that was users/testrobot1/demo.

Screen Shot 2017-03-01 at 12.44.48 PM

Let’s drag an Inject node onto the editor and set it up to inject a string “Hello World!” every 10 seconds.

Screen Shot 2017-03-01 at 12.40.18 PM

So at the end, you should see this in the Node-RED canvas:

Screen Shot 2017-03-01 at 12.45.02 PM

Now, if you go to the dashboard page of your STS-MQTT platform, and subscribe to “users/{your username}/demo” by entering this topic in the subscribe field and click the “subscribe” button. You should then see the “hello world” data coming in.

Screen Shot 2017-03-01 at 12.56.54 PM

You can also monitor the messages in that topic by going to the topic tab, and click on a topic that you would like to monitor. A dialog will show up to display the real-time messages.Screen Shot 2017-03-01 at 2.26.59 PM

Now we have the connection between the Raspberry Pi and STS-MQTT platform set up. We can move on to connecting FRED.

Connect to STS-MQTT from FRED

On FRED, let’s drag out a MQTT in node and a debug node onto the editor.  Just like the other client we had setup on the Raspberry Pi, we will fill in the information we have when we created the “client-on-FRED” client.

Screen Shot 2017-03-01 at 1.49.44 PM

Screen Shot 2017-03-01 at 1.49.52 PM

Screen Shot 2017-03-01 at 1.49.59 PM

So, what you have in the FRED Node-RED canvas should be something like this:

Screen Shot 2017-03-01 at 2.21.59 PM

Once deployed, and if all of your configurations are correct, you should see messages coming in the debug tab:

Screen Shot 2017-03-01 at 2.22.08 PM

Troubleshooting

  • I cannot log into the STS-MQTT platform.

Your STS service accounts are unified which means that your account can be used on our FRED system and STS-MQTT.

If you have forgotten your password, simply reset your password at the login page.

If you have not yet activated your account via our confirmation email, you can request to resend the activation email at the login page.

  • I cannot create new clients.

Only paid users are allowed to create clients. If you have reached the limit on your client list, try removing unused clients.

  • I cannot create new topics.

Only paid users are allowed to create topics.

  • Can I reuse client ID

The short answer is no. Each client ID is unique and only one connection is allowed with a client ID at any time.

  • I forgot my client key

Client key is required when you connect to the STS-MQTT platform. The client key is only visible when the client was created. If you lost your client key, you will need to regenerate the client key or update with your own key.

To do so, simply go to the Clients tab, click the edit button of the client that you had forgotten the client key. In the pop-up dialog, enter your own client key or click regenerate client key. Ensure you copy the new client key and then click the Update button.

  • What do I put in for the password field in the MQTT node on Node-RED/FRED?

You will enter your client key in the password field in the MQTT node.

  • I cannot connect to STS-MQTT on my Raspberry Pi/FRED

Ensure you have entered the correct server address, server port, client-ID, user name and client key in your MQTT node. Also, check if you have successfully created the client on STS-MQTT.

You can also view the debug messages in the dashboard of STS-MQTT.

  • My MQTT node keeps connecting and disconnecting alternatingly

This could be due to a duplicate connection to a client ID. Check if there is only one connection to the client ID you are trying to reach.

  • My MQTT node is connected, but I cannot send/receive messages.

You can monitor a topic in STS-MQTT to check if the message is reaching the STS-MQTT platform. You can do so by subscribing the topic in the dashboard or click on a specific topic in the topic field. You can double check if you publish/subscribe to the correct MQTT topic, and if you have the right to publish/subscribe to this MQTT topic.

Advanced ways of using STS-MQTT

For full details and more advanced information on using STS-MQTT, please refer to the documentation here.

About Sense Tecnic

Sense Tecnic Systems Inc have been building IoT applications and services since 2010. We provide FRED, cloud hosted Node-RED as a service to the community. We also offer a commercial version to our customers, as well as professional services. Learn more.