MATLAB code has not completed execution within the allocated time. Consider optimizing your MATLAB code to execute within the available time limit.

6 次查看(过去 30 天)
MATLAB code has not completed execution within the allocated time.
Consider optimizing your MATLAB code to execute within the available time limit.
  4 个评论
naveen s
naveen s 2024-5-2
编辑:Christopher Stapels 2024-5-7
% Define ThingSpeak URL
channelID = '2406188'; % Replace 'YourChannelID' with your actual ThingSpeak channel ID
% Provide the ThingSpeak alerts API key. All alerts API keys start with TAK.
alertApiKey = 'takxxxxxxxxxxxxxxxx';
% Set the address for the HTTTP call
alertUrl="https://api.thingspeak.com/alerts/send";
readAPIKey = 'yyyyyyyyyyyyyyyy'; % Replace 'YourReadAPIKey' with your actual ThingSpeak read API key
thingSpeakURL = ['https://api.thingspeak.com/channels/', channelID, '/fields/1/last.json?api_key=', readAPIKey];
% webwrite uses weboptions to add required headers. Alerts needs a ThingSpeak-Alerts-API-Key header.
options = weboptions("HeaderFields", ["ThingSpeak-Alerts-API-Key", alertApiKey ]);
% Set the email subject.
alertSubject = sprintf("Temperature alert");
threshold = 25;
% Read last temperature value from ThingSpeak channel
data = webread(thingSpeakURL);
% Check the structure of the data variable
disp(data);
% Extract temperature value from the data struct
if isfield(data, 'field1')
temperature = str2double(data.field1);
% Check if temperature exceeds threshold
if temperature > threshold
% Display alert message
fprintf('Temperature alert! Current temperature: %.2f\n', temperature);
% Send email alert
recipientEmail = 'nithinnaveen50@gmail.com'; % Specify the recipient email address
subject = 'Temperature Alert'; % Email subject
message = sprintf('Temperature alert! Current temperature: %.2f', temperature); % Email message
% Configure email settings vdjuiqfdvplznyjb
setpref('Internet','SMTP_Server','smtp.gmail.com');
setpref('Internet','E_mail','ncbsthermocouple@gmail.com');
setpref('Internet','SMTP_Username','ncbsthermocouple@gmail.com');
setpref('Internet','SMTP_Password','****************');
% Send email
sendmail(recipientEmail, subject, message);
% (Add your email sending code here)
else
fprintf('Temperature normal. Current temperature: %.2f\n', temperature);
end
else
disp('Error: Temperature data not found in response.');
end

请先登录,再进行评论。

采纳的回答

Christopher Stapels
I suggest using thingSpeakRead and the Alerts service. Your code will preform much faster and you wont have these errors.
  3 个评论
naveen s
naveen s 2024-5-7
% Define ThingSpeak URL
channelID = '2406188'; % Replace 'YourChannelID' with your actual ThingSpeak channel ID
% Provide the ThingSpeak alerts API key. All alerts API keys start with TAK.
alertApiKey = 'takxxxxxxxxxxxxxxxx';
% Set the address for the HTTTP call
alertUrl="https://api.thingspeak.com/alerts/send";
readAPIKey = 'yyyyyyyyyyyyyyyy'; % Replace 'YourReadAPIKey' with your actual ThingSpeak read API key
thingSpeakURL = ['https://api.thingspeak.com/channels/', channelID, '/fields/1/last.json?api_key=', readAPIKey];
% webwrite uses weboptions to add required headers. Alerts needs a ThingSpeak-Alerts-API-Key header.
options = weboptions("HeaderFields", ["ThingSpeak-Alerts-API-Key", alertApiKey ]);
% Set the email subject.
alertSubject = sprintf("Temperature alert");
threshold = 25;
% Read last temperature value from ThingSpeak channel
data = webread(thingSpeakURL);
% Check the structure of the data variable
disp(data);
% Extract temperature value from the data struct
if isfield(data, 'field1')
temperature = str2double(data.field1);
% Check if temperature exceeds threshold
if temperature > threshold
% Display alert message
fprintf('Temperature alert! Current temperature: %.2f\n', temperature);
% Send email alert
recipientEmail = 'nithinnaveen50@gmail.com'; % Specify the recipient email address
subject = 'Temperature Alert'; % Email subject
message = sprintf('Temperature alert! Current temperature: %.2f', temperature); % Email message
% Configure email settings vdjuiqfdvplznyjb
setpref('Internet','SMTP_Server','smtp.gmail.com');
setpref('Internet','E_mail','ncbsthermocouple@gmail.com');
setpref('Internet','SMTP_Username','ncbsthermocouple@gmail.com');
setpref('Internet','SMTP_Password','****************');
% Send email
sendmail(recipientEmail, subject, message);
% (Add your email sending code here)
else
fprintf('Temperature normal. Current temperature: %.2f\n', temperature);
end
else
disp('Error: Temperature data not found in response.');
end
Christopher Stapels
ThingSpeak wont allow you to send emails to anyone but yourself. The SMTP code wont work in ThingSpeak code.
What you can do is to use the alerts service to forward it to yourself and then set a rule in your inbox to forward to additional emails. You can use a trigger code in the email body or subject so that your email client can detect the emails you want to forward.
Also you can use a thrd party service like Twillio. They take care of making sure the users you wnat to send emails to have opted in. ThingSpeak and Mathworks dont have a process for that agreement for emails from MATLAB. You can use MATLAB analysis code to trigger the Twillio API. There are many posts in this forum that show the method.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Write Data to Channel 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by