Search
  • Medium One

Step-by-Step Guide on MQTT.fx and Medium One

Updated: 7 days ago

In this tutorial, we will explain how to send MQTT messages to Medium One’s IoT Platform using MQTT.fx. This is a useful tutorial to get started on understanding how Medium One's MQTT broker works before implementing in your device.


In this tutorial, you will:

  1. Publish events to Medium One from MQTT.fx

  2. Subscribe to events from Medium One in MQTT.fx

  3. Build a workflow to publish events on Medium One


Here’s what you need to get started:

  1. Medium One Prototyping Sandbox [Buy]

  2. MQTT.fx [Download]

  3. Completion of our Quick Start Guide [Link]


Step 1. Get MQTT username and Client ID

The username is <project_id>/<mqtt_user_id> and the Client ID is <project_id><mqtt_user_id> (the username without the "/").

In this example, the mqtt_username is: AfzvMXSqmrM/4KEHYifLft0 and the Client ID is AfzvMXSqmrM4KEHYifLft0.

Step 2. Get the MQTT password

mqtt_password = <api_key>/<user_password>

Here’s the api key.

You should know the user password. If not, you can reset it here.

Step 3. Setup MQTT.fx

Click the settings gear box to configure (below).




Be sure to use the Client ID from step 1.


Enter the MQTT Username and password from above here:



Click OK when done, then click connect. You should see a green box on the right (below)





Next, we'll publish an event by going to the "Publish" tab.


To publish to your user, specify the Topic in the dropdown using the format 0/<project MQTT ID>/<user MQTT ID>/<device>. The device can be anything. Make sure to put {“event_data”:{“hello”:”world”}} as the text to send as the Payload and then click "Publish"



Step 4. See Events on Medium One

On the Medium One dashboard, select the Real Time Events Log widget. Then select the user from the top right of the widget. Click "Publish" again to see the event come in.

If you want to enable the events for Workflows, go to Config > Data Streams > raw. You will need to do this in preparation for a later step of the tutorial.

For any tag you want to enable, click the checkbox in the row for the corresponding tag name and click "Save Data Stream".


Step 5. Subscribe to an Event on MQTT.fx


Click the "Subscribe" tab. To subscribe to data from your account, the format is 1/<project_mqtt_id>/<user_mqtt_id>/<device>/event. Note that the last parameter is the word “event” and does not change.  Remember what you put in for <device>, you will need it in the next step. We put “test_device” for this example. Click on “Subscribe.”



Step 6. Build a Workflow to Respond to MQTT.fx

Click on “Workflow Studio" and then “Create a New Workflow.” Name it “MQTT Response”.


Under Tags and Triggers in the righthand sidebar, click on raw->hello. Drag and drop this tag onto the screen.



Under “Modules,” find Foundation->Base Python. Drag this onto the screen as well. Connect out1 from the green box into in1 in the blue box. Your workflow should look like this.

Double click on the blue box and replace the code in there with the following python code:

import MQTT
MQTT.publish_event_to_client('test_device', '{"hi": "back"}', encoding='utf-8') 

Make sure to replace ‘test_device’ with whatever device name you subscribed to in the previous step.

Click “Save and Activate.”


Step 7. See the Communication Flow in MQTT.fx

Go to the Publish tab Mqtt.fx and click “Publish.” Wait a few moments. Click on the Subscribe tab. You should see that a new event came in. Click on it.

In the black box, you should see the text specified in the workflow.

Congratulations! You have written a workflow that can publish events back to an MQTT client.



Want to learn more about what Medium One can do?

Check out our documentation and tutorials & get started on your next IoT project!

© 2018 Medium One   All Rights Reserved

4633 Old Ironsides Dr. #280, Santa Clara, CA  95054                

info@mediumone.com

|

  • YouTube - White Circle