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 view (last 30 days)
% 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

Accepted Answer

Christopher Stapels
Christopher Stapels on 1 Dec 2021
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.

More Answers (1)

Christopher Stapels
Christopher Stapels on 29 Nov 2021
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.
Also see the doc page for thingSpeakRead().
  1 Comment
Mohd Shahzrie
Mohd Shahzrie on 30 Nov 2021
but I made it to public already... I forgot to mention that the output sometimes works perfectly fine but the there are days where it isnt consistent...When i look at the serial monitor at arduino ide, the output shows that it should send me a notification about its gonna be a sunny day but instead it says no data read for todays weather

Sign in to comment.

Communities

More Answers in the  ThingSpeak Community

Categories

Find more on Read Data from Channel in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!