This sample project is about interacting with a third party service like the IFTTT Maker Channel. This channel allows executing multiple actions like sending tweets, posting to Facebook, doing push notifications, sending emails or SMS, and many many other actions you must check. As an illustrative example, this project will tweet the temperature and humidity periodically. Also it will allow reading the humidity and temperature remotely, changing the tweet interval, and sending test tweets.
Hardware
For this example project we will use a NodeMCU
board, that is based in the ESP8266 chip, along with a DHT11
sensor. The NodeMCU
board will be connected to the DHT11
sensor in the digital pin D1
. You can use however any other board that you want as the code will be practically the same.
Software
We will use the Arduino IDE for programming the NodeMCU and the thinger.io libraries for connecting to the platform. Please, take a look to this post if you need help starting with the ESP8266 and the Arduino Environment.
Picture
The hardware is really simple. Just a NodeMCU connected to the DHT11 sensor.
Before Programing
For this project we need to have an account in the IFTTT site. Please register now if you don’t have an account. After we are registered we are going to create a new recipe. A recipe let us configure a trigger event to start doing some action. In our case we are going to send an event to the Maker Channel in order to send a tweet. So the first step now is to create a recipe.
The first thing to do is select what is going to trigger the recipe. Click on this and search for the Maker Channel. Once you find it, client on its icon. You may be prompted to connect to this channel in this step.
Now it is time to select the trigger type for the Maker Channel. This channel only has one trigger type, so click on Receive a web request.
The trigger requires some configuration. In this case we must fill a event name that will represent our action. For example, we can name it as temperature. You can choose whatever you want, but remember this name for its configuration in the thinger.io platform. Then click on Create Trigger.
At this stage we have configured yet the channel trigger, basically consisting on defining a event name. Now you must click on the word that to start configuring the action to be executed if we submit this event.
Now you can select any action you want, like sending an email, a push notification, and so on. In this example we are going to send tweets, so search for twitter and click on its icon. You may be prompted to connect to this channel. You need to grant rights to the IFTTT app so it can send tweets with your account.
The twitter channel has some different actions like you can see in the following picture. We are going to click on the Post a Tweet box.
We are almost done… Now we can see how the tweet looks like. The maker channel allows sending up to three different values named as Value1
, Value2
, and Value3
. Those parameters are also called ingredients of the recipe. We are going to use this parameters for sending the temperature and humidity of our DHT11. So, type something like in the following picture, and when you are done click on Create Action.
Now we can see a summary of our recipe, that basically consists on sending a tweet when the temperature event is triggered. So, click on the Create Action button to complete the IFTTT part.
Now we need to link the thinger.io platform with the IFTTT event we have created. This is an easy step. Just go the the Endpoints section in your console. Click on Add Endpoint and configure the Endpoint similar to the following picture. Notice that our endpoint id is temperature_tweet
, that is necessary for the thing code. Also we have introduced here the event name we set in IFTTT Maker Channel, which was temperature, and also we need to set our Maker Channel Secret Key, that should be available in your maker channel. After you are done click on the Add Endpoint button.
So we are done configuring the platform. We just basically created a recipe in the IFTTT page to send tweets based on our calls to the temperature event. And also we have configured an endpoint called temperature_tweet in the thinger.io platform so the device can call it easily. So now we can start programming our device for reading the DHT11 sensor and calling the endpoint we have created.
Sketch Code
The sketch code is relatively simple. We have started using the ESP8266 example from the thinger.io libraries and added the headers and configuration for the DHT11 sensor. Then we have defined some thing resources, like dht11
that allows reading the current temperature and humidity, the tweet_interval
, that allows configuring remotely the tweet interval, and finally, the tweet_test
resource, that forces a tweet post.
There is also some code in the loop that basically checks if it is necessary to send a tweet according to the current time and our latest tweet time. The interesting part on this code is the call_temperature_endpoint function. This function call the endpoint we have configured in the thinger.io platform, and fill also the parameters required for our IFTTT recipe. If you remember, we were using value1
and value2
on our tweet content (the ingredients of our recipe). These parameters are filled in the code with the temperature and humidity that we are reading from the DHT11. These values are stored in a pson
structure, that basically is like a json
document but for microcontrollers. This structure is converted in the server to a json
format that the IFTTT platform can understand. So our device efficiently call our thinger.io endpoint with the recipe parameters, and the platform automatically trigger the IFTTT event that will post the tweet. Notice that although those parameters starts with a capital letter in our recipe, they should be all in lowercase, as in the example.
So you can update the following sketch with your username, your device id, your device credential, and also the Wifi SSID/Password to start running this sample project.
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <ThingerWifi.h>
#include "DHT.h"
#define USERNAME "your_user_id"
#define DEVICE_ID "your_device_id"
#define DEVICE_CREDENTIAL "your_device_credential"
#define SSID "your_wifi_ssid"
#define SSID_PASSWORD "your_wifi_password"
// dht config
#define DHTPIN D1
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
ThingerWifi thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);
unsigned long previousMillis = 0; // to know when to post the temperature
unsigned int minutes_interval = 60; // variable in minutes for posting the temperature
void call_temperature_endpoint(){
digitalWrite(BUILTIN_LED, LOW);
pson tweet_content;
tweet_content["value1"] = dht.readTemperature();
tweet_content["value2"] = dht.readHumidity();
thing.call_endpoint("temperature_tweet", tweet_content);
digitalWrite(BUILTIN_LED, HIGH);
}
void setup() {
dht.begin();
pinMode(BUILTIN_LED, OUTPUT);
// turn off led
digitalWrite(BUILTIN_LED, HIGH);
thing.add_wifi(SSID, SSID_PASSWORD);
// resource for reading sensor temperature from API
thing["dht11"] >> [](pson& out){
out["humidity"] = dht.readHumidity();
out["celsius"] = dht.readTemperature();
out["fahrenheit"] = dht.readTemperature(true);
};
// resource that will allow changing the tweet interval
thing["tweet_interval"] << [](pson& in){
if(in.is_empty()) in = minutes_interval;
else minutes_interval = (unsigned int) in;
};
// this resource will allow sending the tweet as requested
thing["tweet_test"] = [](){
call_temperature_endpoint();
};
}
void loop() {
thing.handle();
unsigned long currentMillis = millis();
if(minutes_interval>0 && currentMillis - previousMillis >= minutes_interval * 60000) {
previousMillis = currentMillis;
call_temperature_endpoint();
}
}
Web Control
Although this sketch will start running and posting tweets every hour (by default), we can also access to the device api dashboard to interact with our defined resources in the code. You can access to the current temperature and humidity, you can see and change the current tweet interval period, and you can test your implementation by directly forcing a tweet post. You should see something similar to the following picture in your device dashboard.
Result
If we have done all the steps fine, we should see some tweets appearing in our timeline every hour (or what you have configured in the tweet_interval
resource). You can force some testing tweets by manually running the tweet_test
resource. In our case, our tweets are as in the following picture: