ThingSpeak Email Alerts - ArtTHEbard/Hydro-Pi-nics GitHub Wiki
Email Alerts
In order to have email alerts for the sensors through ThingSpeak, we need to set up both Analytics and Reacts in ThingSpeak
Analytics
Navigate to Apps > MATLAB Analysis, and select new Analysis. Select Custom Code option. The following code is used to monitor the different sensor fields and send alerts. Change the relevant values to match your sensor needs.
% Read the soil moisture channel data from the past two weeks.
% Send an email and tell the user to add water if the value
% is in the lowest 10 %.
% Store the channel ID for the moisture sensor channel.
channelID = 1662352;
% Provide the ThingSpeak alerts API key. All alerts API keys start with TAK.
alertApiKey = 'TAK30QEZ9QNTBQG51ZMFW';
% 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("Plant Water Information");
% Read the recent data.
PH = thingSpeakRead(channelID,'NumDays',5,'Fields',1);
% Check to make sure the data was read correctly from the channel.
if isempty(PH)
alertBody = ' No data read from plant. ';
else
% Get the most recent point in the array of moisture data.
lastValue = PH(end);
% Set the outgoing message
if (lastValue<=5.5)
alertBody = ' PH LOW! ';
elseif (lastValue>=7)
alertBody = ' PH HIGH! ';
else
alertBody = ' PH FINE! ';
end
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
ThingSpeak Reacts
Next, navigate to Apps > React. Select New. You will need to create a React entry for both the High and Low alerts. Enter the following settings for the React.

Change the values depending on the field and values. Both the High and Low React can use the same Analysis you created for each field.