Start Smart Life With IFTTT 2 | IoT Cloud Kit for microbit-MakeCode-Tutorial

0 2218 Medium

Lesson 2: Start Smart Life With IFTTT 2

Objectives

1. Introduction to Webhooks

2. Learn how to connect hardware to IFTTT

Learning Process

Preparation

Teachers: Computer/Smart phone, good internet, IoT cloud kit

Students: Computer/Smart phone, good internet, IoT cloud kit

Learning Contents

Lead-in: We have learned how to use IFTTT to automate the Internet in the previous lesson. However, for carrying out IoT-related projects, we still need smart hardware. Currently, it has been spread across the population, and major service providers, such as Apple, Google, Amazon, and Philips, are also committed to popularizing smart hardware. Besides, they will also launch their own IoT service platforms one after another while they are developing their hardware, and then link them together through the platform to complete complex automation tasks.

Introduction to Webhooks

 

In the previous lesson, we learned that IFTTT was designed to help people to use open API in various websites, monitoring the Triggers set by users. However, what if some applications do not provide an open API interface? For example, if you want to connect your hardware to IFTTT, how to make it?

Think: How to connect IoT cloud kit to IFTTT?

What is Webhooks

There is a service in IFTTT called Webhooks, which allows users to customize the API in IFTTT. For trigger actions without API, we can trigger them by customizing API to execute Action.

How to Use Webhooks

Everyone has a unique key in IFTTT, and it’s an important part of API that we mainly use to distinguish who created the Webhooks. Click link https://ifttt.com/maker webhooks to check the key assigned by IFTTT.

Principle of Webhooks

IFTTT provides a unique address for each Webhooks user, and strictly defines the request method and request parameters of Webhooks.

If you know HTTP, you should be clear about how to use Webhooks. We only need to request IFTTT to allocate URLs via POST or GET. Webhooks can be equivalent to a Trigger. As long as we request the address of Webhooks, IFTTT will be triggered, then the Action we set before can be executed.

Next, we will take several cases to see how to use Webhooks to create automation tasks.

Project -- Connect IoT Cloud Kit to IFTTT

As we have learned how to use IFTTT to automate the Internet before, now we are going to learn how to perform automation tasks with IFTTT by the case of accessing hardware. In this lesson, we mainly talk about how to send a message with one click and how to use the clock of the hardware itself to make a drinking reminder.

Task 1: Send Messages with One Click

Are you troubled with the check-in on social platforms that you need to open the software to send a message every day? On this occasion, how amazing it would be to have a device that can make you only need to press a button to check in on the platform? We will solve this problem next! Once button A on micro:bit mainboard is pressed, a new message will be posted to Twitter.

Create A New Automation Task

First, let’s use webhooks in IFTTT and Twitter to build a workflow as the image shows below.

Create Trigger

1. Configure Trigger. Select service as Webhooks. Set the trigger condition to“Receive a web request”.

2. Configure the Event Name in Webhooks

3. Click Create trigger to complete the setting. Then we are going to create Action.

Create Action

1. To begin with, we must make clear what we need to do: When Trigger is triggered, a message will be posted to Twitter automatically. So, we need to select the service corresponding to Action -- Twitter, and set the action condition to "Post a tweet".

2. Set the message posted by Twitter. First, clear the original template provided by IFTTT, then add the parameter value via Add ingredient.

With the above settings, if we request Webhooks, IFTTT will be triggered, and Value1, Value2, and Value3 in the "request parameters" will be written to Twitter.

3. Click Create action to complete the settings. Click "Continue" → "Finish" continuously, your Applet has been created as shown below:

Hardware Preparation

As we will use IoT cloud kit in this project, we need to prepare the hardware as shown in the figure below.

Hardware Connection

Insert the WiFi card into the SD card slot on the back of the micro:IoT Board for Cloud as shown in the figure below.

 

Programming

Add library file

Add IoT Cloud Kit library file to Makecode: https://github.com/DFRobot/pxt-DFRobot_IoT_Cloud_Kit

