Main Content

Analyze Your Data

This example shows how to read temperature and humidity data from ThingSpeak channel 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 the Apps tab, and click MATLAB Analysis.

  2. Click New. Select the Custom template, and click Create.

  3. In the Name field, enter Dew 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 the My Channels tab.

    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,[temp,humidity,dewPointF],'Fields',[1,2,3],...
    'TimeStamps',time,'Writekey',writeKey);

    The full block of code now appears as:

     See the Full Code

  6. Click Save and Run to validate and process your code.

    Any errors in the code are indicated in the Output field.

  7. To see if your code ran successfully, click your Dew Point Measurement channel link in the Channel Info panel.

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

Schedule Code

Use the TimeControl app 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. Click TimeControl to open the app with MATLAB Analysis preselected in the Actions field and the Dew Point Calculation as the Code to execute.

  2. Name your new TimeControl Dew Point TC

  3. Choose Recurring in the Frequency field.

  4. Choose Minute in the Recurrence field.

  5. Select 30 in the Every — minutes field.

  6. Keep the Start Time at the default value.

  7. Verify that the Action is MATLAB Analysis, and the Code to execute is your Dew Point Calculation.

  8. 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. 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 the plot (MATLAB) function to show all three data points in a single visualization.

Go to Apps > MATLAB Visualizations, and click New to create a visualization.

Alternately, you can click MATLAB Visualization in your Dew Point Measurement channel view.

  1. Select the Custom template, and click 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'});
    grid on;

    Your code will look similar to this code:

     See the Full Code

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

Next Steps

In the example Act 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