Tutorial designed by Dan Kane
This is an introductory tutorial that demonstrates how Medium One cloud connects to and communicates with Raspberry Pi.
In this tutorial, you will make a simple weather station by connecting a Raspberry Pi with an attached Sense Hat to the Medium One cloud. The Sense Hat includes temperature, humidity, and barometric pressure sensors.
Simple workflows created in the Medium One Sandbox will periodically request temperature, humidity, and barometric pressure measurements from the Pi. You will use Medium One dashboard widgets to visualize the data.
Communication between the Raspberry Pi and the Medium One cloud is established via MQTT. Medium One has a comprehensive MQTT library.
Note that the bulk of the work in this tutorial is spent installing an MQTT library on the Raspberry Pi and configuring it as a Medium One MQTT client. Once that is done, we’ll demonstrate simple workflows to request data from the Pi. Much more elaborate data analysis is possible, but this tutorial will get you started.
This tutorial assumes you have properly set up and updated your Pi, and your Pi is connected to the internet. Instructions for doing this can be found at the Raspberry Pi website: www.raspberrypi.org. The Raspberry Pi 3 is strongly recommended due to its speed and built in wifi, but a Pi 2 or B+ should also work.
This tutorial assumes you are using Python 3.
What you need:
- Raspberry Pi 2 or 3 (https://www.raspberrypi.org/products/raspberry-pi-3-model-b/)
- Sense Hat (https://www.raspberrypi.org/products/sense-hat/)
- USB keyboard/mouse
- Monitor screen with HDMI connector and HDMI cable to connect to Raspberry Pi
- Small philips head screwdriver (for attaching the Sense Hat)
- Ethernet cable
- Free Medium One developer sandbox account (https://app-sandbox.mediumone.com/subscribe)
1) Connect the Sense Hat to the Raspberry Pi
The hardware installation is straightforward. Here are detailed instructions from the Raspberry Pi website: https://www.raspberrypi.org/learning/astro-pi-guide/assemble.md
2) Install the Python Sense-Hat package
This python package allows you to access all of the hardware on the Sense Hat including the LED matrix and the temperature, humidity, and barometric pressure sensors, which we’ll use in this tutorial. Type the following commands in a terminal window:
sudo apt-get update
sudo apt-get install sense-hat
3) Install the paho-mqtt package
The paho-mqtt package allows you to set the Raspberry Pi up 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
4) Configure the Raspberry Pi as an MQTT client
Open Python 3 (IDLE) and paste the following 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.
Note that this code requires several user credentials which you will obtain from Medium One in the next step.
When you run the Medium One client above, it first attempts to connect to the Medium One cloud over MQTT using the credentials you’ll obtain in the next step. Upon successful connection, the client then subscribes to the Medium One topic which will be used to transmit requests for information from the cloud to the Pi. Once that is done, the client loops in a separate thread waiting for messages from Medium One published to the subscribed topic. These might include “Temp_Request” or “Pressure_Request” etc. The client then responds by performing the requested action.
Note: The following picture shows powered up board. There is a USB connector providing the power, the HDMI connector for display/TV, Ethernet cable, and USB connectors for keyboard and mouse.
5) Sign up for a Medium One Developer Sandbox account
Sign up for a free account here: https://app-sandbox.mediumone.com/subscribe
Once you have an account, you’ll receive the credentials you need. You can then plug them into the above code as follows. Note that you need to replace the strings currently entered under “m1_user”, “m1_password”, “m1_apikey”, “m1_project_id”, and “m1_userMQTT_id”.
Below is a summary of required Medium One Credentials, collected from Medium One Sandbox account.
Note: These all need to be set to your values obtained when registering with Medium One.
m1_user = “user1”
m1_password = “Abxxx3$!”
m1_apikey = “RVWEY2BLKAXJL6UEPEONQFZQGVRXXXXXXVQTGZBQGQ3TAMBQ”
m1_project_id = “0KwhxxxUBdQ” #Project MQTT ID
m1_userMQTT_id = “yazNxxxUyH8” #User MQTT ID
m1_client_id = “Rasp_Pi”
NOTE: Even though ‘m1_client_id’ can be any arbitrary string, ensure it is a unique non-zero string.
6) Connect the Raspberry Pi to the cloud
First run the Pi client code that you just created. To run the code in Python 3, got to terminal and enter:
NOTE: The file medium_one_client.py uses a forever function to ensure ongoing communication between the Raspberry Pi and Medium One’s cloud. To stop the program from running, use CTRL+C (for PC) to end it. To run it again, you need to execute the above line of code.
The following picture shows Raspberry Pi connected to Medium One.
After that, log in to the Medium One Sandbox. In the ‘Data Viewer’ select ‘Data Streams:raw.’
If the Raspberry Pi connected successfully there will be at least one event in the raw stream. Expand the most recent event, and it will be a ‘Connected:True’ event.
7) Activate the ‘raw:Connected‘ tag
In Medium One dashboard, switch to the ‘Config’ view and click the ‘Edit’ icon of raw stream.
Activate “Connected” tag by clicking the check mark icon. Then click “Save Data Stream”.
Any successfully received tags are auto-detected, and they show up here in the ‘Config’ view where you must activate them.
8) Build the first workflow
Before starting this step. Stop and restart the client program on the Raspberry Pi. Since the raw:Connected tag is now active, it will be detected and added as an option in the Workflow studio. In the Workflow Studio, create a new workflow titled ‘Initialize Weather Station’. Drag a Base Python module onto the screen.
Next, drag a raw:Connected trigger onto the screen and connect it to the base python module with a line.
The final workflow looks like this:
Double click on the Base Python module and replace the default code with the code below then click ‘Save and Activate’.
9) Build the second workflow
Before creating this workflow, restart the Raspberry Pi client code. That will result in the Pi transmitting a ‘raw:Initialized’ tag. Activate and save it as you did in step 7 for the ‘raw:Connected’ tag. Restart the client code one more time so that the now active ‘raw:Initialized’ tag shows up as a trigger option in the workflow studio.
Now create a second workflow called ‘Request Weather Data’. This workflow requires a base python module, a raw:Initialized trigger, and a custom scheduled trigger that runs every 30 minutes. You’ll need to add a second input to the base python module. Double click the module to add an input.
Here’s the final workflow.
In the ‘Custom’ trigger, be sure to set the ‘Minutely Interval’ to 30 minutes. Double click on the base python module and replace the default code with the following code.
Click Save and Activate to complete the workflow.
10) Create the final workflow
Restart the Raspberry Pi client code again. This time, it will transmit raw:Temperature, raw:Pressure, and raw:Humidity tags which you must activate in step 7. When activating these three tags, make sure you set the Extended Type to ‘Number – Time Series.’ After activating them, run the Pi code one more time.
Create a new workflow titled ‘Process Temperature’. The Pi transmits temperature data in Celsius. This workflow converts Celsius to Fahrenheit and creates a processed stream which can be used directly or as a trigger for other workflows.
You will need a Base Python module, a raw:Temperature trigger, and this time you’ll add a single processed stream connected to the output. Here’s the final workflow.
Replace the default code in the Base Python module with the following:
This time, when you restart the Pi client code, a ‘processed:Temp_Proc’ tag will be autodetected. You must activate this final tag and re run the Pi client code one last time. The workflows are now complete, and it is time to visualize the data.
11) Visualize the weather data using Medium One widgets
Now that the weather station is running, we need a way to access the temperature, humidity, and barometric pressure 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 the single user line chart widget.
To configure the first widget, select the user (‘device’ in this case), and the processed:Temp_Proc tag.
Finally, set the time period to ‘today.’
Add two more single user line chart widgets. Both need to be configured to the correct user (device in this example) and the time period should be set for ‘today.’ One should be based on the raw:Humidity tag and the other on raw:Pressure.
Save this dashboard configuration by clicking on the ‘Save As’ icon and naming it, ‘Weather Station.’ It will now be readily accessible every time you log on to your Medium One account.
Here’s an example of a barometric pressure trend.
This is just one example of how to use Medium One tools to analyze the weather data. There are also other sensors on the Sense Hat which are all accessible by modifying the client code.
The magnetometer, accelerometer, and all the features of the LED display are available.
This tutorial is just a starting point for your custom cloud based Raspberry Pi application.