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.
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 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
% 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
Risposte (1)
Christopher Stapels
il 14 Set 2023
Spostato: Christopher Stapels
il 17 Ott 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.
0 Commenti
Community
Più risposte nel ThingSpeak Community
Vedere anche
Categorie
Scopri di più su REST API 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!