Analyze Channel Data to Send Email Notification
This example shows how to use the ThingSpeak alerts service to send notifications as email messages. Use the TimeControl app to trigger a MATLAB Analysis app at regular intervals. The MATLAB Analysis app analyzes the data to decide the appropriate email message to generate based on soil moisture data. Using the analytical power of MATLAB, you can generate filtered, targeted, and specific notifications of channel activity from ThingSpeak data.
Channel 276330 logs a soil moisture measurement from an office plant. In this example, you useThingSpeak alerts to receive an email notification with the last soil sensor value when the plant needs water. See Moisture Sensor Using HTTP POST Requests to Channel to learn how to set up a soil moisture monitor that records your data on ThingSpeak.
Create a MATLAB Analysis
Analyze ThingSpeak data with MATLAB. You can use the result of your analysis to trigger web requests, such as a request for email from ThingSpeak alerts. This analysis reads four weeks of data to calculate a threshold based on historical data. A measurement lower than 10% of the range of data changes the output message.
1) Select Apps > MATLAB Analysis and select New.
2) Select Read Channel to Trigger Email in the Examples section. The code below is prepopulated in your MATLAB analysis window.
3) Name your analysis and modify the code. Change alertApiKey
to match your alerts API key. To read from your own public channel, change the channelID
value. Start by setting the channel ID and alerts key. All alerts API keys start with TAK
.
channelID = 276330;
alertApiKey = 'TAKXXXXXXXXXXXXX';
4) Set the URL and header. The alerts service requires a ThingSpeak-Alerts-API-Key
header. Use weboptions
to set the header.
alertUrl = "https://api.thingspeak.com/alerts/send"; options = weboptions("HeaderFields", ["ThingSpeak-Alerts-API-Key", alertApiKey ]); alertSubject = sprintf("Plant soil information");
5) Read the recent data using thingSpeakRead.
moistureData = thingSpeakRead(channelID,'NumDays',30,'Fields',1);
6) Make sure that there is data read from the channel and set the message accordingly. Calculate a 10% threshold value from the span of the data. Use the most recent value to set the alert body message.
if isempty(moistureData) alertBody = ' No data read from plant. '; else % Calculate a 10% threshold value based on recent data. span = max(moistureData) - min(moistureData); dryValue = 0.1 * span + min(moistureData); % Get the most recent point in the array of moisture data. lastValue = moistureData(end); % Set the outgoing message if (lastValue<dryValue) alertBody = ' I need water! '; end if (lastValue>dryValue) alertBody = ' No water needed. '; end end
7) User webwrite
to send the alert request. Wrap the send request in a try/catch
to prevent the MATLAB Analysis from being disbled if the request fails for any reason.
try webwrite(alertUrl , "body", alertBody, "subject", alertSubject, options); catch someException fprintf("Failed to send alert: %s\n", someException.message); end
Create a Time Control to Run Your Analysis
The TimeControl app can evaluate your ThingSpeak channel data and trigger other events. Create an instance of the TimeControl app that calls your MATLAB Analysis code every day. Select Apps > TimeControl, and then click New TimeControl.
Name — Name the TimeControl.
Frequency — Select Recurring.
Recurrence — Select Day.
Action — Select MATLAB Analysis. In the code to execute list, select the name of the MATLAB Analysis you wrote previously.
Each time the TimeControl app runs, you receive an email letting you know if the plant needs water. The 10% threshold is only an estimate; ThingSpeak assumes no responsibility for your plant.
Note: This configuration in this example consumes one email alert each day. Your total number of alerts is limited; if you exceed the limit, you can no longer trigger new email alerts.
See Also
Write
Data
| webwrite
(MATLAB) | Send
Alert
| Get Alert
History