Main Content

Calculate High and Low Temperatures

此示例演示了如何读取数据,确定某些元素并显示结果。在示例中,您可以修改MATLAB分析和MATLAB可视化应用程序提供的代码模板之一。该示例使用ThingsPeak的数据channel 12397,它从马萨诸塞州纳蒂克市的Arduino®气象站收集天气数据。

从模板代码创建MATLAB分析脚本

To calculate the maximum and minimum daily temperatures from the Natick weather station, write a MATLAB® script using the code template provided.

Appstab in ThingSpeak and selectMATLAB分析。ClickNew中,选择Calculate high and low temperatures, and click创造

Analyze Your Data

MATLAB Codefield is prepopulated with code to calculate the maximum and minimum temperatures over the past 24 hours.

1) Set the variables for communicating with ThingSpeak. ThereadChannelIDis the channel ID for the public channel that collects data from the weather station. ThetemperatureFieldIDis 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;温度fieldid = 4;ReadApikey ='';

2)使用过去24小时读取温度值things peakreadfunction.

[tempF,timeStamp] = thingSpeakRead(readChannelID,'Fields',temperatureFieldID,'numDays',1,'ReadKey',readAPIKey);

3) Calculate the maximum and minimum temperatures in Fahrenheit usingmaxmin。这n, 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');
102
display(minTempF,'Minimum temperature for the past 24 hours is');
81

4) Execute your code by clicking保存并运行。这Output字段显示您的结果。

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 theChannelstab and selectMy Channels。ClickNew Channel。Select the corresponding check box, and enter these channel setting values:

  • Name —Temperature Measurement

  • Field 1 —温度(F)

ClickSave Channel

2) In theMATLAB Codefield, set the variables for writing to your private channel. Replace the given values forwriteChannelIDWriteapikeywith your values. You can find the channel ID and write API key in theChannel Info页面右侧的面板。

% 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, changemaxTempFtominTempF

%thingspeakwrite(Writechannelid,maxtempf,'timestamp',timemaxtemp,'writekey',writeapikey);

4) Execute your code by clicking保存并运行。这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 theChannel Info页面右侧的面板。

See Also

Functions

相关的Examples

More About