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
- It is required to have the Arduino IDE with the thinger.io libraries installed, and the ESP8266 board configured also in the IDE.
- In this project it is also required to have installed the oled screen libraries from Digole.
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.