Return to 9. MATLAB Scripts

Current Weather – Imperial Units

This MATLAB visualization shows current values from your weather station in conventional US units. The values are taken from your ThingSpeak channel at the time your webpage is opened. You must manually update the chart by refreshing your browser.

% Displays Current Imperial Values in a Text Box
 % Formatted for IoT Kits Weather Stations
 % Karl Berger
 % 2022.08.09
 readChannelID = 286120;
 fieldTempC = 1; % temp C
 fieldHumid = 2; % humidity
 fieldAwake = 3; % not used
 fieldSLP   = 4; % SLP 
 fieldLux   = 5; % lux
 fieldVcell = 6; % Vcell
 fieldRSSI  = 7; % RSSI
 fieldTempF = 8; % temp F
 textSize = 10;
 %% Read Current Data %%
 curTempC = thingSpeakRead(readChannelID, 'Fields', fieldTempC);
 curTempF = thingSpeakRead(readChannelID, 'Fields', fieldTempF);
 curHum = thingSpeakRead(readChannelID, 'Field', fieldHumid);
 % convert hPa to inches of Mercury
 curSLP = 0.02953 * thingSpeakRead(readChannelID, 'Field', fieldSLP);
 curLux = thingSpeakRead(readChannelID, 'Field', fieldLux);
 curVcell = thingSpeakRead(readChannelID, 'Field', fieldVcell);
 curRSSI = thingSpeakRead(readChannelID, 'Field', fieldRSSI);
 % read sea level pressure and time for 3 hours (180 minutes)
 numMinutes = 180;
 [baro3hr, dataTime] = thingSpeakRead(readChannelID, 'Fields', fieldSLP, 'NumMinutes', numMinutes);
 dataLength = length(baro3hr);                    % number of barometric data points
 deltaBaro = baro3hr(dataLength) - baro3hr(1);    % earliest data minus most recent data
 strDelta = string(deltaBaro) + " mb";            % convert to string and add pressure unit
 % convert 3-hour trend value to words
 if deltaBaro > 6.0
     strTrend = "Rising Very Rapidly";
 elseif deltaBaro > 3.6
     strTrend = "Rising Quickly";
 elseif deltaBaro > 1.6
     strTrend = "Rising";
 elseif deltaBaro > 0.1
     strTrend = "Rising Slowly";
 elseif deltaBaro > -0.1
     strTrend = "Steady";
 elseif deltaBaro > -1.6
     strTrend = "Falling Slowly";
 elseif deltaBaro > -3.6
     strTrend = "Falling";
 elseif deltaBaro > -6.0
     strTrend = "Falling Quickly";
 else
     strTrend = "Falling Very Rapidly";
 end
 % determine LiPo State Of Charge
 % soc range 0-1
 if curVcell > 4.05
     soc = 1;
 elseif curVcell > 3.8
     soc = 0.7 + 1.2 * (curVcell - 3.8);
 elseif curVcell > 3.5
     soc = 2.33 * (curVcell - 3.5);
 else
     soc = 0;
 end
 %convert SOC to percent
 soc = 100 * soc;
 %% display current values
 first_line = 0.85;
 second_line = 0.2;
 box_height = 0.55;
 label_column = 0.1;  % 0.1
 label_width = 0.4;  % 0.25
 value_column = 0.25;  % was 0.25
 value_width = 0.3; % was 0.4
 unit_column = 0.55;
 unit_width = 0.3;
 % Time of Data
 str = {strcat('Data Date:', {' '}, string(dataTime(dataLength))),…
        'Refresh browser to update'};
 annotation('textbox',[label_column first_line label_width 0.12],…
 'HorizontalAlignment','left',…
 'VerticalAlignment','top',…
 'LineStyle',':',…
 'String',str,…
 'FontSize',textSize,…
 'Color', 'red','FitBoxToText','on');
 % labels
 str = {'Temperature …',…
        'Humidity ……….',…
        'Barometer …….',…
        '     Trend……….',…
        'Light …………….',…       
        'LiPo Cell ……….',…
        '     Charge………',…
        'WiFi ……………..'};
 annotation('textbox',[label_column second_line label_width box_height],…
 'HorizontalAlignment','left',…
 'VerticalAlignment','top',…
 'LineStyle','none',…
 'String',str,…
 'FontSize',textSize);
 % values
 str = {num2str(curTempF, '%.1f'),…
        num2str(curHum, '%.1f'),…
        num2str(curSLP, '%.2f'),…
        strTrend,…                     % place holder for trend
        num2str(curLux),…       
        num2str(curVcell, '%.2f'),…
        num2str(soc, '%.0f'),…
        num2str(curRSSI)};
 annotation('textbox',[value_column second_line value_width box_height],…
 'HorizontalAlignment','right',…
 'VerticalAlignment','top',…
 'LineStyle','none',…
 'String',str,…
 'FontSize',textSize,…
 'Color', 'blue');
 % units
 str = {'°F',…
        '%RH',…
        'inHg',… 
        '',…
        'lux',…       
        'Volts',…
        '%',…
        'dBm'};
 annotation('textbox',[unit_column second_line unit_width box_height],…
 'HorizontalAlignment','left',…
 'VerticalAlignment','top',…
 'LineStyle','none',…
 'String',str,…
 'FontSize',textSize);

Permanent link to this article: https://w4krl.com/iot-kits/matlab-scripts/current-weather-imperial/