ThingSpeak for Trendy Users

ThingSpeak Overview

ThingSpeak is an open data platform and API for the Internet of Things that enables you to collect, store, analyze, visualize, and act on data gathered directly from sensors or from other web sources. With ThingSpeak and its built-in MATLAB Analysis and MATLAB Visualizations apps, you can create custom trends and plots.

The primary element of ThingSpeak activity is the channel, which contains data fields, location fields, and a status field. After you create a ThingSpeak channel, you can write data to the channel, process and view the data with MATLAB® code, visualize the data with MATLAB, and react to the data with tweets and other alerts. The typical ThingSpeak workflow lets you:

ThingSpeak Tutorial

In this tutorial, we are going to show you how to use ThingSpeak to collect data about the Mary Maersk container ship and visualize its location on a map. The Maersk Triple E class container ships include a family of very large container ships. With a length of 400 m (1,312 ft), when they were built, they were the largest container ships in the world. We are going to use ThingSpeak to track the Mary Maersk around the world. We will do this using a ThingSpeak Channel, a TimeControl scheduled MATLAB Analysis, and a MATLAB visualization all within the ThingSpeak app.

Note: By default, the data stored in a ThingSpeak channel is private by default. This means to read this data, you will need to send a ThingSpeak Read API Key. If the channel is public, other users can read data from the channel without an API key.

Set Up a MATLAB Analysis to Scrape Web Data

This procedure reads the ship’s latitude and longitude that is reported at MarineTraffic.com using the “urlfilter” command in ThingSpeak’s MATLAB Analysis app. We will parse the location data and write the data to our ThingSpeak channel. “urlfilter” is able to read data from a webpage and parse the page for a field. We will read the MarineTraffic.com page for the “Latitude / Longitude” field and write the latitude to field1 and the longitude to field2 on our ThingSpeak channel using the “thingSpeakWrite” command.

Go to the Apps tab and click MATLAB Analysis. Then click New. Select the Get data from a webpage template, and click Create.

In the Name field, enter

Mary Maersk Location

In the MATLAB Code field, enter the following code customizations:

url:

http://www.marinetraffic.com/en/ais/details/ships/219018692

targetString:

Latitude / Longitude

writeChannelID:

(Enter your Channel ID)

writeAPIKey:

(Enter your Channel's Write API Key)

urlfilter:

data = urlfilter(url, targetString, 2);

Click Run and Save to validate your code. Any errors in the code will be indicated in the Output field. Data will be written to the ThingSpeak channel every time this MATLAB Analysis is ran.

Schedule MATLAB Analysis with TimeControl

Use the TimeControl app to schedule the Mary Maersk Location MATLAB Analysis code to be ran automatically every hour. By scheduling the MATLAB code, the ThingSpeak channel will get updated with the ship’s location and we can track its position over time.

On your MATLAB Analysis page, scroll to the bottom, and click TimeControl to open the app with MATLAB Analysis preselected in the Actions field and the Mary Maersk Location as the Code to execute.

Name your new TimeControl Mary Maersk Location Tracking

Choose Recurring in the Frequency field.

Choose Hour in the Recurrence field.

Click Save TimeControl

Create a MATLAB Visualization

We now have the raw location values stored in our ThingSpeak channel. We will use MATLAB Visualizations to plot the location over time on a map. This will give us a much cleaner way to see the ship’s location.

Go to Apps > MATLAB Visualizations, and click New to create a visualization. Alternately, you can click on MATLAB Visualization in your Mary Maersk Location channel view.

Select the Custom (no starter code) template.

Enter a name for the visualization: Map Plot of Mary Maersk’s Location

Click Run and Save to validate your code and see a map plot of the ship’s location.

Next Steps

ThingSpeak has lots of possible uses by combining the different features and adding the power of MATLAB to drive analysis and visualizations. Try out this tutorial and see if you can track other ships monitored by MarineTraffic.com.