Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Analyze Channel Data to Send Email Notification from IFTTT

The analytical power of ThingSpeak™ and MATLAB® allow you to generate filtered, targeted, and specific notifications of channel activity. The external web service IFTTT lets you turn ThingSpeak notifications into email and text messages. This example shows how to use theTimeControl App to run the MATLAB Analysis App to trigger an HTTP request to IFTTT. The triggered IFTTT Applet generates an email message.

Channel 276330 logs a soil moisture measurement from an office plant. This example shows how to use IFTTT to receive an email notification 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 use 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, or log into your existing account.

  2. Create an Applet. Select My Applets, and then click the New Applet button.

  3. Select input action. Click the word this.

  4. Select the Webhooks service. Enter Webhooks in the search field. Select the Webhooks card.

  5. Complete the trigger fields. After you select Webhooks as the trigger, click the Receive a web request box to continue. Enter an event name. This example uses PlantThirsty as the event name. Click Create Trigger.

    Now the trigger word this is the Webhooks icon.

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

  7. Select the Send me an Email box and then enter the message information. You can pass data about the event that triggered your message 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 to finish the new Applet.

  8. Retrieve your Webhooks trigger information. Select My Applets > Services, and search for Webhooks. Select Webhooks and then the 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/PlantThirsty/with/key/XXXXXXXXXXXXXXXXXXXXXX
    

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

Create a MATLAB Analysis

The MATLAB lets you 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 the historic data. If the present measurement is lower than 10 percent of the range of data, it triggers the notification.

  1. Choose Apps > MATLAB Analysis and select New.

  2. Choose a Name for your analysis and enter this code. Change the iftttURL to match your 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);
  3. 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);
  4. 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
  5. 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 the NewTimeControl button.

  • Choose a Name.

  • Select Recurring for Frequency.

  • Choose Day for Recurrence.

  • Select MATLAB Analysis as the Action, and choose the name of the MATLAB Analysis you wrote previously.

Save your TimeControl. You will 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. Keep in mind that there must be a pattern of data for at least two watering cycles for the analysis to work correctly.

See Also

Related Topics

External Websites

Was this topic helpful?