Convert Temperature Units
This example shows how to read data, perform a calculation using the data, and display the result. In the example, you modify one of the code templates provided by the MATLAB Analysis and MATLAB Visualizations apps. This example demonstrates how to read data, perform a calculation, and display the data. 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 convert temperature measurements from the Natick weather station from Fahrenheit to Celsius, write a MATLAB® script using a code template.
Go to the Apps tab in ThingSpeak and select MATLAB Analysis. Click New, select Convert temperature units, and click Create.
Analyze Your Data
The MATLAB Code field is prepopulated with code to convert temperature from Fahrenheit to Celsius.
1) Set the variables for communicating with ThingSpeak. readChannelID
is the channel ID for the public channel that collects data from the weather station. 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 the most recent temperature value using the thingSpeakRead
function.
tempF = thingSpeakRead(readChannelID,'Fields',temperatureFieldID,'ReadKey',readAPIKey);
3) Calculate the temperature in Celsius and display the result.
tempC = (5/9)*(tempF-32);
display(tempC,'Temperature in Celsius');
21.5000
4) Execute your code by clicking Save and Run. The Output field displays your results.
Write Data to a Channel
1) Store your temperature conversion results by writing them 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:
Name —
Temperature Measurements
Field 1 —
Temperature (F)
Field 2 —
Temperature (C)
Click Save Channel.
2) 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 ID of the channel to write data to: writeChannelID = 17504; % Enter the write API key between the ''. writeAPIKey = '23ZLGOBBU9TWHG2H';
3) Write the Fahrenheit and Celsius temperature readings to your channel.
thingSpeakWrite(writeChannelID,[tempF,tempC],'Writekey',writeAPIKey);
4) Execute your code by clicking Save and Run. Each of the charts in your ThingSpeak channel is populated with a single point representing the latest temperature reading. You can access your channel by clicking the channel link in the Channel Info panel on the right side of the page.
5) Click Save and Run again after a few minutes to update your channel with another data point.