thingspeak react trigger issues

10 visualizzazioni (ultimi 30 giorni)
tao
tao il 10 Apr 2025
Modificato: Vinod il 10 Apr 2025
Tingspeak: I use the first chennel to receive data, and every time the received data is greater than 0, matlab analysis is triggered by react, and the results are written into the second channel. The first channel receives data every 20 seconds. Finally, it was found that the second channel missed a lot of data. May I ask what causes it?
  2 Commenti
Christopher Stapels
Christopher Stapels il 10 Apr 2025
You can retest your logic at any time, since the data in the channel should be persistant. For the test, you could just make a count of values over zero, so you dont consume messages.
It is possible your device is wrtitng something as a string that ThingSpeak is not able to complete the logic test to compare to zero. For example if you were including units in the data string. Is your channel public? If yes, please share the channel ID and we can take a look at the data and see. If the cahnnel is not public, please share a sample of the data here and perhaps we can help you figure out what happened.
tao
tao il 10 Apr 2025
Modificato: Vinod il 10 Apr 2025
I actually want react to be triggered every time I receive data, and every data is greater than 0, so this is the setting.
All I'm transmitting is numbers.
My first channel: https://thingspeak.mathworks.com/channels/2906848
My code:
try
% ==== Read sensor data + sample number (field5 holds the row number) ====
data = thingSpeakRead(2906848, ...
'Fields', [1 2 3 4 5], ...
'NumPoints', 1, ...
'ReadKey', '___REDACTED___');
% Check if the data is valid (not empty or containing NaN)
if isempty(data) || any(isnan(data(:)))
disp('⚠ No valid sensor data available. Skipping this analysis run.');
return;
end
% Extract input features and sample number
x = data(1:4); % Sensor data: [volt, rotate, pressure, vibration]
number = data(5); % Sample number from field5
% ==== Perform prediction using trained SVM model ====
w = [0.0807; 0.0097; 0.0725; 0.1669]; % SVM weights
b = -37.5876; % SVM bias
prediction = dot(w, x) + b >= 0; % Linear SVM decision function
% Display debug information in MATLAB console
fprintf('📥 Sample Number: %d | Prediction: %d\n', number, prediction);
% ==== Write results to Output Channel ====
% field1 = prediction result (0 = normal, 1 = fault)
% field2 = sample number
thingSpeakWrite(2907450, ...
'Fields', [1 2], ...
'Values', [prediction, number], ...
'WriteKey', 'xxxxxxxxxxxxxxxx');
disp('✅ Prediction result and sample number successfully written to Output Channel');
catch ME
% If any error occurs, display the error message
fprintf('❌ Error occurred: %s\n', ME.message);
end
Since I use the free version of tingspeak, is it possible that the frequent triggering of react causes the triggering of some times to be unsuccessful?

Accedi per commentare.

Risposte (1)

Christopher Stapels
Christopher Stapels il 10 Apr 2025
Modificato: Vinod il 10 Apr 2025
Using the paid version might help your scheduling somewhat, but it would not completely prevent this problem. React is not scheduled to run instantaneously after your data is entered. It will run, but not necessarily immediately. Thus there can still be times when your react will try to write twice to the write channel within the time window. The write window is 15 s for free, but the same thing could still happen at the paid tier with 1 s (though a lot less likely).
I recommend batch processing (a thing MATLAB is pretty terrific at.) Read the channel every 5 minutes, or hour, or day, and process all the data from that time interval. Then store the data as a table and bulk write it (You can just use thingSpeakWrite with a table or timetable as the data argument to get bulk write)
The time interval between sequential bulk-update calls should be 15 seconds or more.
You will still consume the same number of messages this way (bulk writes cost multiple messages), but then your react operations wont ever run into eachother.
Writing timetable example from the doc.
% Generate random data
dataField1 = randi(10,10,1);
dataField2 = randi(10,10,1);
% Generate timestamps for the data
tStamps = [datetime('now')-minutes(9):minutes(1):datetime('now')]';
% Create timetable
dataTable = timetable(tStamps,dataField1,dataField2);
channelID = 17504; % Change to your channel ID
writeKey = '___REDACTED___'; % Change to your Write API Key
% Write 10 values to each field of your channel along with timestamps
thingSpeakWrite(channelID,dataTable,'WriteKey',writeKey)

Community

Più risposte nel  ThingSpeak Community

Categorie

Scopri di più su Prepare and Analyze Data in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by