필터 지우기
필터 지우기

ThingSpeak don't update channel automatic

조회 수: 9 (최근 30일)
Sergio R
Sergio R 2019년 5월 6일
답변: Sergio R 2019년 5월 8일
Hello, in a ThingSpeak channel, I am reading every one second the information of two current signals (I have the licensed application).
I currently have a MATLAB Analysis routine that adds those two signals and writes, on the same channel, the result of that sum. In the routine I can check that effectively if the sum of the two channels is being done.
I do that now with a good result but my problem is that the channel is not updated except when I run MATLAB Analysis.
How can I make the channel automatically update every second?
-------------------------------------------------------------------------------
This is my routine:
readChannelID = 123;
readAPIKey = 'XXX';
writeChannelID = 123;
writeAPIKey = 'YYY';
%%%%%%%%%%%%%%%%%%%%%% CALCULO DE CORRIENTE %%%%%%%%%%%%%%%%%%%%%%
[Cte1, timeStamp] = thingSpeakRead(readChannelID,'Fields',1,'numPoints', 2, 'ReadKey', readAPIKey);
anyMissingValues = sum(isnan(Cte1));
if sum(anyMissingValues) > 0
missingValueIndex = find(~sum(isnan(Cte1),2));
cleanCte1 = Cte1(missingValueIndex, :);
cleanTimeStamps = timeStamp(missingValueIndex);
else
cleanCte1 = Cte1;
cleanTimeStamps = timeStamp;
end
[Cte2, timeStamp] = thingSpeakRead(readChannelID,'Fields',4,'numPoints', 2, 'ReadKey', readAPIKey);
anyMissingValues = sum(isnan(Cte2));
if sum(anyMissingValues) > 0
missingValueIndex = find(~sum(isnan(Cte2),2));
cleanCte2 = Cte2(missingValueIndex, :);
cleanTimeStamps = timeStamp(missingValueIndex);
else
cleanCte2 = Cte2;
cleanTimeStamps = timeStamp;
end
Corriente = round([cleanCte1 + cleanCte2],1);
display (Corriente, 'Corriente')
%%%%%%%%%%%%%%%%%%%%%% VOLTAJE %%%%%%%%%%%%%%%%%%%%%%
[Voltaje, timeStamp] = thingSpeakRead(readChannelID,'Fields',2,'numPoints', 2, 'ReadKey', readAPIKey);
anyMissingValues = sum(isnan(Voltaje));
if sum(anyMissingValues) > 0
missingValueIndex = find(~sum(isnan(Voltaje),2));
cleanVoltaje = Voltaje(missingValueIndex, :);
cleanTimeStamps = timeStamp(missingValueIndex);
else
cleanVoltaje = Voltaje;
cleanTimeStamps = timeStamp;
end
display (cleanVoltaje, 'Voltaje')
%%%%%%%%%%%%%%%%%%%%%% CALCULO DE POTENCIA %%%%%%%%%%%%%%%%%%%%%%
Potencia = round([Corriente * cleanVoltaje],0)
%%%%%%%%%%%%%%%%%%%%%% CALCULO DE ENERGIA %%%%%%%%%%%%%%%%%%%%%%
[Energia1, timeStamp] = thingSpeakRead(readChannelID,'Fields',3,'numPoints', 2, 'ReadKey', readAPIKey);
anyMissingValues = sum(isnan(Energia1));
if sum(anyMissingValues) > 0
missingValueIndex = find(~sum(isnan(Energia1),2));
cleanEnergia1 = Energia1(missingValueIndex, :);
cleanTimeStamps = timeStamp(missingValueIndex);
else
cleanEnergia1 = Energia1;
cleanTimeStamps = timeStamp;
end
[Energia2, timeStamp] = thingSpeakRead(readChannelID,'Fields',5,'numPoints', 2, 'ReadKey', readAPIKey);
anyMissingValues = sum(isnan(Energia2));
if sum(anyMissingValues) > 0
missingValueIndex = find(~sum(isnan(Energia2),2));
cleanEnergia2 = Energia2(missingValueIndex, :);
cleanTimeStamps = timeStamp(missingValueIndex);
else
cleanEnergia2 = Energia2;
cleanTimeStamps = timeStamp;
end
Energia = [cleanEnergia1 + cleanEnergia2];
display (Energia, 'Energia')
%%%%%%%%%%%%%%%%% Write the results in fields 6, 7 and 8 of the same channel %%%%%%%%%%%%%%%%%%%%
thingSpeakWrite(writeChannelID, 'Fields', [6,7,8], 'Values',...
[Corriente, Potencia, Energia],'WriteKey',writeAPIKey);
OUTPUT:
Corriente = 3.4000
Voltaje = 116
Potencia = 394
Energia = 0.0040

채택된 답변

Sergio R
Sergio R 2019년 5월 8일
I have followed your advice and it actually works. The channel is already updated automatically.
thanks for your help

추가 답변 (2개)

Hans Scharler
Hans Scharler 2019년 5월 7일
I would write to a second ThingSpeak channel instead of writing data back to the same channel. The reason is that ThingSpeak will overwrite other fields with nulls for the fields that do not have a value. I think that you are writing to the 6,7,8 fields, then your channel updates and overrides their data with nulls.
Also, you can read an entire channel's worth of data in one thingSpeakRead request. This will speed up your script.
  댓글 수: 2
