ThingSpeak for Trendy Users

ThingSpeak Overview

ThingSpeak IoT Homepage

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:

  1. Create a Channel and collect data
  2. Analyze and Visualize the data
  3. Act on the data using any of several Apps

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.

Create a ThingSpeak Channel

  1. Sign In to ThingSpeak using either your MathWorks Account or ThingSpeak account, or create a new ThingSpeak account.
  2. Click Channels, and then My Channels.

  3. On the Channels page, click New Channel.
  4. Check the boxes next to Fields 1 and 2. Enter these field values:
    • Name: Mary Maersk Location
    • Field 1: Latitude
    • Field 2: Longitude
    • TagsMATLABNew Channel on ThingSpeak
  5. 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.

  1. Go to the Apps tab and click MATLAB Analysis. Then click New. Select the Get data from a webpage template, and click Create.
  2. In the Name field, enter Mary Maersk Location
  3. In the MATLAB Code field, enter the following code customizations:
    • url: https://www.marinetraffic.com/en/ais/details/ships/219018692
    • targetString: Latitude / Longitude
    • writeChannelID(Enter your Channel ID)
    • writeAPIKey(Enter your Channel's Write API Key)
    • urlfilterdata = urlfilter(url, targetString, 2);
  4. 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.
MATLAB Analysis Example

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.

  1. 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.
  2. Name your new TimeControl Mary Maersk Location Tracking
  3. Choose Recurring in the Frequency field.
  4. Choose Hour in the Recurrence field.
  5. Click Save TimeControl
MATLAB Analysis 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.

  1. 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.
  2. Select the Custom (no starter code) template.
  3. Enter a name for the visualization: Map Plot of Mary Maersk’s Location
  4. 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);
    image(img/2+127)
    axis off
    % 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
    hold on
    plot(x,y, 'r.', 'MarkerSize',10)
    plot(x(end),y(end), 'ro', 'MarkerSize',10)
    hold off
    axis equal
    axis([325 719 84 395])

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

Next Steps

ThingSpeak Channel View

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.