Main Content

Analyze Your Data

This example shows how to read temperature and humidity data fromThingSpeakchannel 12397, which collects weather-related data from an Arduino®device. You write the temperature and humidity data into your Dew Point Measurement channel, along with the calculated dew point data. Then use ThingSpeak™ to visualize the results on your channel.

Prerequisite Steps

This example requires that you have already performed these steps:

Read Data from a Channel

Read the humidity and temperature from the public WeatherStation channel Fields 3 and 4, and write that data to Fields 2 and 1, respectively, of your Dew Point Measurement channel. Dew point is calculated and written to Field 3.

Use MATLAB®Analysis to read, calculate, and write your data.

  1. Go to theAppstab, and clickMATLAB Analysis.

  2. ClickNew. Select theCustom模板,然后单击Create.

  3. In theNamefield, enterDew Point Calculation.

  4. In the MATLAB Code field, enter the following lines of code.

    1. Save the public Weather Station channel ID and your Dew Point Measurement channel ID to variables.

      readChId = 12397; writeChId = 671;% replace with your channel number
    2. Save your Write API Key to a variable.

      writeKey ='F6CSCVKX42WFZN9Y';% Replace with your channel write key

      To find your Channel ID and Write API Key, refer to Channel Info on theMy Channelstab.

    3. Read the latest 20 points of temperature data with timestamps and humidity data from the public Weather Station channel into variables.

      [temp,time] = thingSpeakRead(readChId,'Fields',4,'NumPoints',20); humidity = thingSpeakRead(readChId,'Fields',3,'NumPoints',20);

Calculate the Dew Point

Add the following MATLAB code to calculate the dew point using temperature and humidity readings:

  1. Convert the temperature from Fahrenheit to Celsius.

    tempC = (5/9)*(temp-32);

  2. Specify the constants for water vapor (b) and barometric pressure (c).

    b = 17.62; c = 243.5;

  3. Calculate the dew point in Celsius.

    gamma = log(humidity/100) + b*tempC./(c+tempC); dewPoint = c*gamma./(b-gamma)

  4. Convert the result back to Fahrenheit.

    dewPointF = (dewPoint*1.8) + 32;

  5. Write data to your Dew Point Measurement channel. This code posts all the available data in one operation and includes the correct timestamps.

    thingSpeakWrite (writeChId(温度、湿度、dewPointF],'Fields',[1,2,3],...'TimeStamps',time,'Writekey',writeKey);

    The full block of code now appears as:

    See the Full Code

  6. ClickSave and Runto validate and process your code.

    Any errors in the code are indicated in theOutputfield.

  7. To see if your code ran successfully, click yourDew Point Measurementchannel link in theChannel Info面板。

The Dew Point Measurement channel now shows charts with channel data from each Field.

Schedule Code

Use theTimeControlapp to schedule the dew point calculation in your MATLAB Analysis code. Schedule it to read data from the weather station every 30 minutes and calculate the dew point.

  1. Scroll to the bottom of your MATLAB Analysis Dew Point Calculation page. ClickTimeControlto open the app with MATLAB Analysis preselected in theActionsfield and the Dew Point Calculation as theCode to execute.

  2. Name your new TimeControlDew Point TC

  3. ChooseRecurringin theFrequencyfield.

  4. ChooseMinutein theRecurrencefield.

  5. Select30in theEvery — minutesfield.

  6. Keep theStart Timeat the default value.

  7. Verify that theActionisMATLAB Analysis, and theCode to executeis yourDew Point Calculation.

  8. ClickSave 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. Make sure that the data you write to a channel does not overlap in the time domain as it causes unnecessary use of messages.

Visualize Dew Point Measurement

Use the MATLAB Visualizations app to visualize the measured dew point data, temperature, and humidity from your Dew Point Measurement channel. This example uses theplot(MATLAB)function to show all three data points in a single visualization.

Go toApps>MATLAB Visualizations, and clickNewto create a visualization.

Alternately, you can clickMATLAB Visualizationin your Dew Point Measurement channel view.

  1. Select theCustom模板,然后单击Create.

  2. Name the visualization "Dew Point."

  3. Create variables for your Dew Point Measurement channel ID and your Read API Key. Replace the values in the code with your channel ID and Read API Key.

    readChId = ZZZZ readKey ='XXXXXXXXXXXXXXXX';

  4. Read data from your channel fields, and get the last 100 points of data for:

    • Temperature: from Field 1

    • Humidity: from Field 2

    • Dew point: from Field 3

      [dewPointData,timeStamps] = thingSpeakRead(readChId,'fields',[1,2,3],...'NumPoints', 100,'ReadKey',readKey);
  5. Plot the data with x and y labels, a title, and a legend.

    plot(timeStamps,dewPointData); xlabel('TimeStamps'); ylabel('Measured Values'); title('Dew Point Measurement'); legend({'Temperature','Humidity','Dew Point'}); gridon;

    Your code will look similar to this code:

    See the Full Code

  6. ClickSave and Run. If your MATLAB code has no errors, the plot output looks similar to the plot shown here:

Next Steps

In the exampleAct on Your Data, you can track your calculated dew point to trigger an automatic tweet when a specified level is exceeded.

See Also

||(MATLAB)|(MATLAB)|(MATLAB)|(MATLAB)|(MATLAB)|(MATLAB)

Related Topics