Webhook IFTTT Integration

In Last Lesson programs runs every hour with obniz Event.
You can choose "Webhook" as trigger for an event. It runs a program by calling a unique URL.

Facebook and various services has webhook functions. It calls when something happens.

Now We are going to use IFTTT.
It can handle many things as trigger. And webhook available.

Managing Reminders

Today we are going to make a smart flag. It up when a task added to iPhone Reminder. In opposit, It down by mark task done.

Two webhook needed

  1. Configure IFTTT to call webhook when new task added. Event triggered then turn servo motor.
  2. Configure IFTTT to call webhook when task done. Event triggered then turn servo motor to other direction.

Wire motor

Connect a servo motor to your obniz.
First, we should check does servo motor works well by pressing buttons.

<!-- HTML Example -->
<html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <script src="https://obniz.io/js/jquery-3.2.1.min.js"></script>
  <script src="https://unpkg.com/obniz@2.4.0/obniz.js" crossorigin="anonymous"></script>
</head>
<body>

<div id="obniz-debug"></div>

<button id="up">Up</button>
<button id="down">Down</button>

<script>
var obniz = new Obniz("OBNIZ_ID_HERE");
obniz.onconnect = async function () {

  var servo = obniz.wired("ServoMotor", {gnd:0 , vcc:1 , signal:2 });
  $('#up').on("click",function () {
      servo.angle(90);
  });
  $('#down').on("click",(function () {
      servo.angle(180);
  });
}

obniz.onclose = async function(){
   $('#up').off("click");
   $('#down').off("click");
}
</script>
</body>
</html>

The motor movevs by pressing buttons?

Next, adopt to event. This program will get request body from webhook.
We are going to add body parameters to detect task was create or done.

You can check how to get webhook body with obniz Event on obniz event.

Below is the today's final code.

It handle request body and move servo motor regarding to request body.
We will add this body setting to IFTTT later.

<!-- HTML Example -->
<html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <script src="https://obniz.io/js/jquery-3.2.1.min.js"></script>
  <script src="https://unpkg.com/obniz@latest/obniz.js" crossorigin="anonymous"></script>
</head>
<body>

<script>
var obniz = new Obniz("OBNIZ_ID_HERE");
obniz.onconnect = async function () {

  if (typeof req === "object") {

     if(req.body.created) {
      var servo = obniz.wired("ServoMotor", {gnd:0 , vcc:1 , signal:2 });
      servo.angle(90);
      await obniz.wait(2000);

    } else if (req.body.done) {
      var servo = obniz.wired("ServoMotor", {gnd:0 , vcc:1 , signal:2 });
      servo.angle(180);
      await obniz.wait(2000);
    } 
  }

  if (typeof done === "function") {
    done();
  }
}
</script>
</body>
</html>

After sevo.angle(), wait is performed for waiting until motor angle is correctly fixed.

Next goint to create an event.

  1. name: anything. I use IFTTT.
  2. WebAppToRun: Program above.
  3. EventTriggers: Choose Webhook

Then create it.

After creation, Webhook URL will be shoed. It url should begin with https: //obniz.io/events.
So, that program will run by calling this URL.

Next create IFTTT applet to call this Webhook.

Make IFTTT

Install IFTTT app to your iPhone and Start create new Applet.
Choose reminder "Any new reminder" as "IF".

Choose webhook as "Then"

Input the Webhook url ( you get after event creation ).
And Method "POST", ContentType "application/json".
Body should be {"created":true}.(Attention. double quotation is always confusing. not ”. " is correct)

Done?
OK, Create one more Applet. reminder done as "IF". and Same Webhook but body is {"done":true}

All done!

Let's test

OK, Let's add a task to Reminder on iPhone.

Does flag up? IFTTT takes a time to detect event. Open the IFTTT app, It will detect event more faster.

Then, Let's try task done. Flags down?

Reminder can be controlled via Siri. Just Say to controll flag.
IFTTT has many triggers. Enjoy integration.





You will Get in Few Days

Circuit for Starter “obniz Board” is available on Amazon and other online stores.
You can get it at below

Our products and resellers

Forum

Visit our developer’s forum to discuss and discover technologies.

Forum

Contact

Feel free to contact out support and technical team.

Contact us