Analyze Channel Data to Send Email Notification from IFTTT

This example shows how to use the external web service IFTTT to send ThingSpeak notifications as email and text messages. Use theTimeControl App to trigger the MATLAB Analysis App to generate an HTTP request to IFTTT. The triggered IFTTT applet generates an email message. 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. This example shows how to use IFTTT to receive an email notification with the last soil sensor value when the plant needs water.

Create an IFTTT Applet

IFTTT is a web service that lets you create applets that act in response to another action. You can use the IFTTT Webhooks service to create web requests to trigger an action. The incoming action is an HTTP request to the web server, and the outgoing action is an email message.

1) Create an IFTTT account if you do not already have one. Log in to your account.

2) Create an applet. Select your user icon, and then click Create.

3) Select the input action. Click the word this.

4) Enter Webhooks in the search field. Select the Webhooks card, and then Receive a web request.

5) Complete the trigger fields. Enter an event name. This example uses PlantInfo as the event name. Click Create Trigger.

Now the trigger word this is a Webhooks icon.

6) Select the action. Click the word that. Enter email in the search bar, and select the Email box.

7) Enter any additional message information in the subject or body fields. You can pass data about the event that triggered your message by using ingredients. For example, including {{Event Name}} adds the event name to your text message. The Body section must include at least {{Value1}} and {{Value2}}. Click Create action and then Finish to complete the new applet.

8) Retrieve your Webhooks trigger information. Visit the maker_webhooks site and click Webhooks and Documentation button. You see your key and the format for sending a request. Enter the event name. The event name for this example is PlantInfo.

https://maker.ifttt.com/trigger/{event}/with/key/XXXXXXXXXXXXXXXXXXXXXX
https://maker.ifttt.com/trigger/PlantInfo/with/key/XXXXXXXXXXXXXXXXXXXXXX

You can test the service using the test button or by pasting the URL into your browser. Now create a MATLAB Analysis to evaluate the data and trigger the email request from IFTTT.

Create a MATLAB Analysis

Analyze ThingSpeak data with MATLAB. You can use the result of your analysis to trigger web requests, such as writing a trigger to IFTTT. This analysis reads two weeks of data to calculate a threshold based on historical data. A measurement lower than 10 percent of the range of data triggers the notification.

1) Choose Apps > MATLAB Analysis and select New.

2) Select Trigger Email from IFTTT in the Examples section. The code below is prepopulated in your MATLAB analysis window.

3) Name your analysis and modify the code. Change the iftttURL to match your IFTTT key. To read from your own public channel, change the channelID. Start by getting the data from ThingSpeak.

channelID = 276330;
iftttURL = 'https://maker.ifttt.com/trigger/PlantData/with/key/XXXXXXXXXXXXXXXXXXXX';
moistureData = thingSpeakRead(channelID,'NumDays',14,'Fields',1);

4) Calculate the span of the historical data, and then determine the dry value with a 10 percent threshold.

span = max(moistureData)-min(moistureData);
dryValue = 0.1*span+min(moistureData);

5) Read the last value from the channel and compare it to the dry value. Set the email message based on a comparison to the target.

url=strcat('https://api.thingspeak.com/channels/',string(channelID),'/fields/1/last.txt');
lastValue = str2num(webread(url));

if (lastValue<dryValue)
    plantMessage = ' I need water! ';
    webwrite(iftttURL,'value1',lastValue,'value2',plantMessage);    
end

if (lastValue>dryValue)
    plantMessage = ' No Water Needed. ';
    webwrite(iftttURL,'value1',lastValue,'value2',plantMessage);    
end

6) Save your MATLAB Analysis. Now create a TimeControl to trigger this analysis at regular intervals.

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. Choose Apps > TimeControl, and then click New TimeControl.

  • Name - Name the analysis.

  • 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.

Save your TimeControl. You now receive daily notifications of the plant status.

Receive Your Message

Once the moisture measurement in the channel is below 10 percent of the span of recent data, the message in the email changes. For the analysis to work correctly, note that data for at least two watering cycles must be available.

See Also

Related Topics

External Websites