Calculate High and Low Temperatures
This example demonstrates how to read data, identify certain elements, and display the results. In the example, you modify one of the code templates provided by the MATLAB Analysis and MATLAB Visualizations apps. The example uses data from ThingSpeak channel 12397, which collects weather data from an Arduino® based weather station in Natick, MA.
Create a MATLAB Analysis Script from Template Code
To calculate the maximum and minimum daily temperatures from the Natick weather station, write a MATLAB® script using the code template provided.
Go to the Apps tab in ThingSpeak and select MATLAB Analysis. Click New, select Calculate high and low temperatures, and click Create.
Analyze Your Data
The MATLAB Code field is prepopulated with code to calculate the maximum and minimum temperatures over the past 24 hours.
1) Set the variables for communicating with ThingSpeak. The
readChannelID is the channel ID for the public channel that collects data from the weather station. The
temperatureFieldID is the field in the channel that contains temperature values. Assign a value to readAPIkey only if you are reading data from a private channel. The weather station is public, so for this example, do not set readAPIkey.
readChannelID = 12397; temperatureFieldID = 4; readAPIKey = '';
2) Read temperature values from the past 24 hours using the
[tempF,timeStamp] = thingSpeakRead(readChannelID,'Fields',temperatureFieldID,'numDays',1,'ReadKey',readAPIKey);
3) Calculate the maximum and minimum temperatures in Fahrenheit using
min. Then, identify the corresponding timestamps and display the results.
[maxTempF,maxTempIndex] = max(tempF); [minTempF,minTempIndex] = min(tempF); timeMaxTemp = timeStamp(maxTempIndex); timeMinTemp = timeStamp(minTempIndex); display(maxTempF,'Maximum temperature for the past 24 hours is');
display(minTempF,'Minimum temperature for the past 24 hours is');
4) Execute your code by clicking Save and Run. The Output field displays your results.
Write Data to a Channel
1) Store your maximum or minimum temperature calculation result 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 box, and enter these channel setting values:
Field 1 —
Click Save Channel.
2) In the MATLAB Code field, set the variables for writing to your private channel. Replace the given values for
writeAPIKey with your values. You can find the channel ID and write API key in the Channel Info panel on the right side of the page.
% Replace with the ID of the channel to write data to. writeChannelID = 17504; % Enter the write API key between the ''. writeAPIKey = '23ZLGOBBU9TWHG2H';
3) Uncomment the following line to write the maximum temperature reading to your channel. To save the minimum temperature value, change
4) Execute your code by clicking Save and Run. The chart in your ThingSpeak channel is populated with a single point representing the maximum temperature reading at the time when it was recorded. You can access your channel by clicking the channel link in the Channel Info panel on the right side of the page.
- Calculate and Display Average Humidity
- Calculate Wind Chill and Update Channel
- Convert Temperature Units
- Remove Outliers from Wind Speed Data
- Read Live Web Data for Vessels at the Port of Boston