ThingSpeak for Trendy Users
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:
- Create a Channel and collect data
- Analyze and Visualize the data
- Act on the data using any of several Apps
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.
Create a ThingSpeak Channel
- Sign In to ThingSpeak using either your MathWorks Account or ThingSpeak account, or create a new ThingSpeak account.
- Click Channels, and then My Channels.
- On the Channels page, click New Channel.
- Check the boxes next to Fields 1 and 2. Enter these field values:
- Name: Mary Maersk Location
- Field 1:
- Field 2:
- Click Save Channel.
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:
Latitude / Longitude
(Enter your Channel ID)
(Enter your Channel's Write API Key)
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
- Enter the following MATLAB code:
% Read the last 100 data points from a ThingSpeak Channel
data = thingSpeakRead(73734,'ReadKey','2I1LW1LFEWZZ0JEV','NumPoints',100);
% Assign latitude to field1
latitude = data(:,1);
% Assign longitude to field2
longitude = data(:,2);
% Load map image
url = 'https://upload.wikimedia.org/wikipedia/commons/thumb/7/74/Mercator-projection.jpg/773px-Mercator-projection.jpg';
img = imread(url);
[imgH,imgW,~] = size(img);
% Adjust lat/long for map image
mercatorLon = @(lon) mod((lon+180)*imgW/360, imgW);
mercatorLat = @(lat) imgH/2-log(tan((lat+90)*pi/360))*imgW/(2*pi);
x = mercatorLon(longitude);
y = mercatorLat(latitude);
% Plot markers on map
plot(x,y, 'r.', 'MarkerSize',10)
plot(x(end),y(end), 'ro', 'MarkerSize',10)
axis([325 719 84 395])
- Click Run and Save to validate your code and see a map plot of the ship’s location.
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.