Thingspeak analysis & predicting sensor data

andy neo on 16 May 2021
Latest activity Edit by Christopher Stapels on 21 May 2021

Hi, I have an AIOT project on vertical farming project. Currently, I am using Thingspeak to store sensor values. I am planning to do the followings:

1) do an analysis on the data collected for each sensors on their max, min and median (50%) to determine that the sensor value is currently ok for the plants. I am aware that Thingspeak is able to calculate these and I am learning about this now.

2) I would like to predict the sensor values collected ( eg temperature/humidity etc). I feel that this is a cool feature to have in my project but I am not sure how do I go about this. For experts in Thingspeak, do you think this is a must have for an internship project on vertical farming and how do I apply this in my project? I am not sure if this is manageable for a single student to do this and I really appreciate it if anyone would guide me along.

Christopher Stapels
Christopher Stapels on 17 May 2021 (Edited on 20 May 2021)

This type of project is totally within the reach of a student, and ThingSpeak is a pretty perfect platform for it (at least in my opinion)

I'm not sure prediction is a must have, but it would certainly make your project awesome. The data collection over time will be a major hassle. In order for prediction algorithms to converge, often you need fairly consistent data. As long as you have patience, ThingSpeak will faithfully record and store your data. This example shows some good prediction, but you will need come additional toolboxes in MATLAB to run it. Here is a soil monitor example , but I recommend you use the ThingSpeak library instead of directly making your HTTP calls as in this example. Here is an example using the library . You can use MATLAB analysis app to write the code for the statistical calculations. Let me know if you need help with those parts as well.

andy neo
andy neo on 19 May 2021

Hi Christopher,

Thank you for replying. While I was waiting for my data to be collected for several days, I was reading up Thingspeak analysis as well as the links you provided to me.

I have been looking at examples Thingspeak has regarding Thingspeak analysis and I would like to ask a few questions. As my goal for now is to calculate the min, max and the mean for all of the sensors, all I have done for now is to make use of the example codes in Thingspeak analysis, "Calculate high and low temperatures" and "Calculate and display average humidity". I just changed the Write API, channel ID, Read API, and change NumMins to NumPoints. Is it really that simple? It is scary how easy it is to analyze sensor data that are collected in Thingspeak if thats the case.

From what I have learnt, after I do the analysis, Thingspeak is able to produce the results in another Channel by using the Write API and Write Channel ID. However, I am unable to see the results in the Write Channel ID fields. I did delete the "%" for the last line but it does not show it to me as well. May I know if I get the concept wrong please?

As for Thingspeak prediction, do you recommend me to download other stuff besides "System Identification Toolbox"? So far, I have been using Thingspeak website version to do Matlab analysis and visualization and I have not downloaded anything for Thingspeak as of now.

I am very sorry to bother you with these questions!

Christopher Stapels
Christopher Stapels on 20 May 2021

The workflow you describe to write to another channel with your analyzed results sounds correct. I think you will have to share some part of your code for us to be sure and to help you solve the problem. Make sure the channel ID's and write API keys match for the channels you are reading and writing in. Feel free to redact the API keys though, they are like passwords.

I'm not sure what you mean about downloads. System ID toolbox works in ThingSpeak, you wont need to download anything, but you would have to have a MATLAB license and a license for system ID Toolbox as well. It is possible and encouraged to use MATLAB to read ThingSpeak data and analyze it there, if you have a MATLAB license.

We are happy to help our users get more out of ThingSpeak, we appreciate your discussion!

andy neo
andy neo on 21 May 2021 (Edited on 21 May 2021)

Hi, I am sorry to bother you again. My idea for this code is to display the min, max and the mean value of the temperature in a single graph.

% Enter your MATLAB Code below

readChannelID = 1212070;

TemperatureFieldID = 2;

readAPIKey = 'ReadAPI';

temperature = thingSpeakRead(readChannelID,'Fields',TemperatureFieldID,'NumPoints',2500,'ReadKey',readAPIKey); avgTemp = mean(temperature); display(avgTemp,'Average Temperature');

[tempF,timeStamp] = thingSpeakRead(readChannelID,'Fields',TemperatureFieldID, ... 'DateRange',[datetime(2021,4,1,11,00,01),datetime(2021,5,3,0,02,52)],'ReadKey',readAPIKey);

[maxTempF,maxTempIndex] = max(tempF); [minTempF,minTempIndex] = min(tempF);

timeMaxTemp = timeStamp(maxTempIndex); timeMinTemp = timeStamp(minTempIndex);

display(maxTempF,'Maximum Temperature for the the first 2500 points is'); display(minTempF,'Minimum Temperature for the first 2500 points is');

writeChannelID = 1212074; writeAPIKey = 'WriteAPI';

thingSpeakWrite(writeChannelID,maxTempF,'timestamp',timeMaxTemp,minTempF,'timestamp',timeMinTemp,avgTemp,'WriteKey',writeAPIKey);

I managed to display the min, max and mean temperature in the console but I got the following error when trying to write the data in another channel:

Error using Custom (no starter code) 4 (line 24) Expected a string scalar or character vector for the parameter name.

As for Thingspeak prediction, it looks like that we have to pay for both MATLAB license and System ID Toolbox license respectively. I am aware that this may sound stupid, but is it possible to do the coding for prediction in Thingspeak's analysis custom code? Or may I know if there are other ways to do it to predict sensor values stored in Thingspeak for free please? I discussed with my lecturers and they told me that they hope to reduce the cost as much as possible.

Christopher Stapels
Christopher Stapels on 21 May 2021 (Edited on 21 May 2021)

Have a look at the doc page for thingSpeakWrite . I think you have made some basic mistakes.

For plotting the data in the same plot, I would read the data in (perhaps use a timetable) and the use several plot commands to add each value.

plot(set1);
hold on
plot(set2);
plot(set3,etc)

You can read several variables in in one call to thingSpeakRead instead of doing two sequentially. Use the name value pair 'fields',[1 2 3 all fields] and if you use 'outputformat','timetable' then your return value will be timetable with data from each field you specify.

For example:

   myData=thingSpeakRead(...'fields',[1 3],'outputformat',timetable'...);
will give
myData.TimeStamps with all timestamps, and 
myData.<field1Name> with field 1 data, etc.

You can certainly write your own prediction code, and there is a lot of math available in base MATLAB that you can use in ThingSpeak. Please share if you develop a good method! File exchange is one good place to share functions or look for others functions.