This tutorial uses the Thunderboard Sense 2 connecting to a Raspberry Pi 3 over Bluetooth to send your sensor data to the cloud over Ethernet or WiFi.
In this Quick Start Guide, you will:
Set up your Raspberry Pi 3 and Thunderboard
Visualize sensor data on your Medium One dashboard
Create a workflow to notify you when the Thunderboard moves
Here’s what you need to get started:
Medium One Prototyping Sandbox Account + Silicon Labs Thunderboard Sense 2 + Raspberry Pi 3 [Buy]
Micro SD card
USB Keyboard and mouse
Step 1. Set up your Raspberry Pi 3 (RPI)
Now that you have your Raspberry Pi 3, we will go through the following steps to set it up:
Set up your SD card with the Raspberry Pi Operating System
Set up your environment
Download files on to the RPI
Set up the RPI to automatically connect to the Thunderboard when it turns on
1.1 Set up your SD card
Follow the steps here.
1.2 Set up your environment
Put the SD card in your Raspberry Pi.
Plug an Ethernet cable and HDMI cord into your RPI.
(Note: If you don't have Ethernet, go to this guide to configure your WiFi to the RPI)
Plug in a USB keyboard and mouse.
Turn on the Raspberry Pi by plugging in a USB Mini and connecting it to a power source.
Wait and watch on your monitor until the RPI has completed turning on. You should see a screen like this.
If the screen is black, make sure the SD card is plugged in correctly, then unplug it then plug it back in.
If it is still black or it does not load correctly, a problem may have occurred during installation - try step 1.1 again.
1.3 Set up the RPI to automatically connect to Thunderboard
Next, open a web browser by clicking on the blue orb in the upper lefthand corner. We are going to download some files to prepare to connect to the Thunderboard later.
Visit the following link:
Click the green "Code" button and then click "Download ZIP".
The file should save and show up on the bottom of the browser. Click on it.
A dialog box should open. Click Action→ Extract.
Click on the folder button to change where it is saved. It is very important to follow these steps to extract the script in the right folder! The script to set up the Raspberry Pi expects all the files to be here, and it will not run if they are not here.
Click on “pi” on the side.
Click on “Open.”
Uncheck the box “Extract with full path.”
1.4 Run setup script
Now that you have downloaded the files into the correct directory, click on the black box in the upper left hand corner to open the terminal. You can close the web browser and unarchiver.
First, you'll want to change the terminal's location to the directory containing the setup script. If you unzipped the files correctly, the command should be the following:
"cd" tells the terminal to "change directory", and "raspberry_pi_python_code" is the folder we need to go to.
Next, you'll run the setup script, which moves key files to an "m1" folder, tells the RPI to automatically connect to the Thunderboard. To accomplish this, run the following command:
sudo sh setup.sh
"sudo" tells the terminal to run the script with administrative permissions, and "sh" runs the setup.sh script.
After about 40 seconds, you will see a message that says “Are you sure you would like to install? Y/N” in the command line. Type Y and hit enter.
Wait another minute for the script to complete.
Congrats, you have set up your Raspberry Pi!
You will need to come back to the terminal later to configure it with information from your Medium One account and Thunderboard.
Step 2. Set up your Medium One Account
In this step, we will walk you through the following steps:
Create a device user for your Thunderboard
Find your API key
Create two data streams
Later on, you will log back in, set up your dashboard, and create a workflow.
During this step, you will get some information from your Medium One account that you will need to use later when configuring your Raspberry Pi. This information is:
2.1 Create a device user for your Thunderboard.
On the left hand side, click on “Setup.” Then, click on “Manage Users.”
Click “Add New User”
In the first box, enter "thundersense". This is the device username. Enter a device password in the next two boxes.
Make note of both the device username and password for a later step.
Tip: If you add other Raspberry Pi + Thunderboard combinations to this project, simply create new users "thundersense2", "thundersense3", etc.
2.2 Find your API key
Under “Setup,” click on “Manage API Keys.”
Click “Add New API key”
Enter any description you want, make sure “Enabled” stays checked, and click “Save.” You will be redirected to the next page. The long string outlined in RED is your API key. Save this for a later step.
2.3 Create data streams
You will now create data streams for the Thunderboard to send data to. On the left hand side, click Config→ Data Streams.
Click “Create a new stream.”
For the name, type “sensor_data.” Click “Save Data Stream.”
Once again, click “Create a new stream.”
For the name, type “device_data.” Click “Save.”
You should see both of your streams now.
Step 3. Set up your Thunderboard Sense 2
In this step, we will run a script on the RPI to find the device mac address of your Thunderboard. This is necessary in order to connect to Medium One.
In the same directory that you were in for Step 1, run the following command:
sudo python scan.py
This script scans and looks for any Thunderboards nearby, and prints their device mac addresses.
Unbox and power on your Thunderboard. If you previously connected it to your phone via the mobile app, make sure bluetooth is off on your phone so it does not automatically connect.
Important: In this step and in future use, we recommend powering the Thunderboard via USB. If you don't do this, the Thunderboard will automatically go to sleep after 10 minutes to conserve battery power and will stop communicating with the RPI.
Now that it is turned on, look back at the terminal. You should see output like the following on your screen after a couple seconds.
If nothing shows up, try turning your Thunderboard off and back on.
The sequence of letters, numbers, and colons in the red box after the word “addr” is your device mac address.
Make sure to take note of the mac address as you will need this in the next step. You can end the script by typing “CTRL-C,” or just closing the terminal.
If this step isn't working, please read "Important Note: Boot up Sequence" at the bottom of this tutorial.
Step 4. Link your RPI Thunderboard to Medium One
Now, we will put everything together.
In the terminal, enter the following command:
sudo nano /m1/login.txt
This opens a text editor you will use to put your login info. You should see the following on the screen.
device_username Samplepw1 ABCDEFGHIJKLMNOPQRSTUVWXYZ 01:23:45:67:89:00
Type in, on four separate lines:
Line 1: Device Username ("thundersense" - NOT your login credentials for the Medium One platform)
Line 2: Device Password (created in Step 2 - NOT your login credentials for the Medium One platform)
Line 3: API key (created in Step 2)
Line 4: Device Mac Address (recorded in step 3)
When you are done, press CTRL-X to save. You will get prompted to overwrite or not. Type Y.
It will prompt you where to save it. Hit Enter.
Reset the board. Congratulations, you have linked everything together! Now the fun begins...
Step 5. Set up your Dashboard
You will now make sure everything is hooked together and visualize some data on the Dashboard.
First you'll restart the RPI, then you'll set up the following widgets:
Real Time Events Stream
Real Time Line Chart
Real Time Gauge
Last Value Table
5.1 Restart Raspberry Pi
First, you need to restart the RPI in order to initiate the Medium One Thunderboard script. The script starts automatically upon restart.
In order to restart, turn the Pi off and back on. Wait about 1-2 minutes or, if you are still connected to the monitor, when it has completely turned on and is on the desktop.
5.2 Real Time Events Stream
Log into your Medium One account. At the bottom of the screen, in the widgets, find “Real Time Events Stream.”
In the drop down box, find the device username you picked earlier.
Now, plug in your Thunderboard. After a few moments, it should start blinking a red light. On Medium One, you should see data starting to come in!
This widget is called the “Real Time Events Stream.” Each of the lines above, beginning with the date in blue on the left, is a piece of data coming in from the Thunderboard.
You can see sensor readings like pressure, temperature, and humidity. You can also see x_max, y_max, and z_max, which are the maximum vibration in the x, y, and z directions since data was last sent.
5.3 Real Time Line Chart
Now, scroll down and click on “Real Time Line Chart.”
Select your username from the dropdown. Then, click on the gear.
Select “sensor_data.x_max,” “sensor_data.y_max,” and “sensor_data.z_max.”
The Real Time Line Chart widget shows you the maximum values of the vibration in the x, y, and z directions over time. You will use this widget later on to see how motion sends out an alert.
5.4 Real Time Gauge
Scroll down and click on “Real Time Gauge.”
Select your user, then click on the gear.
Select “sensor_data.co2”, “sensor_data.temperature”, “sensor_data.pressure”, and “sensor_data.humidity.”
The Real Time Gauges show you readings from the different sensors on the Thunderboard. The values will change on these gauges as new data comes in.
5.5 Last Value Table
Scroll down and click on Last Value table.
Select your user. Then, click on the gear.
Select “device_data.ip_address,” “device_data.lan_ip_address,” “device_data.mac_address,” and “sensor_data.battery.”
The Last Value Table shows the last value for key information such as IP addresses, device address and battery. The values will change in real time as new values are sent.
Tip: Scroll up to the top and click “Save” (floppy disk). Otherwise, you’ll need to set up the Dashboard every time you log in!
Now that you've verified the data is coming into Medium One, you can build your first workflow to process the data.
Step 6: Build a Workflow
In this step, you’re going to create a workflow. Workflows are Python applications that run as data comes in.
This particular workflow checks to see if the Thunderboard has moved based on the G-Force. If the board has moved, it sends an email and text message alert. It limits the number of alerts to be sent to one per day. We’re going to modify the workflow to send the alerts to your phone number and email address.
6.1 Set up workflow components
Click on “Workflow Studio.”
Click on “Create”
Where it says Enter Workflow Name, type “Alert on Vibration.”
We are going to first connect the pieces of the workflow, then edit the Python code. On the righthand side, click where it says “Tags and Triggers.”
Click on “Sensor Data.” See the tags representing your sensor data drop down.
Click and drag “x_max” onto the blank canvas. A green box should appear.
Repeat this with y_max, then z_max. It should look like this.
Now, click on “Modules.”
Click on “Foundation.”
Click and drag “Base Python” onto the screen. Your workflow should look like this.
Double click on “Base Python.” A box should come up.
Click on “Inputs/Outputs.”
Click “Add Input” twice. It should look like this. Click “Save”.
There should now be three white circles on the top of the blue box, labeled “in1,” “in2,” and “in3.” Drag the white circle under the green box that says sensor_data.x_max to “in1.”
Drag the white circle under the green box that says sensor_data.y_max to “in2.”
Drag the white circle under the green box that says sensor_data.z_max to “in3.”
6.2 Set up Python code
Now we are going to add the Python code. Double click on the blue box, and delete everything in the text area so it is empty.
Copy and paste the Python code from here:
On line 10, replace the phone number with your phone number, excluding country code. Make sure to keep it in the quotation marks.
On line 11, replace the email with your email address. Make sure to keep it in the quotation marks.
Click “Save and Activate”.
6.3 Test Your Alerts
Make sure your Thunderboard is still blinking a red light. If not, press the reset button.
Go back to the dashboard on Medium One by clicking on the Dashboard icon in left menu. Find the “Real Time Line Chart”. Here, you can see the changes in vibration on the Thunderboard.
Shake the Thunderboard board back and forth for about 10 seconds. The Real Time Line Chart should jump, and you should receive a text message and an email.
Congratulations! You have connected your Thunderboard Sense 2 device to the cloud using Ethernet over a Raspberry Pi 3.
Want to learn more about what Medium One can do?
Important Note: Boot up Sequence
The Thunderboard 2 will try and find a device to pair with for about twenty seconds, and then it goes into sleep mode. You can tell if the Thunderboard 2 looking to pair because a green light on it blinks.
Because of this short pairing window, it is important to turn on the Raspberry Pi first, then turn on the Thunderboard 2. That way, the Raspberry Pi will pair with the Thunderboard 2 right when it turns on. If the Thunderboard 2 is not blinking a green light to indicate it is searching, or a red light to indicate it is connected to the RPI and the cloud, then you should power it off and back on.
Optional: How to Keep the Thunderboard Script Alive
The above setup will start the script upon restart of the RPI, but in the case that the script errors out or is killed, it won't restart unless the RPI is restarted.
Use this method if you want the script to auto-restart in the case of failure.
Step 1. Rename m1_thundersense_rpi_demo.py to m1_thundersense_rpi.py
With the original method, upon restart, the RPI looks for "m1_thundersense_rpi_demo.py" and runs it.
In order to keep these instructions simple, we’re going to rename the file so we don’t have to worry about undoing the startup script and can avoid duplicate processes running.
In the terminal, first change to the m1 directory:
Then run the following command:
sudo mv m1_thundersense_rpi_demo.py m1_thundersense_rpi.py
"mv" moves the file from the original filename to our new name, "m1_thundersense_rpi.py".
Step 2. Create a monitoring script
Now you'll create a script that checks if the m1_thundersense_rpi.py script is running, and if it's not, starts it as a background process.
First, change to the home directory:
Then, run the following command:
sudo nano checkTB.sh
Copy the following script into the file:
#!/bin/sh ME=`whoami` if [ -z "`ps aux | grep ^$ME | grep m1_thundersense_rpi.py | grep -v grep`" ]; then echo 'Restarting m1_thundersense_rpi.py' cd /m1 python m1_thundersense_rpi.py & fi
When you are done, press CTRL-X to save. You will get prompted to overwrite or not. Type Y.
It will prompt you where to save it. Hit Enter.
Step 3. Set monitoring script to run every minute
Now we will use "crontab" in order to schedule the script to run at the beginning of every minute. Cron is a program used to schedule recurring tasks, known as "cron jobs".
In the terminal, run the following to open up our "cron file":
(if this is your first time, follow the instructions on the screen to select the editor you want to use.)
Append the following lines to crontab and save and exit.
* * * * * cd ~; sh checkTB.sh;
(Don't forget to add an empty line after as shown above - otherwise the script may not run)
The * * * * * (note the spaces between each asterisk) indicates that the task should run at the beginning of each minute. (Go here to learn more about scheduling cron jobs)
cd~; changes to the home directory, and sh checkTB.sh; runs the script we created in the previous step.
Step 4. Verify
You can verify this is working by rebooting the RPI. Unplug and plug the USB power.
Wait at least 5 minutes for it to fully boot. Reset the Thunderboard to force it to pair. Observe if any events arrive on the Medium One platform.