Can someone help me with this coding? When I run it always says No data read, it should be show the result I write there because it already passed the conditions
1 回表示 (過去 30 日間)
古いコメントを表示
Mohd Shahzrie
2021 年 11 月 27 日
コメント済み: Mohd Shahzrie
2021 年 12 月 5 日
% Store the channel ID for the sensors channel.
channelID = 1517893;
% Provide the ThingSpeak alerts API key. All alerts API keys start with TAK.
alertApiKey = 'TAKQXKX7Xxxxxxxxxxx';
% Set the address for the HTTTP call
alertUrl="https://api.thingspeak.com/alerts/send";
% 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("Weather Monitoring System Update");
% Read the recent data.
temperatureData = thingSpeakRead(channelID,'NumDays',1,'Fields',1);
humidityData = thingSpeakRead(channelID,'NumDays',1,'Fields',2);
% Check to make sure the data was read correctly from the channel.
if temperatureData > 27 & humidityData < 80
fprintf("Sunny day \n")
alertBody = "The temperature is above 27°C while the humidity is below 75% which indicates that today is probably gonna be a sunny day.";
elseif temperatureData < 27 & humidityData > 80
fprintf("Rainy day \n")
alertBody = "The temperature is below 27°C while the humidity is above 75% which indicates that rain is likely to be coming.";
elseif temperatureData < 27 & humidityData < 80
fprintf("Forecast \n")
alertBody = "The temperature is below 27°C while the humidity is below 75% which indicates that there's a high chance of the weather being overcast";
elseif temperatureData > 27 & humidityData > 80
fprintf("Sunshower \n")
alertBody = "The temperature is above 27°C while the humidity is above 75% which indicates that there will probably be a rare weather occurence which is sunshower";
else
alertBody = "No data read for today's weather."
end
% Catch errors so the MATLAB code does not disable a TimeControl if it fails
try
webwrite(alertUrl , "body", alertBody, "subject", alertSubject, options);
catch someException
fprintf("Failed to send alert: %s\n", someException.message);
end
0 件のコメント
採用された回答
Christopher Stapels
2021 年 12 月 1 日
I just read your channel and there is no data in the past day. NumDays ,1 privides the data from the last day starting now.
You can use the NumPoints parameter to get the last n points.Or use the datarange parameter to specify a datarange.
その他の回答 (1 件)
Christopher Stapels
2021 年 11 月 29 日
Channel 1517893 is not public.
In this line:
temperatureData = thingSpeakRead(channelID,'NumDays',1,'Fields',1);
you wil need to provide the read API Key to read from a private channel.
I would suggest you start with reading from a private channel before implementing the alert. You can use the template code when you create a new MATLAB analysis or visualization for help.
コミュニティ
その他の回答 ThingSpeak コミュニティ
参考
カテゴリ
Help Center および File Exchange で Read Data from Channel についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!