人感センサー×SNS

Use StarterKit to send a message to SNS when someone comes in!

Make things

Send messages to SNS if anyone comes by Motion Sensor in Starter Kit.

Materials

*Included in the Starter Kit.

How to make

Hardware connection

Connect a Keyestudio PIR Motion Sensor to an obniz Board like the table and the image below by referring to Keyestudio PIR Motion Sensor Library.

obniz Keyestudio PIR Motion Sensor
0 Signal
1 +
2

obniz Boardと人感センサー

Software

Connect IFTTT and Twitter to make a system that tweets through IFTTT if a PIR sensor detects anyone.

Here’s a quick introduction to what IFTTT is all about.

IFTTT is a service that allows you to do “If XX (This) happens, then XX (That) will be executed. To illustrate with a concrete example, you can create things like “When it’s tweeted, I’ll summarize the content on a spreadsheet” or “When it’s close to my house, I’ll turn on the air conditioner”.

First, set the “if” part. Click on “This“. Choose a service(Step 1 of 6 )

Type “webhooks” in the search field and click on the one that comes up. Choose trigger (Step 2 of 6 )
Click on “Receive a web request“. Complete trigger fields (Step 2 of 6 )
Enter “obniz_sns” in the EventName field and click [Create trigger].Next, click on “That” to set the recording destination.

Choose action service (Step 3 of 6 )
Type “twitter” in the search field and click on the one that comes up.

(You may need to select your Twitter account and allow access from IFTTT.)

Choose action (Step 4 of 6)
Click on “Post a tweet“. Complete action fields (Step 5 of 6)
Set the information to be posted on Twitter. Due to the specification of Twitter, it is impossible to send exactly the same message in succession. So,  the time is added at the back of the message.

  • Tweet text: What to tweet.

Review and finish (Step 6 of 6)
Click [Finish] to complete your creation.

Also, there are some items to check for the settings.
Go to the [Webhooks] settings page and click on “Documentation” in the upper right-hand corner. Copy the bold letters following the word “Your key is”. I’ll use it later in the program.

Replace the IFTTT_WEBHOOK_KEY (code.js line2) with the key.

The program detects the value change of the PIR sensor at this part. If anyone comes, tweet through IFTTT.

sensor.onchange = async val => {
    console.log(val ? "Moving!" : "Stopped");
    if (val)
      await fetch(`https://maker.ifttt.com/trigger/${EVENT}/with/key/${KEY}`);
};

Program

How to use

If the sensor reacts, it tweets like the image below.

(It might delay up to 15 minutes).

in the end

We used an obniz Board and IFTTT to easily connect to SNS. It can be linked with other services, so please use it well and make use of the IoT!