Make your IoT things react to hundred of IFTTT Events

In thinger.io you can connect your devices easily to the Internet and manage their resources trough a REST API or the API Explorer available in the dashboard console. Moreover, you can integrate your devices with third party services like IFTTT. IFTTT allows configuring several triggers that execute some action. Those trigger includes things like your mobile phone entered in a given location, tomorrow is going to rain, you have a new mention on twitter, you have a new email, and so on. In this how-to we will learn how to integrate our things with the IFTTT platform, so our things can react to hundred of events. In this case our things receive events from the Internet, but they can also send IFTTT events, as it was explained in this other tutorial.

For this project we are going to build a simple IoT device that will display our latest Twitter follower in a small Oled Screen.

Hardware

  • NodeMCU ESP8266. But you can use any other microcontroller with Internet connection.
  • Small Oled Screen (128x64). This one is from digole, and can be connected with SPI, I2C, and Serial. But you can use any other screen you have around. We are using here the serial port connecting only the TX from the microcontroller, as the screen has nothing to transmit. Also the screen is connected directly to VIN pin, which provides 5v from USB. You can connect it also to 3v3 but the regulator does not provide enough current for both MCU and display. The screen has a wide voltage input range from 2.5 to 7.5, so it is safe to connect it to 5v.

Software

Arduino Project

The first thing we are going to do is create our Arduino sketch so our device is able to receive some info about the new followers and display it from screen:

#include <SPI.h>
#include <ESP8266WiFi.h>
#include <ThingerWifi.h>

#define _Digole_Serial_UART_
#include <DigoleSerial.h>
DigoleSerialDisp mydisp(&Serial, 9600);

#define USERNAME "your_username"
#define DEVICE_ID "your_device_id"
#define DEVICE_CREDENTIAL "your_device_credential"

#define SSID "your_wifi_ssid"
#define SSID_PASSWORD "your_wifi_password"

ThingerWifi thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);

void setup() {
  pinMode(BUILTIN_LED, OUTPUT);

  mydisp.begin();
  mydisp.clearScreen();
  mydisp.drawStr(0, 1, "Waiting");
  mydisp.drawStr(5, 3, "Followers!");
  mydisp.enableCursor();

  thing.add_wifi(SSID, SSID_PASSWORD);

  // input resource that prints follower info
  thing["new_follower"] << [](pson& in){ 
    mydisp.clearScreen();
    mydisp.setFont(10);
    
    mydisp.drawStr(0, 0, "<NAME>"); 
    mydisp.drawStr(0, 1, in["name"]);
    
    mydisp.drawStr(0, 2, "<LOCATION>");
    mydisp.drawStr(0, 3, in["location"]);

    mydisp.drawStr(0, 4, "<WEBSITE>");
    mydisp.drawStr(0, 5, in["website"]);
  };
}

void loop() {
  thing.handle();
}

In this sketch we have defined a input resource named new_follower that basically takes the information provided in the parameters name, location, and website, and display them in the screen. If we upload this sketch (modifying the info about our device, and WiFi), we can already start interacting with our device from the API Explorer. For example, try entering some information in the explorer, and running the resource.

You should see how the screen has been updated with the information we have provided in the explorer, and now it looks like the following picture:

Wow! Thats cool! but now we know how it works, we are going to connect it to the IFTTT service to see real followers data!

Device Token Configuration

Our devices, and their resources are normally protected through the OAuht2 authentication mechanism, were we provide our username and password, but we do not want to share our account information with a third party service. In this case we are going to generate a device token, so other services can interact only with the device and resources we want. To create a device token, please go to the device dashboard in the thinger.io console, and click on Add button that is present in the Device Tokens section.

This button will open a popup where we can configure some parameters. Please enter a proper token name so you can recognize it in the future. Here it is called IFTTT Twitter Notification. One feature of the device tokens, is that they allow to restrict the resource access. So one token may only read a value, while other can set some value. This is specially useful if you want to share a device with someone but restricting the access to some device resources. In our case we are going to restrict the access to the new_follower resource we defined in our sketch, as shown in the following picture.

The device tokens can also expire after some time, so they provide a limited time access. We want that our device is working forever, so the token will not expire in this case. So if we press click on create, we should see a new generated token like the following picture. We will use this token while configuring the IFTTT service, so keep it at hand.

Configuring IFTTT

In this part we are going to connect the twitter channel to our device resource, so our device can display the information about our new followers.

The first thing to do is to create a new recipe with the Twitter channel as Trigger Channel

Next we are going to select the New Follower event. Notice that you may need to grant access to Twitter Channel in this step if it is the first time you use it.

So we have just configured our trigger event based on new followers

Then it is time to select the action to perform, that will be based on the Maker Channel. Please search and click on the Maker channel in this step.

