Video length is 6:19

IoT Data Explorer for ThingSpeak and MATLAB

Use IoT Data Explorer for ThingSpeak™ and MATLAB® to easily read data in your ThingSpeak channels and compare data from two different time periods. Compare yesterday to today, this hour to an hour last week, or perform week over week comparisons to see changes or anomalies. See how to use the app to read and analyze air quality monitor data and compare prequarantine to quarantine data. In the second half of the video, learn how to use App Designer to build the app, and watch the thingSpeakRead function in MATLAB read data from a particular time range.

 

Published: 19 Aug 2020

This video will demonstrate how to explore ThingSpeak data using a custom-built MATLAB app, and then go into more detail on how the app was built using App Designer.

ThingSpeak is an IoT analytics platform service that allows you to aggregate, visualize, and analyze live data streams in the cloud. You can send data to ThingSpeak from your devices, create instant visualization of live data, and send alerts.

ThingSpeak Channel Views provide a snapshot of your streaming data. This traffic monitor channel is observing live traffic data for east- and west-bound traffic on the main road outside MathWorks headquarters.

You can use MATLAB to create custom visualizations for your data, such as this week-over-week comparison, or the 48-hour summary. However, exploratory data analysis can be somewhat tedious without graphical controls to quickly modify your data visualizations.

The IoT data Explorer for ThingSpeak provides an easy-to-use, graphical interface to explore data read in from ThingSpeak.

The app and its code are publicly available on file Exchange and on the ThingSpeak GitHub site, and we will use it to look at the effect of the recent quarantine on pollution and traffic levels around MathWorks Headquarters.

After it is installed, this MATLAB app can be opened from the MATLAB app gallery. We’ll look at some features specific to this app, and afterwards, demonstrate how you can make a similar app on your own.

This channel shows measurements from an Air quality monitor made by PurpleAir that is installed in one of the parking garages at MathWorks headquarters. As more people have started working from home, fewer people are driving to work and using the garage, so we should expect to see an improvement in air quality.

First, as a sort of control, let’s compare air quality from two dates during quarantine. We’ll look at April 28 and compare it with one week before. Fill in the channel ID and set the date here, then specify [Duration] the width of time, which goes forward from midnight of the indicated date. To compare it with one week prior, we set this field [Compare Length] to one week.

Even though both dates occurred during quarantine, we see a significant difference in air quality – blue for the indicated date, orange for the week before. Perhaps temperature changes throughout the day affect the sensor readings. Field 6 of our channel contains this data, so we can check it, literally. A cursory glance of the two visualizations suggests that there could be an effect, but it’s not immediately clear.

Even though both dates occurred during quarantine, we see a significant difference in air quality – blue for the indicated date, orange for the week before. Perhaps temperature changes throughout the day affect the sensor readings. Field 6 of our channel contains this data, so we can check it, literally. A cursory glance of the two visualizations suggests that there could be an effect, but it’s not immediately clear.

Now let’s compare data from the current date to pre-quarantine times. We’ll compare this data with data from 7 weeks prior, when there was no quarantine. As we might expect, the air quality is worse, as indicated by the higher numbers. However, the temperature was also much higher.

Let’s see if the trend continues over several days. Since we can get up to 8000 data points with each read, we can visualize nearly 3 consecutive days’ worth of data. We see similar air quality levels at these times, despite quarantine vs not quarantine, and note that both date ranges do not contain weekends. Since we also see temperature become similar around those times, this seems to indicate that temperature might affect air quality more than the number of cars utilizing the garage.

[“Note that if the raw data across your time range is very cluttered, this app lets you quickly apply the MATLAB retime function to see things like hourly averages instead.” – to replace all red text.]

Hopefully you’ve seen how interactive graphical tools make it much easier to perform exploratory data analysis. Let’s talk about how you can make your own IoT data explorer in MATLAB.

As mentioned before, the app you’re seeing in this video is available from File exchange and the ThingSpeak github site, and you are highly encouraged to download it and leverage it as a starting point. MATLAB App Designer enables you to create your own apps from scratch, or edit an existing one.

Simply type “appdesigner” in the MATLAB command line.

For context, the ThingSpeak Explorer App is a 2-panel app with reflow. Let’s find the app and open it in App Designer. While you can certainly make an app that automatically updates visualizations every time one of these inputs are changed, this app keeps things simple by allowing you to set up parameters at your own pace, then use the Update button when you’re ready.

Here on the left, you can see some of the controls used to provide input, a date picker, dropdowns, and switches to select the time. [While adding another checkbox:] Simply drag-and-drop controls onto the interface, and click on them to edit and rename.

Right-click on any element to create a callback. These callbacks activate whenever the element is triggered in some relevant manner. One great use for callbacks is input validation: communicating to the user on things like, if a field requires an integer, or if a date needs to set in the past relative to another.

This IoT explorer app keeps things simple by doing all input validation on the callback for this Update button, which is also where the main action happens. Let’s take a look at the callback function.

There are three main parts of this function. First, we get all the inputs from the UI, and perform input validation. Next, we use that input to gather data from ThingSpeak – first from the selected date, then from the date in the past. And finally, we plot the data visualizations.

Let’s look at the key functionality: reading data from ThingSpeak. You can navigate to the function definition by right-clicking on the function name and clicking “Open” While it looks like there’s a lot of code here, most of it is just to make the app a little more robust, because reading data from ThingSpeak is entirely contained in this one line of code.

ThingSpeakRead needs to know a few pieces of information, particularly the channel ID. Then, you can specify additional data using name-value pairs, such as DateRange with a start and end date, selected fields as indicated by which checkboxes were checked, an API key if the channel was private, and the output format for the results. MATLAB Timetables are a great output format as they make it easy to plot and retime data.

This next section helps our app run more smoothly by ensuring we actually received data from ThingSpeakRead, and it notifies the user if something went wrong.

The final section performs retiming, if that option was selected. That concludes the process of getting data for the present and past dates, and the Update callback function will proceed to the next step.

Hope you’ve been able to see how easy it is to get data from ThingSpeak, and use MATLAB App Designer to build and modify custom apps so that you and your colleagues can analyze and visualize data in an interactive manner. Use the links below to find the entry on Github, where you can download the app, try it out for yourself, and also leave comments and feedback.