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일)
이전 댓글 표시
% 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!