This example shows how to read data from a public channel, analyze the data, and schedule the code to run at regular intervals. In the example, you modify one of the code templates provided by the MATLAB Analysis and MATLAB Visualizations apps. This example calculates wind chill by using data from ThingSpeak channel 12397, which collects temperature and wind speed from an Arduino based weather station in Natick, MA.
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
Field 1 —
Field 2 —
Wind Speed (mph)
Field 3 —
Wind Chill (F)
Click Save Channel.
To analyze data from a public channel and write it to your private channel, you can write a MATLAB® script using a code template. 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.
1) Go to the Apps tab and select MATLAB Analysis. Click New, select the Calculate wind chill and update channel option, and click Create.
2) 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 write API Key under the Channel Info panel on the right side of the page.
readChID = 12397; % Replace the following with your channel ID. writeChID = 17504; % Enter your write API key between the ''. writeAPIKey = '23ZLGOBBU9TWHG2H';
3) Use the
thingSpeakRead function to retrieve the latest temperature and wind speed reading from fields 4 and 2, respectively, in the weather station channel. This function also records the timestamp.
[temp,time] = thingSpeakRead(readChID,'Fields',4); windSpeed = thingSpeakRead(readChID,'Fields',2);
4) Calculate and display the wind chill temperature. Wind chill is a measure of the air temperature that takes into account the cooling effects of wind on human skin. The measure 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 National Weather Service formula 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');
5) Write the two measured values and the calculated value to fields 1, 2, and 3 in your private ThingSpeak channel.
6) 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. Running the code at regular intervals generates a continuous plot of wind chill over time in your ThingSpeak channel.
1) On the page with your MATLAB code, scroll to the bottom and open the TimeControl app settings.
2) Name your new TimeControl
Wind Chill Control. Set Frequency to Recurring and Recurrence to Minute. Select
30 in the Every — minutes dropdown list.
3) Set Action to
MATLAB Analysis and Code to execute to
Calculate wind chill and update channel.
4) Click Save TimeControl.
Note: 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 update with a new wind chill value every 30 minutes.