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 uses a code template to demonstrate how to read data from a public channel, analyze the data, and schedule the code to run at regular intervals. This example uses data from ThingSpeak channel 12397, which collects temperature and wind speed from an Arduino-based weather station in Natick, MA, to determine wind chill.
Sign in to ThingSpeak to create a private channel to record and plot temperature, wind speed, and wind chill. Go to the Channels tab, and select My Channels. Click New Channel. Select the corresponding check box and enter these channel setting values:
Wind Chill Measurement
Wind Speed (mph)
Wind Chill (F)
Click Save Channel.
To analyze data from a public channel and write it to your private channel, you write a MATLAB® script using the code template provided. The weather station in Natick collects weather-related data and sends the results to a public ThingSpeak channel. You can access the live weather station data to calculate the wind chill and automatically graph it.
Navigate to the Apps tab, and select MATLAB Analysis. Click New, choose the Calculate wind chill and update channel option, and click Create.
The MATLAB Code field is prepopulated with the code to
analyze and write data. Replace the given values for
writeAPIKey with your
channel settings. You can find the channel ID and API Key under the
Channel Info panel on the right side of the page. Since
this channel is private, specify the API Key.
readChID = 12397; % Replace  with your channel ID writeChID = ; % Enter your Write API Key between the '' writeAPIKey = '';
thingSpeakRead function to retrieve the latest
temperature and wind speed reading from fields 4 and 2, respectively, in the
weather station channel. This function records the timestamp as well.
[temp,time] = thingSpeakRead(readChID,'Fields',4); windSpeed = thingSpeakRead(readChID,'Fields',2);
Calculate and display wind chill temperature from the values obtained in the previous step. Wind chill is used as a measure of the air temperature, taking into account the cooling effects of wind on human skin. It is only valid when the air temperature is less than 50 degrees Fahrenheit and the wind speed is greater than 3 miles per hour. This example uses the formula found on the National Weather Service website to calculate wind chill.
windChill = 35.74 + (0.06215*temp) - (35.75*windSpeed^0.16) + (0.4275*temp*windSpeed^0.16); display(windChill,'Wind Chill');
Write the two measured values and the calculated value to fields 1, 2, and 3 in your private ThingSpeak channel.
Execute your code by clicking Save and Run. Each of the three charts in your ThingSpeak channel is populated with a single point. You can access your channel by clicking the channel link in the Channel Info panel on the right side of the page.
You can use the TimeControl app to set your code to run at regular intervals. By doing so, you generate a continuous plot of wind chill over time in your ThingSpeak channel.
On the page with your MATLAB code, scroll to the bottom, and open the TimeControl app settings.
Name your new TimeControl
Wind Chill Control. Set
Frequency to Recurring
and Recurrence to Minute.
30 in the Every —
minutes dropdown list.
Set Action to
Analysis and Code to execute to
Calculate wind chill and update channel.
Click Save TimeControl.
Setting up a TimeControl to write data to your channel uses available messages on your ThingSpeak account. This action can eventually exhaust available messages, which results in rejection of channel feed updates.
The three charts in your ThingSpeak channel now update with a new wind chill value every 30 minutes.