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:
Sign In either to your MathWorks® Account or ThingSpeak account, or create a new MathWorks account.
Create a Channel as your Dew Point Measurement channel.
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 a MATLAB® Analysis app to read, calculate, and write your data.
Go to the Apps tab, and click MATLAB Analysis.
Click New. Select the Custom template, and click Create.
In the Name field, enter
Dew Point Calculation
.In the MATLAB Code field, enter the following lines of code.
Save the public Weather Station channel ID and your Dew Point Measurement channel ID to variables.
readChId = 12397; writeChId = ZZZZZ; % Replace with your channel number
Save your Write API Key to a variable.
writeKey = 'XXXXXXXXXXXXXXXX'; % Replace with your channel write key
To find your Channel ID and Write API Key, refer to Channel Info on the My Channels tab.
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:
Convert the temperature from Fahrenheit to Celsius.
tempC = (5/9)*(temp-32);
Specify the constants for water vapor (
b
) and barometric pressure (c
).b = 17.62; c = 243.5;
Calculate the dew point in Celsius.
gamma = log(humidity/100) + b*tempC./(c+tempC); dewPoint = c*gamma./(b-gamma);
Convert the result back to Fahrenheit.
dewPointF = (dewPoint*1.8) + 32;
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:
Click Save and Run to validate and process your code.
Any errors in the code are indicated in the Output field.
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.
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.
Name your new TimeControl
Dew Point TC
Choose Recurring in the Frequency field.
Choose Minute in the Recurrence field.
Select 30 in the Every — minutes field.
Keep the Start Time at the default value.
Verify that the Action is
MATLAB Analysis
, and the Code to execute is yourDew Point Calculation
.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.
Select the Custom template, and click Create.
Name the visualization "Dew Point."
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 = ZZZZZ; readKey = 'XXXXXXXXXXXXXXXX';
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);
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:
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 email when a specified level is exceeded.
See Also
thingSpeakRead
| thingSpeakWrite
| plot
(MATLAB) | xlabel
(MATLAB) | ylabel
(MATLAB) | title
(MATLAB) | legend
(MATLAB) | grid
(MATLAB)