Effacer les filtres
Effacer les filtres

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 vue (au cours des 30 derniers jours)
% 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 commentaire
Rainer Gerald
Rainer Gerald le 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.

Connectez-vous pour commenter.

Réponses (1)

Christopher Stapels
Christopher Stapels le 14 Sep 2023
Déplacé(e) : Christopher Stapels le 17 Oct 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.

Communautés

Plus de réponses dans  ThingSpeak Community

Catégories

En savoir plus sur REST API dans Help Center et File Exchange

Tags

Produits


Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by