• Medium One

Connect Raspberry Pi or any Embedded Linux Device to Medium One

Updated: May 16

This is an introductory tutorial that demonstrates how Medium One cloud connects and communicates with Raspberry Pi or any embedded linux device.

Communication between the Raspberry Pi (or any other embedded linux device) and the Medium One cloud is established via MQTT. Medium One has a comprehensive MQTT library.

In this tutorial, you will find that the bulk of the work in this tutorial is spent installing an MQTT library on the device and configuring it as a Medium One MQTT client. Once that is done, we’ll demonstrate simple workflows to request data from the device.  Much more elaborate data analysis is possible, but this tutorial will get you started.

Note: This tutorial assumes you have properly set up and updated your Raspberry Pi (RPi) or any linux based device and connected it to the internet.

You can find the instructions for setting up your Raspberry Pi here. The Raspberry Pi 3 is strongly recommended due to its speed and built in wifi, but a Pi 2 or B+ should also work.  

In this tutorial, you will:

  1. Configure the device as an MQTT client.

  2. Connect the device to the Medium One cloud and Publish/Subscribe MQTT events to/from the cloud.

  3. Create workflow to receive subscribed MQTT events from Medium One cloud.

  4. Create workflow to receive subscribed ‘Initialize request’ event from Medium One cloud.

  5. Visualize the data using Medium One widgets.

Here’s what you need to get started:

  1. Medium One Prototyping Sandbox Account [Buy]

  2. Raspberry Pi 2 or 3 or any linux device

  3. USB keyboard and mouse

  4. Monitor screen with HDMI connector and HDMI cable to connect to Raspberry Pi

  5. Ethernet cable

Step 1. Install paho-mqtt Package

The paho-mqtt package allows you to set up your device as an MQTT client so it can communicate with the Medium One cloud.  

In a terminal window, type the following:

sudo pip3 install paho-mqtt

Note: pip3 will only work with python 3, not with any lower version of python.

Step 2. Configure the Device as an MQTT client

  1. Clone or download the python code from

  2. Open Python 3 and paste the Raspberry Pi code into an empty file.

  3. After making sure the paste didn’t create any indentation problems, save the file as ‘’ or any other name you choose.

Update the credential values (m1_user, m1_password, m1_apikey, m1_project_mqtt_id, m1_user_mqtt_id, m1_client_id) in the code with the credentials from your Medium One account. For more information, check our docs.

Step 3. Publish/Subscribe MQTT events to/from the cloud

Now switch your screen to the dashboard of your Medium One account.

Configure a Real Time Streams widget. From the menu list, select Dashboard, then select Single User Real Time Events Stream widget.

Select the same user you used in your MQTT configuration from the drop-down menu on the right.

Now run the device client code you updated in the previous step.

To run the code in Python 3, go to the terminal and enter:


NOTE: The file uses a forever( ) function to ensure ongoing communication between the device and Medium One’s cloud. To stop the ongoing process, use CTRL+C keys on your keyboard. To run it again, you need to execute the above line of code.

Execution of the code will generate an event and you can see that published event on your dashboard.

When you run the Medium One client above, it first attempts to connect to the Medium One cloud over MQTT. Upon successful connection, the client then subscribes to the Medium One topic which will be used to transmit requests for data from cloud to the device. Once that is done, the client loops in a separate thread waiting for messages from cloud published to the subscribed topic. The client then responds by performing the requested action.

To double check the connection of the device with the cloud or in case you miss the connected event on the dashboard, select Data Viewer → Data Streams → raw events on sandbox dashboard’s left menu bar.

If the device connected successfully, there will be at least one event in the raw stream. Expand the most recent event using + icon and you will see an event with "Connected": "True".

Step 4. Create Workflow to Receive Subscribed MQTT Events

From the left menu bar on Medium One sandbox, select Config → Data Streams and click the raw data stream.

Activate ‘raw:Connected’ tag by selecting the Active checkbox for it.

Any successfully received tags are auto-detected, and they show up here in the Config view, where you must activate them.

Click Save Data Stream.

On the left again, select Workflow Studio and click Create to create a new workflow. 

