The write channel is same as read channel data. Why the data is not processed?
5 次查看(过去 30 天)
显示 更早的评论
We collect the real time data from read channel and want process it before sending it to write channel, but the data values in the write channel replicate the read channel values for the following code. Could anyone give suggession how to get the processed data?
% Source ThingSpeak Channel ID for reading data
readChannelID = 2522808; % Replace with your source channel ID
% Destination ThingSpeak Channel ID for writing data
writeChannelID = 2522810; % Replace with your destination channel ID
% ThingSpeak Read API Key for the source channel
readAPIKey = 'XXX'; % Replace with your read API key
% ThingSpeak Write API Key for the destination channel
writeAPIKey = 'XXX'; % Replace with your write API key
%% Read Data %%
% Read all available data from the source channel
data = thingSpeakRead(readChannelID, 'ReadKey', readAPIKey, 'Fields', [1, 2]);
% Extract the values from the read data
values1 = data(:, 1); % Values from field 1 i.e. Estimated Voltage
values2 = data(:, 2); % Values from field 2 i.e. Input Current
% Determine the number of data points retrieved
numPoints = size(data, 1); % Assign the number of rows in the variable 'data' to the variable 'numPoints'
% Generate timestamps for the data
timeStamps = datetime('now') - minutes(numPoints:-1:1);
% Initialize Y arrays
Y1 = zeros(size(values1)); % Create a new array 'Y1' filled with zeros that has same size as the array 'values1'
Y2 = zeros(size(values2)); % Create a new array 'Y2' filled with zeros that has same size as the array 'values2'
% Perform the operation Y(i) = i * I(i) for each value in the read data
for i = 1:length(values1)
disp(['Processing index ', num2str(i)]);
disp(['values1(', num2str(i), ') = ', num2str(values1(i))]);
disp(['values2(', num2str(i), ') = ', num2str(values2(i))]);
Y1(i) = i * values1(i);
Y2(i) = i * values2(i);
disp(['Y1(', num2str(i), ') = ', num2str(Y1(i))]);
disp(['Y2(', num2str(i), ') = ', num2str(Y2(i))]);
end
% Write the data to the destination channel with timestamps
thingSpeakWrite(writeChannelID, 'WriteKey', writeAPIKey, 'Values', [Y1', Y2'], 'Fields', [1, 2], 'Timestamps', timeStamps);
0 个评论
采纳的回答
Christopher Stapels
2024-5-7
length(values1) is 1 so i is 1, and
Y1(i) = i * values1(i); just multiplies your value by 1.
Perhaps you want to read more than just one value at a time?
data = thingSpeakRead(readChannelID, 'ReadKey', readAPIKey, 'Fields', [1, 2]),'numpoints',5;
Also I suggest redacting your api keys above. They are kind of like passwords.
8 个评论
Christopher Stapels
2024-5-17
The code you wrote will work to process the values in the variable values1 and values2. If there is only 1 value read, there will be not change in the value written from the value read. The code will scale all points by their index in the list.
If you do not want to use a set number of entries, you can use a set time, such as a day or a number of minutes. You could also use some other condition to trigger a read such as a particular value being over or under a threshold.
更多回答(1 个)
Christopher Stapels
2024-5-21
a = 1:5
for i = 1: length(a)
out = i * a(i)
end
If you read more then one value from your channel at a time, then length(values1) will be longer than 1. That is why I suggested adding 'numpoints', 5 to the thingSpeakRead command in your code.
2 个评论
Christopher Stapels
2024-5-23
编辑:Christopher Stapels
2024-5-28
There are many many ways. One is to use a time. 'numDays' or 'numminutes'.
You can also store the last value read, and compare the list to find where the last value read occurs, or store the timestamp of the last value read and then read or save all timestamps after that time. There is a daterange attribute in ThingSpeakRead and start and stop dateime parameters in the api. You could write a special stop value to the channel each time you process the data, and then parse the channel and look for that stop value to know when how many values to process.
You could reprocess the whole channel each time you read and completely start over.
Whatever process writes values to the initial channel can also record the number of values written, perhaps in another field value or in the channel metadata. Then the read and process code reads that value and knows how many to write.
You can for the write process to always write a fixed number of values, perhaps even by padding the channel with empty values just to make sure the number of values is consistent, then you can filter those out later.
And so on.
社区
更多回答在 ThingSpeak Community
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Read Data from Channel 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!