Return to 9. MATLAB Scripts

Time Awake Histogram

This MATLAB visualization displays the D1 Mini time awake for each reporting cycle over the most recent 24 hours. It calculates the total awake time and the average awake time per cycle.

% Microporocessor time awake histogram
% (C) 2020 Karl Berger
% IoT Kits

%%% ENTER YOUR CHANNEL ID
 chID = 286120;

%%% ENTER TYOUR READ API KEY 
 readAPIKey = 'XXXXXXXXXX';

 % Time Awake Field ID
 timeAwakeFld = 3;
 dataMins = 24 * 60;
 timeAwake = thingSpeakRead(chID, 'Fields', timeAwakeFld, 'NumMinutes', dataMins, 'ReadKey', readAPIKey);

 %%%%%%%%%%%%%%%%%%%%%%%
 % Check for outliers
 anyOutliers = sum(timeAwake > 20);
 % If any outliers are found in the data
 if anyOutliers > 0
 % Find the indices of elements which are not outliers cleanDataIndex = find(timeAwake <= 20); % Select only elements that are not outliers cleanData = timeAwake(cleanDataIndex);
 else
     % If no outliers are found, directly use the data
     cleanData = timeAwake;
 end
 %%%%%%%%%%%%%%%%%%%%%%%
 histogram(cleanData, 'faceColor', 'r');
 xlabel('Time Active (seconds)');
 ylabel('Instances');
 title('Histogram of Time Awake over last 24-Hours');
 grid on
 totalAwake = sum(cleanData);
 awakeEvents= length(cleanData);
 averageAwake = totalAwake / awakeEvents;
 avgText = "Average: " + num2str(averageAwake, '%.2f') + " sec";
 totalText = "Total Time: " + num2str(totalAwake, '%.0f') + " secs";
 str = {totalText, avgText};
 dim = [.4 .5 .3 .3];
 annotation('textbox',dim,'String',str,'FitBoxToText','on');

Permanent link to this article: https://w4krl.com/iot-kits/matlab-scripts/time-awake-histogram/