There is only one possible action in the Maker Channel, so please click on the Make a web request action

Now it is time to configure the Maker Channel action. We are going to start with the url. The url needs to be something like the following string. You must change the username, the device_id, the resource, and device_token according to your parameters. Removing << and >>.

https://api.thinger.io/v2/users/<<username>>/devices/<<device_id>>/<<resource>>?authorization=<<device_token>

In the following picture there is a complete example of the full url. Here we are calling the resource new_follower from the nodmcu device, that is owned by alvarolb. Notice also how it is configured a parameter called authorization that contains the device token we have generated in the previous step.

Now that we have configured the url, we need to configure the information we are going to send to the device resource. We are going to submit a POST request with application/json content type. If we look at our new_follower resource defined in the Arduino sketch, we can see how are used three fields called name, location, and website that are taken from the in parameter. We are going to fill this fields with the information that provides this recipe. We need to create a Json field with the following content:

{
    "in": {
        "name": "{{FullName}}",
        "location": "{{Location}}",
        "website": "{{UserWebsiteUrl}}"
    }
}

The in key in this JSON is used by thinger.io to indicate the resource input. It could be a boolean, a number, a string or any other value. However our new_follower resource expect an object with three different fields. So the in key contains in this case a JSON object with the name, location, and website fields as required by our resource. This fields are filled with some information available in the recipe IFTTT recipe. The recipe data can be easily put in our JSON document by placing variables with brackets {{ }}. This variables are replaced automatically by IFTTT with our new follower information. Each recipe contains different available data that you can easily check when you are composing the body.

The final configuration should look as the following picture.

After editing the Maker Channel info, you need to confirm your new recipe by pressing the Create Recipe button.

Thats All!

In this how-to we have learn how to create a device token to allow interacting with our device from third party services, moreover we have see how to configure the IFTTT Maker Channel to call a device resource based on a Twitter channel trigger. The information provided by the Twitter channel was used in our device resource to display some information about our new follower. You just need to wait for new Twitter followers and the device will automatically display them.

2 Likes

@alvarolb Hi, great tutorial!! I have a couple of questions about how to connect to IFTTT.
1.- What form should i give the body to use the example that comes in the Arduino IDE in order to light a LED connected to GPI0?

2.-What is the syntax to control GPIO in Arduino IDE? Is this : thing[“led”] << [](pson& in){ digitalWrite(BUILTIN_LED, in ? HIGH : LOW); };

I’m using a ESP-01, I can connect seamlessly to thinger.io ( I see it plugged into the dashboard ) and achieve to create a token with the resource led but can’t make it work with IFTTT.

Regars,

PA

Hi @psukez, thanks for trying the tutorial =)

1 - The content type should be application/json, and for example, to turn on the led the content body should be something like this:

{"in":true} 

For turning off the led, it can be something like that:

{"in":false}

This is basically a JSON document where you can place the function input in the "in" key. The value can be also another JSON object, like the example in this tutorial. But the content will generally depend on how this resource is defined.

2 - For defining a device resource that needs to control only a GPIO (on/off) we can use something like the LED example:

  thing["gpio"] << [](pson& in){ 
    digitalWrite(MY_GPIO_PIN (2 in ESP-01 for GPIO_0), in ? HIGH : LOW); 
  };

You can change the "gpio" resource name to something that represent the underlying control, a light, a led, a relay, and so on. Here the in parameter of the function will content the value passed in the JSON document, that is true or false. In the Arduino example the HIGH and LOW are inverted as how the BUILTIN_LED (in a NodeMCU) work.

Once you define the resource you can test it in the Device API Explorer to see that is working. Then you can start working with the IFTTT knowing that the resource you have created works.

Hope I explained it enough! Let me know any advance in your integration!

1 Like

Thanks a lot @alvarolb, it worked like a charm!
Here is a video of a relay attached to a led controlled by a Pebble Time watch trough IFTTT and thinger.io

1 Like

Wow @psukez !!! It worked!!! Many thanks for the video!! It is so cool!! :grinning:
I will post this video in the thinger.io twitter account. Are you planning to make something real with it? turn on/off a led, open a garage door? or something like that? Tell us if you evolve the project =)

Hi @alvarolb here is what i intended to do with it! :sunglasses:

Regards!!

3 Likes

Wow @psukez!! that’s so cool!!! :smiley: it works so fine!!! what is the power supply you are using for the esp?? can you still turn on and off the light from the wall switch?

I’m using a 18566 (4.2v, 2000mAh) battery, it last only 1 and a half day. In the next iteration of this project i will try the sleep function of the esp8266 and maybe remove the leds from the relay module and from the esp8266. If you known about a better power source for this module let me know please.

ps. the switch still can be used but independently from the esp8266 module so if you turn on the light with the switch you can’t turn it off with the esp8266 module and the same happen the other way.