*As this library file is suitable for all subsequent projects, we will not introduce the library file address later.

Function Analysis

This project triggers the IFTTT event "send" by pressing button A, and displays the message on the OLED screen. When this event is triggered, the parameter values “Hi (value1), DFRobot (value2)” we set in the program module will be sent to Twitter. The program flow chart is as follows:

Program Flow Chart

Sample Program

*Program link:https://makecode.microbit.org/_8p26iDJgPDgc

Note: Google Chrome is recommended.

Program Download

Connect the micro:bit V2 to your computer. Download or copy the file in hex format to the MICROBIT drive letter.

Access Network

1. Install the micro:bit V2 motherboard into the slot of the micro:IoT Board for Cloud.

2. After plugging the USB power cord into the power port (DC 5V) on the micro:IoT Board for Cloud expansion board, turn on the power switch, and the micro:bit V2 will automatically enable the network connection.

3. WIFI Connection

Note: In each following project, the steps of "program download" and "access network" are required. As these steps are the same, they will not be introduced in subsequent projects.

Operating Effect

Once you press button A on micro:bit main-board, it will send a “Hi, DFRobot” to Twitter.

When setting up if webhooks then Twitter workflow, we enabled IFTTT App push notifications (The method is in the timing project). In this case, when this workflow is triggered, the IFTTT App will send a push notification.

IFTTT App Push Notification

Twitter

 

Summary

In this project, we introduced Webhooks for the first time. At the same time, we also tried to connect the hardware to IFTTT to perform the one-click message sending function to Twitter. Now we have learned to customize the API with Webhooks and complete automation tasks. As the following hardware-related IFTTT projects will be implemented based on the Webhooks service, we must remember our Event Name and the Key assigned by IFTTT in the process.

Task 2: Drink Water Reminder

We mentioned that Date & Time is a timing service provided by IFTTT before. At the appointed time, it can automatically do some simple things for us according to the rules we set. In fact, the IoT cloud kit also has a clock service. We can set the time at will, which can be used to trigger some simple tasks, such as reminding yourself to drink water every few hours.

Create New Automation Tasks

To start with, use webhooks and Notifications of IFTTT to build a work flow as below:

Create Trigger

1. Configure Trigger. Select Webhooks service. Set condition to “Receive a web request”

2. Set Webhooks Event Name as “drink_water”

3. Click Create trigger to complete settings. Then we are going to create Action.

Create Action

1. To start with, we must make clear what we need to do: When Trigger is triggered, a message will be pushed automatically via IFTTT App. So, we need to select the service corresponding to Action –Notifications, and set the action condition to “Send a notification from the IFTTT app”.

2. Set the message to be pushed. First, clear the original template provided by IFTTT. Then add the parameter value – “Value 1” via Add ingredient.

3. Click Create action to complete settings. Click "Continue" → "Finish" continuously, your Applet will be created as shown below:

Hardware Preparation

Insert the WiFi card into the SD card slot on the back of the micro:IoT Board for Cloud. The operating method is the same as the previous project.

Programming

Function Analysis

This project triggers the IFTTT event "drink_water" mainly by the real-time clock function on the micro:IoT Board for Cloud expansion board. This function is the same with that of the timing service provided by IFTTT. We only need to set and get the time to do some projects that require regular reminders. It's just that IFTTT is set in the web platform, and the real-time clock on the board needs to be set in the program module. Next, let's get to know the program of the real-time clock.

According to our health needs, we need to drink 7 to 8 glasses of water a day at least. If we start at 8:00 and drink a glass of water every 2 hours, we will have 7 cups of water until 20:00. So, we need to drink water at 8:00, 10:00, 12:00, 14:00, 16:00, 18:00, 20:00 every day. Therefore, the drink water reminder needs to remind us to drink water at those times. The program flow chart is as follows:

Program Chart Flow
Sample Program

Time Function:

Overall Program:

*Program Link: https://makecode.microbit.org/_crvdb8Yew16q

