Connecting Raspberry Pi or any Embedded linux device to Medium One

Level: Intermediate

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.

Primary steps of this tutorial:

  1. Installing paho-mqtt package.
  2. Configure the device as a MQTT client.
  3. Connect the device to the Medium One cloud and Publish/Subscribe MQTT events to/from the cloud.
  4. Create Workflow to receive subscribed MQTT events from Medium One cloud.
  5. Create Workflow to receive subscribed ‘Initialize request’ event from Medium One cloud.
  6. Visualize the data using Medium One widgets.

What you need to get started?

  • Medium One Sandbox Account
  • Raspberry Pi 2 or 3 [Buy Kit] or any linux device
  • USB keyboard and mouse
  • Monitor screen with HDMI connector and HDMI cable to connect to Raspberry Pi
  • Ethernet cable

Prerequisites:

  • This tutorial assumes you have properly set up and updated your Raspberry Pi (RPi) or any of your linux based device, and connected 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.  

  • Medium One sandbox account and required credentials (Username, User Password, Api Key, Project MQTT  ID, User MQTT ID) in hand. If you are new to Medium One, you can refer Getting started with Medium One sandbox tutorial, to create and set up your Medium One sandbox account.
  • This tutorial assumes you are using Python 3. You can install python3 using following command on your terminal:

Step 1. Installing 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:

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

 

Step 2. Configure the device as an MQTT client

  • Clone or download the python code from https://github.com/Medium-One/raspberry-pi-m1-cloud.
  • Open Python 3 (IDLE) and paste the code into an empty file.
  • After making sure the paste into IDLE didn’t create any indentation problems, save the file as ‘medium_one_client.py’ or any other name you choose.

Update the credential’s value (m1_user, m1_password, m1_apikey, m1_project_mqtt_id, m1_user_mqtt_id, m1_client_id) in the code with the credential’s value (user name, user password, api key, project mqtt id, user mqtt id) you obtained from your Medium One sandbox account during its setup.

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

  • Now switch your screen to the dashboard of your Medium One sandbox account.
  • Configure a Real Time Streams widget. From the menu list, select Dashboard, then select Single User Real Time Events Stream widget.

 

  • Select User from the drop-down menu on the right.


NOTE: Click the ‘disc+’ icon in the top right corner to save your Dashboard preferences.

 

  • Now run the device client code that you just created and updated with your sandbox credentials.

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

NOTE: The file medium_one_client.py 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 ViewerData Streamsraw 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 there must be a ‘Connected:True’ event.

Step 4. Create Workflow to receive subscribed MQTT events from Medium One cloud

  • From the left menu bar on Medium One sandbox, select ConfigData Streams, click Edit for the raw Data Stream.

 

  • Activate ‘raw:Connected’ tag by selecting 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 → Click Create to create a new workflow. 

 

  • Give your workflow a name like ‘RPi_Request_Initialize’.

 

  • Select Tags & Triggers pane on the right toolbar.

 

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

This is a trigger that defines the workflow execution.

 

  • Next, from the Modules pane, select 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:

 

  • 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 triggered when the device client successfully connect to the Medium One cloud. As soon as this happen, the Medium One MQTT broker publishes an ‘Initialize Request’ which initializes the device.

Step 5. Create Workflow to receive subscribed ‘Initialize request’ event from Medium One cloud

  • 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’.

This workflow requires a raw:Initialized trigger, a base python module, and a scheduler: custom trigger that runs every 15 minutes.

 

  • For this, select Tags & Triggers pane on the right toolbar.

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

 

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

 

  • In the Custom tag, be sure to set the Minutely Interval to 15 minutes. Double click on Custom tag and select the user.

 

  • Enter Minutely Interval as 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 module 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.

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.

Enter the following in the script text area:

 

  • 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 terminal.

 

  • Medium One cloud will now receive the test data at every 15 minutes. Go back to dashboard on Medium One sandbox 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 published by the client.

 

  • On terminal, you can observe the data request at every 15 second.

Step 6. Visualize the data using Medium One widgets

As the device is sending the data at regular interval, 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 data as trends over the past day.

 

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

 

  • To configure the widget, select the user (‘device’ in this case) on right, and the raw:Connected tag on left.

 

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

 

  • 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. One should be based on the raw:Connected tag and the other on raw:Initialized.

 

  • 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 disc+ icon) and naming it as ‘Test Data Station.’  So, it will be readily accessible every time you log on to your Medium One account.

 

Congratulations! You have successfully completed the tutorial.

Feel free to experiment further with the workflows and envision new interesting IOT use cases with Raspberry Pi or any otherEmbedded linux devices. This tutorial is just a starting point for your custom cloud based application.

For more tutorials on other boards and devices, please visit: https://blog.mediumone.com/category/tutorials/.

somya.bansal@medium.one'

Author: Somya Bansal