Эта страница является
переводом страницы
IFTTT. Перевод выполнен на 100%.
Work of iRidium with IFTTT service
General manual on setting iRidium work with IFTTT service
IFTTT platform allows to connect two cloud services.
There can be many examples how this platform can be used in remote control of Smart home.
Let's have e look at 2 most popular ways to work with IFTTT:
1 Activation of IFTTT trigger when a smart home device is on
An example When the front door lock is opened, a notification is sent to an e-mail.
2 A device is turned on, when IFTTT trigger is activated
An example When a new message comes to the e-mail, a lamp is to be turned on.
Description below shows how to set the given examples.
iRidium server and constant internent access are required to work with IFTTT service.
Activation of IFTTT trigger when a smart home device is on
Setting Webhook
1 Go to IFTTT web-site and authorize
2 After authorization, click on your account and select New Applet from the list that appears.
3 A standard form appears, click +this
4 Find and select Webhooks in the list that appears
5 To create a trigger create a new request (Receive a web request)
6 Create an event, for example, door_opened and click create trigger
7 Click +that and in the list that appears select what to do when the door_opened event happens.
In our example when door_opened event happens, a letter is sent to an e-mail notifying about it.
8 We'll select Gmail
9 Select an action - to send a letter to yourself (Send yourself an email). A review of the trigger appears, we must have a close look at it to check that everything is correct and to complete creation by clicking the Finish button.
To test the trigger work go to Webhooks tab.
Go to the documenatation page
Create a request on this page. Enter the name of the event in the event field (in our example, it's door_opened) and click Test it.
On the same page you'll see your unique key. It will be used when you pass on to setting iRidium project.
Setting iRidium pro project
1 Create a server project.
2 Add to it AV&Custom Systems (HTTP) driver
3 Set as shown below.
4 Add "Event" virtual channel
5 Create a new script in the project and add the following to it
IR.AddListener(IR.EVENT_START,0,function(){
var IFTTT_Token = "your key"; //enter Webhook secret key here (take it from the documentation section of Webhook)
var IFTTT = IR.GetDevice("IFTTT (Custom HTTP (TCP))"); //driver name
IR.SetGlobalListener(IR.EVENT_GLOBAL_TAG_CHANGE, function(in_sName, in_nValue){
IFTTT.Send(["GET,/trigger/"+in_nValue+"/with/key/"+IFTTT_Token+"/"]);
});
IR.SubscribeTagChange("Server.Channels.Event"); //name of a virtual channel
IR.AddListener(IR.EVENT_RECEIVE_TEXT, IFTTT, function(text) {
IR.Log(text);
});
});
6 Enter your key (from the Webhook documenation) in the IFTTT_Token variable
7 Add drivers that you will work with (we'll use Modbus TCP as a example) and set them
8 Set feedback channels to devices
9 Create a routine for Webhook event to be activated
When=> a driver tag gets data that the door is opened
Then=> send text with the event name to virtual channel
Write the same event name as in the Webhook trigger
When the door is opened and a value comes to the tag, a routine is activated that sends Webhook information that door-opened event happened and a notification about it comes to the e-mail.
A device is activated when a letter comes to Gmail.
This way, like the first one, can work via Webhook
For the device to be activated, IFTTT trigger can be activated that sends a letter to Gmail with the event name in the topic
For example, you want the light to turn on when a letter with Light_on topic comes and to turn off when a letter with Light_off topic comes.
Setting Gmail
1 Go to Google Apps Script web-site.
2 Click Start scripting
3 Click Create script
4 Enter the following script
function doGet(in_sResponse)
{
var l_sToken = ""; //enter here the key that only you will know.
var l_aEvents = in_sResponse.parameter.name.split(' ');
var l_nMailCount = 0;
var l_Object={};
var l_Response = Gmail.Users.Messages.list('me');
// Go through all the messages
for(var iMsg in l_Response.messages)
{
l_nMailCount++;
var l_nMessageID = l_Response.messages[iMsg].id;
var l_Message = Gmail.Users.Messages.get('me',l_nMessageID);
if(l_Message)
{
// Go through all the message headers
for(var iHeaders in l_Message.payload.headers)
{
if(l_Message.payload.headers[iHeaders].name == "Subject")
{
// Check for the presence of headers in events
for(var i = 0;i < l_aEvents.length;i++)
{
if(l_aEvents[i] == l_Message.payload.headers[iHeaders].value)
{
if(!l_Object[l_aEvents[i]])
l_Object[l_aEvents[i]] = l_nMessageID;
Gmail.Users.Messages.trash('me',l_nMessageID)
}
}
}
}
// Work with a maximum of 5 letters
if(l_nMailCount==5){break}
} else
break;
}
return ContentService.createTextOutput(JSON.stringify(l_Object));
}
Write your unique key in the var l_sToken variable for nobody but you to use this funtion. Later on this key must be written in the body of a letter.
5 Save it by clicking
icon or use Ctrl+S, and give a random name to it
6 To work with Gmail, Gmail API has to be enabled. To do it, go to Resources/Advanced Google services
7 Find Gmail API in the list that appears and enable it.
8 A notification appears below the list that this service has to be enabled in the Google Cloud Platform API Dashboard. Follow the link.
9 Fill in the form and click Accept.
10 Click Enable API and services button on the page that appears.
11 Find and select Gmail API in the list
12 Turn it on.
13 Click Create Credentials for API in the window that appears.
14 Fill in the form as shown below.
15 The system will ask your permission to access data.
16 Click Review Permissions and a window appears. Select your account, click Advanced, then click Go to.
17 Click Allow
18 Now that you have allowed the script to get access to Gmail data, it must be made public. Go to Publish tab and select Deploy as web app
19 Fill in the form as shown below.
A window appears next where you can see URL. It will be used in further work with iRidium pro. Copy it startting with /macros
Setting a project in iRidium pro
1 Create a server project.
2 Add to it AV&Custom Systems (HTTP) driver
3 Set it as shown below.
4 Add one more AV&Custom Systems (HTTP) driver and set it as shown below.
5 Add Event virtual channels and Event_
Out virtual tag.
6 Create a new script in the project and add the following to it
IR.AddListener(IR.EVENT_START,0,function(){
var IFTTT_Token = "your key"; // enter Webhook secret key here (take it from the documentation section of Webhook)
var Script_google_URL = "/macros/____________"; // URL from the last step of Gmail setting
var Event = "light_on+light_off"; // name of event (written in the topic of a letter)
var Interval = 10000; // an interval between requests to Gmail
//-------------------------------------------------------------------------
var IFTTT = IR.GetDevice("IFTTT (Custom HTTP (TCP))");
var Script_google = IR.GetDevice("script_google");
IR.SetGlobalListener(IR.EVENT_GLOBAL_TAG_CHANGE, function(in_sName, in_nValue){
IFTTT.Send(["GET,/trigger/"+in_nValue+"/with/key/"+IFTTT_Token+"/"]);
});
IR.SubscribeTagChange("Server.Channels.Event");
IR.SetInterval(Interval, function (){
if(Script_google_URL!=""){
Script_google.Send(["GET,"+Script_google_URL+"?name="+Event]);
}
})
IR.AddListener(IR.EVENT_RECEIVE_TEXT, IFTTT, function(text) {
IR.Log("IFTTT "+text);
});
IR.AddListener(IR.EVENT_RECEIVE_TEXT, Script_google, function(text) {
if(text.substr(0,1)=="{"){
var oText = JSON.Parse(text);
for(key in oText){
IR.SetVariable("Server.Tags.Event_Out", key);
IR.Log('Successful: '+key);
}
}
});
});
7 Enter your key, that you got on the documentation page Webhook in the IFTTT_Token variable;
8 Enter URL, that you got in the last step of setting Gmail in the Script_google_URL variable
9 Write the name of event that activates a device in the Event Variable.
Names of events must be written in the topic of a letter, that comes to Gmail.
10 Add drivers that you will work with (we'll use Modbus TCP as a example) and set them
9 Create a routine:
When=> a name of event comes from Gmail to Event_out virtual tag
Write the name of event that is to be written in the topic of a letter
Then=> send a command to a device
Testing
1 Upload a project to the server
2 Send a letter to the Gmail address where the script is set.
Write the name of an event, for example, light-on in the topic of a letter
Write your uniques key that you entered in the var l_sToken vriable of the Google script in the body of the letter.
3 В зависимости от заданного в скрипте вашего проекта интервала, дождитесь, когда событие сработает. В окне сервера появится уведомление о том, что событие сработало и устройство выполнит команду.
Work with IFTTT in iRidium lite
Work with IFTTT in iRidium lite can be set via Gmail module. Gmail module can be used to set e-mail notification about different events and to create a routine to control equipment via e-mail. Then connect Gmail to any cloud service available in IFTTT.
Internet connection and Google account are required to work with IFTTT.
It is recommended to use iRidium server.
Let's have a look at 2 examples of work with this service:
1 Sending a notification to an e-mail, when a smart home device is activated.
An example When a leakage sensor is activated, send a notification to an e-mail.
2 A device is activated when a letter comes to the e-mail.
An example When a new letter comes to the e-mail, heating/cooling system must be turned on.
Below we'll describe how to set iRidium lite projects to realize the tasks in the examples.
Setting Gmail module
1 Add Gmail for Server module to the project.
2 Set the interval at which the module requests Gmail service if there is a new e-mail. Press Add.
3 Go to the module and authorize in it.
Press "Advanced" in the browser window that appears and follow Go to iridiummobile.net link.
Press Allow in the window that appears.
The module is set, you can pass on to setting routines.
Sending a notification to an e-mail, when a smart home device is activated.
Here is the setting for the first example: when a leakage sensor is activated, send a letter to the e-mail.
Go to the Routines section and create a server routine. (Работа с правилами)
Select activation of a leakage sensor in the When' event:
Set sending a letter via Gmail module in Actions.
Write the e-mail, where letters are to be sent, the topic and the text of the message.
A ready routine looks this way:
The setting is complete and the routine can be tested. When sensor contacts are closed, a notification is sent to the e-mail.
A device is activated when a letter comes to the e-mail.
Here is the setting for the second example: when a new message with a definite topic comes to the e-mail, the heating system must be turned on.
Go to the Routines section and create a server routine. (Work with routines)
Set Gmail module to keep track of new letters with a definite topic in the When event.
In the topic write the name of the event and a secret key that only a user knows (if required):
Select the temperature or the heating mode in Actions.
A ready routine looks this way:
The setting is complete and the routine can be tested. When a letter with a definite topic is sent to the e-mail, assigned to the Gmail module, a routine is activated and a device is turned on.