Operating Effect

When powered on and networked,OLED screen will display time (Start counting from the time you set). When it’s 8:00, 10:00, 12:00, 14:00, 16:00, 18:00, 20:00, IFTTT App will push a message to remind you that it’s time to drink water.

Summary

In this project, we used the real-time clock function of the IoT cloud kit to perform the automated task -- "remind me to drink water at the set time". As this function is the same with the service Date & Time provided by IFTTT, projects that the service can do can also be implemented by using the IoT cloud kit. In the last lesson, we used Date & Time to implement the task of "automatically create a diary template at 3 pm every day". At this time, can you do the same task with the kit?

Question: Try doing a project that automatically create a diary template in Evernote at 9 pm every day with IoT cloud kit.

Discussion

NumberQuestionAnswer1What service is used to connect hardware to IFTTT? 2If you want to combine the IoT cloud kit with IFTTT to do an automated task, what do you want to do most? 3Write the making process of Question 2

Extension

In Task 3 of the first lesson, we asked a question: What if we need to synchronize the message to many other platforms at the same time? Or how to publish a message on one platform, and then synchronize it to other platforms at the same time? Can you solve this problem according to the following tips?

In task 1, once button A on the micro:bit motherboard is pressed, a message will be sent to Twitter. If we add two more automated tasks, can we synchronize the messages in Twitter to Facebook and Email?

If we want to synchronize the messages in Twitter to Facebook and Email, we also need to establish two more workflows for automated tasks. The steps are as follows:

1.Set up the workflow of if Twitter then Facebook Pages

Create Trigger—if Twitter then Facebook Pages

1. Set Trigger. Select Twitter service. Set the condition to “New tweet by you”

2. Set Trigger 2, tick Include

3. Click Create trigger to complete settings. Then we are going to create Action 2.

Create Action—if Twitter then Facebook Pages

1. To begin with, we need to make clear what we need to do: When Trigger 2 is triggered, it will automatically sync a message to Facebook. In this case, we need to choose the service corresponding to Action -- Facebook Pages and set the action condition to "Create a status message".

2. After selecting trigger condition, click Connect to link to Facebook Pages. Please set status as the steps in the figure below.

3. Set status message, add parameter “Text” by Add ingredient.

 

4. Click Create action to complete settings. Click "Continue" → "Finish" , your Applet will be created.

2.Build the workflow of if Twitter then Email

Create Trigger—if Twitter then Email

Set Trigger, select Twitter service, set condition to “New tweet by you

Set Trigger, check Include.

Click Create trigger to complete settings. Then we are going to create Action 3.

Note: For detailed steps, please refer to Create Trigger—if Twitter then Facebook Pages

Create Action 3—if Twitter then Email

1. To begin with, we need to make clear what we need to do: When Trigger 3 is triggered, it will automatically sync a message to Email. In this case, we need to choose the service corresponding to Action -- Email and set the action condition to "Send me an email"

2. Set the content of Email. Clear the original template provided by IFTTT and then add parameters by Add ingredient.

3. Click Create action to complete settings. Click "Continue" → "Finish" continuously, your Applet will be created as shown below:

Having created a new automated task according to the above steps, once we post a new message in Twitter, the entire task will be triggered, and both Facebook and email will post a same one.

 

Twitter

IFTTT homepage on Facebook

Gmail Mailbox

If you add the two workflows above -- if Twitter then Facebook Pages and if Twitter then Email -- according to the project steps of task one, it will perform the function that send messages to multiple platforms with one click.

 

Summary

We have learned two aspects about IFTTT: 1. Simply use IFTTT to perform automated tasks. 2. Connect the hardware to IFTTT to perform the automation task. For similar functions, we have used two different ways to do it. I believe that now you also master the basic usage of IFTTT. In the following projects, we will still use the IoT cloud kit and other platforms to make and IoT-related projects. For example, next lesson, we will learn how to use ThingSpeak to collect data.

 

 

 

 

 

 

License
All Rights
Reserved
licensBg
0