The MATLAB Analysis and MATLAB Visualizations apps in ThingSpeak™ let you pick from a selection of code templates as a starting point for building your own project. This example demonstrates how to scrape the latest live data from a website and display it. The example uses data from the MarineTraffic website, which contains information about the Port of Boston traffic.
To scrape web data about vessels at the Port of Boston, you can write a MATLAB® script using the code template provided.
Navigate to the Apps tab in ThingSpeak, and select MATLAB Analysis. Click New, choose Read live web data for vessels at the Port of Boston, and click Create.
The MATLAB Code field is prepopulated with code to read live web data and count the number of vessels.
Specify the URL containing information about vessels at the Port of Boston.
Use urlFilter
to read the first two
numbers that appear after the target string. This function identifies the target
string in the source code of the page and returns the numbers that appear
immediately after it.
url = 'https://www.marinetraffic.com/en/ais/details/ports/131/USA_port:BOSTON'; filteredData = urlfilter(url,'Vessels in Port:',2);
Display the results from scraping the website data. In this example, two elements are recorded and displayed.
display(filteredData(1),'Vessels in Port'); display(filteredData(2),'Expected Arrivals');
Execute your code by clicking Save and Run. The Output field displays your results.
Store your results by writing it to a private channel. To create a ThingSpeak channel, go to the Channels tab, and select My Channels. Click New Channel. Select the corresponding check boxes, and enter these channel setting values:
Name: Vessels at the Port of
Boston
Field 1: Vessels in
Port
Field 2: Expected
Arrivals
Click Save Channel.
In the MATLAB Code field, set the variables for writing to your
private channel. Replace the given values for writeChannelID
and writeAPIKey
with your values. You can find the channel ID
and API Key under the Channel Info panel on the right side
of the
page.
% Replace the [] with channel ID to write data to: writeChannelID = []; % Enter the Write API Key between the '' below: writeAPIKey = '';
Write the vessel data to your channel.
thingSpeakWrite(writeChannelID,filteredData,'Writekey',writeAPIKey);
Execute your code by clicking Save and Run. The charts in your ThingSpeak channel are populated with a single point in each, representing the value for each element from the website. You can access your channel by clicking the channel link in the Channel Info panel on the right side of the page.
Click Save and Run again after some time to update your channel with another data point.