I want to calculate the derivative of a channel. Can you suggest the best way to do so.
1 view (last 30 days)
Show older comments
I am logging voltage on a solar powered IOT device. I would like to calculate the delta V to get an indication of charge rate.
The data is typically sent every 30 minutes, however not always. What would be the best way to calculate this and display in a line chart to view?
My channel is here and the voltage in field 2: https://thingspeak.com/channels/175965 and https://www.hackster.io/macsboost/you-ve-got-mail-2e2d8e
0 Comments
Answers (1)
Christopher Stapels
on 21 Feb 2018
One way would be to use an instance of the React App to test if field 2 has a value in it, perhaps "if field2 < 0". Then have the react call a MATLAB analysis. The analysis reads many values of field 2 and finds the last two non NaN entries, then calculates the slope. Finally, write the data to another channel, or another field in that channel. The field plot for the write channel will show you a visualization of the derivative data. Here is an example analysis you could use, you may have to play with the value of readPoints for your channel. Don't forget to edit the channel numbers and the API keys.
readChannelID = 1111;
writeChannelID = 2222;
fieldID1 = 1;
readPoints=50;
readAPIKey = 'XXXXXXXXXXXXXXXX';
writeAPIKey = 'xxxxxxxxxxxxxxxx';
%% Read Data %%
[data, times] = thingSpeakRead(readChannelID, 'Fields', fieldID1,'NumPoints',readPoints, 'ReadKey', readAPIKey);
i=0;
lastPoint=0;
%Get the last non empty entry in the data while ((lastPoint==0) (i>=readPoints))
if isnan(data(readPoints-i))
i=i+1;
else
lastPoint=data(readPoints-i);
lastTime=times(readPoints-i);
end
end
%Get the second to last non empty entry in the data
i=i+1;
firstPoint=0;
while ((firstPoint==0)|| (i>=readPoints))
if isnan(data(readPoints-i))
i=i+1;
else
firstPoint=data(readPoints-i);
firstTime=times(readPoints-i);
end
end
deriv=(lastPoint-firstPoint)/seconds(lastTime-firstTime);
thingSpeakWrite(writeChannelID,deriv,'writeKey',writeAPIKey);
0 Comments
Communities
More Answers in the ThingSpeak Community
See Also
Categories
Find more on Visualize Data in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!