Failed to send alert: Unrecognized function or variable 'alertBody'.

조회 수: 4 (최근 30일)
Mei Sing Wang
Mei Sing Wang 2020년 3월 7일
편집: Vinod 2020년 3월 9일
I follow the instruction in https://www.mathworks.com/help/thingspeak/analyze-channel-data-to-send-email.html?s_eid=EML_16314 However it shows that Failed to send alert: Unrecognized function or variable 'alertBody'. Anyone know why it does not recognized 'alertBody'?
Below is my code:
channelID = 995250;
alertApiKey = 'MY_ALERTS_API_KEY';
alertUrl="https://api.thingspeak.com/alerts/send";
options = weboptions("HeaderFields", ["ThingSpeak-Alerts-API-Key", alertApiKey ]);
alertSubject = sprintf("Surrounding temperature and humidity");
moistureData = thingSpeakRead(channelID,'NumDays',30,'Fields',1);
if isempty(moistureData)
alertBody = ' No data read from surrounding. ';
while humidity < 60 or temperature > 32:
alertBody = sprintf(" The weather is too hot! ", "The temperature is %0.2f",temp) ;
end
end
try
webwrite(alertUrl , "body", alertBody, "subject", alertSubject, options);
catch someException
fprintf("Failed to send alert: %s\n", someException.message);
end
  댓글 수: 2
Walter Roberson
Walter Roberson 2020년 3월 7일
moistureData was not empty so alertBody did not get assigned to, but the urlwrite failed because alertBody is not defined so the catch was executed.
Also you need to replace the or in while test with || because or spelled out can only be used in function call form, not as an operator.
Also you need to replace the while with if because otherwise you have an infinite loop. On the other hand temp is not defined. Perhaps you want different logic there??
I have to wonder if you want to send an alert st all if you received data? Perhaps if you did receive data you want to go through the data you received and create an alert if needed? Your current code does the while only if data is empty; perhaps it should be only if the data is received?
Perhaps you should initialize alertBody to '' and have
if ~isempty(alertBody) around the try/catch?
Mei Sing Wang
Mei Sing Wang 2020년 3월 8일
편집: Vinod 2020년 3월 9일
Thanks for your clarification :) I have ammended my code and it runs well.
channelID = 995250;
alertApiKey = 'MY_ALERTS_API_KEY';
alertUrl="https://api.thingspeak.com/alerts/send";
options = weboptions("HeaderFields", ["ThingSpeak-Alerts-API-Key", alertApiKey ]);
temperature = thingSpeakRead(channelID,'NumDays',30,'Fields',1);
humidity = thingSpeakRead(channelID,'NumDays',30,'Fields',2);
recentvalue = temperature(end)
alertSubject = sprintf("Surrounding temperature and humidity");
alertBody = sprintf(" The weather is too hot!,The temperature is %0.2f",recentvalue);
if recentvalue > 32
webwrite(alertUrl , "body", alertBody, "subject", alertSubject, options);
end

댓글을 달려면 로그인하십시오.

답변 (0개)

커뮤니티

더 많은 답변 보기:  ThingSpeak 커뮤니티

카테고리

Help CenterFile Exchange에서 Instrument Control Toolbox에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by