{"id":1365,"date":"2016-03-23T08:33:36","date_gmt":"2016-03-23T13:33:36","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/?p=1365"},"modified":"2018-02-15T16:54:58","modified_gmt":"2018-02-15T21:54:58","slug":"explore-your-iot-data-with-thingspeak-and-matlab","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2016\/03\/23\/explore-your-iot-data-with-thingspeak-and-matlab\/","title":{"rendered":"Explore your IoT data with ThingSpeak and MATLAB"},"content":{"rendered":"<div class=\"content\"><!--introduction--><p>Today I'd like to introduce you to a guest blogger, Eric Wetjen, who is a Product Marketing Manager here at MathWorks. Today, Eric will be discussing ThingSpeak, a platform for collecting and analyzing Internet of Things (IoT) data with MATLAB.<\/p><!--\/introduction--><h3>Contents<\/h3><div><ul><li><a href=\"#5e89d109-b688-4603-ae87-da9d5675cdea\">Introduction<\/a><\/li><li><a href=\"#5535004c-9ab9-41d6-8c07-3af7576fc8e6\">Offline Analysis: Analyzing Data stored on ThingSpeak<\/a><\/li><li><a href=\"#2bd1bb70-0864-4ff8-a9ae-743f10390361\">Reading One Week of Traffic Data into MATLAB<\/a><\/li><li><a href=\"#d624797a-9919-41e4-9ff5-226c9beaef84\">Plotting Raw Traffic Data<\/a><\/li><li><a href=\"#f061d751-6212-4ae7-8124-4ba57047cb30\">Looking at Daily Sums for Eastbound Traffic as a histogram<\/a><\/li><li><a href=\"#d1e3af6f-eddd-46b3-884d-737163d5bed6\">Taking a Deeper Look at an Individual Day<\/a><\/li><li><a href=\"#0e572a88-262b-49d5-b4fc-ada2ce67d410\">Downsampling into 48 Bins of 30 Minute Chunks of Data and Finding Peaks<\/a><\/li><li><a href=\"#0682de68-a24d-4748-8deb-5ace90f70351\">Online Analysis: Calculating and Scheduling MATLAB Code inside ThingSpeak<\/a><\/li><li><a href=\"#df7f6df6-cbab-4494-bb83-5703c88d46b0\">Viewing the Calculated Data Online<\/a><\/li><\/ul><\/div><h4>Introduction<a name=\"5e89d109-b688-4603-ae87-da9d5675cdea\"><\/a><\/h4><p>Did you know you can collect and analyze data from internet-connected sensors using <a href=\"https:\/\/thingspeak.com\">ThingSpeak<\/a>?  ThingSpeak is a web service for prototyping IoT applications.  ThingSpeak allows you to collect, analyze and act on your data and includes a MATLAB Analysis App in the cloud for analyzing data you have sent to ThingSpeak. We'll show how you can analyze your ThingSpeak data in desktop MATLAB, and we'll also show how can run your MATLAB analysis code in the cloud using the MATLAB Analysis App built-into by ThingSpeak.<\/p><p>In this project, we used a Raspberry Pi, a web cam and ThingSpeak to count cars and analyze traffic patterns on a busy highway. Our car counter sends data to <a href=\"https:\/\/thingspeak.com\/channels\/38629\">channel 38629<\/a> on ThingSpeak every 15 seconds.<\/p><p>The counting of the cars from the web cam images is done on a Raspberry Pi 2 and only the number of cars seen every 15 seconds is sent by the device to ThingSpeak. The hardware setup and the development of the car counting algorithm is described completely in the following <a href=\"https:\/\/www.mathworks.com\/\/matlabcentral\/fileexchange\/52456\">File Exchange submission.<\/a>  A picture of the sensor setup and a view of how the raw data is displayed in ThingSpeak are shown below.<\/p><p>Once the data is collected in ThingSpeak, we are ready to use MATLAB to look at the traffic patterns. Today, we will focus on the data analysis and visualization of the traffic data using MATLAB and ThingSpeak.<\/p><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/2016\/IMG_1918.JPG\" alt=\"\"> <\/p><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/2016\/IMG_38629_small.png\" alt=\"\"> <\/p><h4>Offline Analysis: Analyzing Data stored on ThingSpeak<a name=\"5535004c-9ab9-41d6-8c07-3af7576fc8e6\"><\/a><\/h4><p>In order to gain some insight into our traffic data, we first need to import the data from ThingSpeak into desktop MATLAB.   To simplify the retrieval of the data from ThingSpeak, we use the functions from the <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/52244-thingspeak-support-toolbox\">ThingSpeak Support Toolbox<\/a>, available on MATLAB Central File Exchange.<\/p><h4>Reading One Week of Traffic Data into MATLAB<a name=\"2bd1bb70-0864-4ff8-a9ae-743f10390361\"><\/a><\/h4><p>We begin by specifying a start date and an end date using a <tt>datetime<\/tt> object. Because we are sending data to ThingSpeak once every 15 seconds, we have approximately 40,000 data points to retrieve.  ThingSpeak allows only 8000 points in a single read operation, so we create a  <tt>for loop<\/tt> to gather the data in batches.  We then append the traffic and time data from each iteration into two vectors, called <tt>alltrafficData<\/tt> and <tt>timestamp<\/tt>.<\/p><pre class=\"codeinput\">endDate = datetime(<span class=\"string\">'3\/13\/2016'<\/span>, <span class=\"string\">'InputFormat'<\/span>, <span class=\"string\">'MM\/dd\/yyyy'<\/span>);\r\nstartDate = datetime(<span class=\"string\">'3\/6\/2016'<\/span>, <span class=\"string\">'InputFormat'<\/span>, <span class=\"string\">'MM\/dd\/yyyy'<\/span>);\r\n<span class=\"comment\">% Create date vector<\/span>\r\ndateVector = startDate: endDate;\r\n<span class=\"comment\">% check to see that the last dateVector value is the same as endDate, if<\/span>\r\n<span class=\"comment\">% not append it<\/span>\r\n<span class=\"keyword\">if<\/span> (dateVector(end) ~= endDate)\r\n   dateVector = [dateVector, endDate];\r\n<span class=\"keyword\">end<\/span>\r\nalltrafficData = [];\r\ntimestamp = [];\r\n<span class=\"comment\">% Read data in chunks because ThingSpeak has a limit of 8000 pts per read<\/span>\r\n<span class=\"keyword\">for<\/span> dayCount = 1:length(dateVector)-1\r\n   dateRange = [dateVector(dayCount), dateVector(dayCount+1)];\r\n   [channelData, t] = thingSpeakRead(38629, <span class=\"string\">'DateRange'<\/span>, dateRange);\r\n   [alltrafficData] = [alltrafficData; channelData ];\r\n   [timestamp] = [timestamp; t];\r\n<span class=\"keyword\">end<\/span>\r\n<\/pre><h4>Plotting Raw Traffic Data<a name=\"d624797a-9919-41e4-9ff5-226c9beaef84\"><\/a><\/h4><p>Next we plot the data and label the graph. The daily fluctuations in traffic are clearly visible from the raw data.  Immediately, we can see the peaks corresponding to the morning and afternoon \"rush hours\".<\/p><pre class=\"codeinput\">figure\r\nplot(timestamp, alltrafficData)\r\ndatetick\r\nxlabel(<span class=\"string\">'Date'<\/span>)\r\nylabel(<span class=\"string\">'Vehicle Count per 15 second interval'<\/span>)\r\ngrid <span class=\"string\">on<\/span>\r\ntitle(<span class=\"string\">'Traffic Volume for the week of March 6'<\/span>)\r\nlegend(<span class=\"string\">'West Bound Traffic'<\/span>,<span class=\"string\">'East Bound Traffic'<\/span>)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/2016\/traffic_analysis_blog_01.png\" alt=\"\"> <h4>Looking at Daily Sums for Eastbound Traffic as a histogram<a name=\"f061d751-6212-4ae7-8124-4ba57047cb30\"><\/a><\/h4><p>To visualize the volume of traffic each day, we sum up the raw traffic data and use a bar chart to visualize it. It looks like our busy highway sees from 45,000 to 65,000 cars per day!<\/p><pre class=\"codeinput\">eastTraffic = alltrafficData(:,2);\r\nwestTraffic = alltrafficData(:,1);\r\n<span class=\"keyword\">for<\/span> i=1:4  <span class=\"comment\">% remove a few pts to be evenly divisible by 5647 (points in a day)<\/span>\r\neastTraffic(i) = [];\r\nwestTraffic(i) = [];\r\n<span class=\"keyword\">end<\/span>\r\n<span class=\"comment\">% Divide data into 7 chunks (approximates a 24 hour day)<\/span>\r\ndailysumeast = sum(reshape(eastTraffic, floor(length(alltrafficData)\/7),[]));\r\ndates = dateVector; <span class=\"comment\">% convert to serial date for bar plot<\/span>\r\ndates(8) = [];\r\ndates = datenum(dates);\r\nfigure\r\nbar(dates,dailysumeast)\r\ngrid <span class=\"string\">on<\/span>\r\nxlabel(<span class=\"string\">'Date'<\/span>)\r\nylabel(<span class=\"string\">'Vehicle Count per day'<\/span>)\r\nax = gca;\r\nax.YAxis.Exponent = 3;\r\ntitle(<span class=\"string\">'East Bound Traffic Volume for the week of March 6'<\/span>)\r\ndatetick\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/2016\/traffic_analysis_blog_02.png\" alt=\"\"> <h4>Taking a Deeper Look at an Individual Day<a name=\"d1e3af6f-eddd-46b3-884d-737163d5bed6\"><\/a><\/h4><p>To gain more insight into our traffic data, we look at traffic on an individual day. For each day, we specify a start time and a stop time.<\/p><pre class=\"codeinput\">clear <span class=\"string\">all<\/span>\r\nstartTime{1} = <span class=\"string\">'March 8, 2016 00:00:00'<\/span>;\r\nstopTime{1} = <span class=\"string\">'March 8, 2016 23:59:59'<\/span>;\r\n<span class=\"comment\">% We retrieve the data from ThingSpeak, and we<\/span>\r\n<span class=\"comment\">% downsample the data to remove short-term fluctuations.  We then use the<\/span>\r\n<span class=\"comment\">% findpeaks function to plot and find the times where the traffic volume is<\/span>\r\n<span class=\"comment\">% highest. For simplicity, we look at the eastbound data only.<\/span>\r\nstartDate = datetime(startTime{1}, <span class=\"string\">'InputFormat'<\/span>, <span class=\"string\">'MMMM d, yyyy HH:mm:ss '<\/span>);\r\nendDate = datetime(stopTime{1}, <span class=\"string\">'InputFormat'<\/span>, <span class=\"string\">'MMMM d, yyyy HH:mm:ss '<\/span>);\r\ndatevector = [startDate, endDate];\r\n[Daily, t] = thingSpeakRead(38629, <span class=\"string\">'DateRange'<\/span>, datevector);\r\n<span class=\"comment\">% Look at east bound traffic only<\/span>\r\nDailyEast = Daily(:, 2);\r\ntimestamp = datetime(t,<span class=\"string\">'ConvertFrom'<\/span>,<span class=\"string\">'datenum'<\/span>);\r\ndateAnalyzed = startTime{1};\r\ndateAnalyzed = {dateAnalyzed(1:(end-8))};\r\n<\/pre><h4>Downsampling into 48 Bins of 30 Minute Chunks of Data and Finding Peaks<a name=\"0e572a88-262b-49d5-b4fc-ada2ce67d410\"><\/a><\/h4><p>The raw traffic data is very spiky and hard to visualize.  If we want to see what time of day has the highest volume of traffic, we need to look at the data on a time scale larger than 15 seconds. To do this we divide the 24 hour day into 30 minute segments.  Each segment begins at the top of the hour and ends 30 minutes later.<\/p><pre class=\"codeinput\">downsamplesize = floor(length(DailyEast)\/48);\r\nteastper30=datetime(startTime{1});\r\nDailyEastper30(1:48) = 0; <span class=\"comment\">% pre-allocate<\/span>\r\n<span class=\"keyword\">for<\/span> k = 1:48  <span class=\"comment\">% calucate daily tarffic in each 30 minute segment<\/span>\r\nDailyEastper30(k) = sum(DailyEast(1+downsamplesize*(k-1):downsamplesize*k));\r\nteastper30(k+1) = teastper30(k)+1\/48; <span class=\"comment\">% timestamps showing end of each 30 minute period<\/span>\r\n<span class=\"keyword\">end<\/span>\r\nteastper30=teastper30';\r\nteastper30(1) = []; <span class=\"comment\">% start first bin at 12:30 am<\/span>\r\ntimestampPer30 = teastper30;\r\n<span class=\"comment\">% Find peaks and their times (locations)<\/span>\r\n[peaks,location] = findpeaks(DailyEastper30,  <span class=\"string\">'Threshold'<\/span>,100, <span class=\"string\">'MinPeakHeight'<\/span>, 1100);\r\n<span class=\"comment\">% Plot peaks<\/span>\r\nfigure\r\nfindpeaks(DailyEastper30, datenum(timestampPer30),<span class=\"string\">'Threshold'<\/span>,100, <span class=\"string\">'MinPeakHeight'<\/span>, 1100)\r\ndatetick\r\nxlabel(<span class=\"string\">'Time of Day'<\/span>)\r\nylabel(<span class=\"string\">'Vehicle count per 30 minutes'<\/span>)\r\ntitle(strcat(<span class=\"string\">'Peak volume on '<\/span>, {<span class=\"string\">' '<\/span>}, dateAnalyzed))\r\ndateAnalyzed\r\npeaktimes = timestampPer30(location)\r\nDailyVolume = sum(DailyEast)\r\n<\/pre><pre class=\"codeoutput\">dateAnalyzed = \r\n    'March 8, 2016 '\r\npeaktimes = \r\n   08-Mar-2016 06:00:00\r\n   08-Mar-2016 07:30:00\r\n   08-Mar-2016 12:30:00\r\n   08-Mar-2016 14:00:00\r\n   08-Mar-2016 15:30:00\r\n   08-Mar-2016 17:30:00\r\n   08-Mar-2016 20:00:00\r\nDailyVolume =\r\n       51175\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/2016\/traffic_analysis_blog_03.png\" alt=\"\"> <h4>Online Analysis: Calculating and Scheduling MATLAB Code inside ThingSpeak<a name=\"0682de68-a24d-4748-8deb-5ace90f70351\"><\/a><\/h4><p>So far we have brought the data from ThingSpeak back into MATLAB to do some offline analysis of weekly and daily traffic patterns. But what if we want to compute the daily volume of traffic and send that data to another channel on ThingSpeak?  It&#8217;s very simple to do the calculation offline, but ThingSpeak with its MATLAB integration enables us to do this online too.  To calculate the daily volume of traffic, we use the MATLAB Analysis App available on Thingspeak.com.  We write code to fetch the previous day&#8217;s raw traffic data and we sum up the results.  We then use the <tt>thingSpeakWrite<\/tt> command to send the data to a new channel called Daily Volume.<\/p><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/2016\/TS_ML_Code.png\" alt=\"\"> <\/p><p>Finally, we schedule our analysis to run once a day just after midnight using the ThingSpeak TimeControl App.<\/p><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/2016\/timeControl.png\" alt=\"\"> <\/p><h4>Viewing the Calculated Data Online<a name=\"df7f6df6-cbab-4494-bb83-5703c88d46b0\"><\/a><\/h4><p>We have now created a channel that reports the daily volume of cars counted. Because this channel is on ThingSpeak and the MATLAB Analysis App is built in, we can look at this analysis at any time using any web browser on a PC or mobile device. To see the calculated value, go to <a href=\"https:\/\/thingspeak.com\/channels\/51671\">channel 51671 on ThingSpeak<\/a>.<\/p><p>Because ThingSpeak is a cloud service, it will execute the MATLAB analysis code to calculate this value every day even when you are not logged into ThingSpeak.<\/p><p>For more information on how to send data to ThingSpeak, see this <a href=\"https:\/\/www.mathworks.com\/hardware-support\/thingspeak.html\">support page<\/a>. If you have a MathWorks account, you already have access to ThingSpeak. Just log-in and explore at ThingSpeak.com!<\/p><p>What kind of IoT data are you trying to access? As you prototype your IoT system, how are you doing your online and offline analysis?  We'd like to hear <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=1365#respond\">here<\/a>!<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_a9b4491ab6f44dce80af71c20d1c7b9a() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='a9b4491ab6f44dce80af71c20d1c7b9a ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' a9b4491ab6f44dce80af71c20d1c7b9a';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        copyright = 'Copyright 2016 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add copyright line at the bottom if specified.\r\n        if (copyright.length > 0) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n\r\n        d.title = title + ' (MATLAB code)';\r\n        d.close();\r\n    }   \r\n     --> <\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_a9b4491ab6f44dce80af71c20d1c7b9a()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2016a<br><\/p><\/div><!--\r\na9b4491ab6f44dce80af71c20d1c7b9a ##### SOURCE BEGIN #####\r\n%% Explore your IoT data with ThingSpeak and MATLAB\r\n% Today I'd like to introduce you to a guest blogger, Eric Wetjen, who is a\r\n% Product Marketing Manager here at MathWorks. Today, Eric will be\r\n% discussing ThingSpeak, a platform for collecting and analyzing Internet\r\n% of Things (IoT) data with MATLAB.\r\n%% Introduction\r\n% Did you know you can collect and analyze data from internet-connected\r\n% sensors using <https:\/\/thingspeak.com ThingSpeak>?  ThingSpeak is a web\r\n% service for prototyping IoT applications.  ThingSpeak allows you to\r\n% collect, analyze and act on your data and includes a MATLAB Analysis App\r\n% in the cloud for analyzing data you have sent to ThingSpeak. We'll show\r\n% how you can analyze your ThingSpeak data in desktop MATLAB, and we'll\r\n% also show how can run your MATLAB analysis code in the cloud using the\r\n% MATLAB Analysis App built-into by ThingSpeak.\r\n%\r\n% In this project, we used a Raspberry Pi, a web cam and ThingSpeak to\r\n% count cars and analyze traffic patterns on a busy highway. Our car\r\n% counter sends data to <https:\/\/thingspeak.com\/channels\/38629 channel\r\n% 38629> on ThingSpeak every 15 seconds.\r\n%\r\n% The counting of the cars from the web cam images is done on a Raspberry\r\n% Pi 2 and only the number of cars seen every 15 seconds is sent by the\r\n% device to ThingSpeak. The hardware setup and the development of the car\r\n% counting algorithm is described completely in the following\r\n% <https:\/\/www.mathworks.com\/\/matlabcentral\/fileexchange\/52457.>  A picture of the sensor setup and a view of how the raw data\r\n% is displayed in ThingSpeak are shown below.\r\n%\r\n% Once the data is collected in ThingSpeak, we are ready to use\r\n% MATLAB to look at the traffic patterns. Today, we will focus on the data\r\n% analysis and visualization of the traffic data using MATLAB and\r\n% ThingSpeak.\r\n%\r\n% <<IMG_1918.JPG>>\r\n%\r\n% <<IMG_38629_small.png>>\r\n%% Offline Analysis: Analyzing Data stored on ThingSpeak\r\n% In order to gain some insight into our traffic data, we first need to import\r\n% the data from ThingSpeak into desktop MATLAB.   To simplify the retrieval\r\n% of the data from ThingSpeak, we use the functions from the\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/52244-thingspeak-support-toolbox\r\n% ThingSpeak Support Toolbox>, available on MATLAB Central File Exchange.\r\n%\r\n%% Reading One Week of Traffic Data into MATLAB\r\n % We begin by specifying a start date and an end date using a |datetime|\r\n % object. Because we are sending data to ThingSpeak once every 15 seconds,\r\n % we have approximately 40,000 data points to retrieve.  ThingSpeak allows\r\n % only 8000 points in a single read operation, so we create a  |for loop|\r\n % to gather the data in batches.  We then append the traffic and time data\r\n % from each iteration into two vectors, called |alltrafficData| and\r\n % |timestamp|.\r\nendDate = datetime('3\/13\/2016', 'InputFormat', 'MM\/dd\/yyyy');\r\nstartDate = datetime('3\/6\/2016', 'InputFormat', 'MM\/dd\/yyyy');\r\n% Create date vector\r\ndateVector = startDate: endDate;\r\n% check to see that the last dateVector value is the same as endDate, if\r\n% not append it \r\nif (dateVector(end) ~= endDate)\r\n   dateVector = [dateVector, endDate]; \r\nend\r\nalltrafficData = [];\r\ntimestamp = [];\r\n% Read data in chunks because ThingSpeak has a limit of 8000 pts per read\r\nfor dayCount = 1:length(dateVector)-1\r\n   dateRange = [dateVector(dayCount), dateVector(dayCount+1)];\r\n   [channelData, t] = thingSpeakRead(38629, 'DateRange', dateRange);\r\n   [alltrafficData] = [alltrafficData; channelData ];\r\n   [timestamp] = [timestamp; t];\r\nend\r\n%% Plotting Raw Traffic Data\r\n% Next we plot the data and label the graph. The daily fluctuations in\r\n% traffic are clearly visible from the raw data.  Immediately, we can see\r\n% the peaks corresponding to the morning and afternoon \"rush hours\".\r\nfigure\r\nplot(timestamp, alltrafficData)\r\ndatetick\r\nxlabel('Date')\r\nylabel('Vehicle Count per 15 second interval')\r\ngrid on\r\ntitle('Traffic Volume for the week of March 6')\r\nlegend('West Bound Traffic','East Bound Traffic')\r\n%% Looking at Daily Sums for Eastbound Traffic as a histogram\r\n% To visualize the volume of traffic each day, we sum up the raw traffic\r\n% data and use a bar chart to visualize it. It looks like our busy highway\r\n% sees from 45,000 to 65,000 cars per day!\r\neastTraffic = alltrafficData(:,2);\r\nwestTraffic = alltrafficData(:,1);\r\nfor i=1:4  % remove a few pts to be evenly divisible by 5647 (points in a day)\r\neastTraffic(i) = [];\r\nwestTraffic(i) = [];\r\nend\r\n% Divide data into 7 chunks (approximates a 24 hour day)\r\ndailysumeast = sum(reshape(eastTraffic, floor(length(alltrafficData)\/7),[]));\r\ndates = dateVector; % convert to serial date for bar plot\r\ndates(8) = [];\r\ndates = datenum(dates);\r\nfigure\r\nbar(dates,dailysumeast)\r\ngrid on\r\nxlabel('Date')\r\nylabel('Vehicle Count per day')\r\nax = gca;\r\nax.YAxis.Exponent = 3;\r\ntitle('East Bound Traffic Volume for the week of March 6')\r\ndatetick\r\n%% Taking a Deeper Look at an Individual Day\r\n% To gain more insight into our traffic data, we look at traffic on an\r\n% individual day. For each day, we specify a start time and a stop time.\r\nclear all\r\nstartTime{1} = 'March 8, 2016 00:00:00';\r\nstopTime{1} = 'March 8, 2016 23:59:59';\r\n% We retrieve the data from ThingSpeak, and we\r\n% downsample the data to remove short-term fluctuations.  We then use the\r\n% findpeaks function to plot and find the times where the traffic volume is\r\n% highest. For simplicity, we look at the eastbound data only.\r\nstartDate = datetime(startTime{1}, 'InputFormat', 'MMMM d, yyyy HH:mm:ss ');\r\nendDate = datetime(stopTime{1}, 'InputFormat', 'MMMM d, yyyy HH:mm:ss ');\r\ndatevector = [startDate, endDate];\r\n[Daily, t] = thingSpeakRead(38629, 'DateRange', datevector);\r\n% Look at east bound traffic only\r\nDailyEast = Daily(:, 2);\r\ntimestamp = datetime(t,'ConvertFrom','datenum'); \r\ndateAnalyzed = startTime{1};\r\ndateAnalyzed = {dateAnalyzed(1:(end-8))};\r\n%% Downsampling into 48 Bins of 30 Minute Chunks of Data and Finding Peaks\r\n% The raw traffic data is very spiky and hard to visualize.  If we want to\r\n% see what time of day has the highest volume of traffic, we need to look\r\n% at the data on a time scale larger than 15 seconds. To do this we divide\r\n% the 24 hour day into 30 minute segments.  Each segment begins at the top\r\n% of the hour and ends 30 minutes later.\r\ndownsamplesize = floor(length(DailyEast)\/48);\r\nteastper30=datetime(startTime{1});\r\nDailyEastper30(1:48) = 0; % pre-allocate\r\nfor k = 1:48  % calucate daily tarffic in each 30 minute segment\r\nDailyEastper30(k) = sum(DailyEast(1+downsamplesize*(k-1):downsamplesize*k));\r\nteastper30(k+1) = teastper30(k)+1\/48; % timestamps showing end of each 30 minute period\r\nend\r\nteastper30=teastper30';\r\nteastper30(1) = []; % start first bin at 12:30 am\r\ntimestampPer30 = teastper30;\r\n% Find peaks and their times (locations)\r\n[peaks,location] = findpeaks(DailyEastper30,  'Threshold',100, 'MinPeakHeight', 1100);\r\n% Plot peaks\r\nfigure\r\nfindpeaks(DailyEastper30, datenum(timestampPer30),'Threshold',100, 'MinPeakHeight', 1100)\r\ndatetick\r\nxlabel('Time of Day')\r\nylabel('Vehicle count per 30 minutes')\r\ntitle(strcat('Peak volume on ', {' '}, dateAnalyzed)) \r\ndateAnalyzed\r\npeaktimes = timestampPer30(location)\r\nDailyVolume = sum(DailyEast)\r\n%% Online Analysis: Calculating and Scheduling MATLAB Code inside ThingSpeak \r\n% \r\n% So far we have brought the data from ThingSpeak back into MATLAB to do\r\n% some offline analysis of weekly and daily traffic patterns. But what if\r\n% we want to compute the daily volume of traffic and send that data to\r\n% another channel on ThingSpeak?  It\u00e2\u20ac\u2122s very simple to do the calculation\r\n% offline, but ThingSpeak with its MATLAB integration enables us to do this\r\n% online too.  To calculate the daily volume of traffic, we use the MATLAB\r\n% Analysis App available on Thingspeak.com.  We write code to fetch the\r\n% previous day\u00e2\u20ac\u2122s raw traffic data and we sum up the results.  We then use\r\n% the |thingSpeakWrite| command to send the data to a new channel called\r\n% Daily Volume.\r\n%\r\n% <<TS_ML_Code.png>>\r\n%\r\n% Finally, we schedule our analysis to run once a day just after midnight\r\n% using the ThingSpeak TimeControl App.\r\n%\r\n% <<timeControl.png>>\r\n%\r\n%% Viewing the Calculated Data Online\r\n% We have now created a channel that reports the daily volume of cars\r\n% counted. Because this channel is on ThingSpeak and the MATLAB Analysis\r\n% App is built in, we can look at this analysis at any time using any web\r\n% browser on a PC or mobile device. To see the calculated value, go to\r\n% <https:\/\/thingspeak.com\/channels\/51671 channel 51671 on ThingSpeak>.\r\n%\r\n% Because ThingSpeak is a cloud service, it will execute the MATLAB\r\n% analysis code to calculate this value every day even when you are not\r\n% logged into ThingSpeak.\r\n%\r\n% For more information on how to send data to ThingSpeak, see this\r\n% <https:\/\/www.mathworks.com\/hardware-support\/thingspeak.html support page>.\r\n% If you have a MathWorks account, you already have access to ThingSpeak.\r\n% Just log-in and explore at ThingSpeak.com!\r\n%\r\n% What kind of IoT data are you trying to access? As you prototype your IoT\r\n% system, how are you doing your online and offline analysis?  We'd like to\r\n% hear <https:\/\/blogs.mathworks.com\/loren\/?p=1365#respond here>!\r\n\r\n##### SOURCE END ##### a9b4491ab6f44dce80af71c20d1c7b9a\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/2016\/traffic_analysis_blog_03.png\" onError=\"this.style.display ='none';\" \/><\/div><!--introduction--><p>Today I'd like to introduce you to a guest blogger, Eric Wetjen, who is a Product Marketing Manager here at MathWorks. Today, Eric will be discussing ThingSpeak, a platform for collecting and analyzing Internet of Things (IoT) data with MATLAB.... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2016\/03\/23\/explore-your-iot-data-with-thingspeak-and-matlab\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[66,6],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/1365"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/users\/39"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/comments?post=1365"}],"version-history":[{"count":8,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/1365\/revisions"}],"predecessor-version":[{"id":2702,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/1365\/revisions\/2702"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=1365"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=1365"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=1365"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}