{"id":4495,"date":"2017-03-23T18:15:53","date_gmt":"2017-03-23T22:15:53","guid":{"rendered":"https:\/\/blogs.mathworks.com\/community\/?p=4495"},"modified":"2018-02-15T16:41:28","modified_gmt":"2018-02-15T21:41:28","slug":"thingspeak-your-matlab-powered-window-on-the-world","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/community\/2017\/03\/23\/thingspeak-your-matlab-powered-window-on-the-world\/","title":{"rendered":"ThingSpeak: Your MATLAB-Powered Window on the World"},"content":{"rendered":"<p>Have you tried <a href=\"https:\/\/thingspeak.com\/\">ThingSpeak<\/a> yet? It&#8217;s cool, it&#8217;s free, and it talks to MATLAB. I like all three of those things. Let me show you how it works.<\/p>\n<p>ThingSpeak is called an &#8220;Internet of Things platform&#8221;. You can use it to collect data from internet-connected devices, analyze it, plot it, and then use it to kick off some other event. So, for example, you could have a sensor that <a href=\"https:\/\/thingspeak.com\/channels\/50289\">measures the tide in Ockway Bay<\/a>\u00a0and then makes a nice chart and maybe even <a href=\"https:\/\/www.hackster.io\/matlab-iot\/measure-and-analyze-tide-levels-with-thingspeak-and-matlab-efa405\">tweets when the water is deep enough for you to go boating<\/a>.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"481\" height=\"282\" class=\"alignnone size-full wp-image-4496\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/tide-level.png\" alt=\"\" \/><\/p>\n<p>I&#8217;m going to use ThingSpeak to sense the environment, but not something as exotic as the tides around Cape Cod. My environment is a <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/cody\/solutions\">web page that shows how\u00a0many solutions have been submitted to Cody<\/a>. My sensor is a screen-scraping function that grabs the right number from the page.<\/p>\n<p>My ultimate goal is to make an automatically updated MATLAB plot that shows me the number of new Cody solutions that have come in every day for the past 60 days. To do this, I&#8217;m going to use four different ThingSpeak services:<\/p>\n<ol>\n<li>a timer<\/li>\n<li>a MATLAB code block to grab the number and store it in a ThingSpeak channel<\/li>\n<li>the channel itself<\/li>\n<li>a MATLAB code block to take 60 days worth of data and plot it<\/li>\n<\/ol>\n<p>Here&#8217;s how it all fits together<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"600\" height=\"305\" class=\"alignnone size-full wp-image-4497\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/thingspeak-diagram.png\" alt=\"\" \/><\/p>\n<p>Once a day, the TimeControl app will kick off a MATLAB Analysis app to grab the number of Cody solutions. I&#8217;ll take that number and save it to a ThingSpeak channel. Associated with that channel, I&#8217;ll have a MATLAB Visualization app that does some further analysis and turns it into a pretty plot.<\/p>\n<p>The <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/cody\/solutions\">Cody Solutions page<\/a> has the number of solutions displayed in the upper right. I use the <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/44751-url-filter\">urlfilter<\/a> command to grab that number and then push it to the ThingSpeak channel.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"586\" height=\"332\" class=\"alignnone size-full wp-image-4498\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/scraping.png\" alt=\"\" \/><\/p>\n<p>Here&#8217;s the code that runs every day on ThingSpeak.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"501\" height=\"296\" class=\"alignnone size-full wp-image-4500\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/matlab-analysis.png\" alt=\"\" \/><\/p>\n<p>My channel (<a href=\"https:\/\/thingspeak.com\/channels\/217011\">number 217011<\/a>) comes with a nice default chart.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"475\" height=\"327\" class=\"alignnone size-full wp-image-4501\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/solutions-chart.png\" alt=\"\" \/><\/p>\n<p>I can see that the number is very big and steadily rising. That&#8217;s great, but it&#8217;s hard to see much detail here.\u00a0What I&#8217;d really like is a plot of the number of solutions submitted per day. This is where the MATLAB Visualization app comes in handy. I want to read in the data and then put it into a <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/timetables.html\">timetable<\/a>. This makes it easy for me to resample it once every day. Once I do this, I can feel confident that the time interval I get when I take the element-by-element difference is always exactly 24 hours. I can plot this daily difference and then include, for good measure, a seven day moving average to wash out any weekly artifacts. All in a handful of lines of code. Look:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"492\" height=\"510\" class=\"alignnone size-full wp-image-4502\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/matlab-plot.png\" alt=\"\" \/><\/p>\n<p>And finally, here is the plot I&#8217;ve been working toward.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"500\" height=\"415\" class=\"alignnone size-full wp-image-4503\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/final-plot.png\" alt=\"\" \/><\/p>\n<p>Every day we get between 400 and 900 new solutions on Cody. And now I can see that there was a marked rise in the number of solutions submitted in mid-February. I wonder why? That&#8217;s a question for another day. For now I want to close by pointing out that all this was free and based on the latest version of MATLAB. From now until the day I shut down this channel, I will have this automatically generated plot of up-to-date Cody statistics. Thanks ThingSpeak!<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/tide-level.png\" onError=\"this.style.display ='none';\" \/><\/div>\n<p>Have you tried ThingSpeak yet? It&#8217;s cool, it&#8217;s free, and it talks to MATLAB. I like all three of those things. Let me show you how it works.<br \/>\nThingSpeak is called an &#8220;Internet of&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/community\/2017\/03\/23\/thingspeak-your-matlab-powered-window-on-the-world\/\">read more >><\/a><\/p>\n","protected":false},"author":69,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/4495"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/users\/69"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/comments?post=4495"}],"version-history":[{"count":8,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/4495\/revisions"}],"predecessor-version":[{"id":5304,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/4495\/revisions\/5304"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/media?parent=4495"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/categories?post=4495"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/tags?post=4495"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}