Email Alerts

If you'd like to receive an alert if the humidity or temperature reaches an unacceptable range, you can use the MATLAB Analysis App to design an alert email and the React App to trigger the alert email if the temperature or humidity reaches a certain point.

MATLab Analysis Email Template

Before we can use the MATLAB Analysis App, we need to get our Alerts API Key. Simply click on the profile icon in the top-right header of the page, select My Profile, and copy the text in the Alerts API Key box. After you have the key, click on the Apps drop-down, select "MATLAB Analysis", and click "New". I named my Analysis "Alert Email - Humidity" and since I already designed a bit of code, I select  "Custom" under Templates.

%
% If the humidity rises above 55%, send out an alert
%

% Store the channel ID for the BME280 sensor channel.
channelID = 1400669;

% Provide the ThingSpeak alerts API key.  All alerts API keys start with TAK.
alertApiKey = 'TAKT82EY2V4FHPQWRVQHM';

% 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("Warning - Humidity has risen above 55%");

% Read the recent data.
humidityData = thingSpeakRead(channelID,'NumDays',30,'Fields',2);


% Set the outgoing message humidityData(end) is the last value sent
alertBody = ' Warning: Humidity has risen to ' + humidityData(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
 

  • Replace the value of channelID with the channel ID for your ThingSpeak Channel
  • Replace the value of alertApiKey with the Alert API Key generated in your profile settings
  • Replace the value of fieldnum with the number of the field you're reading. You choose what data goes to which channel when you set up your ThingSpeak channel

React Trigger

  • React Name: I went with "Email Alert - Humidity"
  • Condition Type: Numeric. The humidity is being uploaded as an integer
  • Condition: field 2 (Humidity) Is greater than 55
  • Action: MATLAB Analysis
  • Code to execute: Alert Email - Humidity