Write processed data to a ThingSpeak channel using thingSpeakWrite
Humidity and temperature for the last ten minutes are read from a ThingSpeak channel to calculate dew point. The calculated dew point is then written to another ThingSpeak channel.
Channel 12397 contains data from the MathWorks Weather Station, located in Natick, Massachusetts. The data is collected once every minute. Read data from % Humidity (3) and Temperature (4) fields of channel 12397 on ThingSpeak.com
readChannelID = 12397; % Humidity Field ID HumidityFieldID = 3; % Temperature Field ID TemperatureFieldID = 4;
To store the calculated dew point, write it to a channel other than the one used for reading data. To write to a channel, assign the write channel ID to the 'writeChannelID' variable, and the write API Key to the 'writeAPIKey' variable below. Find the write API Key in the right side pane of this page.
% TODO - Replace the  with channel ID to write data to: writeChannelID = ; % TODO - Enter the Write API Key between the '' below: writeAPIKey = '';
Get latest temperature data from the MathWorks Weather Station channel.
[tempF, timeStamps] = thingSpeakRead(readChannelID, 'Fields', TemperatureFieldID, 'NumMinutes', 10)
tempF = 78.8000 78.7000 78.7000 78.7000 78.7000 78.9000 79.0000 79.0000 79.1000 79.0000 timeStamps = 17-Aug-2015 07:20:59 17-Aug-2015 07:21:59 17-Aug-2015 07:22:59 17-Aug-2015 07:23:59 17-Aug-2015 07:24:59 17-Aug-2015 07:25:59 17-Aug-2015 07:27:00 17-Aug-2015 07:28:00 17-Aug-2015 07:29:00 17-Aug-2015 07:30:00
Get latest humidity data from the MathWorks Weather Station channel
humidity = thingSpeakRead(readChannelID, 'Fields', HumidityFieldID, 'NumMinutes', 10)
humidity = 74.9000 75.0000 75.2000 75.0000 74.7000 74.6000 74.5000 74.5000 74.5000 74.5000
Convert temperature from Fahrenheit to Celsius
tempC = (5/9)*(tempF-32);
Calculate dew point. Specify the constants for water vapor (b) and barometric (c) pressure.
b = 17.62; c = 243.5; % Calculate the intermediate value 'gamma' gamma = log(humidity/100) + b*tempC ./ (c+tempC); % Calculate dew point in Celsius dewPoint = c*gamma ./ (b-gamma); % Convert to dew point in Fahrenheit dewPointF = (dewPoint*1.8) + 32; display(dewPointF, 'Dew point');
Dew point = 70.1504 70.0932 70.1715 70.0932 69.9754 70.1289 70.1859 70.1859 70.2823 70.1859
Write the dew point value to another channel specified by the 'writeChannelID' variable. Note: To successfully write data to another channel assign the write channel ID and API Key to writeChannelID and writeAPIKey variables above. Also uncomment the line of code below.
% thingSpeakWrite(writeChannelID, dewPointF, 'TimeStamps', timeStamps, 'Writekey', writeAPIKey);