Give your workflow a name like ‘RPi_Request_Initialize’.

Select the Tags & Triggers pane on the right toolbar.

Under the raw drop-down, drag and drop the Connected tag onto the main canvas.

This is a trigger that defines the workflow execution.

Next, from the Modules pane, select the Foundation dropdown then drag and drop the Base Python module onto the canvas.

Base Python is a programmatic module that you can code in Python to process your data.

Connect the raw:Connected module to the input of Base Python.

Double click on the Base Python module box, and replace the default script with the following in the script text area:

import MQTT
MQTT.publish_event_to_client('Rasp_Pi','{"Initialize Request":"True"}',encoding='utf-8')

Click Save and Activate.

Go back to the terminal and press Ctrl+C to exit from the execution of the code.

Run the code again.

Now, you will observe a subscribed message ‘Initialized Request message {“Initialize Request”:”True”}’ on terminal.

This workflow will trigger when the device client successfully connects to the Medium One cloud. As soon as this happens, the Medium One MQTT broker publishes an ‘Initialize Request’ which initializes the device.

Step 5. Create Workflow to Receive Subscribed ‘Initialize request’ Event

The previous step will result in the device transmitting a ‘raw:Initialized’ tag.

Activate and save it as you did in the previous step for the ‘raw:Connected’ tag. Now active ‘raw:Initialized’ tag will show up as a trigger option in the workflow studio.

Now create a second workflow called ‘RPi_Request_Data’.

Select the Tags & Triggers pane on the right toolbar.

Under the raw drop-down, drag and drop the Initialized tag onto the main canvas.

In the same pane, under the Scheduler drop-down, drag and drop the Custom tag onto the canvas.

Double click the Custom tag and select the user.

Set the Minutely Interval to 15. Then click Save and Activate.

Next, from the Modules pane, click on the Foundation dropdown, then drag and drop the Base Python module onto the canvas.

Connect the Initialized trigger to the first input of Base Python.

You’ll need to add a second input to the base python module to connect the custom module.

Double click the Base Python module to add an input. Expand Inputs/Outputs and click Add Input to add an additional input to this module. Click "Save".

Now, you will see two input nodes.

Connect the Custom module to the second input of Base Python.

Double click on the Base Python module box, and replace the default script with the following:

import MQTT
import Store
in1_triggered = IONode.is_trigger('in1')
if (in1_triggered):
    Store.set_global_data("initialized", "True")
is_initialized = Store.get_global("initialized")
is_initialized_string = is_initialized.encode("latin-1")
if (is_initialized_string == "True"):
    MQTT.publish_event_to_client('Rasp_Pi','{"Data Request":"True"}',encoding='utf-8')

Click Save and Activate.

Go back to the terminal and exit from the current execution of the code.

Execute the code the run again.

Now, you will observe a subscribed message ‘Data Request message: {“Data Request”:”True”}’ on the terminal.

Medium One cloud will now receive the test data every 15 minutes. Go back to the dashboard on Medium One and observe the test data events at every 15 minute interval.

This workflow will publish data requests to the device according to the custom scheduler. It will start only after the “Initialized“:“True“ message has been published by the client.

On the terminal, you can observe the data requests.

Step 6. Visualize the Data using Medium One widgets

As the device is sending the data, we need a way to access the data. This could be emailed, pushed to a mobile app, or analyzed using any number of tools.

In this case, we’ll make use of the Medium One single user line chart widget to display the data as trends over the past day.

In the Dashboard view, select Single User Line Chart widget.

To configure the widget, select the user on the right, and the raw:Connected tag on left.

Finally, set the time period to ‘Today’ on the right.

Observe the activity for the selected time period.

Add one more Single User Line Chart widget. It should be configured same as that of first one, except with the raw:Initialized tag.

You can now observe the activity for raw:Initialized tag for the selected time period.

Note: You can save the dashboard configuration by clicking on the Save As (or floppy disk+ icon) and naming it as ‘Test Data Station.’  Then it will be readily accessible every time you log on to your Medium One account.

Congratulations! You have successfully completed the tutorial.

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        


  • YouTube - White Circle