The write channel is same as read channel data. Why the data is not processed?

2 visualizaciones (últimos 30 días)
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);

Respuesta aceptada

Christopher Stapels
Christopher Stapels el 7 de Mayo de 2024
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 comentarios
Christopher Stapels
Christopher Stapels el 17 de Mayo de 2024
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.
Tanusree
Tanusree el 21 de Mayo de 2024
Thanks for the suggestion. But the for following code,
for i = 1:length(values1)
Y1(i) = i * values1(i);
end
I would like to create a new array Y1 where each element is the product of its index and the corresponding element in the input array values1.
But as you mentioned earlier,
'' length(values1) is 1 so i is 1, and
Y1(i) = i * values1(i); just multiplies your value by 1.''
How can I get different output instead of same?
For example,
Suppose my values1 = [1 5 2 7 8 3 5], then Y1 should be [0 5 4 21 32 15 30]. But in my case, it is showing same output as input i.e. [1 5 2 7 8 3 5].
Is there any way to resolve it?

Iniciar sesión para comentar.

Más respuestas (1)

Christopher Stapels
Christopher Stapels el 21 de Mayo de 2024
a = 1:5
a = 1x5
1 2 3 4 5
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
for i = 1: length(a)
out = i * a(i)
end
out = 1
out = 4
out = 9
out = 16
out = 25
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 comentarios
Tanusree
Tanusree el 23 de Mayo de 2024
Thanks for the response. But it is possible when you know the exact numpoint value. If I don't know how many datas will be retrieved in real time, how could I mention the exact count?
Christopher Stapels
Christopher Stapels el 23 de Mayo de 2024
Editada: Christopher Stapels el 28 de Mayo de 2024
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.

Iniciar sesión para comentar.

Comunidades de usuarios

Más respuestas en  ThingSpeak Community

Categorías

Más información sobre Read Data from Channel en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by