Return to IoT Kits

6. Try MATLAB Scripts

ThingSpeak can be used “right out of the box” to display the raw data from your weather station. MATLAB gives you the ability to analyze your data and present it in different ways. Here is a selection of MATLAB Visualizations for ThingSpeak addins that may be used with the IoT Kits to display the weather data in different formats.

Display current values in a text box.

The values do not align well because the font is proportional.

% Displays Current Values in a Text Box
% Needs better formatting
% Would be nice to add time of data measurement

% TO DO: Replace the [] with your channel ID
readChannelID = [];

% enter your field IDs if different from the D1M-WX1 definitions
fieldTempC = 1; % temp C
fieldHumid = 2; % humidity
fieldSLP = 4;   % Sea Level Pressure 
fieldLux = 5;   % lux
fieldVcell = 6; % Vcell
fieldRSSI = 7;  % RSSI

%% Read Data %%
%[tempC, time] = thingSpeakRead(readChannelID, 'Field', fieldID1, 'NumPoints', 30);
curTempC = thingSpeakRead(readChannelID, 'Field', fieldTempC);
curHum = thingSpeakRead(readChannelID, 'Field', fieldHumid);
curSLP = 0.02953 * thingSpeakRead(readChannelID, 'Field', fieldSLP);
curLux = thingSpeakRead(readChannelID, 'Field', fieldLux);
curVcell = thingSpeakRead(readChannelID, 'Field', fieldVcell);
curRSSI = thingSpeakRead(readChannelID, 'Field', fieldRSSI);

%% convert Celsius to Fahrenheit
curTempF = 1.8 * curTempC + 32;

%% Visualize Data %%
str = {strcat('Temperature...', num2str(curTempF, '%.1f'), ' °F'),...
       strcat('Humidity..........', num2str(curHum, '%.1f'), ' %RH'),...
       strcat('Pressure.........', num2str(curSLP, '%.2f'), ' inHg'),...
       strcat('Light...............', num2str(curLux), ' lux'),...       
       strcat('V cell..............', num2str(curVcell, '%.2f'), ' Volts'),...
       strcat('WiFi...............', num2str(curRSSI), ' dBm')};
annotation('textbox',[0.1 0.2 0.7 0.6],...
%%'String',[num2str(currentTempF, '%.1f') '°F'],...

Temperature & Dew Point on left axis and Humidity on right axis

This uses ThingSpeakPlotYY to have different left and right axes. Temperature and Dew Point are plotted using the left axis. There does not appear to be an easy way to have multiple series for each axis so dew point and temperature are plotted in the same color and there is a spurious line connecting the last data point of temperature to the first data point of dew point.

% Humidity and temperature are read from a ThingSpeak channel to calculate
% dew point.

% Script expects temperature in °C. It then calculates dewpoint in °C.
% Calculated temperatures are converted to °F for charting.
% If you read temperature in °F, you must first convert it to °C
% before doing the dewpoint calculation.

% TO DO: Replace the [] with channel ID to read data from:
readChannelID = [];

% Temperature Field ID
TemperatureFieldID = 1;
% Humidity Field ID
HumidityFieldID = 2;

% Number of data points to plot
dataPoints = 100;

% Read field data and time for Temperature sensor
[tempC, time] = thingSpeakRead(readChannelID, 'Fields', TemperatureFieldID, 'NumPoints', dataPoints);
% Read field data for Humidity sensor
humidity = thingSpeakRead(readChannelID, 'Fields', HumidityFieldID, 'NumPoints', dataPoints);

% Calculate dew point
% Specify the constants for water vapor (b) and barometric (c) pressure.
b = 17.67;
c = 243.5;

% Calculate the intermediate value 'gamma'
gamma = log(humidity / 100) + b * tempC ./ (c + tempC);

% Calculate dew point in Celsius
dewPointC = c * gamma ./ (b - gamma);

% Convert temperatures to Fahrenheit for display
dewPointF = (dewPointC * 1.8) + 32;
tempF = (tempC * 1.8) + 32;

%%thingSpeakPlot(time, [tempF, dewPointF], 'ylabel','Temperature (°F)','legend',...
%%{'Temp °F', 'Dew Point °F', 'Humidity % RH'}, 'Grid', 'on', 'linewidth', 2);

thingSpeakPlotYY([time; time], [tempF; dewPointF], time, humidity,...
'ylabel1', 'Temperature & Dew Point °F', 'ylabel2','Relative Humidity %',...
'xGrid', 'on', 'yGrid1', 'on');

Temperature & Humidity Correlation

% Read temperature and humidity from a ThingSpeak channel and visualize the
% relationship between them using the MATLAB THINGSPEAKSCATTER plot

%TO DO: replace [] with your channel ID
readChannelID = [];

% Temperature Field ID
TemperatureFieldID = 1;
% Humidity Field ID
HumidityFieldID = 2;

dataPoints = 100;

% Channel Read API Key 
% If your channel is private, then enter the read API
% Key between the '' below: 
readAPIKey = '';

% Read Temperature Data. Learn more about the THINGSPEAKREAD function by
% going to the Documentation tab on the right side pane of this page.

% use this line for reading data from a private channel
%temperatureData = thingSpeakRead(readChannelID, 'Fields', TemperatureFieldID, 'NumPoints', dataPoints, 'ReadKey', readAPIKey);

% use this line for reading data from a public channel
tempC = thingSpeakRead(readChannelID, 'Fields', TemperatureFieldID, 'NumPoints', dataPoints);

% convert degrees C to F
tempF = 1.8 * tempC + 32;

% Read Humidity Data
humidityData = thingSpeakRead(readChannelID, 'Fields', HumidityFieldID, 'NumPoints', dataPoints);

% make the correlation
linearCoef = polyfit(tempF, humidityData, 1);
linearFit = polyval(linearCoef, tempF);

%% Visualize Data %%
plot(tempF, humidityData, 's', tempF, linearFit, 'r-')
ylabel('Humidity (%)'),xlabel('Temperature (F)');

Leave a Reply