Filter löschen
Filter löschen

I want to rewrite this code that it keeps looping so when the channel 1 data reaches the threshold level, it automatically sends an alert but I also need it to stop sending if

1 Ansicht (letzte 30 Tage)
% ThingSpeak Channel ID and Read API Key for the channel you want to monitor
channelID = 2226948; % Replace with your actual ThingSpeak Channel ID
readAPIKey = 'T9OH3JOE1B5WT1SU'; % Replace with your actual ThingSpeak Read API Key
% Field ID for the channel you want to monitor (e.g., Field 1)
fieldID = 1;
% Threshold value for the channel
threshold1 = 100; % Your threshold value for the channel
threshold2 = 200;
threshold3 = 300;
% Read the latest data from the specified channel and field
try
% Read the latest entry from the specified field
response = thingSpeakRead(channelID, 'Fields', fieldID, 'NumPoints', 1, 'ReadKey', readAPIKey);
% Extract the latest data point from the response
latestData = response(end, fieldID);
% Call SMS integration function
if latestData >= threshold1 && latestData <= threshold2
sendMessage('+639319217695,+639105034409', 'Threshold exceeded in Yellow!');
elseif latestData >= threshold2 && latestData <= threshold3
sendMessage('+639319217695,+639105034409', 'Threshold exceeded in Orange!');
elseif latestData >= threshold3
sendMessage('+639319217695,+639105034409', 'Threshold exceeded in Red!');
end
catch
disp('Error retrieving data from ThingSpeak'); % Handle data retrieval errors here
end
function sendMessage(number, message)
% Replace with your SMS service API endpoint and API key
apiUrl = '- - - - -';
apiKey = '- - - - -';
% Compose the message payload
payload = struct('apikey', apiKey, 'number', number, 'message', message);
try
% Send the SMS using the SMS service API
response = webwrite(apiUrl, payload);
% Process the response as needed
disp('SMS sent successfully');
catch
disp('Error sending SMS');
end
end
  1 Kommentar
Rainer Gerald
Rainer Gerald am 14 Sep. 2023
I want to rewrite this code that it keeps looping so when the channel 1 data reaches the threshold level, it automatically sends an alert but I also need it to stop sending if the first alert level has already been reached.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Christopher Stapels
Christopher Stapels am 14 Sep. 2023
Verschoben: Christopher Stapels am 17 Okt. 2023
That is also I problem Ive had. When you send an alert, you can write to another channel a message sent flag and read that channel before you decide to send the email. Then just remember to write the flag back to sero once you acknowledge the alert or the threshold is no longer crossed.
Another way to do it is to use time. i.e. If you check the threshold evey five minutes, then check the time, if its ten minutes (or more) after the threshold was crossed, then assume the alert was sent last time.

Communitys

Weitere Antworten in  ThingSpeak Community

Kategorien

Mehr zu REST API finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by