Sergio R
Sergio R 2019년 5월 7일
Hi Hans,
I already made the change you suggested, but it does not work either.
The values are not updated automatically, except when I give "Save and run" to the application.
In a first channel (AAAA) I read two current signals, two Voltage signals, two power signals and two energy signals, then I write them the result of the sum of the current, power and energy in a new channel (BBBB) .
At the exit of the application I can assure you that there are indeed valid values for each variable.
How to make the values automatically update?
readChannelID = AAAAA;
readAPIKey = '12345678';
%%%%%%%%%%%%%%%%%%%%%% READ CURRENT IN CHANNEL AAAAA, Fields 1 & 5 %%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%% CURRENT CALCULATION %%%%%%%%%%%%%%%%%%%%%%
[Cte1, timeStamp] = thingSpeakRead(readChannelID,'Fields',1,'numPoints', 2, 'ReadKey', readAPIKey);
anyMissingValues = sum(isnan(Cte1));
if sum(anyMissingValues) > 0
missingValueIndex = find(~sum(isnan(Cte1),2));
cleanCte1 = Cte1(missingValueIndex, :);
cleanTimeStamps = timeStamp(missingValueIndex);
else
cleanCte1 = Cte1;
cleanTimeStamps = timeStamp;
end
[Cte2, timeStamp] = thingSpeakRead(readChannelID,'Fields',5,'numPoints', 2, 'ReadKey', readAPIKey);
anyMissingValues = sum(isnan(Cte2));
if sum(anyMissingValues) > 0
missingValueIndex = find(~sum(isnan(Cte2),2));
cleanCte2 = Cte2(missingValueIndex, :);
cleanTimeStamps = timeStamp(missingValueIndex);
else
cleanCte2 = Cte2;
cleanTimeStamps = timeStamp;
end
Corriente = round([cleanCte1 + cleanCte2],1);
display (Corriente, 'Corriente')
%%%%%%%%%%%%%%%%%%%%%% READ VOLTAGE IN CHANNEL AAAAA Field 2 %%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%% VOLTAGE CALCULATION %%%%%%%%%%%%%%%%%%%%%%
[Voltaje, timeStamp] = thingSpeakRead(readChannelID,'Fields',2,'numPoints', 2, 'ReadKey', readAPIKey);
anyMissingValues = sum(isnan(Voltaje));
if sum(anyMissingValues) > 0
missingValueIndex = find(~sum(isnan(Voltaje),2));
cleanVoltaje = Voltaje(missingValueIndex, :);
cleanTimeStamps = timeStamp(missingValueIndex);
else
cleanVoltaje = Voltaje;
cleanTimeStamps = timeStamp;
end
display (cleanVoltaje, 'Voltaje')
%%%%%%%%%%%%%%%%%%%%%% POWER CALCULATION %%%%%%%%%%%%%%%%%%%%%%
Potencia = round([Corriente * cleanVoltaje],0)
%%%%%%%%%%%%%%%%%%%%%% READ ENERGY IN CHANNEL AAAAA, Fields 4 & 8 %%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%% ENERGY CALCULATION %%%%%%%%%%%%%%%%%%%%%%
[Energia1, timeStamp] = thingSpeakRead(readChannelID,'Fields',4,'numPoints', 2, 'ReadKey', readAPIKey);
anyMissingValues = sum(isnan(Energia1));
if sum(anyMissingValues) > 0
missingValueIndex = find(~sum(isnan(Energia1),2));
cleanEnergia1 = Energia1(missingValueIndex, :);
cleanTimeStamps = timeStamp(missingValueIndex);
else
cleanEnergia1 = Energia1;
cleanTimeStamps = timeStamp;
end
[Energia2, timeStamp] = thingSpeakRead(readChannelID,'Fields',8,'numPoints', 2, 'ReadKey', readAPIKey);
anyMissingValues = sum(isnan(Energia2));
if sum(anyMissingValues) > 0
missingValueIndex = find(~sum(isnan(Energia2),2));
cleanEnergia2 = Energia2(missingValueIndex, :);
cleanTimeStamps = timeStamp(missingValueIndex);
else
cleanEnergia2 = Energia2;
cleanTimeStamps = timeStamp;
end
Energia = [cleanEnergia1 + cleanEnergia2];
display (Energia, 'Energia')
%%%%%%%%%%% Write the results in fields 1, 2, 3 & 4 in another channel (BBBBB) %%%%%%%%%%%%%%%%
writeChannelID = BBBBB;
writeAPIKey = '12345678';
thingSpeakWrite(writeChannelID, 'Fields', [1,2,3,4], 'Values',...
[Corriente, cleanVoltaje, Potencia, Energia],'WriteKey',writeAPIKey);
OUTPUT:
Corriente = 4.3000
Voltaje = 118
Potencia = 507
Energia = 0.0020
Hans Scharler
Hans Scharler 2019년 5월 7일
I see. This means that the MATLAB code is not scheduled to run automatically. At the bottom of the page, there is a place to add a TimeControl or a React. TimeControl will schedule your code to run and React will run the code based on a condition.

댓글을 달려면 로그인하십시오.


Sergio R
Sergio R 2019년 5월 7일
편집: Sergio R 2019년 5월 7일
True. But the minimum control time is every 5 minutes. I need it to be every 1 or 2 seconds.
How can I make it update automatically every 1 or 2 seconds?
  댓글 수: 1
Hans Scharler
Hans Scharler 2019년 5월 8일
You can use React. Select "On data insert". This will cause the MATLAB code to run every time data gets sent to ThingSpeak.

댓글을 달려면 로그인하십시오.

커뮤니티

더 많은 답변 보기:  ThingSpeak 커뮤니티

카테고리

Help CenterFile Exchange에서 Visualize Data에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by