I though you were using some AC/DC converter so the project does not rely on a battery. There are some cheap AC/DC modules that provides 3.3v. Anyway, it is a good start to think about using sleep functions. Didn’t try them yet in the ESP8266, but sleeping the device periodically (like 10 seconds) may save battery. However, sleeping the device will increase the response time of on/off functions. Let me know any advance in the project!! :grinning:

Hi. How to use a String without an LCD display, just using Serial.print? I can’t seem to work it with the following code:

thing["ifttt"] << [](pson& in) { 
    Serial.println(in["value1"]);
    Serial.println(in["value2"]);
    Serial.println(in["value3"]);
  };

Any help is appreciated.

hola, para hacer un guiño al sofware spanis:stuck_out_tongue_winking_eye: que tal lo verías hacer algo parecido pero con la aplicación de

www.noysi.com
es el whasapp de las empresas.
tiene una appi que seguro que se puede integrar fácil con thinger.

Hi @balsimpson I answered to your question in the other post. Only for reference in this post, the solution is to cast or assign the value to a const char* data type, like the following:

  thing["ifttt"] << [](pson& in) { 
    Serial.println((const char*)in["value1"]);
    Serial.println((const char*)in["value2"]);
    Serial.println((const char*)in["value3"]);
  };

Thank you so much. It worked.

1 Like

Can you help me, Why don’t work POST request IFTTT?

I’ve struggled with this as well. What does your code look like that is getting this value from Thinger.io? This part may be working fine and the problem can be on the other end…

-Peter

yes. I connect NodeMCU to Thinger.io.
I want to control LED on NodeMCU by POST-request from IFTTT/
I have button-IFTT. if I press button, POST-request send to Thinger.io. and control LED.

Can you control this from the Thinger API Explorer? Are you passing a variable to your script and then turning the LED on and off?
Have you tried using the Advanced REST Client plug-in to Chrome to test the URL to Thinger? There you can see if there is any response.
It gets tricky to check all of the steps in the process.

  • Peter
1 Like

hi @alvarolb I’m trying to figure out how to use your code with 2 daisy chained 8x8 max7219 led matrix,
how can i adapt it to use with max7219?
i just want to print “value1” - “value2” - “value3” on led matrix without looping
im printing on matrix with this code;

[code]#include <SPI.h>
#include “LedMatrix.h”

#define NUMBER_OF_DEVICES 1
#define CS_PIN 2
LedMatrix ledMatrix = LedMatrix(NUMBER_OF_DEVICES, CS_PIN);

void setup() {
Serial.begin(115200); // For debugging output
ledMatrix.init();
ledMatrix.setIntensity(4); // range is 0-15
ledMatrix.setText(“MAX7219 Demo”);
}

void loop() {
ledMatrix.clear();
ledMatrix.scrollTextLeft();
ledMatrix.drawText();
ledMatrix.commit(); // commit transfers the byte buffer to the displays
delay(200);
}[/code]

Here is the code i came up with
first of all thank you @alvarolb :slight_smile:
here is my new problem;
its scrolling value 1 for ever how can i stop it after 1 scroll
i tried while(1){} but :confused:
i guess i need to loop it (character count of value 1 x 8) times
x8 because i have 8 pixel wide on my led matrix

[code]#include <SPI.h>
#include <ESP8266WiFi.h>
#include <ThingerWifi.h>
#include “LedMatrix.h”
#define NUMBER_OF_DEVICES 2
#define CS_PIN 4
LedMatrix ledMatrix = LedMatrix(NUMBER_OF_DEVICES, CS_PIN);

#define USERNAME “xxxxxxx”
#define DEVICE_ID “xxxxxxx”
#define DEVICE_CREDENTIAL “xxxxxxx”

#define SSID “xxxxxxx”
#define SSID_PASSWORD “xxxxxx”

ThingerWifi thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);

void setup() {
pinMode(BUILTIN_LED, OUTPUT);
thing.add_wifi(SSID, SSID_PASSWORD);
Serial.begin(115200); // For debugging output
thing[“iffttt”] << [](pson& in){;
ledMatrix.init();
ledMatrix.setIntensity(4); // range is 0-15
ledMatrix.setText((const char*)in[“value1”]);
};

}

void loop() {
ledMatrix.clear();
ledMatrix.scrollTextLeft();
ledMatrix.drawText();
ledMatrix.commit(); // commit transfers the byte buffer to the displays
delay(80);
thing.handle();
}[/code]

Hi,
Before sorry for my english. I want to create a notification but the notification appears in this application’s own thinger, not through another application. I’ve seen in the list of applications connected with IFTTT, no Thinger.io app. My question, whether can directly bring up the notification in Thinger.io application without IFTTT ?? is there any other solutions to appear notification on the application Thinger.io is ???
Thank you very much.