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.
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.
Create an IFTTT account, or log into your existing account.
Create an Applet. Select My Applets, and then click the New Applet button.
Select input action. Click the word this.
Select the Webhooks service. Enter
Webhooks in the
search field. Select the Webhooks card.
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
PlantData as the event
name. Click Create Trigger.
Now the trigger word this is the Webhooks icon.
Select the resulting action. Click the word that.
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
Name}} adds the event name to your text message. The
Body section must include at least
Create action to finish the new Applet.
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
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.
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.
Choose Apps > MATLAB Analysis and select New.
Select Trigger Email from IFTTT in the Examples section. The code below is pre-populated into your MATLAB analysis window.
Choose a Name for your analysis and modify the code.
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);
Calculate the span of the historical data, and then determine the dry value with a 10 percent threshold.
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
Save your MATLAB Analysis. Now create a TimeControl to trigger this analysis at regular intervals.
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 now receive daily notifications of the plant status.
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.